Browse Source

save 22-11-12-1

master
nicolas-arnaud 2 years ago
parent
commit
f0cd0af617
  1. 6
      includes/Client.hpp
  2. 2
      includes/Route.hpp
  3. 4
      srcs/load/Route.cpp
  4. 81
      srcs/sock/Client.cpp
  5. 5
      srcs/sock/Master.cpp

6
includes/Client.hpp

@ -27,9 +27,13 @@ class Client {
bool parseHeader(Env *env); bool parseHeader(Env *env);
string header_pick(string key, size_t id); string header_pick(string key, size_t id);
void answer(); void answer();
bool check_method(string method); bool check_method();
void send_cgi(string cgi, string path); void send_cgi(string cgi, string path);
void send_error(int error_code); void send_error(int error_code);
void send_answer(string msg); void send_answer(string msg);
bool getHeader(Env *env, string paquet);
bool getBody(string paquet);
friend class Master; friend class Master;
}; };

2
includes/Route.hpp

@ -11,7 +11,7 @@ class Route {
public: public:
std::vector< string > _indexs; std::vector< string > _indexs;
std::vector< string > _headers; std::vector< string > _allowed_methods;
std::map< string, string > _cgi; std::map< string, string > _cgi;
int _client_max_body_size; int _client_max_body_size;

4
srcs/load/Route.cpp

@ -26,11 +26,11 @@ Route::Route(Server *server, string location, JSONNode *datas)
_indexs.push_back((*it)->str()); _indexs.push_back((*it)->str());
} }
} }
if ((tmp = object["add_header"])) { if ((tmp = object["allowed_methods"])) {
JSONList headers = tmp->lst(); JSONList headers = tmp->lst();
for (JSONList::iterator it = headers.begin(); it < headers.end(); for (JSONList::iterator it = headers.begin(); it < headers.end();
it++) { it++) {
_headers.push_back((*it)->str()); _allowed_methods.push_back((*it)->str());
} }
} }
if ((tmp = object["cgi"])) { if ((tmp = object["cgi"])) {

81
srcs/sock/Client.cpp

@ -25,39 +25,80 @@ void Client::clean(void) {
_request.clear(); _request.clear();
} }
bool Client::getHeader(Env *env, string paquet) {
if (header_pick("Method:", 0) != "")
return true;
std::vector< string > lines = split(paquet, "\r\n");
for (std::vector< string >::iterator it = lines.begin(); it < lines.end();
it++) {
if (*it == "") {
if (!this->parseHeader(env))
return false;
size_t pos = paquet.find("\r\n\r\n");
if (pos != string::npos)
paquet.erase(pos + 4);
return true;
} else
_header += *it + "\r\n";
}
_header.resize(_header.length() - 2);
return false;
}
bool Client::getBody(string paquet) {
std::vector< string > lines = split(paquet, "\r\n");
long chunk_len = header_pick("Transfer-Encoding:", 0) == "chunked"
? std::strtol(lines.at(0).c_str(), 0, 16)
: -1;
std::vector< string >::iterator it;
for (it = lines.begin(); it < lines.end(); it++) {
if (chunk_len == -1 || it != lines.begin())
_content += *it + "\r\n";
}
_content.resize(_content.length() - 2);
if ((header_pick("Method:", 0) == "GET" && *it == "") ||
(chunk_len == 0 ||
std::strtoul(header_pick("Content-Length:", 0).c_str(), 0, 10) <=
_content.length())) {
cout << "Request received\n";
return true;
}
return false;
}
bool Client::getRequest(Env *env, string paquet) { bool Client::getRequest(Env *env, string paquet) {
// cout << "|===|paquet|===>" << paquet << "|===||\n"; cout << "|===|paquet|===>\n" << paquet << "|===||\n";
if (paquet.length() < 1) // HTTPS? if (paquet.length() < 1) // HTTPS?
return false; return false;
std::vector< string > lines = split(paquet, "\n"); std::vector< string > lines = split(paquet, "\n");
long chunk_len; long chunk_len = (_content.length() > 0 &&
chunk_len = (_content.length() > 0 &&
header_pick("Transfer-Encoding:", 0) == "chunked") header_pick("Transfer-Encoding:", 0) == "chunked")
? std::strtol(lines.at(0).c_str(), 0, 16) ? std::strtol(lines.at(0).c_str(), 0, 16)
: -1; : -1;
cout << "Chunk length: " << chunk_len << "\n"; cout << "Chunk length: " << chunk_len << "\r\n";
for (std::vector< string >::iterator it = lines.begin(); it < lines.end(); for (std::vector< string >::iterator it = lines.begin(); it < lines.end();
it++) { it++) {
if (*it == "\r" && _content.length() == 0) { if (*it == "\r" && header_pick("Method:", 0) == "") {
if (!this->parseHeader(env)) if (!this->parseHeader(env))
return false; return false;
} } else if (*it != "\r" && header_pick("Method:", 0) == "")
if (*it != "\r" && _content.length() == 0)
_header += *it + "\n"; _header += *it + "\n";
else if (chunk_len == -1 || it != lines.begin()) else if (*it != "\r" && (chunk_len == -1 || it != lines.begin()))
_content += *it + "\n"; _content += *it + "\n";
} }
cout << "Content: \n-|" << _content << "|-\n"; cout << "Content: \n-|" << _content << "|-\n";
if (_content.length() > 0) { if (_content.length() > 0) {
_content.resize(_content.length() - 1); _content.resize(_content.length() - 1);
if (header_pick("Method:", 0) == "GET" || if ((header_pick("Method:", 0) == "GET" && _content.find("\r\n\r\n")) ||
(chunk_len == 0 || (chunk_len == 0 ||
std::strtoul(header_pick("Content-Length:", 0).c_str(), 0, 10) <= std::strtoul(header_pick("Content-Length:", 0).c_str(), 0, 10) <=
_content.length())) { _content.length())) {
cout << "Request received\n"; cout << "Request received\n";
return true; return true;
} }
} else } else if (header_pick("Method:", 0) == "")
_header.resize(_header.length() - 1); _header.resize(_header.length() - 1);
return false; return false;
} }
@ -96,16 +137,12 @@ bool Client::parseHeader(Env *env) {
return true; return true;
} }
bool Client::check_method(string method) { bool Client::check_method() {
std::vector< string > allowed; std::vector< string > allowed;
if (method != "GET" && method != "POST" && method != "DELETE") if (_method != "GET" && _method != "POST" && _method != "DELETE")
send_error(405); send_error(405);
else if ((allowed = _route->_headers).size() > 0 && else if ((allowed = _route->_allowed_methods).size() > 0 &&
std::find(allowed.begin(), allowed.end(), method) == std::find(allowed.begin(), allowed.end(), _method) ==
allowed.end()) {
send_error(405);
} else if ((allowed = _server->_headers).size() > 0 &&
std::find(allowed.begin(), allowed.end(), method) ==
allowed.end()) { allowed.end()) {
send_error(405); send_error(405);
} else } else
@ -126,7 +163,7 @@ void Client::answer() {
cout << "URI: " << _uri << "\n"; cout << "URI: " << _uri << "\n";
cout << "Host: " << _host << "\n"; cout << "Host: " << _host << "\n";
string ret; string ret;
if (check_method(_method)) { if (check_method()) {
string path = _route->correctUri(_uri); string path = _route->correctUri(_uri);
string cgi = string cgi =
_route->_cgi.size() ? _route->_cgi[get_extension(path)] : ""; _route->_cgi.size() ? _route->_cgi[get_extension(path)] : "";
@ -185,12 +222,12 @@ void Client::send_error(int error_code) {
return send_answer( return send_answer(
"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n"); "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n");
case 405: case 405:
return send_answer( return send_answer("HTTP/1.1 405 Method Not Allowed\r\nConnection: "
"HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n"); "close\r\nContent-Length: 0\r\n\r\n");
case 413: case 413:
return send_answer( return send_answer(
"HTTP/1.1 413 Payload Too " "HTTP/1.1 413 Payload Too "
"Large\r\nConnection:close\r\nContent-Length: 0\r\n\r\n"); "Large\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
} }
} }

5
srcs/sock/Master.cpp

@ -94,12 +94,11 @@ void Master::refresh(Env *env) {
(socklen_t *)&addrlen); (socklen_t *)&addrlen);
delete (*it); delete (*it);
_childs.erase(it); _childs.erase(it);
} else { //} else if ((*it)->getRequest(env, buffer))
if ((*it)->getRequest(env, buffer)) } else if ((*it)->getHeader(env, buffer) && (*it)->getBody(buffer))
(*it)->answer(); (*it)->answer();
} }
} }
}
} }
/* |==========| /* |==========|
* Choose the server which must handle a request * Choose the server which must handle a request

Loading…
Cancel
Save