-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHelper.hpp
More file actions
83 lines (66 loc) · 2.2 KB
/
HttpHelper.hpp
File metadata and controls
83 lines (66 loc) · 2.2 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
#pragma once
#include <iostream>
class HttpHelper
{
static bool parseHeaderField(std::string strField, std::pair<std::string, std::string>* field)
{
size_t pos = strField.find(":");
if (pos == std::string::npos || pos == 0)
{
return false;
}
field->first = strField.substr(0, pos);
size_t p1 = strField.find_first_not_of(" \t\r\n", pos + 1),
p2 = strField.find_last_not_of(" \t\r\n");
if (p1 == std::string::npos || p2 == std::string::npos || p1 > p2)
{
field->second = "";
}
else
{
field->second = strField.substr(p1, p2 - p1 + 1);
}
return true;
}
public:
static std::map<std::string, std::string> parseHeader(std::string fullHeader)
{
std::map<std::string, std::string> headers;
size_t pos = 0,
last = 0;
std::pair<std::string, std::string> field;
do
{
pos = fullHeader.find("\r\n", last);
if (parseHeaderField(fullHeader.substr(last, pos - last), &field))
{
headers.insert(field);
}
last = pos + 2;
} while (pos != std::string::npos);
return headers;
}
static std::string getHeaderValue(std::string allHeaders, std::string headerName)
{
std::string line;
do
{
line = allHeaders.substr(0, allHeaders.find_first_of("\r\n"));
if (line.find(headerName + ":") != std::string::npos)
{
std::string value = line.substr(headerName.size() + 1);
while (value[0] == ' ' || value[0] == '\t' || value[0] == '\r' || value[0] == '\n')
{
value = value.substr(1);
}
return value;
}
int newPos = allHeaders.find_first_of("\r\n");
if (newPos != std::string::npos)
{
allHeaders = allHeaders.substr(newPos + 2);
}
} while (allHeaders.find("\r\n") != std::string::npos);
return "";
}
};