Skip to content

Commit

Permalink
Merge pull request #388 from dantol29/dtolmaco/add-debug-log
Browse files Browse the repository at this point in the history
Dtolmaco/add debug log
  • Loading branch information
552020 authored May 24, 2024
2 parents d043325 + 7ed593e commit 35e9e05
Show file tree
Hide file tree
Showing 27 changed files with 407 additions and 287 deletions.
5 changes: 3 additions & 2 deletions conf/single.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
server {
listen 127.0.0.1:8080;
listen 127.0.0.1:8080 8081 8082;
listen 127.0.0.2:8080;
server_name www.saladbook;
root /var/www/html;
root /var/;
}
11 changes: 11 additions & 0 deletions conf/webserv_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ server {
autoindex on;
cgi_ext .cgi;
server_name www.development_site;
limit_conn 3;
allow_methods GET POST DELETE;
root var/;
}
Expand All @@ -56,4 +57,14 @@ server {
client_max_body_size 1000;
autoindex off;
root var/;
}

server {
listen 8080;
server_name www.saladbook;
limit_conn 2;
allow_methods GET POST DELETE;
client_max_body_size 1000;
autoindex off;
root var/;
}
78 changes: 25 additions & 53 deletions docs/HTTP_codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
### How to trigger?
Send invalid HTTP request

## 401 Unauthorized
_not supported_

## 402 Payment Required
_not supported_

## 403 Forbidden
### How to trigger?
Access file without permissions (TODO: @Leo)
Expand All @@ -20,76 +14,54 @@ Access unexisting page
### How to trigger?
Any request method besides GET, POST, DELETE

## 406 Not Acceptable
_not supported_

## 407 Proxy Authentication Required
_not supported_

## 408 Request Timeout
### How to trigger?
Send a request with incomplete headers

## 409 Conflict
_not supported_

## 410 Gone
_not supported_

## 411 Length Required
### How to trigger?
Send POST request without Content-Length header

## 412 Precondition Failed
_not supported_
Send POST request without `Content-Length` header

## 413 Payload Too Large
### How to trigger?
Send POST request bigger than client_max_body_size
Send POST request bigger than `client_max_body_size`

## 414 URI Too Long
### How to trigger?
Send request with headers > 8KB

## 415 Unsupported Media Type
Send request to CGI that is not in cgi_ext directive

## 416 Range Not Satisfiable
_not supported_

## 417 Expectation Failed
_not supported_
### How to trigger?
Send request to CGI that is not in `cgi_ext` directive

## 418 I'm a teapot
_not supported_
Would be nice to have

## 421 Misdirected Request
_not supported_

## 422 Unprocessable Content (WebDAV)
_not supported_

## 423 Locked (WebDAV)
_not supported_
## 429 Too Many Requests
TODO (@Stefano maybe?)

## 424 Failed Dependency (WebDAV)
_not supported_
## 431 Request Header Fields Too Large
### How to trigger?
Send request with headers > 8KB

## 425 Too Early Experimental
_not supported_
## 500 Internal Server Error
### How to trigger?
Launch CGI with error inside

## 426 Upgrade Required
TODO (@Someone)
## 501 Not Implemented
### How to trigger?
Send a request with unsupported HTTP method

## 428 Precondition Required
_not supported_
## 503 Service Unavailable
### How to trigger?
Set `limit_connn` directive and send more requests than written there

## 429 Too Many Requests
TODO (@Stefano maybe?)
## 504 Gateway Timeout
### How to trigger?
Launch CGI that is executed longer than 5 seconds

## 431 Request Header Fields Too Large
## 505 HTTP Version Not Supported
### How to trigger?
Send request with headers > 8KB
Send a request with invalid HTTP version(not HTTP/1.1)

