wip cgi env variables

This commit is contained in:
hugogogo
2022-08-06 11:34:48 +02:00
parent 83536ee8ad
commit 6f171ec07b
6 changed files with 66 additions and 14 deletions

View File

@@ -174,12 +174,15 @@
#### questions :
- le cgi-script doit renvoyer au moins un header suivit d'une ligne vide
- ce header est soit "Content-Type", soit "Location", soit "Status"
- il peut dans certains cas envoyer d'autres headers
- le serveur doit verifier qu'il n'y a pas de doublons dans les headers
- le serveur doit verifier le formatage des headers (typiquement l'encodage, par exemple pour les newlines)
- ? comment on passe le body-message au script ? section 4.2
-> en ecrivant le body dans le fd temp sur lequel on a branché stdin et stdout avant d'execve le script
- ? on doit gerer l'authentification ?
- ? pourquoi on doit construire un script-cgi ? section 3.3
-> pas pour l'instant on va dire :p
- ? pourquoi on construit un script-cgi ? section 3.3
- ? si l'uri correspond au script-cgi, ca appel le script donc ? section 3.3

View File

@@ -43,16 +43,20 @@ void Client::parse_request()
list.erase(list.begin());
// headers
_parse_request_headers(list);
//body- message
// body-message
_parse_request_body(pos + 4);
// port and hostname
_parse_port_hostname(_request.headers["Host"]);
}
std::string Client::get_method() { return _request.method; }
std::string Client::get_path() { return _request.path; }
std::string Client::get_version() { return _request.version; }
std::string Client::get_body() { return _request.body; }
std::string Client::get_method() { return _request.method; }
std::string Client::get_path() { return _request.path; }
std::string Client::get_version() { return _request.version; }
std::string Client::get_body() { return _request.body; }
std::string Client::get_port() { return _request.port; }
std::string Client::get_hostname() { return _request.hostname; }
std::string Client::get_headers(std::string key) { return _request.headers[key]; }
/*********************************************
@@ -113,4 +117,25 @@ void Client::_parse_request_body( size_t pos )
_request.body = body;
}
void Client::_parse_port_hostname(std::string host)
{
size_t pos;
if (host == "")
{
std::cerr << "no host\n";
throw std::runtime_error("no host in request");
}
pos = host.find(':');
// port :
if (pos == std::string::npos)
_request.port = "4040"; // TODO: make equal to default port in config
else
_request.port = host.substr(pos);
if (_request.port == ":")
_request.port = "";
// hostname :
_request.hostname = host.substr(0, pos);
}

View File

@@ -15,6 +15,8 @@ struct Request
std::string path;
std::string version;
std::string body;
std::string port;
std::string hostname;
};
class Client
@@ -26,6 +28,8 @@ class Client
//Client &operator=(Client const &rhs);
int fd;
std::string port;
std::string ip;
std::string raw_request;
std::string response;
unsigned int status;
@@ -34,6 +38,8 @@ class Client
std::string get_path();
std::string get_version();
std::string get_body();
std::string get_port();
std::string get_hostname();
std::string get_headers(std::string key);
void parse_request();
@@ -44,6 +50,7 @@ class Client
void _parse_request_line( std::string rline );
void _parse_request_headers( std::vector<std::string> list );
void _parse_request_body( size_t pos );
void _parse_port_hostname(std::string host);
};

View File

@@ -22,5 +22,19 @@ void Webserv::_accept_connection(int fd)
_clients.push_back(Client());
_clients.back().fd = accepted_fd;
// HUGO WIP
//
_clients.back().port = ::itos(addr.sin_port);
struct in_addr tmp = { addr.sin_addr.s_addr };
_clients.back().ip = inet_ntoa( tmp );
std::cout
<< "port:" << _clients.back().port
<< " ip:" << _clients.back().ip
<< "\n";
//
// HUGO END
_epoll_update(accepted_fd, EPOLLIN, EPOLL_CTL_ADD);
}

View File

@@ -36,17 +36,18 @@ char** Webserv::_set_env(Client *client)
env[4] = _dup_env("PATH_INFO");
env[5] = _dup_env("PATH_TRANSLATED");
env[6] = _dup_env("QUERY_STRING");
env[7] = _dup_env("REMOTE_ADDR");
env[7] = _dup_env("REMOTE_ADDR", client->ip);
env[8] = _dup_env("REMOTE_HOST", client->get_headers("Host")); // just test
env[9] = _dup_env("REMOTE_IDENT");
env[10] = _dup_env("REMOTE_USER");
env[9] = _dup_env("REMOTE_IDENT"); // TODO: implement if authentification
env[10] = _dup_env("REMOTE_USER"); // TODO: implement if authentification
env[11] = _dup_env("REQUEST_METHOD", client->get_method());
env[12] = _dup_env("SCRIPT_NAME");
env[13] = _dup_env("SERVER_NAME");
env[14] = _dup_env("SERVER_PORT");
env[12] = _dup_env("SCRIPT_NAME"); // TODO: define (https://www.rfc-editor.org/rfc/rfc3875#section-4.1.13
// ,https://www.rfc-editor.org/rfc/rfc3875#section-8.2)
env[13] = _dup_env("SERVER_NAME", client->get_hostname());
env[14] = _dup_env("SERVER_PORT", client->get_port());
env[15] = _dup_env("SERVER_PROTOCOL", client->get_version());
env[16] = _dup_env("SERVER_SOFTWARE");
env[17] = _dup_env("REDIRECT_STATUS");
env[16] = _dup_env("SERVER_SOFTWARE", "webser/1.0");
env[17] = _dup_env("REDIRECT_STATUS", "200");
env[18] = NULL;
return env;

View File

@@ -24,6 +24,7 @@ void Webserv::init_virtual_servers(std::vector<ServerConfig>* servers)
std::perror("err socket()");
throw std::runtime_error("Socket init");
}
// HUGO ADD
//
// allow socket descriptor to be reuseable
@@ -36,6 +37,7 @@ void Webserv::init_virtual_servers(std::vector<ServerConfig>* servers)
}
//
// HUGO ADD END
_listen_sockets.push_back(ret);
_bind(_listen_sockets.back(), std::atoi(it->port.data()), it->host);