Skip to content

Commit 680fbf1

Browse files
committed
Codebase modernization
1 parent a727f26 commit 680fbf1

File tree

8 files changed

+34
-42
lines changed

8 files changed

+34
-42
lines changed

src/HTTPMessage.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ HTTPMessage::HTTPMessage(std::string const& sData) : ByteBuffer(sData.size() + 1
2828
HTTPMessage::HTTPMessage(byte* pData, unsigned int len) : ByteBuffer(pData, len) {
2929
}
3030

31-
HTTPMessage::~HTTPMessage() {
32-
}
33-
3431
/**
3532
* Put Line
3633
* Append a line (string) to the backing ByteBuffer at the current position
@@ -256,7 +253,7 @@ void HTTPMessage::addHeader(std::string const& line) {
256253
* @param value String representation of the Header value
257254
*/
258255
void HTTPMessage::addHeader(std::string const& key, std::string const& value) {
259-
headers.insert(std::pair<std::string, std::string>(key, value));
256+
headers.try_emplace(key, value);
260257
}
261258

262259
/**
@@ -269,7 +266,7 @@ void HTTPMessage::addHeader(std::string const& key, std::string const& value) {
269266
void HTTPMessage::addHeader(std::string const& key, int value) {
270267
std::stringstream sz;
271268
sz << value;
272-
headers.insert(std::pair<std::string, std::string>(key, sz.str()));
269+
headers.try_emplace(key, sz.str());
273270
}
274271

275272
/**
@@ -331,7 +328,7 @@ std::string HTTPMessage::getHeaderStr(int index) const {
331328
*
332329
* @return size of the map
333330
*/
334-
int HTTPMessage::getNumHeaders() {
331+
int HTTPMessage::getNumHeaders() const {
335332
return headers.size();
336333
}
337334

src/HTTPMessage.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
#include "ByteBuffer.h"
2727

2828
// Constants
29-
#define HTTP_VERSION_10 "HTTP/1.0"
30-
#define HTTP_VERSION_11 "HTTP/1.1"
31-
#define DEFAULT_HTTP_VERSION HTTP_VERSION_11
32-
#define NUM_METHODS 9
29+
constexpr std::string HTTP_VERSION_10 = "HTTP/1.0";
30+
constexpr std::string HTTP_VERSION_11 = "HTTP/1.1";
31+
constexpr std::string DEFAULT_HTTP_VERSION = HTTP_VERSION_11;
32+
constexpr int NUM_METHODS = 9;
3333

3434
// HTTP Methods (Requests)
3535

@@ -92,9 +92,9 @@ class HTTPMessage : public ByteBuffer {
9292

9393
public:
9494
HTTPMessage();
95-
HTTPMessage(std::string const& sData);
96-
HTTPMessage(byte *pData, unsigned int len);
97-
virtual ~HTTPMessage();
95+
explicit HTTPMessage(std::string const& sData);
96+
explicit HTTPMessage(byte *pData, unsigned int len);
97+
virtual ~HTTPMessage() = default;
9898

9999
virtual byte* create() = 0;
100100
virtual bool parse() = 0;
@@ -115,7 +115,7 @@ class HTTPMessage : public ByteBuffer {
115115
void addHeader(std::string const& key, int value);
116116
std::string getHeaderValue(std::string const& key);
117117
std::string getHeaderStr(int index) const;
118-
int getNumHeaders();
118+
int getNumHeaders() const;
119119
void clearHeaders();
120120

121121
// Getters & Setters
@@ -124,7 +124,7 @@ class HTTPMessage : public ByteBuffer {
124124
return parseErrorStr;
125125
}
126126

127-
void setVersion(std::string_view v) {
127+
void setVersion(std::string v) {
128128
version = v;
129129
}
130130

src/HTTPRequest.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ HTTPRequest::HTTPRequest(std::string const& sData) : HTTPMessage(sData) {
2828
HTTPRequest::HTTPRequest(byte* pData, unsigned int len) : HTTPMessage(pData, len) {
2929
}
3030

31-
HTTPRequest::~HTTPRequest() {
32-
}
33-
3431
/**
3532
* Takes the method name and converts it to the corresponding method
3633
* id detailed in the Method enum
3734
*
3835
* @param name String representation of the Method
3936
* @return Corresponding Method ID, -1 if unable to find the method
4037
*/
41-
int HTTPRequest::methodStrToInt(std::string_view name) {
38+
int HTTPRequest::methodStrToInt(std::string_view name) const {
4239
// Method name cannot must be between 1 and 10 characters. Anything outside those bounds shouldn't be compared at all
4340
if (name.empty() || (name.size() >= 10))
4441
return -1;
@@ -59,7 +56,7 @@ int HTTPRequest::methodStrToInt(std::string_view name) {
5956
* @param mid Method ID to lookup
6057
* @return The method name in the from of a std::string. Blank if unable to find the method
6158
*/
62-
std::string HTTPRequest::methodIntToStr(unsigned int mid) {
59+
std::string HTTPRequest::methodIntToStr(unsigned int mid) const {
6360
// ID is out of bounds of the possible requestMethodStr indexes
6461
if (mid >= NUM_METHODS)
6562
return "";
@@ -110,7 +107,8 @@ byte* HTTPRequest::create() {
110107
* @param True if successful. If false, sets parseErrorStr for reason of failure
111108
*/
112109
bool HTTPRequest::parse() {
113-
std::string initial = "", methodName = "";
110+
std::string initial = "";
111+
std::string methodName = "";
114112

115113
// Get elements from the initial line: <method> <path> <version>\r\n
116114
methodName = getStrElement();

src/HTTPRequest.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ class HTTPRequest : public HTTPMessage {
2828

2929
public:
3030
HTTPRequest();
31-
HTTPRequest(std::string const& sData);
32-
HTTPRequest(byte *pData, unsigned int len);
33-
virtual ~HTTPRequest();
31+
explicit HTTPRequest(std::string const& sData);
32+
explicit HTTPRequest(byte *pData, unsigned int len);
33+
~HTTPRequest() = default;
3434

3535
virtual byte *create();
3636
virtual bool parse();
3737

3838
// Helper functions
3939

40-
int methodStrToInt(std::string_view name);
41-
std::string methodIntToStr(unsigned int mid);
40+
int methodStrToInt(std::string_view name) const;
41+
std::string methodIntToStr(unsigned int mid) const;
4242

4343
// Info getters & setters
4444

src/HTTPResponse.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,23 @@ HTTPResponse::HTTPResponse(std::string const& sData) : HTTPMessage(sData) {
2828
HTTPResponse::HTTPResponse(byte* pData, unsigned int len) : HTTPMessage(pData, len) {
2929
}
3030

31-
HTTPResponse::~HTTPResponse() {
32-
}
33-
3431
/**
3532
* Determine the status code based on the parsed Responses reason string
3633
* The reason string is non standard so this method needs to change in order to handle
3734
* responses with different kinds of strings
3835
*/
3936
void HTTPResponse::determineStatusCode() {
40-
if (reason.find("Continue") != std::string::npos) {
37+
if (reason.contains("Continue")) {
4138
status = Status(CONTINUE);
42-
} else if (reason.find("OK") != std::string::npos) {
39+
} else if (reason.contains("OK")) {
4340
status = Status(OK);
44-
} else if (reason.find("Bad Request") != std::string::npos) {
41+
} else if (reason.contains("Bad Request")) {
4542
status = Status(BAD_REQUEST);
46-
} else if (reason.find("Not Found") != std::string::npos) {
43+
} else if (reason.contains("Not Found")) {
4744
status = Status(NOT_FOUND);
48-
} else if (reason.find("Server Error") != std::string::npos) {
45+
} else if (reason.contains("Server Error")) {
4946
status = Status(SERVER_ERROR);
50-
} else if (reason.find("Not Implemented") != std::string::npos) {
47+
} else if (reason.contains("Not Implemented")) {
5148
status = Status(NOT_IMPLEMENTED);
5249
} else {
5350
status = Status(NOT_IMPLEMENTED);

src/HTTPResponse.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ class HTTPResponse : public HTTPMessage {
3232

3333
public:
3434
HTTPResponse();
35-
HTTPResponse(std::string const& sData);
36-
HTTPResponse(byte *pData, unsigned int len);
37-
virtual ~HTTPResponse();
35+
explicit HTTPResponse(std::string const& sData);
36+
explicit HTTPResponse(byte *pData, unsigned int len);
37+
~HTTPResponse() = default;
3838

39-
virtual byte* create();
40-
virtual bool parse();
39+
byte* create() final;
40+
bool parse() final;
4141

4242
// Accessors & Mutators
4343

src/HTTPServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ ResourceHost* HTTPServer::getResourceHostForRequest(HTTPRequest* req) {
665665
host = req->getHeaderValue("Host");
666666

667667
// All vhosts have the port appended, so need to append it to the host if it doesnt exist
668-
if (host.find(":") == std::string::npos) {
668+
if (!host.contains(":")) {
669669
host.append(":" + std::to_string(listenPort));
670670
}
671671

src/ResourceHost.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Resource* ResourceHost::getResource(std::string const& uri) {
200200
return nullptr;
201201

202202
// Do not allow directory traversal
203-
if (uri.find("../") != std::string::npos || uri.find("/..") != std::string::npos)
203+
if (uri.contains("../") || uri.contains("/.."))
204204
return nullptr;
205205

206206
std::string path = baseDiskPath + uri;

0 commit comments

Comments
 (0)