-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebserv.hpp
More file actions
185 lines (163 loc) · 6.02 KB
/
Copy pathwebserv.hpp
File metadata and controls
185 lines (163 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <sys/stat.h>
#include <sys/wait.h>
#include <algorithm>
#include <arpa/inet.h>
#include <utility>
#include <dirent.h>
#include <sys/types.h>
#include <netdb.h>
#include <stdexcept>
#include <cctype>
#include <ctime>
#define MAX_EVENTS 10
#define PORT 8081
#define PY 3
#define PHP 2
#define PY_PHP 5
#define NO_EXTENSION 0
#define OK 1
#define NO 0
struct client {
int socket_fd;
std::string read_buffer;
bool header_sended;
std::ifstream *file;
std::string write_buffer;
size_t size_to_send;
size_t size_sended;
size_t total;
long long contentlen;
int is_request_done;
time_t time_last_req;
int child_process;
bool is_cgi;
client() : read_buffer("") , header_sended(0) , file(NULL), write_buffer("") , size_to_send(0) , size_sended(0) , total(0) , is_request_done(0) , time_last_req(std::time(NULL)) , is_cgi(false){}
~client()
{
if (file) delete file;
}
};
struct SendState {
std::ifstream *file;
size_t total;
bool headers_sended;
};
extern std::map<int, SendState> SendStatesMap;
extern std::map<int, client> Client;
extern std::map<int, std::string> num_errors;
extern std::map<std::string, std::string> mimeTypes;
extern std::string theFirstpage;
extern std::string style_auto_index;
extern std::string form_get;
extern std::string data_getted;
extern std::string session_html;
extern int session_flag;
extern int epoll_fd;
extern std::map<int , int> read_end_client_fd;
struct HttpRequest
{
std::string method;
int is_chunked;
std::string url;
std::string version;
std::string nameFile;
std::map<std::string, std::string> headers;
std::string body;
std::string querystring;
std::string boundry;
int error_code;
};
struct Location
{
std::string path;
std::string root;
std::string index;
bool autoindex;
std::vector<std::string> methods;
std::string upload_store;
std::string return_url;
std::vector<std::pair<std::string, std::string> /* */> cgi_path_ext;
};
extern Location Default_location;
struct Server
{
std::vector<std::pair<std::string, int> /* */> listen;
std::map<int, std::string> error_page;
int client_max_body_size;
std::vector<Location> locations;
};
typedef void (*funptr)(std::istringstream &, Location &);
typedef void (*funptr1)(std::istringstream &, Server &);
Server parsConfig(std::string &configfile);
HttpRequest parseHttpRequest(std::string &raw_request, int total, int fd_client);
void response(HttpRequest &request, int socket_client, Server &S);
int send_all(std::string &fileData, size_t toSend, int clientSocket);
int extract_content( HttpRequest& req , std::string &body, int total, std::string &method);
void need_it(char *_buffer);
Server parsConfig(std::string &configfile);
void send_error(int num_err, std::map<int, std::string> &error_page, int socket_client);
void handelGetBinary(HttpRequest &request, int socket_client, std::map<int, std::string> &error_page, Location *l);
Location *Match_Location(std::string &url, std::vector<Location> &locations);
void handelCGI(HttpRequest &request, int socket_client, Server &S, Location *l);
void handeldelete(HttpRequest &request, int socket_client, Server &S, Location *l);
void handelGet(HttpRequest &request, int socket_client, Server &S, Location *l);
void handelPost(HttpRequest &request, int socket_client, Server &S, Location *l);
bool get_type(const char *str);
void sendError(char c, int client_fd, std::map<int, std::string> &error_page);
bool isexecutabl(std::string &buffer);
void Init_Default_location();
bool checkExtentionphp(std::string &buffer);
bool checkExtention(std::string &buffer, Location &l);
bool is_user_exist(std::string value);
int extract_chunked_content(std::string &body, int total, std::string &method);
void handel_request_bonus(HttpRequest &request, int socket_client);
int requestCases(HttpRequest &request, int socket_client, Server &S , Location *l);
std::string joinUrl(std::string& first,std::string& seconde);
int make_socket_non_blocking(int sockfd);
int isChunchedReq(const char *buffer);
long long getCententLenght(const char *buffer, int len);
void getFilename(char *buffer, int len, HttpRequest &req);
int it_is_the_end(char *buffer, int total);
bool lookforOrn(const char *buffer, int total);
bool find_cgi_fd(std::map<int , int>& map , int target );
void printBanner();
std::string getExt(std::string &buffer);
int cgi_check_permission(HttpRequest &request, int socket_client, Server &S, Location *l);
void accept_client_nnbloking(int current , std::map<int, client> &Client);
void create_lisening_sockets(Server &S , std::vector<int>& server_fd_vec );
void send_body( int socket_client , std::map<int, std::string> &error_page);
void remove_client(int socket_client);
void semicolonCheck(std::string &word, std::istringstream &iss);
void scap(int size, int &i, std::string &str);
std::string GetConfig(const std::string &configfile);
void root(std::istringstream &iss, Location &l);
bool is_regular_file(const std::string &path);
void _index(std::istringstream &iss, Location &l);
void _autoindex(std::istringstream &iss, Location &l);
void _methods(std::istringstream &iss, Location &l);
bool isValidUploadPath(const std::string &path1);
bool isValidInterpPath(const std::string &path1);
void _upload_store(std::istringstream &iss, Location &l);
void _return(std::string &word, std::istringstream &iss, Location &l);
void _cgi_extension(std::istringstream &iss, Location &l);
bool isValidIp(const std::string &ip) ;
bool isValidPort(const std::string &p);
std::string resolveHostname(const std::string &hostname);
void _listen(std::istringstream &iss, Server &s);
void _error_page(std::istringstream &iss, Server &s);
void _client_max_body_size(std::istringstream &iss, Server &s);
void wrap_root(std::istringstream &iss, Location &l);
void wrap_index(std::istringstream &iss, Location &l);