-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpUtils.cpp
74 lines (59 loc) · 1.64 KB
/
HttpUtils.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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
#include <assert.h>
#include "HttpUtils.h"
using namespace std;
map<string, string> HttpUtils::params(string query) {
map<string, string> paramMap;
if (query.size() == 0) {
return paramMap;
}
vector<string> pairs = split(query, '&');
for (unsigned idx = 0; idx < pairs.size(); idx++) {
string param = pairs[idx];
vector<string> elements = split(param, '=');
if (elements.size() != 2) {
throw MalformedQueryString(query);
}
// XXX FIXME we need to url decode the strings
paramMap[elements[0]] = elements[1];
}
return paramMap;
}
void HttpUtils::writeChunk(MySocket *client,
const void *buf, int numBytes) {
char chunkHeader[256];
snprintf(chunkHeader, sizeof(chunkHeader), "%x\r\n", numBytes);
client->write(chunkHeader);
if (buf != NULL && numBytes > 0) {
client->write(string((const char *) buf, numBytes));
}
client->write("\r\n");
}
void HttpUtils::writeLastChunk(MySocket *client) {
writeChunk(client, NULL, 0);
}
// split lifted from stackoverflow
// http://stackoverflow.com/questions/236129/split-a-string-in-c
vector<string> &HttpUtils::split(const string &s,
char delim,
vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> HttpUtils::split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
vector<string> result;
for (unsigned int idx = 0; idx < elems.size(); idx++) {
if (elems[idx].size() > 0) {
result.push_back(elems[idx]);
}
}
return result;
}