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