## 451 Unavailable For Legal Reasons
_not supported_
_16 supported errors_
20 changes: 19 additions & 1 deletion docs/config_file/config_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ server {
- cgi_path
- cgi_ext
- return
- upload_path
- limit_conn

### 1. LISTEN
- Can be written multiple times per _server block_
Expand Down Expand Up @@ -118,11 +120,27 @@ server {
### 11. RETURN
- Can be written only once per _server block_
- Redirects user to a certain URL
- is stored in the `std::string>`
- is stored in the `std::string`
- _OUR PROTECTION:_
1. no protection
- _DEFAULT VALUE_

### 12. UPLOAD_PATH
- Can be written only once per _server block_
- Says where files should be uploaded to
- is stored in the `std::string`
- _OUR PROTECTION:_
1. no protection
- _DEFAULT VALUE_

### 13. LIMIT_CONN
- Can be written only once per _server block_
- Controls the number of simultaneous connections
- is stored in the `size_t`
- _OUR PROTECTION:_
1. check if number is valid
- _DEFAULT VALUE_

## OPTIONAL?

### UPLOAD_PATH
6 changes: 5 additions & 1 deletion include/webserv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

#define SEND_BUFFER_SIZE 1024 * 100 // 100 KB
#define BUFFER_SIZE 1025
#define CGI_TIMEOUT_MS 10000
#define CGI_BUFFER_SIZE 100 // 100 B
#define CGI_TIMEOUT_S 6 // 6 seconds
#define CGI_POLL_TIMEOUT_MS 500 // 0.5 seconds
#define CLIENT_POLL_TIMEOUT_MS 12000 // 12 seconds
#define CLIENT_TIMEOUT_S 10 // 10 seconds
#define CONFIG_FILE_DEFAULT_PATH "./conf/webserv_default.conf"

#define RED "\033[1;31m"
Expand Down
34 changes: 17 additions & 17 deletions src/CGIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ CGIHandler &CGIHandler::operator=(const CGIHandler &other)

void CGIHandler::handleRequest(HTTPRequest &request, HTTPResponse &response)
{

std::cout << RED << "Entering CGIHandler::handleRequest" << RESET << std::endl;
Debug::log("CGIHandler::handleRequest", Debug::CGI);
MetaVariables env;
env.HTTPRequestToMetaVars(request, env);
if (!executeCGI(env, response))
Expand All @@ -46,18 +45,19 @@ void CGIHandler::handleRequest(HTTPRequest &request, HTTPResponse &response)
// TODO: it should be hardcoded
response.setBody("500 Internal Server Error");
}
Debug::log("Connection PID" + toString(_connection.getCGIPid()), Debug::CGI);
return;
}

std::vector<std::string> CGIHandler::createArgvForExecve(const MetaVariables &env)
{
std::vector<std::string> argv;
std::string scriptName = env.getVar("SCRIPT_NAME");
std::cout << "createArgvForExecve: scriptName: " << scriptName << std::endl;
Debug::log("createArgvForExecve: scriptName: " + scriptName, Debug::CGI);
std::string pathTranslated = env.getVar("PATH_TRANSLATED");
std::cout << "createArgvForExecve: pathTranslated: " << pathTranslated << std::endl;
Debug::log("createArgvForExecve: pathTranslated: " + pathTranslated, Debug::CGI);
std::string scriptPath = pathTranslated;
std::cout << "createArgvForExecve: scriptPath: " << scriptPath << std::endl;
Debug::log("createArgvForExecve: scriptPath: " + scriptPath, Debug::CGI);

if (env.getVar("X_INTERPRETER_PATH") != "")
{
Expand Down Expand Up @@ -99,12 +99,12 @@ std::vector<char *> CGIHandler::convertToCStringArray(const std::vector<std::str
void handleTimeout(int sig)
{
(void)sig;
std::cout << "CGIHandler: Timeout" << std::endl;
Debug::log("CGIHandler: Timeout", Debug::CGI);
}

bool CGIHandler::executeCGI(const MetaVariables &env, HTTPResponse &response)
{
std::cout << RED << "Entering CGIHandler::executeCGI" << RESET << std::endl;
Debug::log("CGIHandler::executeCGI", Debug::CGI);
std::string cgiOutput;
std::vector<std::string> argv = createArgvForExecve(env);
std::vector<std::string> envp = env.getForExecve();
Expand Down Expand Up @@ -147,7 +147,7 @@ bool CGIHandler::executeCGI(const MetaVariables &env, HTTPResponse &response)

if (access(argvPointers[0], X_OK) == -1)
{
perror("access");
Debug::log("CGIHandler: access failed", Debug::CGI);
return false;
_exit(EXIT_FAILURE);
// TODO: @leo I don't think we should exit here. We don't want to kill the whole server cause of a CGI
Expand All @@ -157,7 +157,7 @@ bool CGIHandler::executeCGI(const MetaVariables &env, HTTPResponse &response)
// execve(argvPointers[0], argvPointers.data(), envpPointers.data());
if (execve(argvPointers[0], argvPointers.data(), envpPointers.data()) == -1)
{
perror("execve");
Debug::log("CGIHandler: execve failed", Debug::CGI);
return false;
// TODO: @leo We should check if execve failed and return an error response and not exti
_exit(EXIT_FAILURE);
Expand All @@ -167,24 +167,24 @@ bool CGIHandler::executeCGI(const MetaVariables &env, HTTPResponse &response)
response.setIsCGI(true);
response.setCGIpipeFD(pipeFD);

std::cout << "PIPE SAVED: "<< *response.getCGIpipeFD() << std::endl;
Debug::log("PIPE SAVED: " + toString(*response.getCGIpipeFD()), Debug::CGI);

close(pipeFD[1]);
EventData data = {1, pid, pipeFD[0], pipeFD[1]}; // Assuming 1 is the event type for CGI started

_eventManager.emit(data); // Emit event indicating a CGI process has started

_connection.addCGI(pid);
std::cout << GREEN << _connection.getCGIPid() << RESET << std::endl;
Debug::log("CGIHandler: CGI PID: " + toString(pid), Debug::CGI);

// clang-format off
std::vector<std::pair<int, int> > pipes = _eventManager.getPipeFDs();
for (std::vector<std::pair<int, int> >::const_iterator it = pipes.begin(); it != pipes.end(); ++it)
{
std::cout << GREEN << "CGIHandler: pipeFDs: " << (*it).first << RESET << std::endl;
}
// std::vector<std::pair<int, int> > pipes = _eventManager.getPipeFDs();
// for (std::vector<std::pair<int, int> >::const_iterator it = pipes.begin(); it != pipes.end(); ++it)
// {
// std::cout << GREEN << "CGIHandler: pipeFDs: " << (*it).first << RESET << std::endl;
// }
// clang-format on
std::cout << RED << "Exiting CGIHandler::executeCGI with true" << RESET << std::endl;
Debug::log("CGIHandler: Waiting for CGI to finish", Debug::CGI);
return true;
}

Expand Down
Loading

0 comments on commit 35e9e05

Please sign in to comment.