+ script output verification works (field duplicate and status)
+ parsing_message_http :
- the file is deleted
- of the three functions it contained, only one still exist
- 'parse_http_headers' is now in utils
+ changes in utils :
- http headers parsing doesn't change key case itself
- a new function change key case tolower case in map<str, str>
- added split_trim() that perform a split and a trim at once
- resolved pbm in trim
- added print_special to print special char '\r' and '\n'
- del_line became extract_line, because it returns the deleted line
- added get line, that does the same without deleting
- moved http_header in utils, and made it more consistent
+ in Client :
- parse_request is now named parse_request_headers to work with parse body
- private function _parse_request_headers is then now _parse_request_fields
(because it doesn't take care of request first line, but only fields)
- added a debug function 'print_client'
165 lines
4.1 KiB
C++
165 lines
4.1 KiB
C++
|
|
#include "Webserv.hpp"
|
|
|
|
void Webserv::_get(Client *client)
|
|
{
|
|
std::string path = client->get_rq_abs_path();
|
|
|
|
std::cerr << "path before = " << path << "\n"; // DEBUG
|
|
if (client->assigned_location->path == "/")
|
|
path.insert(0, client->assigned_location->root);
|
|
else
|
|
path.replace(0, client->assigned_location->path.size(), client->assigned_location->root);
|
|
std::cerr << "path after = " << path << "\n"; // DEBUG
|
|
|
|
// TMP HUGO ( We move this in process_switch() )
|
|
//
|
|
std::string script_output;
|
|
if (_is_cgi(client))
|
|
{
|
|
script_output = _exec_cgi(client);
|
|
_check_script_output(client, script_output);
|
|
client->response += script_output;
|
|
return;
|
|
}
|
|
//
|
|
// END TMP HUGO
|
|
|
|
// Index/Autoindex block
|
|
if (eval_file_type(path) == IS_DIR)
|
|
{
|
|
std::cout << "made it to Index/Autoindex\n";
|
|
if (path[path.size() - 1] != '/')
|
|
path.push_back('/');
|
|
for (size_t i = 0; i < client->assigned_location->index.size(); i++)
|
|
{
|
|
if (eval_file_type(path + client->assigned_location->index[i]) == IS_FILE)
|
|
{
|
|
path.append(client->assigned_location->index[i]);
|
|
_get_file(client, path);
|
|
return ;
|
|
}
|
|
}
|
|
if (client->assigned_location->autoindex == true)
|
|
_autoindex(client, path);
|
|
}
|
|
else
|
|
_get_file(client, path);
|
|
}
|
|
|
|
# define MAX_FILESIZE 1000000 // (1Mo)
|
|
void Webserv::_get_file(Client *client, const std::string &path)
|
|
{
|
|
/*
|
|
std::ios::binary
|
|
https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
|
|
tldr : its seems to not be so simple to do read/write of binary file in a portable way.
|
|
*/
|
|
std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
|
|
std::stringstream buf;
|
|
|
|
std::cout << "made it to get_file\n";
|
|
|
|
if (access(path.c_str(), F_OK) == -1)
|
|
{
|
|
std::perror("err access()");
|
|
client->status = 404;
|
|
return ;
|
|
}
|
|
|
|
if (access(path.c_str(), R_OK) == -1)
|
|
{
|
|
std::perror("err access()");
|
|
client->status = 403;
|
|
return ;
|
|
}
|
|
|
|
ifd.open(path.c_str(), std::ios::ate);
|
|
if (!ifd)
|
|
{
|
|
std::cerr << path << ": ifd.open fail" << '\n';
|
|
client->status = 500;
|
|
}
|
|
else
|
|
{
|
|
/* std::streampos size = ifd.tellg();
|
|
// WIP Low priority : Chunk or not chunk (if filesize too big)
|
|
if (size > MAX_FILESIZE)
|
|
{
|
|
// Then chunk
|
|
client->status = 500; // WIP temp
|
|
std::cerr << "File too large for non chunk body\n";
|
|
return ;
|
|
} */
|
|
|
|
ifd.seekg(0, std::ios::beg);
|
|
buf << ifd.rdbuf();
|
|
if (!ifd || !buf)
|
|
{
|
|
std::cerr << path << ": ifd.read fail" << '\n';
|
|
client->status = 500;
|
|
}
|
|
else
|
|
{
|
|
client->status = 200;
|
|
std::string file_ext = _determine_file_extension(path);
|
|
_append_body(client, buf.str(), file_ext);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Webserv::_autoindex(Client *client, std::string &path)
|
|
{
|
|
std::cout << "made it to _autoindex\n";
|
|
|
|
std::string dir_list;
|
|
DIR *dir;
|
|
struct dirent *ent;
|
|
|
|
std::cout << "location root: " << client->assigned_location->root << " location path: "
|
|
<< client->assigned_location->path << '\n';
|
|
|
|
std::cout << "Path in auto is: " << path << '\n';
|
|
if ( (dir = opendir(path.c_str()) ) != NULL)
|
|
{
|
|
dir_list.append(AUTOINDEX_START);
|
|
dir_list.append(path);
|
|
dir_list.append(AUTOINDEX_MID1);
|
|
dir_list.append(path);
|
|
dir_list.append(AUTOINDEX_MID2);
|
|
/* print all the files and directories within directory */
|
|
while ((ent = readdir (dir)) != NULL)
|
|
{
|
|
std::cout << "ent: " << ent->d_name << '\n';
|
|
if (strcmp(".", ent->d_name) == 0)
|
|
continue ;
|
|
dir_list.append("<a href=\"");
|
|
dir_list.append(client->assigned_location->path + "/");
|
|
dir_list.append(ent->d_name);
|
|
dir_list.append("\">");
|
|
dir_list.append(ent->d_name);
|
|
dir_list.append("</a>");
|
|
dir_list.append("\r\n"); // is this right?
|
|
}
|
|
|
|
// <a href="http://nginx.org/">nginx.org</a>.<br/>
|
|
// <a href="/test/test_deeper/index1.html">index1.html</a>
|
|
|
|
// apparently this is more than good enough!
|
|
// <a href="/test/test_deeper/..">..</a>
|
|
|
|
dir_list.append(AUTOINDEX_END);
|
|
// std::cout << "\n\n" << dir_list << '\n';
|
|
closedir (dir);
|
|
_append_body(client, dir_list, "html");
|
|
}
|
|
else
|
|
{
|
|
// in theory not possible cuz we already checked...
|
|
/* could not open directory */
|
|
// perror ("");
|
|
std::cout << "could not open dir\n";
|
|
return ;
|
|
}
|
|
}
|