4#include <boost/asio.hpp>
14#include "crow/common.h"
15#include "crow/ci_map.h"
16#include "crow/query_string.h"
21 namespace asio = boost::asio;
27 s.erase(std::remove_if(s.begin(), s.end(),
28 [](
char c) { return c ==
'\r' || c ==
'\n'; }),
33 inline const std::string&
get_header_value(
const ci_map& headers,
const std::string& key)
35 static const std::string EMPTY;
36 const auto it = headers.find(key);
37 if (it != headers.end()) {
55 unsigned char http_ver_major, http_ver_minor;
60 void* middleware_context{};
61 void* middleware_container{};
62 asio::io_context* io_context{};
66 method(HTTPMethod::Get)
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)
74 void add_header(std::string key, std::string value)
78 headers.emplace(std::move(key), std::move(value));
81 const std::string& get_header_value(
const std::string& key)
const
86 bool check_version(
unsigned char major,
unsigned char minor)
const
88 return http_ver_major == major && http_ver_minor == minor;
101 template<
typename CompletionHandler>
102 void post(CompletionHandler handler)
104 asio::post(io_context, handler);
108 template<
typename CompletionHandler>
111 asio::dispatch(io_context, handler);
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