fixed mistake in _read_request()

+ changes to some log output
This commit is contained in:
LuckyLaszlo
2022-08-13 19:17:32 +02:00
parent 058701f657
commit b0949615c8
6 changed files with 35 additions and 36 deletions

View File

@@ -92,7 +92,7 @@ void Client::parse_request_headers(std::vector<ServerConfig> &servers)
_parse_request_fields(); _parse_request_fields();
// DEBUG // DEBUG
print_client("headers"); // print_client("headers");
if (status) if (status)
return; return;
@@ -104,7 +104,7 @@ print_client("headers");
_parse_port_hostname(this->get_rq_headers("Host")); // use getter for headers because it works case insensitive _parse_port_hostname(this->get_rq_headers("Host")); // use getter for headers because it works case insensitive
// DEBUG // DEBUG
std::cerr << get_rq_method_str() << " " << get_rq_target() << " " << get_rq_version() << "\n"; // std::cerr << get_rq_method_str() << " " << get_rq_target() << " " << get_rq_version() << "\n";
// dont clear raw_request, we need it for future reparsing of body // dont clear raw_request, we need it for future reparsing of body
// see call of parse_request() in _read_request() // see call of parse_request() in _read_request()
@@ -113,6 +113,7 @@ std::cerr << get_rq_method_str() << " " << get_rq_target() << " " << get_rq_vers
void Client::parse_request_body() void Client::parse_request_body()
{ {
std::cerr << "parse_request_body()\n";
size_t pos; size_t pos;
pos = raw_request.find(CRLF CRLF); pos = raw_request.find(CRLF CRLF);
@@ -142,28 +143,29 @@ void Client::parse_request_body()
pos = 0; pos = 0;
while (chunk_size != 0) while (chunk_size != 0)
{ {
if (pos > _request.body.size()) /* if (pos > _request.body.size())
{ {
std::cerr << "parse_request_body(), pos > size()\n"; std::cerr << "parse_request_body(), pos > size()\n";
// status = 400; // status = 400;
return; return;
} } */
/*
if (pos == _request.body.size()) if (pos == _request.body.size())
{ {
std::cerr << "parse_request_body(), will reread till last chunk\n"; std::cerr << "parse_request_body(), will reread till last chunk\n";
return; return;
} } */
endptr_copy = endptr; /* endptr_copy = endptr; */
(void)endptr_copy;
chunk_size = std::strtoul(&_request.body[pos], &endptr, 16); chunk_size = std::strtoul(&_request.body[pos], &endptr, 16);
if (chunk_size == LONG_MAX && errno == ERANGE) /* if (chunk_size == LONG_MAX && errno == ERANGE)
status = 413; status = 413; */
if (endptr == endptr_copy) /* if (endptr == endptr_copy)
{ {
std::cerr << "parse_request_body(), no conversion possible\n"; std::cerr << "parse_request_body(), no conversion possible\n";
return; return;
} } */
chunk_field_end = _request.body.find(CRLF, pos); chunk_field_end = _request.body.find(CRLF, pos);
@@ -184,6 +186,11 @@ void Client::parse_request_body()
} }
else else
{ {
std::cerr << "Content-Length = " << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n";
std::cerr << "raw_request.size() - pos = " << raw_request.size() - pos << "\n";
_request.body = raw_request.substr(pos);
std::cerr << "_request.body.size() = " << _request.body.size() << "\n";
if (raw_request.size() - pos >= std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10)) if (raw_request.size() - pos >= std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10))
{ {
_request.body = raw_request.substr(pos); _request.body = raw_request.substr(pos);

View File

@@ -310,7 +310,7 @@ void ConfigParser::_set_location_values(LocationConfig *location, \
&& tmp_val[0] != "303" && tmp_val[0] != "307" && tmp_val[0] != "303" && tmp_val[0] != "307"
&& tmp_val[0] != "308") && tmp_val[0] != "308")
throw std::invalid_argument("bad redirect status"); throw std::invalid_argument("bad redirect status");
std::cout << tmp_val[1] << '\n'; // std::cout << tmp_val[1] << '\n';
if (tmp_val[1].compare(0, 7, "http://") if (tmp_val[1].compare(0, 7, "http://")
&& tmp_val[1].compare(0, 8, "https://")) && tmp_val[1].compare(0, 8, "https://"))
throw std::invalid_argument("bad redirect uri"); throw std::invalid_argument("bad redirect uri");

View File

@@ -19,7 +19,7 @@ int main(int ac, char **av)
ConfigParser configParser(config.c_str()); ConfigParser configParser(config.c_str());
configParser._print_content(); // configParser._print_content();
// i don't love that servers has to be a pointer... // i don't love that servers has to be a pointer...
std::vector<ServerConfig>* servers = configParser.parse(); std::vector<ServerConfig>* servers = configParser.parse();
@@ -27,8 +27,9 @@ int main(int ac, char **av)
// use an iterator you moron // use an iterator you moron
for (std::vector<ServerConfig>::iterator it = servers->begin(); it < servers->end(); it++) for (std::vector<ServerConfig>::iterator it = servers->begin(); it < servers->end(); it++)
{ {
(void)0;
// std::cout << it->server_name << " "; // std::cout << it->server_name << " ";
it->print_all(); // it->print_all();
} }

View File

@@ -3,12 +3,13 @@
std::string Webserv::_replace_url_root(Client *client, std::string path) std::string Webserv::_replace_url_root(Client *client, std::string path)
{ {
std::cerr << "path before = " << path << "\n"; // DEBUG std::cerr << "assigned_location->path = " << client->assigned_location->path << "\n"; // debug
std::cerr << "path before = " << path << "\n"; // DEBUG
if (client->assigned_location->path == "/") if (client->assigned_location->path == "/")
path.insert(0, client->assigned_location->root); path.insert(0, client->assigned_location->root);
else else
path.replace(0, client->assigned_location->path.size(), client->assigned_location->root); path.replace(0, client->assigned_location->path.size(), client->assigned_location->root);
std::cerr << "path after = " << path << "\n"; // DEBUG std::cerr << "path after = " << path << "\n"; // DEBUG
return path; return path;
} }
@@ -20,7 +21,6 @@ void Webserv::_get(Client *client, std::string &path)
// Index/Autoindex block // Index/Autoindex block
if (eval_file_type(path) == IS_DIR) if (eval_file_type(path) == IS_DIR)
{ {
std::cout << "made it to Index/Autoindex\n";
if (path[path.size() - 1] != '/') if (path[path.size() - 1] != '/')
path.push_back('/'); path.push_back('/');
for (size_t i = 0; i < client->assigned_location->index.size(); i++) for (size_t i = 0; i < client->assigned_location->index.size(); i++)
@@ -50,7 +50,7 @@ void Webserv::_get_file(Client *client, const std::string &path)
std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ? std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
std::stringstream buf; std::stringstream buf;
std::cout << "made it to get_file\n"; std::cout << "_get_file()\n";
if (access(path.c_str(), F_OK) == -1) if (access(path.c_str(), F_OK) == -1)
{ {
@@ -103,16 +103,15 @@ void Webserv::_get_file(Client *client, const std::string &path)
// const? // const?
void Webserv::_autoindex(Client *client, const std::string &path) void Webserv::_autoindex(Client *client, const std::string &path)
{ {
std::cout << "made it to _autoindex\n"; std::cout << "_autoindex()\n";
std::string dir_list; std::string dir_list;
DIR *dir; DIR *dir;
struct dirent *ent; struct dirent *ent;
std::cout << "location root: " << client->assigned_location->root << " location path: " // std::cout << "location root: " << client->assigned_location->root << " location path: " << client->assigned_location->path << '\n';
<< client->assigned_location->path << '\n';
std::cout << "Path in auto is: " << path << '\n'; // std::cout << "Path in auto is: " << path << '\n';
if ( (dir = opendir(path.c_str()) ) != NULL) if ( (dir = opendir(path.c_str()) ) != NULL)
{ {
dir_list.append(AUTOINDEX_START); dir_list.append(AUTOINDEX_START);
@@ -123,7 +122,7 @@ void Webserv::_autoindex(Client *client, const std::string &path)
/* print all the files and directories within directory */ /* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) while ((ent = readdir (dir)) != NULL)
{ {
std::cout << "ent: " << ent->d_name << '\n'; // std::cout << "ent: " << ent->d_name << '\n';
if (strcmp(".", ent->d_name) == 0) if (strcmp(".", ent->d_name) == 0)
continue ; continue ;
dir_list.append("<a href=\""); dir_list.append("<a href=\"");

View File

@@ -37,7 +37,6 @@ int Webserv::_read_request(Client *client)
char buf[BUFSIZE]; char buf[BUFSIZE];
ssize_t ret; ssize_t ret;
std::cerr << "call recv()" << "\n" ;
ret = ::recv(client->get_cl_fd(), buf, BUFSIZE, 0); ret = ::recv(client->get_cl_fd(), buf, BUFSIZE, 0);
std::cerr << "recv() on fd(" << client->get_cl_fd() << ") returned = " << ret << "\n" ; std::cerr << "recv() on fd(" << client->get_cl_fd() << ") returned = " << ret << "\n" ;
if (ret == -1) if (ret == -1)
@@ -51,6 +50,8 @@ int Webserv::_read_request(Client *client)
return READ_CLOSE; return READ_CLOSE;
} }
client->raw_request.append(buf, ret); client->raw_request.append(buf, ret);
// ::print_special(client->raw_request);
// std::cerr << "__raw_request__\n" << client->raw_request << "\n______\n"; // DEBUG
if (!client->header_complete) if (!client->header_complete)
{ {
@@ -61,8 +62,7 @@ int Webserv::_read_request(Client *client)
{ {
if (client->get_rq_headers("Content-Type").empty() if (client->get_rq_headers("Content-Type").empty()
&& client->get_rq_headers("Content-Length").empty() && client->get_rq_headers("Content-Length").empty()
// && client->get_rq_headers("Transfer-Encoding").empty() && client->get_rq_headers("Transfer-Encoding").empty())
)
return READ_COMPLETE; // No body case return READ_COMPLETE; // No body case
} }
else if (client->raw_request.size() > MAX_HEADER_SIZE) else if (client->raw_request.size() > MAX_HEADER_SIZE)
@@ -71,7 +71,7 @@ int Webserv::_read_request(Client *client)
return READ_COMPLETE; return READ_COMPLETE;
} }
} }
else if (client->header_complete) if (client->header_complete)
{ {
// client->read_body_size += ret; // Not accurate, part of body could have been read with headers, unused for now // client->read_body_size += ret; // Not accurate, part of body could have been read with headers, unused for now
client->parse_request_body(); client->parse_request_body();

View File

@@ -85,8 +85,7 @@ void Webserv::_construct_response(Client *client)
void Webserv::_process_method(Client *client, std::string &path) void Webserv::_process_method(Client *client, std::string &path)
{ {
std::cerr << "assigned_location->path = " << client->assigned_location->path << "\n"; // debug std::cerr << "allow_methods = " << http_methods_to_str(client->assigned_location->allow_methods) << "\n"; // debug
std::cerr << "allow_methods = " << client->assigned_location->allow_methods << "\n"; // debug
switch (client->get_rq_method()) switch (client->get_rq_method())
{ {
@@ -182,8 +181,6 @@ ServerConfig *_determine_process_server(Client *client, std::vector<ServerConfig
// Temporary Global Scope. Probably move to Client in the future. // Temporary Global Scope. Probably move to Client in the future.
const LocationConfig *_determine_location(const ServerConfig &server, const std::string &path) const LocationConfig *_determine_location(const ServerConfig &server, const std::string &path)
{ {
std::cout << "determin location path sent: " << path << '\n';
/* RULES *** /* RULES ***
If a path coresponds exactly to a location, use that one If a path coresponds exactly to a location, use that one
@@ -205,13 +202,8 @@ If we get a url that ends in / ignore the last /
for (std::vector<LocationConfig>::const_iterator it = server.locations.begin(); it != server.locations.end(); it++) 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()) if (it->path.size() > uri.size())
{
std::cout << "skipping this one\n";
continue ; continue ;
}
if (uri.compare(0, it->path.size(), it->path) == 0) if (uri.compare(0, it->path.size(), it->path) == 0)
{ {