added trim in utils, added parse request header, mv pdf in docs
This commit is contained in:
@@ -26,4 +26,4 @@ struct Client
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -107,7 +107,11 @@ class Webserv
|
|||||||
//
|
//
|
||||||
void _serve_file(Client *client, std::string page);
|
void _serve_file(Client *client, std::string page);
|
||||||
void _exec_cgi_script(Client *client);
|
void _exec_cgi_script(Client *client);
|
||||||
void _parse_request();
|
void _parse_request(Client *client);
|
||||||
|
void _parse_request_line(Client *client, std::string rline);
|
||||||
|
void _parse_request_headers
|
||||||
|
( Client *client
|
||||||
|
, std::vector<std::string> list);
|
||||||
//
|
//
|
||||||
// END TMP HUGO TEST CGI
|
// END TMP HUGO TEST CGI
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,7 +7,8 @@
|
|||||||
# include <vector>
|
# include <vector>
|
||||||
|
|
||||||
std::vector<std::string> split(std::string input, char delimiter);
|
std::vector<std::string> split(std::string input, char delimiter);
|
||||||
char* itoa(int n);
|
std::string itoa(int n);
|
||||||
|
std::string trim(std::string str, char c);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
13
index.html
13
index.html
@@ -1,13 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title></title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<form method="post">
|
|
||||||
<input type="submit" value="submit">
|
|
||||||
</form>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
||||||
@@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
std::vector<std::string> split(std::string input, char delimiter)
|
std::vector<std::string> split(std::string input, char delimiter)
|
||||||
{
|
{
|
||||||
std::vector<std::string> answer;
|
std::vector<std::string> answer;
|
||||||
std::stringstream ss(input);
|
std::stringstream ss(input);
|
||||||
std::string temp;
|
std::string temp;
|
||||||
|
|
||||||
while (getline(ss, temp, delimiter))
|
while (getline(ss, temp, delimiter))
|
||||||
answer.push_back(temp);
|
answer.push_back(temp);
|
||||||
@@ -13,13 +13,19 @@ std::vector<std::string> split(std::string input, char delimiter)
|
|||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* itoa(int n)
|
std::string trim(std::string str, char c)
|
||||||
{
|
{
|
||||||
std::stringstream strs;
|
str = str.substr(str.find_first_not_of(c));
|
||||||
char * str;
|
str = str.substr(0, str.find_last_not_of(c) + 1);
|
||||||
|
|
||||||
strs << n;
|
return str;
|
||||||
str = (char*)(strs.str().c_str());
|
}
|
||||||
return (str);
|
|
||||||
|
std::string itoa(int n)
|
||||||
|
{
|
||||||
|
std::stringstream strs;
|
||||||
|
|
||||||
|
strs << n;
|
||||||
|
return ( strs.str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,87 @@ void Webserv::_read_request(Client *client)
|
|||||||
|
|
||||||
buf[ret] = '\0';
|
buf[ret] = '\0';
|
||||||
client->raw_request.append(buf);
|
client->raw_request.append(buf);
|
||||||
|
_parse_request(client);
|
||||||
|
|
||||||
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD, client);
|
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
void Webserv::_parse_request(Client *client)
|
||||||
|
{
|
||||||
|
std::string sub;
|
||||||
|
std::vector<std::string> list;
|
||||||
|
size_t pos;
|
||||||
|
|
||||||
|
pos = (client->raw_request).find("\r\n\r\n");
|
||||||
|
sub = (client->raw_request).substr(0, pos);
|
||||||
|
list = split(sub, '\n');
|
||||||
|
// request_line
|
||||||
|
_parse_request_line(client, *list.begin());
|
||||||
|
list.erase(list.begin());
|
||||||
|
// headers
|
||||||
|
_parse_request_headers(client, list);
|
||||||
|
// message-body
|
||||||
|
client->request.insert( std::pair<std::string, std::string>("Body-Message", &client->raw_request[pos + 4]) );
|
||||||
|
|
||||||
|
// TMP DEBUG
|
||||||
|
// std::map<std::string, std::string>::iterator itm;
|
||||||
|
// std::map<std::string, std::string>::iterator itm_end;
|
||||||
|
// std::cout << "\n====== REQUEST HEADERS\n";
|
||||||
|
// for (itm = client->request.begin(), itm_end = client->request.end(); itm != itm_end; itm++)
|
||||||
|
// std::cout << "[" << itm->first << "] [" << itm->second << "]\n";
|
||||||
|
// std::cout << "\n====== END\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Webserv::_parse_request_line(Client *client, std::string rline)
|
||||||
|
{
|
||||||
|
std::vector<std::string> sline;
|
||||||
|
std::string tmp;
|
||||||
|
|
||||||
|
sline = split(rline, ' ');
|
||||||
|
if (sline.size() != 3)
|
||||||
|
{
|
||||||
|
std::cerr << "err _parse_request_line(): ";
|
||||||
|
throw std::runtime_error("bad request-line header");
|
||||||
|
}
|
||||||
|
// method
|
||||||
|
tmp = ::trim(sline[0], ' ');
|
||||||
|
tmp = ::trim(tmp, '\r');
|
||||||
|
client->request.insert( std::pair<std::string, std::string>("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');
|
||||||
|
client->request.insert( std::pair<std::string, std::string>("Request-URI", tmp) );
|
||||||
|
// http version
|
||||||
|
tmp = ::trim(sline[2], ' ');
|
||||||
|
tmp = ::trim(tmp, '\r');
|
||||||
|
client->request.insert( std::pair<std::string, std::string>("HTTP-Version", tmp) );
|
||||||
|
}
|
||||||
|
|
||||||
|
void Webserv::_parse_request_headers
|
||||||
|
( Client *client
|
||||||
|
, 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++)
|
||||||
|
{
|
||||||
|
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');
|
||||||
|
client->request.insert( std::pair<std::string, std::string>(key, val) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,8 +83,8 @@ void Webserv::_insert_status_line(Client *client)
|
|||||||
void Webserv::_get_ressource(Client *client)
|
void Webserv::_get_ressource(Client *client)
|
||||||
{
|
{
|
||||||
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() ?
|
||||||
char buf[MAX_FILESIZE+1];
|
char buf[MAX_FILESIZE+1];
|
||||||
char *tmp;
|
const char *tmp;
|
||||||
|
|
||||||
// Mini parsing à l'arrache du PATH
|
// Mini parsing à l'arrache du PATH
|
||||||
std::string path;
|
std::string path;
|
||||||
@@ -128,10 +128,8 @@ void Webserv::_get_ressource(Client *client)
|
|||||||
client->response.append("Content-Type: text/html; charset=UTF-8\r\n");
|
client->response.append("Content-Type: text/html; charset=UTF-8\r\n");
|
||||||
|
|
||||||
client->response.append("Content-Length: ");
|
client->response.append("Content-Length: ");
|
||||||
// tmp = ::ft_itoa(ifd.gcount());
|
tmp = ::itoa(ifd.gcount()).c_str();
|
||||||
tmp = ::itoa(ifd.gcount());
|
|
||||||
client->response.append(tmp);
|
client->response.append(tmp);
|
||||||
// delete tmp;
|
|
||||||
client->response.append("\r\n");
|
client->response.append("\r\n");
|
||||||
|
|
||||||
// Body
|
// Body
|
||||||
|
|||||||
Reference in New Issue
Block a user