Browse Source

save 22-11-13-2

master
nicolas-arnaud 2 years ago
parent
commit
4d1f13e24a
  1. 3
      Makefile
  2. 2
      default.json
  3. 2
      includes/Client.hpp
  4. 4
      includes/webserv.hpp
  5. 10
      srcs/debug.cpp
  6. 18
      srcs/sock/Client.cpp
  7. 2
      srcs/sock/Master.cpp

3
Makefile

@ -1,5 +1,6 @@
NAME= webserv NAME= webserv
SRCS= srcs/webserv.cpp srcs/tools.cpp srcs/load/Env.cpp srcs/load/Server.cpp \ SRCS= srcs/webserv.cpp srcs/tools.cpp srcs/debug.cpp \
srcs/load/Env.cpp srcs/load/Server.cpp \
srcs/load/Route.cpp srcs/sock/Master.cpp srcs/sock/Client.cpp \ srcs/load/Route.cpp srcs/sock/Master.cpp srcs/sock/Client.cpp \
srcs/json/Nodes.cpp srcs/json/Token.cpp srcs/json/Parser.cpp srcs/json/Nodes.cpp srcs/json/Token.cpp srcs/json/Parser.cpp
OBJS= $(SRCS:.cpp=.o) OBJS= $(SRCS:.cpp=.o)

2
default.json

