Multiples minors changes (cgi env, comment, ...)

This commit is contained in:
LuckyLaszlo
2022-08-16 04:00:33 +02:00
parent 2a1aec8f1d
commit 4602844f5a
8 changed files with 207 additions and 194 deletions

View File

@@ -75,10 +75,12 @@ Client & Client::operator=( Client const & rhs )
* PUBLIC MEMBER FUNCTIONS
*********************************************/
// http headers :
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
// https://www.ibm.com/docs/en/cics-ts/5.3?topic=protocol-http-requests
// https://www.tutorialspoint.com/http/http_requests.htm
/* HTTP Headers :
https://www.iana.org/assignments/http-fields/http-fields.xhtml
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
https://www.ibm.com/docs/en/cics-ts/5.3?topic=protocol-http-requests
https://www.tutorialspoint.com/http/http_requests.htm
*/
void Client::parse_request_headers(std::vector<ServerConfig> &servers)
{
if (raw_request.find(CRLF CRLF) == NPOS)
@@ -96,8 +98,8 @@ void Client::parse_request_headers(std::vector<ServerConfig> &servers)
if (status)
return;
assigned_server = ::_determine_process_server(this, servers);
assigned_location = ::_determine_location(*assigned_server, _request.abs_path);
assigned_server = _determine_process_server(this, servers);
assigned_location = _determine_location(*assigned_server, _request.abs_path);
_check_request_errors();
if (status)
return;
@@ -389,6 +391,7 @@ void Client::_parse_request_fields()
::str_map_key_tolower(_request.headers);
}
// TODO : I think its now useless. Probably to delete.
void Client::_parse_port_hostname(std::string host)
{
size_t pos;
@@ -453,6 +456,98 @@ void Client::_check_request_errors()
return;
}
ServerConfig *Client::_determine_process_server(Client *client, std::vector<ServerConfig> &servers)
{
/*
Behavior like this :
http://nginx.org/en/docs/http/request_processing.html
*/
std::string server_name = client->get_rq_headers("Host");
std::cerr << "server_name = " << server_name << "\n";
size_t pos = server_name.rfind(':');
if (pos != NPOS)
server_name.erase(pos);
std::cerr << "server_name = " << server_name << "\n";
std::vector<ServerConfig>::iterator it = servers.begin();
std::vector<ServerConfig>::iterator default_server = servers.end();
while (it != servers.end())
{
if (it->host == client->get_cl_lsocket()->host
&& it->port == client->get_cl_lsocket()->port)
{
if ( std::find(it->server_name.begin(), it->server_name.end(), server_name) != it->server_name.end() )
break;
else if (default_server == servers.end())
default_server = it;
}
++it;
}
if (it != servers.end())
return (&(*it));
else
return (&(*default_server));
}
// const?
const LocationConfig *Client::_determine_location(const ServerConfig &server, const std::string &path)
{
/* RULES ***
If a path coresponds exactly to a location, use that one
if no path coresponds then use the most correct one
most correct means the most precise branch that is still above
the point we are aiming for
New Rule for location paths, they never end in /
Sooo
If we get a url that ends in / ignore the last /
*/
std::string uri = path;
if (uri[uri.size() - 1] == '/' && uri.size() != 1)
uri.erase(uri.size() - 1);
for (std::vector<LocationConfig>::const_iterator it = server.locations.begin(); it != server.locations.end(); it++)
{
// std::cout << it->path << " -- ";
if (it->path.size() > uri.size())
continue ;
if (uri.compare(0, it->path.size(), it->path) == 0)
{
if (it->path.size() == uri.size())
return (&(*it));
else if (uri[it->path.size()] == '/')
return (&(*it));
// this works cuz only ever looking for a / burried in a longer path
}
}
return (&(server.locations.back()));
// /test/mdr
// /test/mdr/
// /test/mdrBST
/////// More stuff to check this still works with
// /test/test_
// /test/test_/
// /test/test_deeper
// /test/test_deeper/
// /test/test_deepei
// /test/test_deepei/
// /test/test_deeperi
// /test/test_deeper/super_deep/
// /test/aaaaaaaaaaa/super_deep/
}
/*********************************************
* OVERLOAD
*********************************************/