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 <algorithm>
13
14#include "crow/common.h"
15#include "crow/ci_map.h"
16#include "crow/query_string.h"
17
18namespace crow // NOTE: Already documented in "crow/app.h"
19{
20#ifdef CROW_USE_BOOST
21 namespace asio = boost::asio;
22#endif
23
24 /// Remove CR (\r) and LF (\n) characters from a header name or value to prevent header injection.
25 inline void sanitize_header_value(std::string& s)
26 {
27 s.erase(std::remove_if(s.begin(), s.end(),
28 [](char c) { return c == '\r' || c == '\n'; }),
29 s.end());
30 }
31
32 /// Find and return the value associated with the key. (returns an empty string if nothing is found)
33 inline const std::string& get_header_value(const ci_map& headers, const std::string& key)
34 {
35 static const std::string EMPTY;
36 const auto it = headers.find(key);
37 if (it != headers.end()) {
38 return it->second;
39 }
40 else {
41 return EMPTY;
42 }
43 }
44
45 /// An HTTP request.
46 struct request
47 {
48 HTTPMethod method;
49 std::string raw_url; ///< The full URL containing the `?` and URL parameters.
50 std::string url; ///< The endpoint without any parameters.
51 query_string url_params; ///< The parameters associated with the request. (everything after the `?` in the URL)
52 ci_map headers;
53 std::string body;
54 std::string remote_ip_address; ///< The IP address from which the request was sent.
55 unsigned char http_ver_major, http_ver_minor;
56 bool keep_alive, ///< Whether or not the server should send a `connection: Keep-Alive` header to the client.
57 close_connection, ///< Whether or not the server should shut down the TCP connection once a response is sent.
58 upgrade; ///< Whether or noth the server should change the HTTP connection to a different connection.
59
60 void* middleware_context{};
61 void* middleware_container{};
62 asio::io_context* io_context{};
63
64 /// Construct an empty request. (sets the method to `GET`)
66 method(HTTPMethod::Get)
67 {}
68
69 /// Construct a request with all values assigned.
70 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):
71 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)
72 {}
73
74 void add_header(std::string key, std::string value)
75 {
78 headers.emplace(std::move(key), std::move(value));
79 }
80
81 const std::string& get_header_value(const std::string& key) const
82 {
83 return crow::get_header_value(headers, key);
84 }
85
86 bool check_version(unsigned char major, unsigned char minor) const
87 {
88 return http_ver_major == major && http_ver_minor == minor;
89 }
90
91 /// Get the body as parameters in QS format.
92
93 ///
94 /// This is meant to be used with requests of type "application/x-www-form-urlencoded"
96 {
97 return query_string(body, false);
98 }
99
100 /// Send data to whoever made this request with a completion handler and return immediately.
101 template<typename CompletionHandler>
102 void post(CompletionHandler handler)
103 {
104 asio::post(io_context, handler);
105 }
106
107 /// Send data to whoever made this request with a completion handler.
108 template<typename CompletionHandler>
109 void dispatch(CompletionHandler handler)
110 {
111 asio::dispatch(io_context, handler);
112 }
113 };
114} // namespace crow
A class to represent any data coming after the ? in the request URL into key-value pairs.
Definition query_string.h:306
The main namespace of the library. In this namespace is defined the most important classes and functi...
void sanitize_header_value(std::string &s)
Remove CR (\r) and LF ( ) characters from a header name or value to prevent header injection.
Definition http_request.h:25
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:33
An HTTP request.
Definition http_request.h:47
void post(CompletionHandler handler)
Send data to whoever made this request with a completion handler and return immediately.
Definition http_request.h:102
bool close_connection
Whether or not the server should shut down the TCP connection once a response is sent.
Definition http_request.h:57
bool keep_alive
Whether or not the server should send a connection: Keep-Alive header to the client.
Definition http_request.h:56
std::string raw_url
The full URL containing the ? and URL parameters.
Definition http_request.h:49
query_string url_params
The parameters associated with the request. (everything after the ? in the URL)
Definition http_request.h:51
request()
Construct an empty request. (sets the method to GET)
Definition http_request.h:65
std::string url
The endpoint without any parameters.
Definition http_request.h:50
std::string remote_ip_address
The IP address from which the request was sent.
Definition http_request.h:54
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:70
void dispatch(CompletionHandler handler)
Send data to whoever made this request with a completion handler.
Definition http_request.h:109
bool upgrade
Whether or noth the server should change the HTTP connection to a different connection.
Definition http_request.h:58
const query_string get_body_params() const
Get the body as parameters in QS format.
Definition http_request.h:95