From 27b4f966180ebd16b7ca62d0f8c26c1c59ad68ba Mon Sep 17 00:00:00 2001 From: hugogogo Date: Thu, 11 Aug 2022 00:41:17 +0200 Subject: [PATCH] parsing headers is allright now + adding error codes in readme and in comments in client --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ srcs/Client.cpp | 22 +++++++++++++--------- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2f66508..95ff686 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,45 @@ [7 and 8: usefull informations about implementation and security](https://www.rfc-editor.org/rfc/rfc3875#section-7) +--- +## http errors + +#### HTTP Client Errors +- 400 Bad Request This error code indicates that the request cannot be processed because of wrong syntax usage by the client. +- 401 Unauthorized This error code indicates that the client is not authorized to receive the requested data, without authentication. A login name and password based authentication might be required to access the requested data. +- 403 Forbidden There is no way you can access the requested data. A 403 error announces that the data is off limits. +- 404 Not Found This error indicates that the resources requested by the client are currently unavailable. +- 405 Method Not Allowed This error indicates wrong usage of request method. Depending on the kind of data requested, the appropriate request method must be chosen. +- 406 Not Acceptable When the data provided by a web server does not match the specifications made in ‘Accept’ header of the client HTTP request, this error is the result. +- 407 Proxy Authentication Required This error clearly indicates that an authentication from the proxy server is required to gain access to requested resources. +- 408 Request Timeout This type of error indicates that the client was delayed in making a request, within the specified time allocated to it, by the server. +- 409 Conflict This error code is displayed when the server perceives a conflict between two requests made simultaneously by different clients, for the same resource. +- 410 Gone This error code indicates that the requested data is no longer hosted on the server and therefore further requests made for it, would be futile. +- 411 Length Required If the request made by the client does not include information about the length of the requested data or resource, this error code is displayed. +- 412 Precondition Failed Some requests made by clients come attached with conditions that need to be satisfied by the server, before data transaction may happen. If these conditions are not met, error 412 results. +- 413 Request Entity Too Large When a client makes a request which is too overwhelming for the server’s resources to handle, it presents this error code. +- 414 Requested URI Too Long A Uniform Resource Identifier (URI) is a character string used to describe a data stream or resource on a server. Error 414 occurs when the server is unable to process the URI, because of limited resources and long string length. +- 415 Unsupported Media Type A server may be designed to allow only certain formats for media files. When error 415 is displayed, it indicates that the format of file being uploaded through a client request, does not match the requisite format. +- 416 Request Range Not Satisfiable Sometimes, a client may request for only a small part of a file, instead of asking for the entire file. If this request is not specified properly and the part of the file requested does not exist, this error is displayed. +- 417 Expectation Failed This error code is displayed when the server cannot meet the specifications provided in the request. +- 422 Unprocessable Entity This error is displayed when the request made, cannot be processed due to an error in semantic structure. +- 423 Locked This error is displayed when a requested piece of data or resource has been locked, making it inaccessible for a server. +- 424 Failed Dependency A server may process a succession of requests from a client with the fulfillment of each, dependent on the one made before. This error is displayed when a request made before is not fulfilled, due to which the current request cannot be processed. +- 426 Upgrade Required This error signifies that the client may need to switch over to a secure protocol like TLS to get the request processed. +- 444 No Response This error signifies that the server has simply rejected the client request and terminated connection. +- 449 Retry With This is a request made by the server to the client, to make the request again after executing certain actions or making specific changes in request. This is an error code introduced by Microsoft. +- 499 Client Closed Request When client terminates a connection made with the server, while its processing the associated request, this error code is displayed. +- 450 Blocked By Windows Parental Controls Another error code introduced by Microsoft, this one is displayed when a URL is blocked by parental control settings on the web browsers. + +#### HTTP Server Errors +- 500 Internal Server Error A generic message displayed by the server, when the problem with the request cannot be specified by any other appropriate code. +- 501 Not Implemented This error indicates the inability of the server to process a request, as it hasn’t been configured to respond to the request method used. +- 502 Bad Gateway Sometimes, hosted pages on web servers are transmitted to client via proxy servers. If the proxy server (to which a client has sent a request), fails connecting with the web server (known as the upstream server), error 502 results. +- 503 Service Unavailable When the server is already overloaded with multiple requests, it will temporarily stop entertaining new requests, by displaying a 503 error code. +- 504 Gateway Timeout When the request made by a proxy server to the web server hosting a resource times out, error 504 is reported. +- 505 HTTP Version Not Supported An error code seen rarely, it is displayed when the web server does not support the protocol version of the client request. + + --- ## cgi env variables [cgi env variables](https://www.rfc-editor.org/rfc/rfc3875#section-4.1) diff --git a/srcs/Client.cpp b/srcs/Client.cpp index e10dca5..cb6382a 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -99,17 +99,21 @@ void Client::parse_request(std::vector &servers) // use getter for headers because it works case insensitive _parse_port_hostname(this->get_rq_headers("Host")); -/* dont clear raw_request, we need it for future reparsing of body - see call of parse_request() in _read_request() */ +// dont clear raw_request, we need it for future reparsing of body +// see call of parse_request() in _read_request() // raw_request.clear(); } void Client::parse_request_body() { - // TODO: check error and adjust status - _request.body = ::parse_http_body(raw_request); + size_t pos; + + pos = raw_request.find(CRLF CRLF); + pos += std::string(CRLF CRLF).size(); + _request.body = message.substr(pos); + if (_request.body.size() > assigned_server->client_body_limit) - status = 413; + status = 413; // HTTP Client Errors } bool Client::fill_script_path(std::string script) @@ -279,12 +283,12 @@ void Client::_check_request_errors() ////////////////////// // Request line checks if (_request.method == UNKNOWN) - status = 501; + status = 501; // HTTP Client Errors else if (_request.version.compare(0, sizeof("HTTP/1") - 1, "HTTP/1") != 0) - status = 505; + status = 505; // HTTP Client Errors else if (!(assigned_location->allow_methods & _request.method)) { - status = 405; + status = 405; // HTTP Client Errors response.append("Allow: "); response.append(::http_methods_to_str(assigned_location->allow_methods)); response.append(CRLF); @@ -305,7 +309,7 @@ void Client::_check_request_errors() // Headers checks if (!this->get_rq_headers("Content-Length").empty() && ::atoi(this->get_rq_headers("Content-Length").c_str()) > (int)assigned_server->client_body_limit) - status = 413; + status = 413; // HTTP Client Errors return; }