# Crow: /home/farook/cppsrv/crow-0.3-fordoc/include/crow/http_request.h Source File

http_request.h
#pragma once
#include <boost/asio.hpp>
#include "crow/common.h"
#include "crow/ci_map.h"
#include "crow/query_string.h"
namespace crow
{
/// 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;
}
struct DetachHelper;
/// 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 `?`)
ci_map headers;
std::string body;
std::string remoteIpAddress; ///< The IP address from which the request was sent.
void* middleware_context{};
boost::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)
: 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))
{
}
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);
}
/// Send the request with a completion handler and return immediately.
template<typename CompletionHandler>
void post(CompletionHandler handler)
{
io_service->post(handler);
}
/// Send the request with a completion handler.
template<typename CompletionHandler>
void dispatch(CompletionHandler handler)
{
io_service->dispatch(handler);
}
};
}
crow::query_string
A class to represent any data coming after the ? in the request URL into key-value pairs.
Definition: query_string.h:292
crow::request
An HTTP request.
Definition: http_request.h:27
crow::request::post
void post(CompletionHandler handler)
Send the request with a completion handler and return immediately.
Definition: http_request.h:63
crow::request::request
request(HTTPMethod method, std::string raw_url, std::string url, query_string url_params, ci_map headers, std::string body)
Construct a request with all values assigned.
Definition: http_request.h:46
crow::request::raw_url
std::string raw_url
The full URL containing the ? and URL parameters.
Definition: http_request.h:29
crow::request::url_params
query_string url_params
The parameters associated with the request. (everything after the ?)
Definition: http_request.h:31
crow::request::request
request()
Construct an empty request. (sets the method to GET)
Definition: http_request.h:40
crow::request::url
std::string url
The endpoint without any parameters.
Definition: http_request.h:30
crow::request::dispatch
void dispatch(CompletionHandler handler)
Send the request with a completion handler.
Definition: http_request.h:70
crow::request::remoteIpAddress
std::string remoteIpAddress
The IP address from which the request was sent.
Definition: http_request.h:34