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
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)

2
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/",

2
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;

4
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"

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 = "";
_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);

2
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();
}

Loading…
Cancel
Save