Browse Source

save 22-11-13-3

master
nicolas-arnaud 2 years ago
parent
commit
93ba74d6e5
  1. 33
      includes/Client.hpp
  2. 4
      includes/Master.hpp
  3. 4
      includes/Route.hpp
  4. 15
      includes/webserv.hpp
  5. 11
      srcs/debug.cpp
  6. 22
      srcs/load/Route.cpp
  7. 9
      srcs/load/Server.cpp
  8. 71
      srcs/sock/Client.cpp
  9. 8
      srcs/sock/Master.cpp
  10. 2
      srcs/tools.cpp

33
includes/Client.hpp

@ -1,42 +1,35 @@
#pragma once #pragma once
#include "webserv.hpp" #include "webserv.hpp"
typedef std::map< string, std::vector< string > > request_t; typedef std::map< string, vec_string > request_t;
class Client { class Client {
int _fd; int _fd;
ip_port_t _ip_port; ip_port_t _ip_port;
Master *_parent; Master *_parent;
Server *_server;
string _method; Route *_route;
string _uri; string _method, _uri, _host, _header, _body;
string _host; int _len;
int _len; bool _last_chunk;
bool _last_chunk;
Server *_server;
Route *_route;
string _header;
string _body;
request_t _request; request_t _request;
public:
Client(int fd, ip_port_t ip_port, Master *parent);
~Client(void);
void clean(void); void clean(void);
bool getBody(string paquet);
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(); bool check_method(void);
bool check_method();
void create_file(string path); void create_file(string path);
void send_cgi(string cgi, string path); void send_cgi(string cgi, string path);
void send_cgi_redir(string cgi, string path); void send_redir(int redir_code, string opt);
void send_error(int error_code); void send_error(int error_code);
void send_answer(string msg); void send_answer(string msg);
public:
Client(int fd, ip_port_t ip_port, Master *parent);
~Client(void);
bool getHeader(Env *env, string paquet); bool getHeader(Env *env, string paquet);
bool getBody(string paquet); void answer(void);
friend class Master; friend class Master;
}; };

4
includes/Master.hpp

@ -17,7 +17,5 @@ class Master {
ip_port_t _listen; ip_port_t _listen;
static fd_set _readfds; static fd_set _readfds;
static int _max_fd; static int _max_fd, _min_fd, _amount;
static int _min_fd;
static int _amount;
}; };

4
includes/Route.hpp

