diff --git a/Makefile b/Makefile index a8282d0..5f6a5f7 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ 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/json/Nodes.cpp srcs/json/Token.cpp srcs/json/Parser.cpp OBJS= $(SRCS:.cpp=.o) diff --git a/default.json b/default.json index 0e248ba..4fe61a4 100644 --- a/default.json +++ b/default.json @@ -23,7 +23,7 @@ "cgi": { ".php": "/usr/bin/php-cgi" }, - "client_max_body_size": 100, + "client_max_body_size": 10000, "locations": { "docs/": { "root": "public/documents/", diff --git a/includes/Client.hpp b/includes/Client.hpp index eff5ba4..fae7038 100644 --- a/includes/Client.hpp +++ b/includes/Client.hpp @@ -12,7 +12,7 @@ class Client { string _uri; string _host; int _len; - int _last_len; + bool _last_chunk; Server *_server; Route *_route; diff --git a/includes/webserv.hpp b/includes/webserv.hpp index e4bf716..f8dc5a3 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -49,6 +49,7 @@ class Client; typedef std::map< string, JSONNode * > JSONObject; typedef std::vector< JSONNode * > JSONList; +// tools void *ft_memset(void *b, int c, size_t len); bool isInt(string str); 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 read_file(string path); +// debug +void print_block(string name, string content); + #include "Client.hpp" #include "Master.hpp" diff --git a/srcs/debug.cpp b/srcs/debug.cpp new file mode 100644 index 0000000..6f0f319 --- /dev/null +++ b/srcs/debug.cpp @@ -0,0 +1,10 @@ +#include "webserv.hpp" + +void print_block(string name, string content) { + cout << name + << "\n|===================================================" + "===========================|\n" + << content + << "\n|===========================================================" + "===================|\n"; +} diff --git a/srcs/sock/Client.cpp b/srcs/sock/Client.cpp index 693e44e..f31c5ca 100644 --- a/srcs/sock/Client.cpp +++ b/srcs/sock/Client.cpp @@ -21,7 +21,7 @@ void Client::clean(void) { _uri = ""; _host = ""; _len = 0; - _last_len = -1; + _last_chunk = false; _header = ""; _body = ""; @@ -43,7 +43,7 @@ bool Client::getHeader(Env *env, string paquet) { paquet.clear(); _header += *it + (it + 1 != lines.end() ? "\r\n" : ""); if (_header.find("\r\n\r\n") != string::npos) { - cout << "Header: \n-|" << _header << "|-\n"; + print_block("Header: ", _header); if (!this->parseHeader(env)) return false; if (header_pick("Method:", 0) == "GET" || @@ -70,27 +70,27 @@ bool Client::getBody(string paquet) { if ((*it).length() && _len <= 0 && header_pick("Transfer-Encoding:", 0) == "chunked") { _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 } else if (_len > 0 || it != lines.begin()) { _body += *it + "\r\n"; _len -= ((*it).length() + 2); } cout << "Remaining chunk length: " << _len << "\n"; - cout << "Previous chunk length: " << _last_len << "\n"; + cout << "Is it last chunk ? " << _last_chunk << "\n"; } if (_body.size()) _body.resize(_body.length() - 2); _len += 2; cout << "Remaining chunk characters: " << _len << "\n"; - if (_last_len == 0 && _len == 0) { - cout << "Content:\n-|" << _body << "|-\n"; + if (_last_chunk && _len == 0) { + print_block("Body: ", _body); return true; } string content_length = header_pick("Content_Length:", 0); if (content_length != "" && std::strtoul(content_length.c_str(), 0, 10) <= _body.length()) { - cout << "Content:\n-|" << _body << "|-\n"; + print_block("Body: ", _body); return true; } return false; @@ -125,7 +125,7 @@ bool Client::parseHeader(Env *env) { string len = header_pick("Content-Length:", 0).c_str(); if (len != "") { _len = std::atoi(len.c_str()); - _last_len = 0; + _last_chunk = true; if (_len > _route->_client_max_body_size) { send_error(413); return false; @@ -268,7 +268,7 @@ void Client::send_error(int error_code) { void Client::send_answer(string msg) { #ifdef __linux__ - cout << "Answer: \n-|" << msg << "|-\n"; + print_block("Answer: ", msg); send(_fd, msg.c_str(), msg.length(), MSG_NOSIGNAL); #elif __APPLE__ send(_fd, msg.c_str(), msg.length(), 0); diff --git a/srcs/sock/Master.cpp b/srcs/sock/Master.cpp index b87c5e0..9fadf91 100644 --- a/srcs/sock/Master.cpp +++ b/srcs/sock/Master.cpp @@ -102,7 +102,7 @@ void Master::refresh(Env *env) { delete (*it); _childs.erase(it); } else { - // cout << "Paquet:\n-|" << buffer << "|-\n"; + // print_block("Paquet: ", buffer); if ((*it)->getHeader(env, buffer)) (*it)->answer(); }