11 size_t pos = str.rfind(
'.');
12 if (pos != string::npos)
13 return str.substr(pos);
20 cout <<
"New connection, socket fd is " << fd <<
", ip is : " <<
_ip_port.
ip <<
", port : " <<
_ip_port.
port <<
"\n";
38 if (paquet.length() < 1)
45 for (vec_string::iterator it = lines.begin(); it < lines.end(); it++) {
46 size_t pos = paquet.find(
"\r\n");
47 if (pos != string::npos)
48 paquet.erase(0, pos + 2);
51 _header += *it + (it + 1 != lines.end() ?
"\r\n" :
"");
52 if (
_header.find(
"\r\n\r\n") != string::npos)
60 vec_string::iterator it;
62 for (it = lines.begin(); it < lines.end(); it++) {
64 cout <<
"Remaining length: " <<
_len <<
"\n";
65 if ((*it).length() &&
_len <= 0 &&
header_pick(
"Transfer-Encoding:", 0) ==
"chunked") {
66 _len = std::strtol((*it).c_str(), 0, 16) + 2;
68 }
else if (
_len > 0 || it != lines.begin()) {
69 _body += *it +
"\r\n";
70 _len -= ((*it).length() + 2);
83 cout <<
"Parsing header...\n";
85 method =
split(lines.at(0),
" ");
87 if (lines.size() > 0) {
88 for (vec_string::iterator it = lines.begin() + 1; it < lines.end(); it++) {
89 line =
split(*it,
" ");
98 _uri = uri_split.at(0);
99 if (uri_split.size() > 1)
107 string len =
header_pick(
"Content-Length:", 0).c_str();
109 _len = std::atoi(len.c_str());
127 return std::find(allowed.begin(), allowed.end(),
_method) < allowed.end() ? true :
false;
140 std::cout <<
"||-> Request for " << req_path <<
" received <-||\n";
145 cout <<
"Path: " << req_path <<
"\n";
156 }
else if (ret ==
"403")
159 std::remove(req_path.c_str());
160 else if (cgi_path !=
"")
161 cgi(cgi_path, req_path);
168 std::ofstream file(path.c_str());
174 send_answer(
"HTTP/1.1 201 Accepted\r\nContent-Length: 0\r\n\r\n");
181 std::stringstream ss;
184 if (!std::ifstream(cgi_path.c_str()).good())
188 const char **args =
new const char *[cgi_path.length() + path.length() + 2];
189 args[0] = cgi_path.c_str();
190 args[1] = path.c_str();
193 string query =
"QUERY_STRING=" +
_query;
194 const char **env =
new const char *[path_info.length() + query.length() + 2];
195 env[0] = path_info.c_str();
196 env[1] = query.c_str();
198 dup2(fd[1], STDOUT_FILENO);
201 execve(cgi_path.c_str(), (
char **)args, (
char **)env);
204 waitpid(-1, &status, 0);
206 buffer[read(fd[0], buffer, 10000)] = 0;
207 ret = string(buffer);
208 ss <<
"HTTP/1.1 200 OK\r\nContent-Length: " << ret.length() - ret.find(
"\r\n\r\n") - 4 <<
"\r\n\r\n" << ret;
213 switch (redir_code) {
215 return send_answer(
"HTTTP/1.1 301 Moved Permanently\r\nLocation: " + opt +
"\r\n\r\n");
220 switch (error_code) {
222 return send_answer(
"HTTP/1.1 400 Bad Request\r\nContent-Length: 0\r\n\r\n");
224 return send_answer(
"HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n");
226 return send_answer(
"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n");
228 return send_answer(
"HTTP/1.1 405 Method Not Allowed\r\nConnection: "
229 "close\r\nContent-Length: 0\r\n\r\n");
232 "Large\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
240 send(this->
_fd, msg.c_str(), msg.length(), MSG_NOSIGNAL | MSG_DONTWAIT);
242 send(this->
_fd, msg.c_str(), msg.length(), 0);
string get_extension(string str)
void create_file(string path)
Client(int fd, ip_port_t ip_port, Master *parent)
void send_redir(int redir_code, string opt)
bool parseHeader(Env *env)
void send_answer(string msg)
std::map< string, vec_string > _request
bool getBody(string paquet)
void cgi(string cgi_path, string path)
string header_pick(string key, size_t id)
void send_error(int error_code)
bool getRequest(Env *env, string paquet)
vec_string _allowed_methods
Server * choose_server(Env *env, string host)
std::map< string, string > _cgi
vec_string _allowed_methods
int _client_max_body_size
string getIndex(string uri, string path)
Search for an index while generating autoindex.
Route * choose_route(string uri)
Choose the route an uri asked to the server.
void debug_block(string name, string content)
std::vector< string > vec_string