Crow  0.3
A C++ microframework for the web
http_request.h
1 #pragma once
2 
3 #include <boost/asio.hpp>
4 
5 #include "crow/common.h"
6 #include "crow/ci_map.h"
7 #include "crow/query_string.h"
8 
9 namespace crow
10 {
11  /// Find and return the value associated with the key. (returns an empty string if nothing is found)
12  template <typename T>
13  inline const std::string& get_header_value(const T& headers, const std::string& key)
14  {
15  if (headers.count(key))
16  {
17  return headers.find(key)->second;
18  }
19  static std::string empty;
20  return empty;
21  }
22 
23  struct DetachHelper;
24 
25  /// An HTTP request.
26  struct request
27  {
28  HTTPMethod method;
29  std::string raw_url; ///< The full URL containing the `?` and URL parameters.
30  std::string url; ///< The endpoint without any parameters.
31  query_string url_params; ///< The parameters associated with the request. (everything after the `?`)
32  ci_map headers;
33  std::string body;
34  std::string remoteIpAddress; ///< The IP address from which the request was sent.
35 
36  void* middleware_context{};
37  boost::asio::io_service* io_service{};
38 
39  /// Construct an empty request. (sets the method to `GET`)
41  : method(HTTPMethod::Get)
42  {
43  }
44 
45  /// Construct a request with all values assigned.
46  request(HTTPMethod method, std::string raw_url, std::string url, query_string url_params, ci_map headers, std::string body)
47  : method(method), raw_url(std::move(raw_url)), url(std::move(url)), url_params(std::move(url_params)), headers(std::move(headers)), body(std::move(body))
48  {
49  }
50 
51  void add_header(std::string key, std::string value)
52  {
53  headers.emplace(std::move(key), std::move(value));
54  }
55 
56  const std::string& get_header_value(const std::string& key) const
57  {
58  return crow::get_header_value(headers, key);
59  }
60 
61  /// Send the request with a completion handler and return immediately.
62  template<typename CompletionHandler>
63  void post(CompletionHandler handler)
64  {
65  io_service->post(handler);
66  }
67 
68  /// Send the request with a completion handler.
69  template<typename CompletionHandler>
70  void dispatch(CompletionHandler handler)
71  {
72  io_service->dispatch(handler);
73  }
74 
75  };
76 }
crow::request::url_params
query_string url_params
The parameters associated with the request. (everything after the ?)
Definition: http_request.h:31
crow::request::request
request()
Construct an empty request. (sets the method to GET)
Definition: http_request.h:40
crow::request::remoteIpAddress
std::string remoteIpAddress
The IP address from which the request was sent.
Definition: http_request.h:34
crow::query_string
A class to represent any data coming after the ? in the request URL into key-value pairs.
Definition: query_string.h:291
crow::request::url
std::string url
The endpoint without any parameters.
Definition: http_request.h:30
crow::request
An HTTP request.
Definition: http_request.h:26
crow::request::raw_url
std::string raw_url
The full URL containing the ? and URL parameters.
Definition: http_request.h:29
crow::request::dispatch
void dispatch(CompletionHandler handler)
Send the request with a completion handler.
Definition: http_request.h:70
crow::request::post
void post(CompletionHandler handler)
Send the request with a completion handler and return immediately.
Definition: http_request.h:63
crow::request::request
request(HTTPMethod method, std::string raw_url, std::string url, query_string url_params, ci_map headers, std::string body)
Construct a request with all values assigned.
Definition: http_request.h:46