2014-04-01 13:58:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-28 05:46:31 +00:00
|
|
|
#include <boost/asio.hpp>
|
2014-04-14 15:31:51 +00:00
|
|
|
|
2016-09-21 14:11:06 +00:00
|
|
|
#include "crow/common.h"
|
|
|
|
#include "crow/ci_map.h"
|
|
|
|
#include "crow/query_string.h"
|
|
|
|
|
2014-04-26 17:19:59 +00:00
|
|
|
namespace crow
|
2014-04-01 13:58:52 +00:00
|
|
|
{
|
2020-11-14 02:28:07 +00:00
|
|
|
/// Find and return the value associated with the key. (returns an empty string if nothing is found)
|
2021-11-25 11:45:38 +00:00
|
|
|
template<typename T>
|
2014-09-10 21:32:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-25 11:45:38 +00:00
|
|
|
struct DetachHelper;
|
2016-08-28 05:46:31 +00:00
|
|
|
|
2020-11-14 02:28:07 +00:00
|
|
|
/// An HTTP request.
|
2014-04-01 13:58:52 +00:00
|
|
|
struct request
|
|
|
|
{
|
2014-04-20 08:45:10 +00:00
|
|
|
HTTPMethod method;
|
2021-11-25 11:45:38 +00:00
|
|
|
std::string raw_url; ///< The full URL containing the `?` and URL parameters.
|
|
|
|
std::string url; ///< The endpoint without any parameters.
|
2020-11-18 22:13:57 +00:00
|
|
|
query_string url_params; ///< The parameters associated with the request. (everything after the `?`)
|
2014-09-10 21:32:41 +00:00
|
|
|
ci_map headers;
|
2014-04-01 13:58:52 +00:00
|
|
|
std::string body;
|
2021-08-14 12:31:19 +00:00
|
|
|
std::string remote_ip_address; ///< The IP address from which the request was sent.
|
2014-09-06 16:24:45 +00:00
|
|
|
|
2014-09-12 03:14:54 +00:00
|
|
|
void* middleware_context{};
|
2016-08-28 05:46:31 +00:00
|
|
|
boost::asio::io_service* io_service{};
|
2014-09-12 03:14:54 +00:00
|
|
|
|
2020-11-14 02:28:07 +00:00
|
|
|
/// Construct an empty request. (sets the method to `GET`)
|
2021-11-25 11:45:38 +00:00
|
|
|
request():
|
|
|
|
method(HTTPMethod::Get)
|
2021-11-27 12:28:50 +00:00
|
|
|
{}
|
2014-09-12 03:14:54 +00:00
|
|
|
|
2020-11-14 02:28:07 +00:00
|
|
|
/// Construct a request with all values assigned.
|
2021-11-25 11:45:38 +00:00
|
|
|
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))
|
2021-11-27 12:28:50 +00:00
|
|
|
{}
|
2014-09-12 03:14:54 +00:00
|
|
|
|
2014-09-10 21:32:41 +00:00
|
|
|
void add_header(std::string key, std::string value)
|
|
|
|
{
|
|
|
|
headers.emplace(std::move(key), std::move(value));
|
|
|
|
}
|
|
|
|
|
2015-01-19 09:58:19 +00:00
|
|
|
const std::string& get_header_value(const std::string& key) const
|
2014-09-10 21:32:41 +00:00
|
|
|
{
|
|
|
|
return crow::get_header_value(headers, key);
|
|
|
|
}
|
|
|
|
|
2020-11-14 02:28:07 +00:00
|
|
|
/// Send the request with a completion handler and return immediately.
|
2016-08-28 05:46:31 +00:00
|
|
|
template<typename CompletionHandler>
|
|
|
|
void post(CompletionHandler handler)
|
|
|
|
{
|
|
|
|
io_service->post(handler);
|
|
|
|
}
|
|
|
|
|
2020-11-14 02:28:07 +00:00
|
|
|
/// Send the request with a completion handler.
|
2016-08-28 05:46:31 +00:00
|
|
|
template<typename CompletionHandler>
|
|
|
|
void dispatch(CompletionHandler handler)
|
|
|
|
{
|
|
|
|
io_service->dispatch(handler);
|
|
|
|
}
|
2014-04-01 13:58:52 +00:00
|
|
|
};
|
2021-11-25 11:45:38 +00:00
|
|
|
} // namespace crow
|