http_request.cpp
Go to the documentation of this file.
2 
3 #include <boost/algorithm/string.hpp>
4 #include <boost/foreach.hpp>
5 #include <boost/regex.hpp>
6 
7 namespace async_web_server_cpp
8 {
9 
10 static boost::regex uri_regex("(.*?)(?:\\?(.*?))?");
11 
13 {
14  boost::smatch match;
15  if (regex_match(uri, match, uri_regex))
16  {
17  path.assign(match[1].first, match[1].second);
18  if (match[2].matched)
19  {
20  query.assign(match[2].first, match[2].second);
21 
22  std::vector<std::string> pair_strings;
23  boost::split(pair_strings, query, boost::is_any_of("&"));
24  BOOST_FOREACH (const std::string& pair_string, pair_strings)
25  {
26  std::vector<std::string> pair_data;
27  const auto eq_index = pair_string.find_first_of('=');
28  if (eq_index == std::string::npos)
29  {
30  if (pair_string.size() > 0)
31  {
32  query_params[pair_string] = "";
33  }
34  }
35  else
36  {
37  query_params[pair_string.substr(0, eq_index)] =
38  pair_string.substr(eq_index + 1);
39  }
40  }
41  }
42  return true;
43  }
44  else
45  {
46  return false;
47  }
48 }
49 
50 bool HttpRequest::has_header(const std::string& name) const
51 {
52  typedef std::vector<async_web_server_cpp::HttpHeader> HeaderList;
53  for (HeaderList::const_iterator itr = headers.begin(); itr != headers.end();
54  ++itr)
55  {
56  if (itr->name.compare(name) == 0)
57  return false;
58  }
59  return true;
60 }
61 std::string
63  const std::string& default_value) const
64 {
65  typedef std::vector<async_web_server_cpp::HttpHeader> HeaderList;
66  for (HeaderList::const_iterator itr = headers.begin(); itr != headers.end();
67  ++itr)
68  {
69  if (itr->name.compare(name) == 0)
70  return itr->value;
71  }
72  return default_value;
73 }
74 
75 bool HttpRequest::has_query_param(const std::string& name) const
76 {
77  std::map<std::string, std::string>::const_iterator itr =
78  query_params.find(name);
79  return itr != query_params.end();
80 }
81 
83  const std::string& name, const std::string& default_value) const
84 {
85  std::map<std::string, std::string>::const_iterator itr =
86  query_params.find(name);
87  if (itr != query_params.end())
88  {
89  return itr->second;
90  }
91  else
92  {
93  return default_value;
94  }
95 }
96 
97 } // namespace async_web_server_cpp
async_web_server_cpp::HttpRequest::parse_uri
bool parse_uri()
Definition: http_request.cpp:12
async_web_server_cpp
Definition: http_connection.hpp:14
async_web_server_cpp::HttpRequest::has_query_param
bool has_query_param(const std::string &name) const
Definition: http_request.cpp:75
async_web_server_cpp::HttpRequest::has_header
bool has_header(const std::string &name) const
Definition: http_request.cpp:50
async_web_server_cpp::HttpRequest::path
std::string path
Definition: http_request.hpp:26
async_web_server_cpp::HttpRequest::get_query_param_value_or_default
std::string get_query_param_value_or_default(const std::string &name, const std::string &default_value) const
Definition: http_request.cpp:82
async_web_server_cpp::HttpRequest::get_header_value_or_default
std::string get_header_value_or_default(const std::string &name, const std::string &default_value) const
Definition: http_request.cpp:62
async_web_server_cpp::uri_regex
static boost::regex uri_regex("(.*?)(?:\\?(.*?))?")
async_web_server_cpp::HttpRequest::headers
std::vector< HttpHeader > headers
Definition: http_request.hpp:24
async_web_server_cpp::HttpRequest::query_params
std::map< std::string, std::string > query_params
Definition: http_request.hpp:28
async_web_server_cpp::HttpRequest::uri
std::string uri
Definition: http_request.hpp:21
async_web_server_cpp::HttpRequest::query
std::string query
Definition: http_request.hpp:27
http_request.hpp


async_web_server_cpp
Author(s): Mitchell Wills , Russel Toris
autogenerated on Tue Mar 1 2022 23:49:06