Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

24 http response status codes #62

Merged
merged 11 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 68 additions & 4 deletions src/HTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ HTTPRequest::method HTTPRequest::getMethod() const {

std::string HTTPRequest::getURI() const { return this->URI_; }

std::string HTTPRequest::getQueryParam() const { return this->query_param_; }

std::string HTTPRequest::getProtocol() const { return this->protocol_version_; }

bool HTTPRequest::getKeepalive() const { return this->keepalive_; }

bool HTTPRequest::hasRequestError() const { return this->has_request_error_; }

std::string HTTPRequest::getRequestError() const {
return this->request_error_;
}

unsigned int HTTPRequest::getContentLength() const {
unsigned int res = 0;
std::map<std::string, std::string>::const_iterator cl =
Expand Down Expand Up @@ -108,22 +116,39 @@ HTTPRequest::HTTPRequest() {}

HTTPRequest::~HTTPRequest() {}

HTTPRequest::HTTPRequest(const HTTPRequest& obj) { *this = obj; }
HTTPRequest::HTTPRequest(const HTTPRequest& obj)
: header_(obj.header_),
body_(obj.body_),
request_method_(obj.request_method_),
URI_(obj.URI_),
query_param_(obj.query_param_),
protocol_version_(obj.protocol_version_),
keepalive_(obj.keepalive_),
has_request_error_(obj.has_request_error_),
request_error_(obj.request_error_),
settings_(obj.settings_) {
*this = obj;
}

HTTPRequest& HTTPRequest::operator=(const HTTPRequest& obj) {
this->keepalive_ = obj.request_method_;
this->body_ = obj.body_;
this->header_ = obj.header_;
this->request_method_ = obj.request_method_;
this->URI_ = obj.URI_;
this->query_param_ = obj.query_param_;
this->protocol_version_ = obj.protocol_version_;
this->keepalive_ = obj.keepalive_;
this->has_request_error_ = obj.has_request_error_;
this->request_error_ = obj.request_error_;
this->settings_ = obj.settings_;
return *this;
}

HTTPRequest::HTTPRequest(std::string& input) {
HTTPRequest::HTTPRequest(std::string& input, const Settings& settings) {
if (input.size() <= 1) {
throw std::runtime_error("Error: tried to create request with size <= 1");
}
this->settings_ = settings;
std::size_t header_end = input.find("\r\n\r\n");
std::string header(input.begin(), input.begin() + header_end);
std::string body(input.begin() + header_end + 4, input.end());
Expand All @@ -133,17 +158,56 @@ HTTPRequest::HTTPRequest(std::string& input) {
std::vector<std::string> request_line = this->splitLine(line, " ");
this->request_method_ = this->parseMethodToken(*request_line.begin());
this->URI_ = this->cleanURI(request_line[1]);
size_t query_param_start = this->URI_.find_first_of('?');
if (query_param_start != std::string::npos) {
this->query_param_ =
this->URI_.substr(query_param_start, this->URI_.size());
this->URI_.erase(query_param_start, this->URI_.size());
}
this->protocol_version_ = request_line.at(2);
this->removeTrailingWhitespace(this->protocol_version_);
this->has_request_error_ = false;
while (std::getline(header_iss, line)) {
std::vector<std::string> temp = this->splitLine(line, ": ");
removeTrailingWhitespace(temp.at(2));
this->removeTrailingWhitespace(temp.at(2));
this->header_.insert(
std::pair<std::string, std::string>(temp.at(0), temp.at(2)));
}
if (this->header_.find("Connection")->second.compare("keep-alive") == 0) {
this->keepalive_ = true;
}
this->body_ = body;
this->checkForErrors();
}

void HTTPRequest::checkForErrors() {
if (this->hasRequestError()) {
return;
}
if (this->protocol_version_.compare("HTTP/1.1")) {
this->has_request_error_ = true;
this->request_error_ = STATUS_505;
} else if (this->getContentLength() < this->body_.size()) {
// TODO: this case leads to timeout in the servers main loop, we never get
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this lead to timeout?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be explored in #61

// here
this->has_request_error_ = true;
this->request_error_ = STATUS_413;
} else if (this->getURI().size() + this->getQueryParam().size() >
MAX_CLIENT_HEADER_BUFFER) {
this->has_request_error_ = true;
this->request_error_ = STATUS_414;
} else if (this->getBody().size() >
settings_.getServers()[0].getMaxClientBodySize()) {
this->has_request_error_ = true;
this->request_error_ = STATUS_413;
} else if (this->getMethod() == HTTPRequest::POST &&
this->header_.find("Content-Length") == this->header_.end()) {
this->has_request_error_ = true;
this->request_error_ = STATUS_411;
} else if (this->URI_.size() < 1 || this->protocol_version_.size() < 1) {
this->has_request_error_ = true;
this->request_error_ = STATUS_400;
}
}

std::ostream& operator<<(std::ostream& os, HTTPRequest& obj) {
Expand Down
15 changes: 14 additions & 1 deletion src/HTTPRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
#include <string>
#include <vector>

#include "./parser/Settings.hpp"
#include "HTTPResponseStatus.hpp"

#define MAX_CLIENT_HEADER_BUFFER 8000

class HTTPRequest {
public:
//// Constructors and Operator overloads
HTTPRequest();
~HTTPRequest();
HTTPRequest(const HTTPRequest& obj);
HTTPRequest& operator=(const HTTPRequest& obj);
HTTPRequest(std::string& input);
HTTPRequest(std::string& input, const Settings& settings);

typedef enum {
UNKNOWN,
Expand All @@ -32,8 +37,11 @@ class HTTPRequest {
std::string getBody() const;
HTTPRequest::method getMethod() const;
std::string getURI() const;
std::string getQueryParam() const;
std::string getProtocol() const;
bool getKeepalive() const;
bool hasRequestError() const;
std::string getRequestError() const;
unsigned int getContentLength() const;
void appendBody(std::string input);

Expand All @@ -42,14 +50,19 @@ class HTTPRequest {
std::string body_;
method request_method_;
std::string URI_;
std::string query_param_;
std::string protocol_version_;
bool keepalive_;
bool has_request_error_;
std::string request_error_;
Settings settings_;
//// Private Member Functions
void removeTrailingWhitespace(std::string& str);
method parseMethodToken(std::string& token);
std::vector<std::string> splitLine(
std::string line, std::vector<std::string>::value_type delim);
std::string cleanURI(std::string& uri_str);
void checkForErrors();
};

std::ostream& operator<<(std::ostream& os, HTTPRequest& obj);
Expand Down
85 changes: 58 additions & 27 deletions src/HTTPResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,25 @@ void HTTPResponse::handleGET(HTTPRequest& req) {
path.substr(path.find_last_of('.') + 1, path.size() - 1);
std::string content_type = this->mime_types.find(mimetype)->second;
try {
this->setResponseLine(STATUS_200);
this->addToHeader("Content-Type", content_type);
this->body_ = this->createResponseBody(path, req);
this->header_ = "HTTP/1.1 " + std::string(STATUS_200) +
"\nContent-Type: " + content_type;
if (req.getKeepalive()) {
int size = this->body_.size();
std::stringstream ss;
ss << size;
std::string ssize = ss.str();
this->header_ += "\r\nContent-Length: " + ssize;
}
int size = this->body_.size();
std::stringstream ss;
ss << size;
std::string ssize = ss.str();
this->addToHeader("Content-Length", ssize);
} catch (std::exception& e) {
this->setResponseLine(STATUS_404);
this->addToHeader("Content-Type", "text/html");
this->body_ = this->createResponseBody(
settings_.getServers()[0].getErrorPages()[404], req);
this->body_.replace(this->body_.find("${URI}"), 6, req.getURI());
this->header_ =
"HTTP/1.1 " + std::string(STATUS_404) + "\nContent-Type: text/html";
int size = this->body_.size();
std::stringstream ss;
ss << size;
std::string ssize = ss.str();
this->header_ += "\r\nContent-Length: " + ssize;
this->addToHeader("Content-Length", ssize);
}
return;
}
Expand All @@ -51,8 +49,8 @@ void HTTPResponse::handlePOST(HTTPRequest& req) {
filename);
req_file << req.getBody();
req_file.close();
this->header_ =
"HTTP/1.1 " + std::string(STATUS_201) + "\r\nContent-Length: 0\n";
this->setResponseLine(STATUS_201);
this->addToHeader("Content-Length", "0");
this->body_ = "";
}

Expand Down Expand Up @@ -133,29 +131,39 @@ HTTPResponse::HTTPResponse() {}
HTTPResponse::~HTTPResponse() {}

HTTPResponse::HTTPResponse(const HTTPResponse& obj)
: header_(obj.header_), body_(obj.body_) {}
: request_line_(obj.request_line_),
headers_(obj.headers_),
body_(obj.body_) {}

HTTPResponse& HTTPResponse::operator=(const HTTPResponse& obj) {
if (this != &obj) {
*this = obj;
}
this->header_ = obj.header_;
this->request_line_ = obj.request_line_;
this->headers_ = obj.headers_;
this->body_ = obj.body_;
return *this;
}

HTTPResponse::HTTPResponse(std::string header, std::string body)
: header_(header), body_(body) {}

HTTPResponse::HTTPResponse(HTTPRequest& req, Settings& settings)
HTTPResponse::HTTPResponse(HTTPRequest& req, const Settings& settings)
: settings_(settings) {
HTTPRequest::method req_method = req.getMethod();
if (req.hasRequestError()) {
this->setResponseLine(req.getRequestError());
this->addToHeader("Content-Length", "0");
this->body_ = "";
return;
}
switch (req_method) {
case HTTPRequest::UNKNOWN:
std::cout << "UNKNOWN method" << std::endl;
this->setResponseLine(STATUS_405);
this->addToHeader("Content-Length", "0");
this->body_ = "";
break;
case HTTPRequest::OPTIONS:
std::cout << "OPTIONS method" << std::endl;
this->setResponseLine(STATUS_501);
this->addToHeader("Content-Length", "0");
this->body_ = "";
break;
case HTTPRequest::GET:
this->handleGET(req);
Expand All @@ -168,16 +176,24 @@ HTTPResponse::HTTPResponse(HTTPRequest& req, Settings& settings)
this->handlePOST(req);
break;
case HTTPRequest::PUT:
std::cout << "PUT method" << std::endl;
this->setResponseLine(STATUS_501);
this->addToHeader("Content-Length", "0");
this->body_ = "";
break;
case HTTPRequest::DELETE:
std::cout << "DELETE method" << std::endl;
this->setResponseLine(STATUS_501);
this->addToHeader("Content-Length", "0");
this->body_ = "";
break;
case HTTPRequest::TRACE:
std::cout << "TRACE method" << std::endl;
this->setResponseLine(STATUS_501);
this->addToHeader("Content-Length", "0");
this->body_ = "";
break;
case HTTPRequest::CONNECT:
std::cout << "CONNECT method" << std::endl;
this->setResponseLine(STATUS_501);
this->addToHeader("Content-Length", "0");
this->body_ = "";
break;
}
}
Expand All @@ -186,6 +202,21 @@ HTTPResponse::HTTPResponse(HTTPRequest& req, Settings& settings)

std::string HTTPResponse::toString() const {
std::ostringstream oss;
oss << this->header_ << "\r\n\r\n" << this->body_;
oss << this->request_line_;
for (std::map<std::string, std::string>::const_iterator it =
this->headers_.begin();
it != this->headers_.end(); ++it) {
oss << it->first << ": " << it->second << "\r\n";
}
oss << "\r\n" << this->body_;
return oss.str();
}

void HTTPResponse::setResponseLine(const std::string& status_code) {
this->request_line_ = "HTTP/1.1 " + status_code + "\r\n";
}

void HTTPResponse::addToHeader(const std::string& key,
const std::string& value) {
this->headers_.insert(std::pair<std::string, std::string>(key, value));
}
Loading
Loading