# Crow: include/crow/http_request.h Source File

http_request.h
#pragma once
#ifdef CROW_USE_BOOST
#include <boost/asio.hpp>
#else
#ifndef ASIO_STANDALONE
#define ASIO_STANDALONE
#endif
#include <asio.hpp>
#endif
#include "crow/common.h"
#include "crow/ci_map.h"
#include "crow/query_string.h"
namespace crow // NOTE: Already documented in "crow/app.h"
{
#ifdef CROW_USE_BOOST
namespace asio = boost::asio;
#endif
/// Find and return the value associated with the key. (returns an empty string if nothing is found)
template<typename T>
inline const std::string& get_header_value(const T& headers, const std::string& key)
{
if (headers.count(key))
{
return headers.find(key)->second;
}
static std::string empty;
return empty;
}
/// An HTTP request.
struct request
{
HTTPMethod method;
std::string raw_url; ///< The full URL containing the `?` and URL parameters.
std::string url; ///< The endpoint without any parameters.
query_string url_params; ///< The parameters associated with the request. (everything after the `?` in the URL)
ci_map headers;
std::string body;
std::string remote_ip_address; ///< The IP address from which the request was sent.
unsigned char http_ver_major, http_ver_minor;
bool keep_alive, ///< Whether or not the server should send a `connection: Keep-Alive` header to the client.
close_connection, ///< Whether or not the server should shut down the TCP connection once a response is sent.
upgrade; ///< Whether or noth the server should change the HTTP connection to a different connection.
void* middleware_context{};
void* middleware_container{};
asio::io_service* io_service{};
/// Construct an empty request. (sets the method to `GET`)
request():
method(HTTPMethod::Get)
{}
/// Construct a request with all values assigned.
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):
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)
{}
void add_header(std::string key, std::string value)
{
headers.emplace(std::move(key), std::move(value));
}
const std::string& get_header_value(const std::string& key) const
{
return crow::get_header_value(headers, key);
}
bool check_version(unsigned char major, unsigned char minor) const
{
return http_ver_major == major && http_ver_minor == minor;
}
/// Get the body as parameters in QS format.
///
/// This is meant to be used with requests of type "application/x-www-form-urlencoded"
const query_string get_body_params() const
{
return query_string(body, false);
}
/// Send data to whoever made this request with a completion handler and return immediately.
template<typename CompletionHandler>
void post(CompletionHandler handler)
{
io_service->post(handler);
}
/// Send data to whoever made this request with a completion handler.
template<typename CompletionHandler>
void dispatch(CompletionHandler handler)
{
io_service->dispatch(handler);
}
};
} // namespace crow
crow::query_string
A class to represent any data coming after the ? in the request URL into key-value pairs.
Definition: query_string.h:294
crow
The main namespace of the library. In this namespace is defined the most important classes and functi...
crow::get_header_value
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
crow::request
An HTTP request.
Definition: http_request.h:36
crow::request::request
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
crow::request::post
void post(CompletionHandler handler)
Send data to whoever made this request with a completion handler and return immediately.
Definition: http_request.h:89
crow::request::close_connection
bool close_connection
Whether or not the server should shut down the TCP connection once a response is sent.
Definition: http_request.h:46
crow::request::keep_alive
bool keep_alive
Whether or not the server should send a connection: Keep-Alive header to the client.
Definition: http_request.h:45
crow::request::raw_url
std::string raw_url
The full URL containing the ? and URL parameters.
Definition: http_request.h:38
crow::request::url_params
query_string url_params
The parameters associated with the request. (everything after the ? in the URL)
Definition: http_request.h:40
crow::request::request
request()
Construct an empty request. (sets the method to GET)
Definition: http_request.h:54
crow::request::url
std::string url
The endpoint without any parameters.
Definition: http_request.h:39
crow::request::remote_ip_address
std::string remote_ip_address
The IP address from which the request was sent.
Definition: http_request.h:43
crow::request::dispatch
void dispatch(CompletionHandler handler)
Send data to whoever made this request with a completion handler.
Definition: http_request.h:96
crow::request::upgrade
bool upgrade
Whether or noth the server should change the HTTP connection to a different connection.
Definition: http_request.h:47
crow::request::get_body_params
const query_string get_body_params() const
Get the body as parameters in QS format.
Definition: http_request.h:82