Crow  1.1
A C++ microframework for the web
 
Loading...
Searching...
No Matches
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
16namespace 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 inline const std::string& get_header_value(const ci_map& headers, const std::string& key)
24 {
25 static const std::string EMPTY;
26 const auto it = headers.find(key);
27 if (it != headers.end()) {
28 return it->second;
29 }
30 else {
31 return EMPTY;
32 }
33 }
34
35 /// An HTTP request.
36 struct request
37 {
38 HTTPMethod method;
39 std::string raw_url; ///< The full URL containing the `?` and URL parameters.
40 std::string url; ///< The endpoint without any parameters.
41 query_string url_params; ///< The parameters associated with the request. (everything after the `?` in the URL)
42 ci_map headers;
43 std::string body;
44 std::string remote_ip_address; ///< The IP address from which the request was sent.
45 unsigned char http_ver_major, http_ver_minor;
46 bool keep_alive, ///< Whether or not the server should send a `connection: Keep-Alive` header to the client.
47 close_connection, ///< Whether or not the server should shut down the TCP connection once a response is sent.
48 upgrade; ///< Whether or noth the server should change the HTTP connection to a different connection.
49
50 void* middleware_context{};
51 void* middleware_container{};
52 asio::io_context* io_context{};
53
54 /// Construct an empty request. (sets the method to `GET`)
56 method(HTTPMethod::Get)
57 {}
58
59 /// Construct a request with all values assigned.
60 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):
61 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)
62 {}
63
64 void add_header(std::string key, std::string value)
65 {
66 headers.emplace(std::move(key), std::move(value));
67 }
68
69 const std::string& get_header_value(const std::string& key) const
70 {
71 return crow::get_header_value(headers, key);
72 }
73
74 bool check_version(unsigned char major, unsigned char minor) const
75 {
76 return http_ver_major == major && http_ver_minor == minor;
77 }
78
79 /// Get the body as parameters in QS format.
80
81 ///
82 /// This is meant to be used with requests of type "application/x-www-form-urlencoded"
84 {
85 return query_string(body, false);
86 }
87
88 /// Send data to whoever made this request with a completion handler and return immediately.
89 template<typename CompletionHandler>
90 void post(CompletionHandler handler)
91 {
92 asio::post(io_context, handler);
93 }
94
95 /// Send data to whoever made this request with a completion handler.
96 template<typename CompletionHandler>
97 void dispatch(CompletionHandler handler)
98 {
99 asio::dispatch(io_context, handler);
100 }
101 };
102} // 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 ci_map &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:23
An HTTP request.
Definition http_request.h:37
void post(CompletionHandler handler)
Send data to whoever made this request with a completion handler and return immediately.
Definition http_request.h:90
bool close_connection
Whether or not the server should shut down the TCP connection once a response is sent.
Definition http_request.h:47
bool keep_alive
Whether or not the server should send a connection: Keep-Alive header to the client.
Definition http_request.h:46
std::string raw_url
The full URL containing the ? and URL parameters.
Definition http_request.h:39
query_string url_params
The parameters associated with the request. (everything after the ? in the URL)
Definition http_request.h:41
request()
Construct an empty request. (sets the method to GET)
Definition http_request.h:55
std::string url
The endpoint without any parameters.
Definition http_request.h:40
std::string remote_ip_address
The IP address from which the request was sent.
Definition http_request.h:44
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:60
void dispatch(CompletionHandler handler)
Send data to whoever made this request with a completion handler.
Definition http_request.h:97
bool upgrade
Whether or noth the server should change the HTTP connection to a different connection.
Definition http_request.h:48
const query_string get_body_params() const
Get the body as parameters in QS format.
Definition http_request.h:83