-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.cpp
More file actions
325 lines (278 loc) · 8.67 KB
/
httpserver.cpp
File metadata and controls
325 lines (278 loc) · 8.67 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright (C) 2012 Lars Hall
*
* This file is part of StreamRecorder.
*
* StreamRecorder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* StreamRecorder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with StreamRecorder. If not, see <http://www.gnu.org/licenses/>.
*/
#include "streamrecorder.h"
#include "httpserver.h"
#include "webfrontend.h"
/*
* A simple blocking httpserver
* Uhh keep it safe behind a firewall or something
*
* TODO:
* Add more status codes
* Add charset
* Add content type
*/
HttpServer::HttpServer(uint16_t port, const string &serverPath,
WebFrontend *frontend)
{
this->port = port;
started = false;
this->serverPath = serverPath;
this->frontend = frontend;
}
void HttpServer::startServer()
{
struct sockaddr_in name;
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
fprintf(stderr, "HttpServer: Error creating socket");
int reuse = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(char *)&reuse, sizeof(reuse)) < 0)
fprintf(stderr, "HttpServer: Error setting SO_REUSEADDR");
name.sin_family = AF_INET;
name.sin_port = htons (port);
name.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind (sockfd, (struct sockaddr *) &name, sizeof (name)) < 0)
fprintf(stderr, "HttpServer: Error binding socket\n");
if (listen (sockfd, HTTP_MAX_PENDING) < 0)
fprintf(stderr, "HttpServer: Error listening\n");
}
void HttpServer::run()
{
char buffer[HTTP_MAX_BUFFER];
if (!started)
startServer();
while(true)
{
struct sockaddr_in cliAddr;
socklen_t clientLen = sizeof(cliAddr);
int newsockfd =
accept(sockfd, (struct sockaddr *) &cliAddr, &clientLen);
memset(buffer, 0, HTTP_MAX_BUFFER);
if (newsockfd)
{
int numbytes = read(newsockfd, buffer, HTTP_MAX_BUFFER);
if (numbytes > 0)
{
string contentType = "";
Request request;
parseRequest(request, buffer);
#ifdef DEBUG
fprintf(stderr, "request:%s\n", request.requestStr.c_str());
#endif
uint8_t bytes[HTTP_MAX_FILE_SIZE];
memset(bytes, 0, HTTP_MAX_FILE_SIZE);
int size = 0;
if ((size = frontend->handleRequest(
contentType, bytes, request)) == -1)
{
// try to see there is a file in serverpath to send
if (request.path == "/")
request.path = "/index.html";
size = loadFile(bytes, request.path);
}
char tmp[HTTP_MAX_FILE_SIZE];
memset(tmp, 0, HTTP_MAX_FILE_SIZE);
string header = "";
// Not pretty. Only 200 and 404 are implemented
if (size > -1)
{
header = createHeader(200, contentType, request);
memcpy(tmp, header.c_str(), header.length());
memcpy(tmp + header.length(), bytes, size);
}
else
{
size = 0;
header = createHeader(404, contentType,
request, "404 Not Found");
memcpy(tmp, header.c_str(), header.length());
}
send(newsockfd, tmp, header.length() + size, 0);
close(newsockfd);
}
}
usleep(0.5);
}
}
void HttpServer::parseRequest(Request &request, const string &req)
{
vector<string> tokens;
splitString(tokens, req, " ");
request.requestStr = req;
if (tokens.size() >= 2)
{
if (tokens[0] == "GET")
request.type = HTTP_GET;
// Note: Only get request is supported
else if (tokens[0] == "POST")
request.type = HTTP_POST;
else if (tokens[0] == "PUT")
request.type = HTTP_PUT;
else if (tokens[0] == "DELETE")
request.type = HTTP_DELETE;
string decoded = percentDecode(tokens[1].c_str());
int pos = parseReqParams(request.params, decoded, false);
if (pos > -1)
request.path = decoded.substr(0, (int)pos);
else
request.path = decoded;
}
if (request.type == HTTP_POST)
{
unsigned int bodyPos = req.find("\r\n\r\n");
if ((bodyPos != string::npos) && (bodyPos < req.length() - 1))
{
// extract the body (after "\r\n\r\n")
string body =
percentDecode(req.substr(bodyPos + 3, req.length()));
RequestParams post;
parseReqParams(post, body, true);
RequestParams::iterator it;
// Hack: appending post params to request params
// (treating post key/vals like get key/vals)
for (it = post.begin(); it != post.end(); it++)
request.params.push_back(make_pair(it->first, it->second));
}
}
}
int HttpServer::parseReqParams(RequestParams ¶ms,
const string &req, bool isPost)
{
int ret = -1;
int pos = 0;
if (!isPost)
pos = req.find('?');
if (((pos != (int)string::npos) && ((int)req.length() - 1 > pos)) ||
isPost)
{
ret = pos;
// skipping '?'
string str = req.substr(pos + 1, req.length());
vector<string> keyvals;
// split per '&'
splitString(keyvals, str, "&");
for (unsigned int i = 0; i < keyvals.size(); i++)
{
// splits key=val
vector<string> tokens;
splitString(tokens, keyvals[i], "=");
if (tokens.size() > 0)
{
string key = "";
string value = "";
key = tokens[0];
if (tokens.size() >= 2)
value = tokens[1];
params.push_back(make_pair(key, value));
}
}
}
return ret;
}
string HttpServer::percentDecode(const string &data)
{
string ret = "";
unsigned int i = 0;
while(i < data.size())
{
if ((data[i] == '%') && (i < data.size() - 2))
{
char str[3];
str[0] = data[i+1];
str[1] = data[i+2];
str[2] = '\0';
uint64_t tmp = strtoul(str, NULL, 16);
ret += (char)tmp;
i += 2;
}
else if (data[i] == '+')
ret += " ";
else
ret += data[i];
i++;
}
return ret;
}
string HttpServer::createHeader(int status, const string &contentType,
const Request &request, const string &reason)
{
string ret = "";
string version = "HTTP/1.0";
string type = "text/plain";
if (status == 200)
ret = version + " 200 OK";
else if (status == 404)
ret = version + " 404 Not Found";
if ((status == 200) && (contentType == ""))
{
// trying to guess the content type
// by .extension
// Uhh not pretty
if (request.path.find(".png") != string::npos)
type = "image/png";
else if (request.path.find(".json") != string::npos)
type = "application/json";
else
type = "text/html";
}
else
{
if (contentType != "")
type = contentType;
ret += "\r\nContent-Type:" + type + "\r\n\r\n";
}
ret += "\r\n\r\n";
if (reason != "")
ret += reason;
return ret;
}
int HttpServer::loadFile(uint8_t *bytes, const string &filename)
{
int ret = -1;
ifstream file((serverPath + filename).c_str(),
ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size_t size = file.tellg();
file.seekg(0, ios::beg);
file.read((char*)bytes, size);
file.close();
ret = true;
ret = size;
}
return ret;
}
void HttpServer::splitString(vector<string> &tokens,
const string &str, const string &sep)
{
char tmp[HTTP_MAX_TOKEN];
memset(tmp, 0, HTTP_MAX_TOKEN);
strncpy(tmp, str.c_str(), HTTP_MAX_TOKEN);
char *token = strtok (tmp, sep.c_str());
while (token != NULL)
{
tokens.push_back(token);
token = strtok (NULL, sep.c_str());
}
}
HttpServer::~HttpServer()
{
}