@ -10,8 +10,8 @@ class Route {
bool _autoindex; bool _autoindex;
public: public:
std::vector< string > _indexs; vec_string _indexs;
std::vector< string > _allowed_methods; vec_string _allowed_methods;
std::map< string, string > _cgi; std::map< string, string > _cgi;
int _client_max_body_size; int _client_max_body_size;

15
includes/webserv.hpp

@ -48,15 +48,16 @@ class Client;
typedef std::map< string, JSONNode * > JSONObject; typedef std::map< string, JSONNode * > JSONObject;
typedef std::vector< JSONNode * > JSONList; typedef std::vector< JSONNode * > JSONList;
typedef std::vector< string > vec_string;
// tools // 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); vec_string split(string str, string delim);
ip_port_t get_ip_port_t(string listen); ip_port_t get_ip_port_t(string listen);
ip_port_t get_ip_port_t(string ip, int port); 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 // debug
void print_block(string name, string content); void print_block(string name, string content);

11
srcs/debug.cpp

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

22
srcs/load/Route.cpp

@ -58,12 +58,12 @@ string Route::getReturn(void) { return _ret; }
*/ */
string Route::getIndex(string uri, string path) { string Route::getIndex(string uri, string path) {
std::stringstream content; std::stringstream content;
std::stringstream ret; std::stringstream ret;
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
struct stat info; struct stat info;
std::vector< string >::iterator it; vec_string::iterator it;
if ((dir = opendir(path.c_str())) == NULL) { if ((dir = opendir(path.c_str())) == NULL) {
return ""; return "";
@ -103,13 +103,13 @@ string Route::getIndex(string uri, string path) {
*/ */
string Route::correctUri(string uri) { string Route::correctUri(string uri) {
std::stringstream ret; std::stringstream ret;
std::vector< string >::iterator it; vec_string::iterator it;
std::vector< string >::iterator it2; vec_string::iterator it2;
ret << "./" << _root; ret << "./" << _root;
std::vector< string > loc_split = split(_location, "/"); vec_string loc_split = split(_location, "/");
std::vector< string > uri_split = split(uri, "/"); vec_string uri_split = split(uri, "/");
it2 = uri_split.begin(); it2 = uri_split.begin();
for (it = loc_split.begin(); it < loc_split.end(); it++) { for (it = loc_split.begin(); it < loc_split.end(); it++) {
while (it2 < uri_split.end() && *it2 == "") while (it2 < uri_split.end() && *it2 == "")

9
srcs/load/Server.cpp

@ -91,15 +91,14 @@ std::vector< Master * > Server::get_sockets(JSONNode *server) {
*/ */
Route *Server::choose_route(string uri) { Route *Server::choose_route(string uri) {
std::vector< string > req = split(uri, "/"); vec_string req = split(uri, "/");
std::vector< string > root; vec_string root;
for (std::map< string, Route * >::iterator rit = _routes.begin(); for (std::map< string, Route * >::iterator rit = _routes.begin();
rit != _routes.end(); rit++) { rit != _routes.end(); rit++) {
root = split((*rit).first, "/"); root = split((*rit).first, "/");
cout << "Route: " << (*rit).first << "\n"; cout << "Route: " << (*rit).first << "\n";
std::vector< string >::iterator root_it = root.begin(); vec_string::iterator root_it = root.begin();
for (std::vector< string >::iterator it = req.begin(); it < req.end(); for (vec_string::iterator it = req.begin(); it < req.end(); it++) {
it++) {
while (it != req.end() && *it == "") while (it != req.end() && *it == "")
it++; it++;
if (*it != *(root_it++)) if (*it != *(root_it++))

71
srcs/sock/Client.cpp

@ -1,5 +1,7 @@
#include "webserv.hpp" #include "webserv.hpp"
inline string get_extension(string str) { return str.substr(str.rfind('.')); }
Client::Client(int fd, ip_port_t ip_port, Master *parent) Client::Client(int fd, ip_port_t ip_port, Master *parent)
: _fd(fd), _ip_port(ip_port), _parent(parent) { : _fd(fd), _ip_port(ip_port), _parent(parent) {
clean(); clean();
@ -16,15 +18,9 @@ Client::~Client(void) {
void Client::clean(void) { void Client::clean(void) {
_server = NULL; _server = NULL;
_route = NULL; _route = NULL;
_method = _uri = _host = _header = _body = "";
_method = "";
_uri = "";
_host = "";
_len = 0; _len = 0;
_last_chunk = false; _last_chunk = false;
_header = "";
_body = "";
_request.clear(); _request.clear();
} }
@ -33,9 +29,8 @@ bool Client::getHeader(Env *env, string paquet) {
send_error(403); send_error(403);
if (header_pick("Method:", 0) != "") if (header_pick("Method:", 0) != "")
return getBody(paquet); return getBody(paquet);
std::vector< string > lines = split(paquet, "\r\n"); vec_string lines = split(paquet, "\r\n");
for (std::vector< string >::iterator it = lines.begin(); it < lines.end(); for (vec_string::iterator it = lines.begin(); it < lines.end(); it++) {
it++) {
size_t pos = paquet.find("\r\n"); size_t pos = paquet.find("\r\n");
if (pos != string::npos) if (pos != string::npos)
paquet.erase(0, pos + 2); paquet.erase(0, pos + 2);
@ -43,30 +38,23 @@ 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) {
print_block("Header: ", _header); print_block("HEADER: ", _header);
if (!this->parseHeader(env)) if (!this->parseHeader(env))
return false; return false;
if (header_pick("Method:", 0) == "GET" || if (paquet.length() > 0)
header_pick("Method:", 0) == "HEAD")
return true;
else if (paquet.length() > 0) {
// cout << "Remaining paquet: " << paquet.length() << "\n";
return getBody(paquet); return getBody(paquet);
} return true;
cout << "next: " << *it << "\n";
cout << "paquet length remain: " << paquet.length() << "\n";
} }
} }
return false; return false;
} }
bool Client::getBody(string paquet) { bool Client::getBody(string paquet) {
std::vector< string > lines = split(paquet, "\r\n"); vec_string lines = split(paquet, "\r\n");
std::vector< string >::iterator it; vec_string::iterator it;
cout << paquet << "\n";
for (it = lines.begin(); it < lines.end(); it++) { for (it = lines.begin(); it < lines.end(); it++) {
cout << "line: " << *it << "\n"; // cout << "line: " << *it << "\n";
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;
@ -76,32 +64,28 @@ bool Client::getBody(string paquet) {
_body += *it + "\r\n"; _body += *it + "\r\n";
_len -= ((*it).length() + 2); _len -= ((*it).length() + 2);
} }
cout << "Remaining chunk length: " << _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";
if (_last_chunk && _len == 0) { if (_last_chunk && _len == 0) {
print_block("Body: ", _body); print_block("BODY: ", _body);
return true; return true;
} }
return false; return false;
} }
bool Client::parseHeader(Env *env) { bool Client::parseHeader(Env *env) {
std::vector< string > lines = split(_header, "\r\n"); vec_string lines, method, line;
std::vector< string > method = split(lines.at(0), " ");
_request["Method:"] = method;
std::vector< string > line; lines = split(_header, "\r\n");
method = split(lines.at(0), " ");
_request["Method:"] = method;
if (lines.size() > 0) { if (lines.size() > 0) {
for (std::vector< string >::iterator it = lines.begin() + 1; for (vec_string::iterator it = lines.begin() + 1; it < lines.end();
it < lines.end(); it++) { it++) {
line = split(*it, " "); line = split(*it, " ");
_request[line.at(0)] = _request[line.at(0)] = vec_string(line.begin() + 1, line.end());
std::vector< string >(line.begin() + 1, line.end());
} }
} }
if ((method.at(0) == "POST" || method.at(0) == "PUT") && if ((method.at(0) == "POST" || method.at(0) == "PUT") &&
@ -128,8 +112,8 @@ bool Client::parseHeader(Env *env) {
return true; return true;
} }
bool Client::check_method() { bool Client::check_method(void) {
std::vector< string > allowed; vec_string allowed;
if (_method != "GET" && _method != "POST" && _method != "DELETE" && if (_method != "GET" && _method != "POST" && _method != "DELETE" &&
_method != "PUT") _method != "PUT")
send_error(405); send_error(405);
@ -154,9 +138,7 @@ string Client::header_pick(string key, size_t id) {
return _request[key].at(id); return _request[key].at(id);
} }
inline string get_extension(string str) { return str.substr(str.rfind('.')); } void Client::answer(void) {
void Client::answer() {
cout << "Method: " << _method << "\n"; cout << "Method: " << _method << "\n";
cout << "URI: " << _uri << "\n"; cout << "URI: " << _uri << "\n";
cout << "Host: " << _host << "\n"; cout << "Host: " << _host << "\n";
@ -229,16 +211,15 @@ void Client::send_cgi(string cgi, string path) {
<< ret; << ret;
send_answer(ss.str()); send_answer(ss.str());
} }
/*
void Client::send_redir(int redir_code, string opt) { void Client::send_redir(int redir_code, string opt) {
switch (redir_code) { switch (redir_code) {
case 301: case 301:
return send_answer( return send_answer(
"HTTTP/1.1 301 Moved Permanently\r\nLocation: " + opt + "HTTTP/1.1 301 Moved Permanently\r\nLocation: " + opt + "\r\n\r\n");
"\r\n\r\n");
} }
} }
*/
void Client::send_error(int error_code) { void Client::send_error(int error_code) {
switch (error_code) { switch (error_code) {
case 400: case 400:
@ -262,7 +243,7 @@ void Client::send_error(int error_code) {
void Client::send_answer(string msg) { void Client::send_answer(string msg) {
#ifdef __linux__ #ifdef __linux__
print_block("Answer: ", msg); 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);

8
srcs/sock/Master.cpp

@ -128,8 +128,8 @@ void Master::refresh(Env *env) {
Server *Master::choose_server(Env *env, string host) { Server *Master::choose_server(Env *env, string host) {
std::vector< Server * > exact; std::vector< Server * > exact;
std::vector< Server * > inrange; std::vector< Server * > inrange;
std::vector< string > ip_listen; vec_string ip_listen;
std::vector< string > ip_required; vec_string ip_required;
ip_required = split(_listen.ip, "."); ip_required = split(_listen.ip, ".");
for (std::vector< Server * >::iterator sit = env->_servers.begin(); for (std::vector< Server * >::iterator sit = env->_servers.begin();
@ -145,8 +145,8 @@ Server *Master::choose_server(Env *env, string host) {
} }
bool is_inrange = true; bool is_inrange = true;
ip_listen = split((*it).ip, "."); ip_listen = split((*it).ip, ".");
std::vector< string >::iterator r = ip_required.begin(); vec_string::iterator r = ip_required.begin();
for (std::vector< string >::iterator l = ip_listen.end(); for (vec_string::iterator l = ip_listen.end();
l >= ip_listen.begin(); --l) { l >= ip_listen.begin(); --l) {
if (*l != *r && *l != "0") if (*l != *r && *l != "0")
is_inrange = false; is_inrange = false;

2
srcs/tools.cpp

@ -18,7 +18,7 @@ bool isInt(string str) {
return true; return true;
} }
std::vector< string > split(string str, string delim) { vec_string split(string str, string delim) {
string temp(str); string temp(str);
string token; string token;
size_t pos; size_t pos;

Loading…
Cancel
Save