http_request.cpp
Go to the documentation of this file.
00001 #include <boost/regex.hpp>
00002 #include <boost/foreach.hpp>
00003 #include <boost/algorithm/string.hpp>
00004 #include "async_web_server_cpp/http_request.hpp"
00005 
00006 namespace async_web_server_cpp
00007 {
00008 
00009 static boost::regex uri_regex("(.*?)(?:\\?(.*?))?");
00010 
00011 bool HttpRequest::parse_uri()
00012 {
00013   boost::smatch match;
00014   if (regex_match(uri, match, uri_regex))
00015   {
00016     path.assign(match[1].first, match[1].second);
00017     if (match[2].matched)
00018     {
00019       query.assign(match[2].first, match[2].second);
00020 
00021       std::vector<std::string> pair_strings;
00022       boost::split(pair_strings, query, boost::is_any_of("&"));
00023       BOOST_FOREACH(const std::string & pair_string, pair_strings)
00024       {
00025         std::vector<std::string> pair_data;
00026         const int eq_index = pair_string.find_first_of('=');
00027         if (eq_index == std::string::npos)
00028         {
00029           if (pair_string.size() > 0)
00030           {
00031             query_params[pair_string] = "";
00032           }
00033         }
00034         else
00035         {
00036           query_params[pair_string.substr(0, eq_index)] = pair_string.substr(eq_index + 1);
00037         }
00038       }
00039     }
00040     return true;
00041   }
00042   else
00043   {
00044     return false;
00045   }
00046 
00047 }
00048 
00049 bool HttpRequest::has_header(const std::string &name) const
00050 {
00051   typedef std::vector<async_web_server_cpp::HttpHeader> HeaderList;
00052   for (HeaderList::const_iterator itr = headers.begin(); itr != headers.end(); ++itr)
00053   {
00054     if (itr->name.compare(name) == 0)
00055       return false;
00056   }
00057   return true;
00058 }
00059 std::string HttpRequest::get_header_value_or_default(const std::string &name,
00060     const std::string &default_value) const
00061 {
00062   typedef std::vector<async_web_server_cpp::HttpHeader> HeaderList;
00063   for (HeaderList::const_iterator itr = headers.begin(); itr != headers.end(); ++itr)
00064   {
00065     if (itr->name.compare(name) == 0)
00066       return itr->value;
00067   }
00068   return default_value;
00069 }
00070 
00071 bool HttpRequest::has_query_param(const std::string &name) const
00072 {
00073   std::map<std::string, std::string>::const_iterator itr = query_params.find(name);
00074   return itr != query_params.end();
00075 }
00076 
00077 std::string HttpRequest::get_query_param_value_or_default(const std::string &name,
00078     const std::string &default_value) const
00079 {
00080   std::map<std::string, std::string>::const_iterator itr = query_params.find(name);
00081   if (itr != query_params.end())
00082   {
00083     return itr->second;
00084   }
00085   else
00086   {
00087     return default_value;
00088   }
00089 }
00090 
00091 }


async_web_server_cpp
Author(s): Mitchell Wills
autogenerated on Thu Aug 27 2015 12:27:39