Merge branch 'hugo2'

+ changed the appropriate getters for client
+ moved the functions to parse the http message
  (first line, headers, and body)
  outside client, in parsing_http_message.cpp
+ resolved some of the throw problems
This commit is contained in:
hugogogo
2022-08-09 15:54:40 +02:00
24 changed files with 205391 additions and 214 deletions

View File

@@ -6,13 +6,29 @@
*********************************************/
Client::Client()
: fd(0),
lsocket(NULL),
status(0),
: status(0),
header_complete(false),
read_body_size(0),
assigned_server(NULL),
assigned_location(NULL)
assigned_location(NULL),
_fd(0),
_port(""),
_ip(""),
_lsocket(NULL)
{
return;
}
Client::Client(int afd, listen_socket *lsocket, std::string aport, std::string aip)
: status(0),
header_complete(false),
read_body_size(0),
assigned_server(NULL),
assigned_location(NULL),
_fd(afd),
_port(aport),
_ip(aip),
_lsocket(lsocket)
{
return;
}
@@ -21,12 +37,33 @@ Client::~Client() {
return;
}
// copy constructor :
// Client::Client( Client const & src ) {}
// assignement operator :
// Client & Client::operator=( Client const & rhs ) {}
// WIP not sure fo what is more logic here
Client::Client( Client const & src )
: status ( src.status ),
header_complete ( src.header_complete ),
read_body_size ( src.read_body_size ),
assigned_server ( src.assigned_server ),
assigned_location ( src.assigned_location ),
_fd ( src._fd ),
_port ( src._port ),
_ip ( src._ip ),
_lsocket ( src._lsocket )
{
raw_request = src.raw_request;
response = src.response;
// buf = strdup(src.buf); // TODO: this doesn't work
return;
}
// WIP placeholder because of const values
Client & Client::operator=( Client const & rhs )
{
if ( this != &rhs )
{
// stuff
}
return *this;
}
/*********************************************
@@ -39,22 +76,35 @@ Client::~Client() {
// https://www.tutorialspoint.com/http/http_requests.htm
void Client::parse_request()
{
std::string sub;
std::vector<std::string> list;
size_t pos;
std::map<std::string, std::string> headers;
std::string body;
pos = (raw_request).find(CRLF CRLF);
sub = (raw_request).substr(0, pos);
list = split(sub, '\n');
// request_line
_parse_request_line(*list.begin());
list.erase(list.begin());
// headers
_parse_request_headers(list);
//body- message
_parse_request_body(pos + 4);
// DEBUG
std::cout << "\nREQUEST ____________\n" << raw_request << "\n_____________\n";
// add "raw_request.clear()" after parsing ? for little less memory usage ?
_parse_request_line();
_parse_request_headers();
_parse_request_body();
_parse_port_hostname(this->get_rq_headers("Host"));
raw_request.clear();
}
bool Client::fill_script_path(std::string script)
{
size_t pos;
int len = script.size();
std::string path = this->get_rq_abs_path();
std::string tmp;
pos = path.find(script);
if (pos == 0)
{
tmp = path.substr(0, pos + len);
_request.script.path = "./srcs" + tmp; // TODO: root path ?
_request.script.info = path.substr(pos + len);
return true;
}
return false;
}
void Client::clear()
@@ -71,88 +121,135 @@ void Client::clear()
void Client::clear_request()
{
clear_script();
_request.method = UNKNOWN;
_request.path.clear();
_request.uri.clear();
_request.version.clear();
_request.headers.clear();
_request.body.clear();
_request.abs_path.clear();
_request.query.clear();
_request.port.clear();
_request.hostname.clear();
}
void Client::clear_script()
{
_request.script.path.clear();
_request.script.info.clear();
}
/*********************************************
* GETTERS
*********************************************/
// client side
int Client::get_cl_fd() const { return _fd; }
const std::string & Client::get_cl_ip() const { return _ip; }
const std::string & Client::get_cl_port() const { return _port; }
const listen_socket * Client::get_cl_lsocket() const { return _lsocket; }
// requette
http_method Client::get_rq_method() const { return _request.method; }
std::string Client::get_rq_method_str() const
{ return ::http_methods_to_str(_request.method); }
std::string Client::get_rq_uri() const { return _request.uri; }
std::string Client::get_rq_abs_path() const { return _request.abs_path; }
std::string Client::get_rq_query() const { return _request.query; }
std::string Client::get_rq_version() const { return _request.version; }
std::string Client::get_rq_body() const { return _request.body; }
std::string Client::get_rq_port() const { return _request.port; }
std::string Client::get_rq_hostname() const { return _request.hostname; }
std::string Client::get_rq_script_path()const { return _request.script.path; }
std::string Client::get_rq_script_info()const { return _request.script.info; }
std::string Client::get_rq_headers(const std::string & key) const
{
std::map<std::string, std::string>::const_iterator it;
it = _request.headers.find(::str_tolower(key));
if (it == _request.headers.end())
return "";
return it->second;
}
http_method Client::get_method() { return _request.method; }
std::string &Client::get_path() { return _request.path; }
std::string &Client::get_version() { return _request.version; }
std::string &Client::get_body() { return _request.body; }
std::string &Client::get_headers(const std::string &key) { return _request.headers[key]; }
/*********************************************
* PRIVATE MEMBER FUNCTIONS
*********************************************/
void Client::_parse_request_line( std::string rline )
void Client::_parse_request_line()
{
std::vector<std::string> sline;
std::string tmp;
std::vector<std::string> line;
int ret;
sline = split(rline, ' ');
if (sline.size() != 3)
ret = ::parse_http_first_line(raw_request, line);
if (ret != 3)
{
std::cerr << "err _parse_request_line(): ";
throw std::runtime_error("bad request-line header");
std::cerr << "err _parse_first_line(): wrong number of elements (" << ret << " instead of 3)\n";
status = 400; // "bad request"
}
// method
tmp = ::trim(sline[0], ' ');
tmp = ::trim(tmp, '\r');
_request.method = str_to_http_method(tmp);
// TODO uri in request_line
// https://www.rfc-editor.org/rfc/rfc7230#section-5.3
// https://stackoverflow.com/questions/40311306/when-is-absoluteuri-used-from-the-http-request-specs
tmp = ::trim(sline[1], ' ');
tmp = ::trim(tmp, '\r');
_request.path = tmp;
// http version
tmp = ::trim(sline[2], ' ');
tmp = ::trim(tmp, '\r');
_request.version = tmp;
}
void Client::_parse_request_headers( std::vector<std::string> list )
{
std::string key;
std::string val;
std::vector<std::string>::iterator it;
size_t pos;
for (it = list.begin(); it != list.end(); it++)
else
{
pos = (*it).find(':');
key = (*it).substr( 0, pos );
key = ::trim(key, ' ');
key = ::trim(key, '\r');
val = (*it).substr( pos + 1 );
val = ::trim(val, ' ');
val = ::trim(val, '\r');
_request.headers.insert( std::pair<std::string, std::string>(key, val) );
_request.method = str_to_http_method(line[0]);
_request.uri = line[1];
_parse_request_uri(line[1]);
_request.version = line[2];
}
}
void Client::_parse_request_body( size_t pos )
void Client::_parse_request_uri( std::string uri )
{
// TODO : a revoir avec une std::string,
// pour ne pas avoir le probleme d'un '0' qui marque la fin des données
std::string body = &raw_request[pos];
size_t pos;
_request.body = body;
pos = uri.find("?");
if (pos != std::string::npos)
_request.query = uri.substr(pos + 1);
else
_request.query = "";
_request.abs_path = uri.substr(0, pos);
}
void Client::_parse_request_headers()
{
// TODO: check error and adjust status
_request.headers = ::parse_http_headers(raw_request);
}
void Client::_parse_request_body()
{
// TODO: check error and adjust status
_request.body = ::parse_http_body(raw_request);
}
void Client::_parse_port_hostname(std::string host)
{
size_t pos;
if (host == "")
std::cerr << "no host\n";
pos = host.find(':');
// port :
if (pos == std::string::npos)
_request.port = "4040"; // TODO: make equal to default port in config
else
_request.port = host.substr(pos);
if (_request.port == ":")
_request.port = "";
// hostname :
_request.hostname = host.substr(0, pos);
}
/*********************************************
* OVERLOAD
*********************************************/
bool operator==(const Client& lhs, const Client& rhs)
{ return lhs.fd == rhs.fd; }
{ return lhs.get_cl_fd() == rhs.get_cl_fd(); }
bool operator==(const Client& lhs, int fd)
{ return lhs.fd == fd; }
{ return lhs.get_cl_fd() == fd; }
bool operator==(int fd, const Client& rhs)
{ return fd == rhs.fd; }
{ return fd == rhs.get_cl_fd(); }