Crow  1.1
A C++ microframework for the web
http_request.h
1 #pragma once
2 
3 #ifndef ASIO_STANDALONE
4 #define ASIO_STANDALONE
5 #endif
6 #include <asio.hpp>
7 
8 #include "crow/common.h"
9 #include "crow/ci_map.h"
10 #include "crow/query_string.h"
11 
12 namespace crow
13 {
14  /// Find and return the value associated with the key. (returns an empty string if nothing is found)
15  template<typename T>
16  inline const std::string& get_header_value(const T& headers, const std::string& key)
17  {
18  if (headers.count(key))
19  {
20  return headers.find(key)->second;
21  }
22  static std::string empty;
23  return empty;
24  }
25 
26  /// An HTTP request.
27  struct request
28  {
29  HTTPMethod method;
30  std::string raw_url; ///< The full URL containing the `?` and URL parameters.
31  std::string url; ///< The endpoint without any parameters.
32  query_string url_params; ///< The parameters associated with the request. (everything after the `?` in the URL)
33  ci_map headers;
34  std::string body;
35  std::string remote_ip_address; ///< The IP address from which the request was sent.
36  unsigned char http_ver_major, http_ver_minor;
37  bool keep_alive, ///< Whether or not the server should send a `connection: Keep-Alive` header to the client.
38  close_connection, ///< Whether or not the server should shut down the TCP connection once a response is sent.
39  upgrade; ///< Whether or noth the server should change the HTTP connection to a different connection.
40 
41  void* middleware_context{};
42  void* middleware_container{};
43  asio::io_service* io_service{};
44 
45  /// Construct an empty request. (sets the method to `GET`)
47  method(HTTPMethod::Get)
48  {}
49 
50  /// Construct a request with all values assigned.
51  request(HTTPMethod method, std::string raw_url, std::string url, query_string url_params, ci_map headers, std::string body, unsigned char http_major, unsigned char http_minor, bool has_keep_alive, bool has_close_connection, bool is_upgrade):
52  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)), http_ver_major(http_major), http_ver_minor(http_minor), keep_alive(has_keep_alive), close_connection(has_close_connection), upgrade(is_upgrade)
53  {}
54 
55  void add_header(std::string key, std::string value)
56  {
57  headers.emplace(std::move(key), std::move(value));
58  }
59 
60  const std::string& get_header_value(const std::string& key) const
61  {
62  return crow::get_header_value(headers, key);
63  }
64 
65  bool check_version(unsigned char major, unsigned char minor) const
66  {
67  return http_ver_major == major && http_ver_minor == minor;
68  }
69 
70  /// Get the body as parameters in QS format.
71 
72  ///
73  /// This is meant to be used with requests of type "application/x-www-form-urlencoded"
75  {
76  return query_string(body, false);
77  }
78 
79  /// Send data to whoever made this request with a completion handler and return immediately.
80  template<typename CompletionHandler>
81  void post(CompletionHandler handler)
82  {
83  io_service->post(handler);
84  }
85 
86  /// Send data to whoever made this request with a completion handler.
87  template<typename CompletionHandler>
88  void dispatch(CompletionHandler handler)
89  {
90  io_service->dispatch(handler);
91  }
92  };
93 } // namespace crow
A class to represent any data coming after the ? in the request URL into key-value pairs.
Definition: query_string.h:294
An HTTP request.
Definition: http_request.h:28
request(HTTPMethod method, std::string raw_url, std::string url, query_string url_params, ci_map headers, std::string body, unsigned char http_major, unsigned char http_minor, bool has_keep_alive, bool has_close_connection, bool is_upgrade)
Construct a request with all values assigned.
Definition: http_request.h:51
void post(CompletionHandler handler)
Send data to whoever made this request with a completion handler and return immediately.
Definition: http_request.h:81
bool close_connection
Whether or not the server should shut down the TCP connection once a response is sent.
Definition: http_request.h:38
bool keep_alive
Whether or not the server should send a connection: Keep-Alive header to the client.
Definition: http_request.h:37
std::string raw_url
The full URL containing the ? and URL parameters.
Definition: http_request.h:30
query_string url_params
The parameters associated with the request. (everything after the ? in the URL)
Definition: http_request.h:32
request()
Construct an empty request. (sets the method to GET)
Definition: http_request.h:46
std::string url
The endpoint without any parameters.
Definition: http_request.h:31
std::string remote_ip_address
The IP address from which the request was sent.
Definition: http_request.h:35
void dispatch(CompletionHandler handler)
Send data to whoever made this request with a completion handler.
Definition: http_request.h:88
bool upgrade
Whether or noth the server should change the HTTP connection to a different connection.
Definition: http_request.h:39
const query_string get_body_params() const
Get the body as parameters in QS format.
Definition: http_request.h:74