-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRequest.cpp
211 lines (190 loc) · 4.78 KB
/
HttpRequest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "HttpRequest.h"
#include "Utils.h"
#include <stdlib.h>
#include <algorithm>
using std::string;
using std::pair;
const string HttpRequest::SPACES = "\t ";
const string HttpRequest::NEWLINES = "\r\n";
const string HttpRequest::SEPARATOR = ":";
HttpRequest::HttpRequest() :
m_contentLength(0),
m_keepAlive(false),
m_httpError(0),
m_ifModifiedSince(0)
{
}
size_t HttpRequest::parse(const string& buffer)
{
size_t headerEnd = buffer.find("\r\n\r\n");
if (headerEnd == buffer.npos)
{
return 0;
}
// Ok, now we parse this.
size_t startOffset = 0;
size_t endOffset = 0;
// Special handling for the first line.
endOffset = buffer.find_first_of(SPACES);
if (endOffset == buffer.npos)
{
m_httpError = 400;
return headerEnd + 4;
}
m_method = buffer.substr(startOffset, endOffset);
startOffset = buffer.find_first_not_of(SPACES, endOffset);
if (startOffset == buffer.npos)
{
m_httpError = 400;
return headerEnd + 4;
}
endOffset = buffer.find_first_of(SPACES, startOffset);
if (endOffset == buffer.npos)
{
m_httpError = 400;
return headerEnd + 4;
}
m_path = buffer.substr(startOffset, endOffset - startOffset);
startOffset = buffer.find_first_not_of(SPACES, endOffset);
if (startOffset == buffer.npos)
{
m_httpError = 400;
return headerEnd + 4;
}
endOffset = buffer.find_first_of(NEWLINES, startOffset);
if (endOffset == buffer.npos)
{
m_httpError = 400;
return headerEnd + 4;
}
m_httpVersion = buffer.substr(startOffset, endOffset - startOffset);
size_t pathOffset = m_path.find_first_of("#");
if (pathOffset != m_path.npos)
{
m_path = m_path.substr(0, pathOffset);
}
pathOffset = m_path.find_first_of("?");
if (pathOffset != m_path.npos)
{
m_queryString = m_path.substr(pathOffset);
m_path = m_path.substr(0, pathOffset);
}
// Now we loop across all the headers we've got.
startOffset = buffer.find_first_not_of(NEWLINES, endOffset);
while (endOffset < headerEnd)
{
endOffset = buffer.find_first_of(SEPARATOR, startOffset);
if (endOffset == buffer.npos)
{
break;
}
string headerName = buffer.substr(startOffset, endOffset - startOffset);
headerName = headerName.substr(0, headerName.find_last_not_of(SPACES) + 1);
startOffset = buffer.find_first_not_of(SPACES, endOffset + 1);
if (startOffset == buffer.npos)
{
break;
}
endOffset = buffer.find_first_of(NEWLINES, startOffset);
if (endOffset == buffer.npos)
{
break;
}
string value = buffer.substr(startOffset, endOffset - startOffset);
startOffset = buffer.find_first_not_of(NEWLINES, endOffset);
while ((buffer.find_first_of(SPACES) == startOffset) && (startOffset < headerEnd))
{
endOffset = buffer.find_first_of(NEWLINES, startOffset);
if (endOffset == buffer.npos)
{
break;
}
value += buffer.substr(startOffset, endOffset - startOffset);
startOffset = buffer.find_first_not_of(NEWLINES, endOffset);
if (startOffset == buffer.npos)
{
break;
}
}
toLower(headerName);
if (headerName == "user-agent")
{
m_userAgent = value;
}
else if (headerName == "if-modified-since")
{
m_ifModifiedSince = Utils::parseHttpDate(value);
}
m_headers.insert(pair<string, string>(headerName, value));
}
// Set our keep-alive flag.
HeaderMap::const_iterator it = m_headers.find("connection");
{
string c = it->second;
toLower(c);
if (c.find("keep-alive") != c.npos)
{
m_keepAlive = true;
}
}
// Now we need to parse the request body.
it = m_headers.find("content-length");
if (it != m_headers.end())
{
const string& l = it->second;
m_contentLength = strtoll(l.c_str(), NULL, 10);
}
// '4' is the \r\n\r\n following the headers.
unsigned long long totalSize = headerEnd + 4 + m_contentLength;
if (buffer.size() >= totalSize)
{
m_body = buffer.substr(headerEnd + 4, m_contentLength);
}
else
{
totalSize = 0;
}
return totalSize;
}
void HttpRequest::toLower(string& s)
{
transform(s.begin(), s.end(), s.begin(), ::tolower);
}
bool HttpRequest::isKeepAlive() const
{
return m_keepAlive;
}
const string& HttpRequest::getPath() const
{
return m_path;
}
int HttpRequest::getHttpError() const
{
return m_httpError;
}
void HttpRequest::setHttpError(int httpError)
{
m_httpError = httpError;
}
const string& HttpRequest::getUserAgent() const
{
return m_userAgent;
}
time_t HttpRequest::getIfModifiedSince() const
{
return m_ifModifiedSince;
}
string HttpRequest::getHeader(const string& headerName) const
{
string lcHeader = headerName;
toLower(lcHeader);
HeaderMap::const_iterator it = m_headers.find(lcHeader);
if (it == m_headers.end())
{
return "";
}
else
{
return it->second;
}
}