Skip to content

Commit 38265c5

Browse files
committed
Codebase modernization
1 parent d39040b commit 38265c5

15 files changed

+82
-81
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ PRODFLAGS := -Wall -O2 -fstack-check -fstack-protector-all
1616

1717
# ifeq ($(UNAME), Linux)
1818
# # Linux Flags - only supported if libkqueue is compiled from Github sources
19-
# CFLAGS := -std=c++14 -Iinclude/ $(PRODFLAGS)
19+
# CFLAGS := -std=c++20 -Iinclude/ $(PRODFLAGS)
2020
# LINK := -lpthread -lkqueue $(PRODFLAGS)
2121
# else
2222
# OSX / BSD Flags
23-
CFLAGS := -std=c++14 -Iinclude/ $(PRODFLAGS)
23+
CFLAGS := -std=c++20 -Iinclude/ $(PRODFLAGS)
2424
LINK := $(PRODFLAGS)
2525
# endif
2626

src/ByteBuffer.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void ByteBuffer::clear() {
8989
* @return A pointer to the newly cloned ByteBuffer. NULL if no more memory available
9090
*/
9191
ByteBuffer* ByteBuffer::clone() {
92-
ByteBuffer* ret = new ByteBuffer(buf.size());
92+
auto ret = new ByteBuffer(buf.size());
9393

9494
// Copy data
9595
for(unsigned int i = 0; i < buf.size(); i++) {
@@ -110,7 +110,7 @@ ByteBuffer* ByteBuffer::clone() {
110110
* @param other A pointer to a ByteBuffer to compare to this one
111111
* @return True if the internal buffers match. False if otherwise
112112
*/
113-
bool ByteBuffer::equals(ByteBuffer* other) {
113+
bool ByteBuffer::equals(ByteBuffer* other) const {
114114
// If sizes aren't equal, they can't be equal
115115
if(size() != other->size())
116116
return false;
@@ -143,7 +143,7 @@ void ByteBuffer::resize(unsigned int newSize) {
143143
*
144144
* @return size of the internal buffer
145145
*/
146-
unsigned int ByteBuffer::size() {
146+
unsigned int ByteBuffer::size() const {
147147
return buf.size();
148148
}
149149

@@ -185,7 +185,7 @@ byte ByteBuffer::get() {
185185
return read<byte>();
186186
}
187187

188-
byte ByteBuffer::get(unsigned int index) {
188+
byte ByteBuffer::get(unsigned int index) const {
189189
return read<byte>(index);
190190
}
191191

@@ -327,16 +327,16 @@ void ByteBuffer::setName(std::string n) {
327327
name = n;
328328
}
329329

330-
std::string ByteBuffer::getName() {
330+
std::string ByteBuffer::getName() const {
331331
return name;
332332
}
333333

334-
void ByteBuffer::printInfo() {
334+
void ByteBuffer::printInfo() const {
335335
unsigned int length = buf.size();
336336
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Info Print" << std::endl;
337337
}
338338

339-
void ByteBuffer::printAH() {
339+
void ByteBuffer::printAH() const {
340340
unsigned int length = buf.size();
341341
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII & Hex Print" << std::endl;
342342
for(unsigned int i = 0; i < length; i++) {
@@ -349,7 +349,7 @@ void ByteBuffer::printAH() {
349349
printf("\n");
350350
}
351351

352-
void ByteBuffer::printAscii() {
352+
void ByteBuffer::printAscii() const {
353353
unsigned int length = buf.size();
354354
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII Print" << std::endl;
355355
for(unsigned int i = 0; i < length; i++) {
@@ -358,7 +358,7 @@ void ByteBuffer::printAscii() {
358358
printf("\n");
359359
}
360360

361-
void ByteBuffer::printHex() {
361+
void ByteBuffer::printHex() const {
362362
unsigned int length = buf.size();
363363
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Hex Print" << std::endl;
364364
for(unsigned int i = 0; i < length; i++) {
@@ -367,7 +367,7 @@ void ByteBuffer::printHex() {
367367
printf("\n");
368368
}
369369

370-
void ByteBuffer::printPosition() {
370+
void ByteBuffer::printPosition() const {
371371
unsigned int length = buf.size();
372372
std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << " Read Pos: " << rpos << ". Write Pos: " << wpos << std::endl;
373373
}

src/ByteBuffer.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ class ByteBuffer {
8686
void clear(); // Clear our the vector and reset read and write positions
8787
ByteBuffer* clone(); // Return a new instance of a bytebuffer with the exact same contents and the same state (rpos, wpos)
8888
//ByteBuffer compact(); // TODO?
89-
bool equals(ByteBuffer* other); // Compare if the contents are equivalent
89+
bool equals(ByteBuffer* other) const; // Compare if the contents are equivalent
9090
void resize(unsigned int newSize);
91-
unsigned int size(); // Size of internal vector
91+
unsigned int size() const; // Size of internal vector
9292

9393
// Basic Searching (Linear)
9494
template <typename T> int find(T key, unsigned int start=0) {
@@ -116,7 +116,7 @@ class ByteBuffer {
116116

117117
byte peek(); // Relative peek. Reads and returns the next byte in the buffer from the current position but does not increment the read position
118118
byte get(); // Relative get method. Reads the byte at the buffers current position then increments the position
119-
byte get(unsigned int index); // Absolute get method. Read byte at index
119+
byte get(unsigned int index) const; // Absolute get method. Read byte at index
120120
void getBytes(byte* buf, unsigned int len); // Absolute read into array buf of length len
121121
char getChar(); // Relative
122122
char getChar(unsigned int index); // Absolute
@@ -157,27 +157,27 @@ class ByteBuffer {
157157
rpos = r;
158158
}
159159

160-
int getReadPos() {
160+
int getReadPos() const {
161161
return rpos;
162162
}
163163

164164
void setWritePos(unsigned int w) {
165165
wpos = w;
166166
}
167167

168-
int getWritePos() {
168+
int getWritePos() const {
169169
return wpos;
170170
}
171171

172172
// Utility Functions
173173
#ifdef BB_UTILITY
174174
void setName(std::string n);
175-
std::string getName();
176-
void printInfo();
177-
void printAH();
178-
void printAscii();
179-
void printHex();
180-
void printPosition();
175+
std::string getName() const;
176+
void printInfo() const;
177+
void printAH() const;
178+
void printAscii() const;
179+
void printHex() const;
180+
void printPosition() const;
181181
#endif
182182
};
183183

src/Client.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class Client {
3737
Client(int fd, sockaddr_in addr);
3838
~Client();
3939

40-
sockaddr_in getClientAddr() {
40+
sockaddr_in getClientAddr() const {
4141
return clientAddr;
4242
}
4343

44-
int getSocket() {
44+
int getSocket() const {
4545
return socketDesc;
4646
}
4747

src/HTTPMessage.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ std::string HTTPMessage::getStrElement(char delim) {
149149
return "";
150150

151151
// Grab the std::string from the byte buffer up to the delimiter
152-
char *str = new char[size];
153-
bzero(str, size);
152+
auto str = new char[size];
153+
memset(str, 0x00, size);
154154
getBytes((byte*)str, size);
155155
str[size - 1] = 0x00; // NULL termination
156156
ret.assign(str);
@@ -168,7 +168,8 @@ std::string HTTPMessage::getStrElement(char delim) {
168168
* Parse headers will move the read position past the blank line that signals the end of the headers
169169
*/
170170
void HTTPMessage::parseHeaders() {
171-
std::string hline = "", app = "";
171+
std::string hline = "";
172+
std::string app = "";
172173

173174
// Get the first header
174175
hline = getLine();
@@ -208,10 +209,9 @@ bool HTTPMessage::parseBody() {
208209
// contentLen should NOT exceed the remaining number of bytes in the buffer
209210
// Add 1 to bytesRemaining so it includes the byte at the current read position
210211
if (contentLen > bytesRemaining() + 1) {
211-
/*
212212
// If it exceeds, read only up to the number of bytes remaining
213-
dataLen = bytesRemaining();
214-
*/
213+
// dataLen = bytesRemaining();
214+
215215
// If it exceeds, there's a potential security issue and we can't reliably parse
216216
std::stringstream pes;
217217
pes << "Content-Length (" << hlenstr << ") is greater than remaining bytes (" << bytesRemaining() << ")";
@@ -243,8 +243,9 @@ bool HTTPMessage::parseBody() {
243243
*
244244
* @param string containing formatted header: value
245245
*/
246-
void HTTPMessage::addHeader(std::string line) {
247-
std::string key = "", value = "";
246+
void HTTPMessage::addHeader(std::string const line) {
247+
std::string key = "";
248+
std::string value = "";
248249
size_t kpos;
249250
int i = 0;
250251
kpos = line.find(':');
@@ -271,7 +272,7 @@ void HTTPMessage::addHeader(std::string line) {
271272
* @param key String representation of the Header Key
272273
* @param value String representation of the Header value
273274
*/
274-
void HTTPMessage::addHeader(std::string key, std::string value) {
275+
void HTTPMessage::addHeader(std::string const key, std::string const value) {
275276
headers->insert(std::pair<std::string, std::string>(key, value));
276277
}
277278

@@ -282,7 +283,7 @@ void HTTPMessage::addHeader(std::string key, std::string value) {
282283
* @param key String representation of the Header Key
283284
* @param value Integer representation of the Header value
284285
*/
285-
void HTTPMessage::addHeader(std::string key, int value) {
286+
void HTTPMessage::addHeader(std::string const key, int value) {
286287
std::stringstream sz;
287288
sz << value;
288289
headers->insert(std::pair<std::string, std::string>(key, sz.str()));
@@ -294,7 +295,7 @@ void HTTPMessage::addHeader(std::string key, int value) {
294295
*
295296
* @param key Key to identify the header
296297
*/
297-
std::string HTTPMessage::getHeaderValue(std::string key) {
298+
std::string HTTPMessage::getHeaderValue(std::string const key) {
298299

299300
char c;
300301
std::string key_lower = "";

src/HTTPMessage.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,25 @@ class HTTPMessage : public ByteBuffer {
113113
bool parseBody();
114114

115115
// Header Map manipulation
116-
void addHeader(std::string line);
117-
void addHeader(std::string key, std::string value);
118-
void addHeader(std::string key, int value);
119-
std::string getHeaderValue(std::string key);
116+
void addHeader(std::string const line);
117+
void addHeader(std::string const key, std::string const value);
118+
void addHeader(std::string const key, int value);
119+
std::string getHeaderValue(std::string const key);
120120
std::string getHeaderStr(int index);
121121
int getNumHeaders();
122122
void clearHeaders();
123123

124124
// Getters & Setters
125125

126-
std::string getParseError() {
126+
std::string getParseError() const {
127127
return parseErrorStr;
128128
}
129129

130130
void setVersion(std::string v) {
131131
version = v;
132132
}
133133

134-
std::string getVersion() {
134+
std::string getVersion() const {
135135
return version;
136136
}
137137

@@ -144,7 +144,7 @@ class HTTPMessage : public ByteBuffer {
144144
return data;
145145
}
146146

147-
unsigned int getDataLength() {
147+
unsigned int getDataLength() const {
148148
return dataLen;
149149
}
150150
};

src/HTTPRequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ byte* HTTPRequest::create() {
104104
}
105105

106106
// Allocate space for the returned byte array and return it
107-
byte* createRetData = new byte[size()];
107+
auto createRetData = new byte[size()];
108108
setReadPos(0);
109109
getBytes(createRetData, size());
110110

src/HTTPRequest.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ class HTTPRequest : public HTTPMessage {
4949
method = m;
5050
}
5151

52-
int getMethod() {
52+
int getMethod() const {
5353
return method;
5454
}
5555

5656
void setRequestUri(std::string u) {
5757
requestUri = u;
5858
}
5959

60-
std::string getRequestUri() {
60+
std::string getRequestUri() const {
6161
return requestUri;
6262
}
6363
};

src/HTTPResponse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ byte* HTTPResponse::create() {
114114
}
115115

116116
// Allocate space for the returned byte array and return it
117-
byte* createRetData = new byte[size()];
117+
auto createRetData = new byte[size()];
118118
setReadPos(0);
119119
getBytes(createRetData, size());
120120

src/HTTPResponse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class HTTPResponse : public HTTPMessage {
4949
determineReasonStr();
5050
}
5151

52-
std::string getReason() {
52+
std::string getReason() const {
5353
return reason;
5454
}
5555
};

0 commit comments

Comments
 (0)