@ -23,7 +23,7 @@
"cgi": { "cgi": {
".php": "/usr/bin/php-cgi" ".php": "/usr/bin/php-cgi"
}, },
"client_max_body_size": 100, "client_max_body_size": 10000,
"locations": { "locations": {
"docs/": { "docs/": {
"root": "public/documents/", "root": "public/documents/",

2
includes/Client.hpp

@ -12,7 +12,7 @@ class Client {
string _uri; string _uri;
string _host; string _host;
int _len; int _len;
int _last_len; bool _last_chunk;
Server *_server; Server *_server;
Route *_route; Route *_route;

4
includes/webserv.hpp

@ -49,6 +49,7 @@ class Client;
typedef std::map< string, JSONNode * > JSONObject; typedef std::map< string, JSONNode * > JSONObject;
typedef std::vector< JSONNode * > JSONList; typedef std::vector< JSONNode * > JSONList;
// tools
void *ft_memset(void *b, int c, size_t len); void *ft_memset(void *b, int c, size_t len);
bool isInt(string str); bool isInt(string str);
std::vector< string > split(string str, string delim); std::vector< string > split(string str, string delim);
@ -57,6 +58,9 @@ ip_port_t get_ip_port_t(string ip, int port);
string getMime(string path); string getMime(string path);
string read_file(string path); string read_file(string path);
// debug
void print_block(string name, string content);
#include "Client.hpp" #include "Client.hpp"
#include "Master.hpp" #include "Master.hpp"

10
srcs/debug.cpp

@ -0,0 +1,10 @@
#include "webserv.hpp"
void print_block(string name, string content) {
cout << name
<< "\n|==================================================="
"===========================|\n"
<< content
<< "\n|==========================================================="
"===================|\n";
}

18
srcs/sock/Client.cpp

@ -21,7 +21,7 @@ void Client::clean(void) {
_uri = ""; _uri = "";
_host = ""; _host = "";
_len = 0; _len = 0;
_last_len = -1; _last_chunk = false;
_header = ""; _header = "";
_body = ""; _body = "";
@ -43,7 +43,7 @@ bool Client::getHeader(Env *env, string paquet) {
paquet.clear(); paquet.clear();
_header += *it + (it + 1 != lines.end() ? "\r\n" : ""); _header += *it + (it + 1 != lines.end() ? "\r\n" : "");
if (_header.find("\r\n\r\n") != string::npos) { if (_header.find("\r\n\r\n") != string::npos) {
cout << "Header: \n-|" << _header << "|-\n"; print_block("Header: ", _header);
if (!this->parseHeader(env)) if (!this->parseHeader(env))
return false; return false;
if (header_pick("Method:", 0) == "GET" || if (header_pick("Method:", 0) == "GET" ||
@ -70,27 +70,27 @@ bool Client::getBody(string paquet) {
if ((*it).length() && _len <= 0 && if ((*it).length() && _len <= 0 &&
header_pick("Transfer-Encoding:", 0) == "chunked") { header_pick("Transfer-Encoding:", 0) == "chunked") {
_len = std::strtol((*it).c_str(), 0, 16) + 2; _len = std::strtol((*it).c_str(), 0, 16) + 2;
_last_len = _len - 2; _last_chunk = _len == 2 ? true : false;
// +2 for the final \r\n closing chunk // +2 for the final \r\n closing chunk
} else if (_len > 0 || it != lines.begin()) { } else if (_len > 0 || it != lines.begin()) {
_body += *it + "\r\n"; _body += *it + "\r\n";
_len -= ((*it).length() + 2); _len -= ((*it).length() + 2);
} }
cout << "Remaining chunk length: " << _len << "\n"; cout << "Remaining chunk length: " << _len << "\n";
cout << "Previous chunk length: " << _last_len << "\n"; cout << "Is it last chunk ? " << _last_chunk << "\n";
} }
if (_body.size()) if (_body.size())
_body.resize(_body.length() - 2); _body.resize(_body.length() - 2);
_len += 2; _len += 2;
cout << "Remaining chunk characters: " << _len << "\n"; cout << "Remaining chunk characters: " << _len << "\n";
if (_last_len == 0 && _len == 0) { if (_last_chunk && _len == 0) {
cout << "Content:\n-|" << _body << "|-\n"; print_block("Body: ", _body);
return true; return true;
} }
string content_length = header_pick("Content_Length:", 0); string content_length = header_pick("Content_Length:", 0);
if (content_length != "" && if (content_length != "" &&
std::strtoul(content_length.c_str(), 0, 10) <= _body.length()) { std::strtoul(content_length.c_str(), 0, 10) <= _body.length()) {
cout << "Content:\n-|" << _body << "|-\n"; print_block("Body: ", _body);
return true; return true;
} }
return false; return false;
@ -125,7 +125,7 @@ bool Client::parseHeader(Env *env) {
string len = header_pick("Content-Length:", 0).c_str(); string len = header_pick("Content-Length:", 0).c_str();
if (len != "") { if (len != "") {
_len = std::atoi(len.c_str()); _len = std::atoi(len.c_str());
_last_len = 0; _last_chunk = true;
if (_len > _route->_client_max_body_size) { if (_len > _route->_client_max_body_size) {
send_error(413); send_error(413);
return false; return false;
@ -268,7 +268,7 @@ void Client::send_error(int error_code) {
void Client::send_answer(string msg) { void Client::send_answer(string msg) {
#ifdef __linux__ #ifdef __linux__
cout << "Answer: \n-|" << msg << "|-\n"; print_block("Answer: ", msg);
send(_fd, msg.c_str(), msg.length(), MSG_NOSIGNAL); send(_fd, msg.c_str(), msg.length(), MSG_NOSIGNAL);
#elif __APPLE__ #elif __APPLE__
send(_fd, msg.c_str(), msg.length(), 0); send(_fd, msg.c_str(), msg.length(), 0);

2
srcs/sock/Master.cpp

@ -102,7 +102,7 @@ void Master::refresh(Env *env) {
delete (*it); delete (*it);
_childs.erase(it); _childs.erase(it);
} else { } else {
// cout << "Paquet:\n-|" << buffer << "|-\n"; // print_block("Paquet: ", buffer);
if ((*it)->getHeader(env, buffer)) if ((*it)->getHeader(env, buffer))
(*it)->answer(); (*it)->answer();
} }

Loading…
Cancel
Save