Skip to content

Commit 036036b

Browse files
committed
Removed scoping under the standard namespace. Moved to clang and c++11. Switched maps to unordered_maps (hashtables). Began work on recognizing Mime types. Misc bug fixes
1 parent b814134 commit 036036b

15 files changed

Lines changed: 255 additions & 186 deletions

Makefile

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

src/ByteBuffer.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,15 @@
3434
#include <stdio.h>
3535
#endif
3636

37-
using namespace std;
38-
3937
typedef unsigned char byte;
4038

4139
class ByteBuffer {
4240
private:
4341
unsigned int rpos, wpos;
44-
vector<byte> buf;
42+
std::vector<byte> buf;
4543

4644
#ifdef BB_UTILITY
47-
string name;
45+
std::string name;
4846
#endif
4947

5048
template <typename T> T read() {
@@ -172,8 +170,8 @@ class ByteBuffer {
172170

173171
// Utility Functions
174172
#ifdef BB_UTILITY
175-
void setName(string n);
176-
string getName();
173+
void setName(std::string n);
174+
std::string getName();
177175
void printInfo();
178176
void printAH();
179177
void printAscii();

src/HTTPMessage.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ HTTPMessage::HTTPMessage() : ByteBuffer(4096) {
2222
this->init();
2323
}
2424

25-
HTTPMessage::HTTPMessage(string sData) : ByteBuffer(sData.size()+1) {
25+
HTTPMessage::HTTPMessage(std::string sData) : ByteBuffer(sData.size()+1) {
2626
putBytes((byte*)sData.c_str(), sData.size()+1);
2727
this->init();
2828
}
@@ -44,7 +44,7 @@ void HTTPMessage::init() {
4444

4545
version = HTTP_VERSION; // By default, all create() will indicate the version is whatever HTTP_VERSION is
4646

47-
headers = new map<string, string>();
47+
headers = new std::map<std::string, std::string>();
4848
}
4949

5050
/**
@@ -54,7 +54,7 @@ void HTTPMessage::init() {
5454
* @param str String to put into the byte buffer
5555
* @param crlf_end If true (default), end the line with a \r\n
5656
*/
57-
void HTTPMessage::putLine(string str, bool crlf_end) {
57+
void HTTPMessage::putLine(std::string str, bool crlf_end) {
5858
// Terminate with crlf if flag set
5959
if(crlf_end)
6060
str += "\r\n";
@@ -69,9 +69,9 @@ void HTTPMessage::putLine(string str, bool crlf_end) {
6969
* 'Header: value'
7070
*/
7171
void HTTPMessage::putHeaders() {
72-
map<string,string>::const_iterator it;
72+
std::map<std::string,std::string>::const_iterator it;
7373
for(it = headers->begin(); it != headers->end(); it++) {
74-
string final = it->first + ": " + it->second;
74+
std::string final = it->first + ": " + it->second;
7575
putLine(final, true);
7676
}
7777

@@ -86,13 +86,13 @@ void HTTPMessage::putHeaders() {
8686
*
8787
* @return Contents of the line in a string (without CR or LF)
8888
*/
89-
string HTTPMessage::getLine() {
90-
string ret = "";
89+
std::string HTTPMessage::getLine() {
90+
std::string ret = "";
9191
int startPos = getReadPos();
9292
bool newLineReached = false;
9393
char c = 0;
9494

95-
// Append characters to the return string until we hit the end of the buffer, a CR (13) or LF (10)
95+
// Append characters to the return std::string until we hit the end of the buffer, a CR (13) or LF (10)
9696
for(unsigned int i = startPos; i < size(); i++) {
9797
// If the next byte is a \r or \n, we've reached the end of the line and should break out of the loop
9898
c = peek();
@@ -101,7 +101,7 @@ string HTTPMessage::getLine() {
101101
break;
102102
}
103103

104-
// Otherwise, append the next character to the string
104+
// Otherwise, append the next character to the std::string
105105
ret += getChar();
106106
}
107107

@@ -136,8 +136,8 @@ string HTTPMessage::getLine() {
136136
* @param delim The delimiter to stop at when retriving the element. By default, it's a space
137137
* @return Token found in the buffer. Empty if delimiter wasn't reached
138138
*/
139-
string HTTPMessage::getStrElement(char delim) {
140-
string ret = "";
139+
std::string HTTPMessage::getStrElement(char delim) {
140+
std::string ret = "";
141141
int startPos = getReadPos();
142142
unsigned int size = 0;
143143
int endPos = find(delim, startPos);
@@ -148,7 +148,7 @@ string HTTPMessage::getStrElement(char delim) {
148148
if((endPos == -1) || (size <= 0))
149149
return "";
150150

151-
// Grab the string from the byte buffer up to the delimiter
151+
// Grab the std::string from the byte buffer up to the delimiter
152152
char *str = new char[size];
153153
getBytes((byte*)str, size);
154154
str[size-1] = 0x00; // NULL termination
@@ -167,7 +167,7 @@ string HTTPMessage::getStrElement(char delim) {
167167
* Parse headers will move the read position past the blank line that signals the end of the headers
168168
*/
169169
void HTTPMessage::parseHeaders() {
170-
string hline = "", app = "";
170+
std::string hline = "", app = "";
171171

172172
// Get the first header
173173
hline = getLine();
@@ -194,7 +194,7 @@ void HTTPMessage::parseHeaders() {
194194
*/
195195
bool HTTPMessage::parseBody() {
196196
// Content-Length should exist (size of the Body data) if there is body data
197-
string hlenstr = "";
197+
std::string hlenstr = "";
198198
unsigned int contentLen = 0;
199199
hlenstr = getHeaderValue("Content-Length");
200200

@@ -212,7 +212,7 @@ bool HTTPMessage::parseBody() {
212212
dataLen = bytesRemaining();
213213
*/
214214
// If it exceeds, there's a potential security issue and we can't reliably parse
215-
stringstream pes;
215+
std::stringstream pes;
216216
pes << "Content-Length (" << hlenstr << ") is greater than remaining bytes (" << bytesRemaining() << ")";
217217
parseErrorStr = pes.str();
218218
return false;
@@ -238,16 +238,16 @@ bool HTTPMessage::parseBody() {
238238

239239
/**
240240
* Add Header to the Map from string
241-
* Takes a formatted header string "Header: value", parse it, and put it into the map as a key,value pair.
241+
* Takes a formatted header string "Header: value", parse it, and put it into the std::map as a key,value pair.
242242
*
243243
* @param string containing formatted header: value
244244
*/
245-
void HTTPMessage::addHeader(string line) {
246-
string key = "", value = "";
245+
void HTTPMessage::addHeader(std::string line) {
246+
std::string key = "", value = "";
247247
size_t kpos;
248248
int i = 0;
249249
kpos = line.find(':');
250-
if(kpos == string::npos) {
250+
if(kpos == std::string::npos) {
251251
printf("Could not addHeader: %s\n", line.c_str());
252252
return;
253253
}
@@ -265,26 +265,26 @@ void HTTPMessage::addHeader(string line) {
265265
}
266266

267267
/**
268-
* Add header key-value pair to the map
268+
* Add header key-value std::pair to the map
269269
*
270270
* @param key String representation of the Header Key
271271
* @param value String representation of the Header value
272272
*/
273-
void HTTPMessage::addHeader(string key, string value) {
274-
headers->insert(pair<string, string>(key, value));
273+
void HTTPMessage::addHeader(std::string key, std::string value) {
274+
headers->insert(std::pair<std::string, std::string>(key, value));
275275
}
276276

277277
/**
278-
* Add header key-value pair to the map (Integer value)
278+
* Add header key-value std::pair to the map (Integer value)
279279
* Integer value is converted to a string
280280
*
281281
* @param key String representation of the Header Key
282282
* @param value Integer representation of the Header value
283283
*/
284-
void HTTPMessage::addHeader(string key, int value) {
285-
stringstream sz;
284+
void HTTPMessage::addHeader(std::string key, int value) {
285+
std::stringstream sz;
286286
sz << value;
287-
headers->insert(pair<string, string>(key, sz.str()));
287+
headers->insert(std::pair<std::string, std::string>(key, sz.str()));
288288
}
289289

290290
/**
@@ -293,9 +293,9 @@ void HTTPMessage::addHeader(string key, int value) {
293293
*
294294
* @param key Key to identify the header
295295
*/
296-
string HTTPMessage::getHeaderValue(string key) {
296+
std::string HTTPMessage::getHeaderValue(std::string key) {
297297
// Lookup in map
298-
map<string, string>::const_iterator it;
298+
std::map<std::string, std::string>::const_iterator it;
299299
it = headers->find(key);
300300

301301
// Key wasn't found, return a blank value
@@ -312,10 +312,10 @@ string HTTPMessage::getHeaderValue(string key) {
312312
*
313313
* @ret Formatted string with header name and value
314314
*/
315-
string HTTPMessage::getHeaderStr(int index) {
315+
std::string HTTPMessage::getHeaderStr(int index) {
316316
int i = 0;
317-
string ret = "";
318-
map<string,string>::const_iterator it;
317+
std::string ret = "";
318+
std::map<std::string,std::string>::const_iterator it;
319319
for(it = headers->begin(); it != headers->end(); it++) {
320320
if(i == index) {
321321
ret = it->first + ": " + it->second;

src/HTTPMessage.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ enum Status {
7777

7878
class HTTPMessage : public ByteBuffer {
7979
private:
80-
map<string, string> *headers;
80+
std::map<std::string, std::string> *headers;
8181

8282
protected:
83-
string parseErrorStr;
83+
std::string parseErrorStr;
8484

85-
string version;
85+
std::string version;
8686

8787
// Message Body Data (Resource in the case of a response, extra parameters in the case of a request)
8888
byte* data;
@@ -93,43 +93,43 @@ class HTTPMessage : public ByteBuffer {
9393

9494
public:
9595
HTTPMessage();
96-
HTTPMessage(string sData);
96+
HTTPMessage(std::string sData);
9797
HTTPMessage(byte *pData, unsigned int len);
9898
virtual ~HTTPMessage();
9999

100100
virtual byte* create() = 0;
101101
virtual bool parse() = 0;
102102

103103
// Create helpers
104-
void putLine(string str = "", bool crlf_end=true);
104+
void putLine(std::string str = "", bool crlf_end=true);
105105
void putHeaders();
106106

107107
// Parse helpers
108-
string getLine();
109-
string getStrElement(char delim = 0x20); // 0x20 = "space"
108+
std::string getLine();
109+
std::string getStrElement(char delim = 0x20); // 0x20 = "space"
110110
void parseHeaders();
111111
bool parseBody();
112112

113113
// Header Map manipulation
114-
void addHeader(string line);
115-
void addHeader(string key, string value);
116-
void addHeader(string key, int value);
117-
string getHeaderValue(string key);
118-
string getHeaderStr(int index);
114+
void addHeader(std::string line);
115+
void addHeader(std::string key, std::string value);
116+
void addHeader(std::string key, int value);
117+
std::string getHeaderValue(std::string key);
118+
std::string getHeaderStr(int index);
119119
int getNumHeaders();
120120
void clearHeaders();
121121

122122
// Getters & Setters
123123

124-
string getParseError() {
124+
std::string getParseError() {
125125
return parseErrorStr;
126126
}
127127

128-
void setVersion(string v) {
128+
void setVersion(std::string v) {
129129
version = v;
130130
}
131131

132-
string getVersion() {
132+
std::string getVersion() {
133133
return version;
134134
}
135135

0 commit comments

Comments
 (0)