-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathhttp_request.cpp
More file actions
91 lines (82 loc) · 2.26 KB
/
Copy pathhttp_request.cpp
File metadata and controls
91 lines (82 loc) · 2.26 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
#include <boost/regex.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
#include "async_web_server_cpp/http_request.hpp"
namespace async_web_server_cpp
{
static boost::regex uri_regex("(.*?)(?:\\?(.*?))?");
bool HttpRequest::parse_uri()
{
boost::smatch match;
if (regex_match(uri, match, uri_regex))
{
path.assign(match[1].first, match[1].second);
if (match[2].matched)
{
query.assign(match[2].first, match[2].second);
std::vector<std::string> pair_strings;
boost::split(pair_strings, query, boost::is_any_of("&"));
BOOST_FOREACH(const std::string & pair_string, pair_strings)
{
std::vector<std::string> pair_data;
const size_t eq_index = pair_string.find_first_of('=');
if (eq_index == std::string::npos)
{
if (pair_string.size() > 0)
{
query_params[pair_string] = "";
}
}
else
{
query_params[pair_string.substr(0, eq_index)] = pair_string.substr(eq_index + 1);
}
}
}
return true;
}
else
{
return false;
}
}
bool HttpRequest::has_header(const std::string &name) const
{
typedef std::vector<async_web_server_cpp::HttpHeader> HeaderList;
for (HeaderList::const_iterator itr = headers.begin(); itr != headers.end(); ++itr)
{
if (itr->name.compare(name) == 0)
return false;
}
return true;
}
std::string HttpRequest::get_header_value_or_default(const std::string &name,
const std::string &default_value) const
{
typedef std::vector<async_web_server_cpp::HttpHeader> HeaderList;
for (HeaderList::const_iterator itr = headers.begin(); itr != headers.end(); ++itr)
{
if (itr->name.compare(name) == 0)
return itr->value;
}
return default_value;
}
bool HttpRequest::has_query_param(const std::string &name) const
{
std::map<std::string, std::string>::const_iterator itr = query_params.find(name);
return itr != query_params.end();
}
std::string HttpRequest::get_query_param_value_or_default(const std::string &name,
const std::string &default_value) const
{
std::map<std::string, std::string>::const_iterator itr = query_params.find(name);
if (itr != query_params.end())
{
return itr->second;
}
else
{
return default_value;
}
}
}