key fields headers in map are in lowercase

+ getters for this map are case insensitive
+ change request fiel 'path' for 'uri', 'query', and 'abs_path'
This commit is contained in:
hugogogo
2022-08-07 13:06:16 +02:00
parent 63ff42b57d
commit e0fd743b5b
7 changed files with 89 additions and 40 deletions

View File

@@ -74,6 +74,7 @@ void Client::parse_request()
std::vector<std::string> list;
size_t pos;
// DEBUG
std::cout << "\n"
<< "request:\n" << raw_request
<< "START _______________________\n\n"
@@ -87,7 +88,7 @@ std::cout << "\n"
list.erase(list.begin());
_parse_request_headers(list);
_parse_request_body(pos + 4);
_parse_port_hostname(_request.headers["Host"]);
_parse_port_hostname(this->get_rq_headers("Host"));
// add "raw_request.clear()" after parsing ? for little less memory usage ?
}
@@ -103,10 +104,14 @@ void Client::clear()
void Client::clear_request()
{
_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();
}
@@ -115,25 +120,24 @@ void Client::clear_request()
*********************************************/
// client side
int Client::get_cl_fd() const { return _fd; }
int Client::get_cl_fd() const { return _fd; }
std::string Client::get_cl_ip() const { return _ip; }
listen_socket * Client::get_cl_lsocket() const { return _lsocket; }
std::string Client::get_cl_port() const { return _port; }
// requette
http_method Client::get_rq_method()const { return _request.method; }
http_method Client::get_rq_method() const { return _request.method; }
std::string Client::get_rq_method_str() const
{
std::string method;
method = ::http_methods_to_str(_request.method);
return method;
}
std::string Client::get_rq_path() const { return _request.path; }
{ 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_headers(const std::string & key)
{ return _request.headers[key]; }
std::string Client::get_rq_headers(const std::string & key) const
{ return _request.headers.find(::str_tolower(key))->second; }
/*********************************************
@@ -160,13 +164,26 @@ void Client::_parse_request_line( std::string rline )
// 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;
_request.uri = tmp;
_parse_request_uri( tmp );
// http version
tmp = ::trim(sline[2], ' ');
tmp = ::trim(tmp, '\r');
_request.version = tmp;
}
void Client::_parse_request_uri( std::string uri )
{
size_t pos;
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( std::vector<std::string> list )
{
std::string key;
@@ -180,6 +197,7 @@ void Client::_parse_request_headers( std::vector<std::string> list )
key = (*it).substr( 0, pos );
key = ::trim(key, ' ');
key = ::trim(key, '\r');
key = ::str_tolower(key);
val = (*it).substr( pos + 1 );
val = ::trim(val, ' ');
val = ::trim(val, '\r');

View File

@@ -14,7 +14,9 @@
struct Request
{
http_method method;
std::string path;
std::string uri;
std::string abs_path;
std::string query;
std::string version;
std::map<std::string, std::string> headers;
std::string body;
@@ -38,10 +40,6 @@ class Client
size_t body_size;
unsigned int status;
// std::string port;
// std::string ip;
// listen_socket * lsocket;
// getters
int get_cl_fd() const;
std::string get_cl_port() const;
@@ -51,12 +49,14 @@ class Client
// requests getters
http_method get_rq_method() const;
std::string get_rq_method_str() const;
std::string get_rq_path() const;
std::string get_rq_uri() const;
std::string get_rq_abs_path() const;
std::string get_rq_query() const;
std::string get_rq_version() const;
std::string get_rq_body() const;
std::string get_rq_port() const;
std::string get_rq_hostname() const;
std::string get_rq_headers(const std::string & key);
std::string get_rq_headers(const std::string & key) const;
void parse_request();
void clear();
@@ -70,6 +70,7 @@ class Client
struct Request _request;
void _parse_request_line( std::string rline );
void _parse_request_uri( std::string uri );
void _parse_request_headers( std::vector<std::string> list );
void _parse_request_body( size_t pos );
void _parse_port_hostname(std::string host);
@@ -81,3 +82,4 @@ bool operator==(const Client& lhs, int fd);
bool operator==(int fd, const Client& rhs);
#endif

View File

@@ -105,3 +105,10 @@ bool operator==(const listen_socket& lhs, int fd)
bool operator==(int fd, const listen_socket& rhs)
{ return fd == rhs.fd; }
std::string str_tolower(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}

View File

@@ -6,6 +6,8 @@
# include <string>
# include <sstream>
# include <cstdlib> // atoi
# include <cctype> // tolower
# include <algorithm> // transform
# define CR "\r"
# define LF "\n"
@@ -46,5 +48,6 @@ std::string trim(std::string str, char c);
http_method str_to_http_method(std::string &str);
std::string http_methods_to_str(unsigned int methods);
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr);
std::string str_tolower(std::string str);
#endif

View File

@@ -3,7 +3,8 @@
bool Webserv::_is_cgi(Client *client)
{
if (client->get_rq_path().find("/cgi-bin/") != std::string::npos)
// TMP
if (client->get_rq_abs_path().find("/cgi-bin") != std::string::npos)
return true;
return false;
}
@@ -35,7 +36,7 @@ char** Webserv::_set_env(Client *client)
env[3] = _dup_env("GATEWAY_INTERFACE");
env[4] = _dup_env("PATH_INFO");
env[5] = _dup_env("PATH_TRANSLATED");
env[6] = _dup_env("QUERY_STRING");
env[6] = _dup_env("QUERY_STRING" , client->get_rq_query());
env[7] = _dup_env("REMOTE_ADDR" , client->get_cl_ip());
env[8] = _dup_env("REMOTE_HOST" , client->get_rq_headers("Host")); // just test
env[9] = _dup_env("REMOTE_IDENT"); // authentification not supported

View File

@@ -72,7 +72,7 @@ void Webserv::_construct_response(Client *client, ServerConfig &server)
client->status = 413;
return;
}
LocationConfig &location = _determine_location(server, client->get_rq_path());
LocationConfig &location = _determine_location(server, client->get_rq_uri());
_process_method(client, server, location);
}
@@ -135,7 +135,7 @@ void Webserv::_error_html_response(Client *client, ServerConfig &server)
void Webserv::_get(Client *client, ServerConfig &server, LocationConfig &location)
{
(void)server; // To remove from arg if we determine its useless
std::string path = client->get_rq_path();
std::string path = client->get_rq_uri();
if (path == "/") // TODO : index and autoindex
path.append(INDEX);
@@ -255,7 +255,7 @@ void Webserv::_post(Client *client, ServerConfig &server, LocationConfig &locati
WIP
https://www.rfc-editor.org/rfc/rfc9110.html#name-post
*/
std::string path = client->get_rq_path();
std::string path = client->get_rq_uri();
path.insert(0, location.root);
/* CGI Here ? */
@@ -321,7 +321,7 @@ void Webserv::_delete(Client *client, ServerConfig &server, LocationConfig &loca
WIP
https://www.rfc-editor.org/rfc/rfc9110.html#name-delete
*/
std::string path = client->get_rq_path();
std::string path = client->get_rq_uri();
path.insert(0, location.root);
/* CGI Here ? */