wip, need somes changes in ConfigParser.
+ _determine_location() + http_method changes + http_method switch in _construct_response() + ConfigParser::_str_to_method_type() moved to global utils.cpp
This commit is contained in:
@@ -9,29 +9,6 @@ void Webserv::_response(Client *client)
|
||||
_handle_last_signal();
|
||||
}
|
||||
|
||||
ServerConfig &Webserv::_determine_process_server(Client *client)
|
||||
{
|
||||
/*
|
||||
TODO : determine virtual server based on ip_address::port and server_name
|
||||
Ior now its just based on server_name.
|
||||
(maybe with a map< string, std::vector<ServerConfig> > where the string is "ip_adress::port")
|
||||
http://nginx.org/en/docs/http/request_processing.html
|
||||
*/
|
||||
|
||||
std::string &server_name = client->get_headers("Host");
|
||||
std::vector<ServerConfig>::iterator it = _servers.begin();
|
||||
while (it != _servers.end())
|
||||
{
|
||||
if ( std::find(it->server_name.begin(), it->server_name.end(), server_name) != it->server_name.end() )
|
||||
break;
|
||||
++it;
|
||||
}
|
||||
if (it != _servers.end())
|
||||
return (*it);
|
||||
else
|
||||
return (_servers.front());
|
||||
}
|
||||
|
||||
void Webserv::_send_response(Client *client, ServerConfig &server)
|
||||
{
|
||||
ssize_t ret;
|
||||
@@ -62,7 +39,10 @@ void Webserv::_send_response(Client *client, ServerConfig &server)
|
||||
|
||||
void Webserv::_construct_response(Client *client, ServerConfig &server)
|
||||
{
|
||||
client->status = 200;
|
||||
LocationConfig &location = _determine_location(server, client->get_path());
|
||||
|
||||
client->status = 200; // default value
|
||||
|
||||
client->response.append("Server: Webserv/0.1\r\n");
|
||||
|
||||
if (client->get_headers("Connection") == "close")
|
||||
@@ -70,8 +50,31 @@ void Webserv::_construct_response(Client *client, ServerConfig &server)
|
||||
else
|
||||
client->response.append("Connection: keep-alive\r\n");
|
||||
|
||||
// TODO : Method switch (GET, POST, DELETE)
|
||||
_get_ressource(client, server);
|
||||
|
||||
unsigned int allow_methods = ALL_METHODS; // TEMP VARIABLE
|
||||
// after update in ConfigParser, use the "allow_methods" of location.
|
||||
// TODO in ConfigParser : by default if no field in config file, "allow_methods" must be set to ALL_METHODS
|
||||
|
||||
if (client->get_method() == UNKNOWN)
|
||||
{
|
||||
client->status = 400;
|
||||
}
|
||||
else if (allow_methods & client->get_method())
|
||||
{
|
||||
if (client->get_method() & GET)
|
||||
_get_ressource(client, server, location);
|
||||
else if (client->get_method() & POST)
|
||||
_post(client, server, location);
|
||||
else if (client->get_method() & DELETE)
|
||||
_delete(client, server, location);
|
||||
}
|
||||
else
|
||||
{
|
||||
client->status = 405;
|
||||
client->response.append("Allow: ");
|
||||
client->response.append(::http_methods_to_str(allow_methods));
|
||||
client->response.append("\r\n");
|
||||
}
|
||||
|
||||
_insert_status_line(client, server);
|
||||
}
|
||||
@@ -114,7 +117,7 @@ void Webserv::_insert_status_line(Client *client, ServerConfig &server)
|
||||
#define ROOT "www"
|
||||
#define INDEX "index.html"
|
||||
#define MAX_FILESIZE 1000000 // (1Mo)
|
||||
void Webserv::_get_ressource(Client *client, ServerConfig &server)
|
||||
void Webserv::_get_ressource(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
{
|
||||
std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
|
||||
char buf[MAX_FILESIZE+1];
|
||||
@@ -171,9 +174,8 @@ void Webserv::_get_ressource(Client *client, ServerConfig &server)
|
||||
client->response.append("Content-Type: text/html; charset=UTF-8\r\n"); // TODO : determine Content-Type
|
||||
|
||||
client->response.append("Content-Length: ");
|
||||
|
||||
tmp = ::itos(ifd.gcount());
|
||||
client->response.append(tmp.c_str());
|
||||
client->response.append(tmp);
|
||||
client->response.append("\r\n");
|
||||
|
||||
// Body
|
||||
@@ -184,3 +186,63 @@ void Webserv::_get_ressource(Client *client, ServerConfig &server)
|
||||
}
|
||||
}
|
||||
|
||||
void Webserv::_post(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
{
|
||||
/*
|
||||
TODO
|
||||
*/
|
||||
(void)0;
|
||||
}
|
||||
|
||||
void Webserv::_delete(Client *client, ServerConfig &server, LocationConfig &location)
|
||||
{
|
||||
/*
|
||||
TODO
|
||||
*/
|
||||
(void)0;
|
||||
}
|
||||
|
||||
ServerConfig &Webserv::_determine_process_server(Client *client)
|
||||
{
|
||||
/*
|
||||
TODO : determine virtual server based on ip_address::port and server_name
|
||||
Ior now its just based on server_name.
|
||||
(maybe with a map< string, std::vector<ServerConfig> > where the string is "ip_adress::port")
|
||||
http://nginx.org/en/docs/http/request_processing.html
|
||||
*/
|
||||
|
||||
std::string &server_name = client->get_headers("Host");
|
||||
std::vector<ServerConfig>::iterator it = _servers.begin();
|
||||
while (it != _servers.end())
|
||||
{
|
||||
if ( std::find(it->server_name.begin(), it->server_name.end(), server_name) != it->server_name.end() )
|
||||
break;
|
||||
++it;
|
||||
}
|
||||
if (it != _servers.end())
|
||||
return (*it);
|
||||
else
|
||||
return (_servers.front());
|
||||
}
|
||||
|
||||
LocationConfig &Webserv::_determine_location(ServerConfig &server, std::string &path)
|
||||
{
|
||||
/*
|
||||
Assume there is at least one location in vector<LocationConfig> for path "/"
|
||||
TODO in ConfigParser :
|
||||
If no location block in config file, one need to be generated
|
||||
for path "/", and filled with fields "root" and "index" based on parent server block
|
||||
*/
|
||||
|
||||
std::vector<LocationConfig>::iterator it = server.locations.begin();
|
||||
while (it != server.locations.end())
|
||||
{
|
||||
if (it->path.compare(0, path.size(), path))
|
||||
break;
|
||||
++it;
|
||||
}
|
||||
if (it != server.locations.end())
|
||||
return (*it);
|
||||
else
|
||||
return (server.locations.front());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user