Skip to content

Commit 055e3d1

Browse files
committed
Added protocol macro to http class
1 parent b07fb02 commit 055e3d1

File tree

6 files changed

+19
-159
lines changed

6 files changed

+19
-159
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Introduction
22

3-
This project is a basic HTTP server written in C++98. The standards below (HTTP/CGI) are not completely implemented and were rather seen as a guideline to implement a usable server than a "to be followed requirement".
3+
This project is a basic HTTP server written in C++98. The standards listed below (HTTP/CGI) are not completely implemented and were rather seen as a guideline to implement a usable server than a "to be followed requirement".
44

55
| Functionality | Description | External info
66
| --- | --- | --- |

include/http/Http.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "webserv.hpp"
1414

1515
// WEBSERV_CONFIG ----------- HTTP VALUES --------------------------
16+
#define PROTOCOL "HTTP"
1617
#define HTTP_VERSION "1.1"
1718
#define HTTP_METHODS \
1819
{ "GET", "HEAD", "OPTIONS", "POST", "PUT", "DELETE" }

src/http/Http.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ void Http::OnRequestRecv(std::string msg) {
4646
if (parseRet || !validPath)
4747
processError("400", "Bad Request", true);
4848
else if (isHttpVersionValid(_request.getVersion()) == false) {
49-
processError("505", "HTTP Version Not Supported");
50-
_response.setHeader("Upgrade", "HTTP/" HTTP_VERSION);
49+
processError("505", "HTTP Version Not Supported", true);
50+
_response.setHeader("Upgrade", PROTOCOL "/" HTTP_VERSION);
5151
_response.setHeader("Connection", "Upgrade");
5252
} else if (isMehodImplemented(_request.getMethod()) == false)
53-
processError("501", "Not Implemented");
53+
processError("501", "Not Implemented", true);
5454
if (_response.isReady()) return sendResponse();
5555
setReadState(HEAD);
5656
}
@@ -129,7 +129,7 @@ void Http::OnBodyRecv(std::string msg) {
129129

130130
void Http::OnCgiRecv(std::string msg) {
131131
accessLog_g.write("CGI out: \"" + msg + "\"", VERBOSE);
132-
_response.init("HTTP/1.1", "200", "OK");
132+
_response.init(PROTOCOL "/" HTTP_VERSION, "200", "OK");
133133
int bodySize = msg.size();
134134
_response.setBody(new std::istringstream(msg));
135135

@@ -233,7 +233,7 @@ void Http::processFile(std::string uri) {
233233
}
234234

235235
// Load the file
236-
_response.init("HTTP/1.1", "200", "OK");
236+
_response.init(PROTOCOL "/" HTTP_VERSION, "200", "OK");
237237
_response.setBody(new std::ifstream(file.getPath().c_str()));
238238
int bodySize = file.size();
239239
if (_response.getBody()->good() == false || bodySize < 0)
@@ -256,7 +256,7 @@ void Http::processCgi(std::string contentLength) {
256256
// const values:
257257
env.push_back("GATEWAY_INTERFACE=CGI/1.1");
258258
env.push_back("SERVER_SOFTWARE=" WEBSERV_ID);
259-
env.push_back("SERVER_PROTOCOL=HTTP/" HTTP_VERSION);
259+
env.push_back("SERVER_PROTOCOL=" PROTOCOL "/" HTTP_VERSION);
260260

261261
// request specific values:
262262

@@ -378,15 +378,15 @@ void Http::processPostData(const std::string &data) {
378378

379379
void Http::getPutResponse(std::string uri) {
380380
if (_newFile)
381-
_response.init("HTTP/1.1", "201", "Created");
381+
_response.init(PROTOCOL "/" HTTP_VERSION, "201", "Created");
382382
else
383-
_response.init("HTTP/1.1", "204", "No Content");
383+
_response.init(PROTOCOL "/" HTTP_VERSION, "204", "No Content");
384384
_response.setHeader("Location", getAbsoluteUri(uri));
385385
_response.setReady();
386386
}
387387

388388
void Http::processOptions(std::string uri) {
389-
_response.init("HTTP/1.1", "200", "OK");
389+
_response.init(PROTOCOL "/" HTTP_VERSION, "200", "OK");
390390
_response.setHeader("Allow",
391391
concatenate(getAllowedMethods(uri != "*"), ", "));
392392
_response.setReady();
@@ -401,12 +401,12 @@ void Http::processDelete(std::string uri) {
401401
if (!file.readable()) return processError("403", "Forbidden");
402402
if (std::remove(file.getPath().c_str()) != 0)
403403
return processError("500", "Internal Server Error");
404-
_response.init("HTTP/1.1", "204", "No Content");
404+
_response.init(PROTOCOL "/" HTTP_VERSION, "204", "No Content");
405405
_response.setReady();
406406
}
407407

408408
void Http::processAutoindex(std::string uri) {
409-
_response.init("HTTP/1.1", "200", "OK");
409+
_response.init(PROTOCOL "/" HTTP_VERSION, "200", "OK");
410410
_response.setBody(new std::stringstream("<html>\r\n<head><title>Index of " +
411411
uri + "</title></head>\r\n<body>"));
412412
std::stringstream *body = (std::stringstream *)_response.getBody();
@@ -450,13 +450,13 @@ void Http::processAutoindex(std::string uri) {
450450
}
451451

452452
void Http::processRedirect(std::string uri) {
453-
_response.init("HTTP/1.1", "301", "Moved Permanently");
453+
_response.init(PROTOCOL "/" HTTP_VERSION, "301", "Moved Permanently");
454454
_response.setHeader("Location", getAbsoluteUri(uri));
455455
_response.setReady();
456456
}
457457

458458
void Http::processError(std::string code, std::string reason, bool close) {
459-
_response.init("HTTP/1.1", code, reason);
459+
_response.init(PROTOCOL "/" HTTP_VERSION, code, reason);
460460

461461
if (_virtualHost && _virtualHost->getContext().exists("error_page")) {
462462
std::vector<std::vector<std::string> > &pages =
@@ -595,13 +595,13 @@ bool Http::isMehodImplemented(std::string method) const {
595595
}
596596

597597
bool Http::isHttpVersionValid(std::string version) const {
598-
if (startsWith(version, "HTTP/"))
599-
version.erase(0, std::string("HTTP/").size());
598+
if (startsWith(version, PROTOCOL "/"))
599+
version.erase(0, std::string(PROTOCOL "/").size());
600600
std::vector<std::string> in = split<std::vector<std::string> >(version, ".");
601601
if (in.size() < 1 || in.size() > 2) return false;
602602

603603
std::vector<std::string> reqired =
604-
split<std::vector<std::string> >(HTTP_VERSION, ".");
604+
split<std::vector<std::string> >(HTTP_VERSION, ".", true);
605605
if (reqired.size() < 1 || reqired.size() > 2 || in.size() != reqired.size())
606606
return false;
607607

src/http/Request.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void Request::setHeaderField(std::string key, const std::string &value) {
2626

2727
void Request::setHeaderFieldValue(std::string key, const std::string &value) {
2828
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
29-
_headers[key].push_back(trimStart(value, " \t"));
29+
_headers[key].push_back(trim(value, WHITESPACE));
3030
}
3131

3232
std::string Request::getMethod() const { return _method; }

websites/config_test/listing/createChunkedRequest.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

websites/config_test/listing/testResponses.py

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)