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
#include "webserv.hpp"
typedef std::map< string, std::vector< string > > request_t;
typedef std::map< string, vec_string > request_t;
class Client {
int _fd;
ip_port_t _ip_port;
Master *_parent;
string _method;
string _uri;
string _host;
int _len;
bool _last_chunk;
Server *_server;
Route *_route;
string _header;
string _body;
Server *_server;
Route *_route;
string _method, _uri, _host, _header, _body;
int _len;
bool _last_chunk;
request_t _request;
public:
Client(int fd, ip_port_t ip_port, Master *parent);
~Client(void);
void clean(void);
bool getBody(string paquet);
bool parseHeader(Env *env);
string header_pick(string key, size_t id);
void answer();
bool check_method();
bool check_method(void);
void create_file(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_answer(string msg);
public:
Client(int fd, ip_port_t ip_port, Master *parent);
~Client(void);
bool getHeader(Env *env, string paquet);
bool getBody(string paquet);
void answer(void);
friend class Master;
};

4
includes/Master.hpp

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

4
includes/Route.hpp

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

15
includes/webserv.hpp

@ -48,15 +48,16 @@ class Client;
typedef std::map< string, JSONNode * > JSONObject;
typedef std::vector< JSONNode * > JSONList;
typedef std::vector< string > vec_string;
// tools
void *ft_memset(void *b, int c, size_t len);
bool isInt(string str);
std::vector< string > split(string str, string delim);
ip_port_t get_ip_port_t(string listen);
ip_port_t get_ip_port_t(string ip, int port);
string getMime(string path);
string read_file(string path);
void *ft_memset(void *b, int c, size_t len);
bool isInt(string str);
vec_string split(string str, string delim);
ip_port_t get_ip_port_t(string listen);
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);

11
srcs/debug.cpp

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

22
srcs/load/Route.cpp

@ -58,12 +58,12 @@ string Route::getReturn(void) { return _ret; }
*/
string Route::getIndex(string uri, string path) {
std::stringstream content;
std::stringstream ret;
DIR *dir;
struct dirent *entry;
struct stat info;
std::vector< string >::iterator it;
std::stringstream content;
std::stringstream ret;
DIR *dir;
struct dirent *entry;
struct stat info;
vec_string::iterator it;
if ((dir = opendir(path.c_str())) == NULL) {
return "";
@ -103,13 +103,13 @@ string Route::getIndex(string uri, string path) {
*/
string Route::correctUri(string uri) {
std::stringstream ret;
std::vector< string >::iterator it;
std::vector< string >::iterator it2;
std::stringstream ret;
vec_string::iterator it;
vec_string::iterator it2;
ret << "./" << _root;
std::vector< string > loc_split = split(_location, "/");
std::vector< string > uri_split = split(uri, "/");
vec_string loc_split = split(_location, "/");
vec_string uri_split = split(uri, "/");
it2 = uri_split.begin();
for (it = loc_split.begin(); it < loc_split.end(); it++) {
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) {
std::vector< string > req = split(uri, "/");
std::vector< string > root;
vec_string req = split(uri, "/");
vec_string root;
for (std::map< string, Route * >::iterator rit = _routes.begin();
rit != _routes.end(); rit++) {
root = split((*rit).first, "/");
cout << "Route: " << (*rit).first << "\n";
std::vector< string >::iterator root_it = root.begin();
for (std::vector< string >::iterator it = req.begin(); it < req.end();
it++) {
vec_string::iterator root_it = root.begin();
for (vec_string::iterator it = req.begin(); it < req.end(); it++) {
while (it != req.end() && *it == "")
it++;
if (*it != *(root_it++))

71
srcs/sock/Client.cpp

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

2
srcs/tools.cpp

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

Loading…
Cancel
Save