4#include <unordered_map>
5#include <boost/algorithm/string.hpp>
8#include "crow/http_parser_merged.h"
9#include "crow/http_request.h"
17 template <
typename Handler>
20 static int on_message_begin(http_parser* self_)
26 static int on_url(http_parser* self_,
const char* at,
size_t length)
29 self->raw_url.insert(self->raw_url.end(), at, at+length);
32 static int on_header_field(http_parser* self_,
const char* at,
size_t length)
35 switch (self->header_building_state)
38 if (!self->header_value.empty())
40 self->headers.emplace(std::move(self->header_field), std::move(self->header_value));
42 self->header_field.assign(at, at+length);
43 self->header_building_state = 1;
46 self->header_field.insert(self->header_field.end(), at, at+length);
51 static int on_header_value(http_parser* self_,
const char* at,
size_t length)
54 switch (self->header_building_state)
57 self->header_value.insert(self->header_value.end(), at, at+length);
60 self->header_building_state = 0;
61 self->header_value.assign(at, at+length);
66 static int on_headers_complete(http_parser* self_)
69 if (!self->header_field.empty())
71 self->headers.emplace(std::move(self->header_field), std::move(self->header_value));
73 self->process_header();
76 static int on_body(http_parser* self_,
const char* at,
size_t length)
79 self->body.insert(self->body.end(), at, at+length);
82 static int on_message_complete(http_parser* self_)
87 self->url = self->raw_url.substr(0, self->raw_url.find(
"?"));
90 self->process_message();
96 http_parser_init(
this, HTTP_REQUEST);
101 bool feed(
const char* buffer,
int length)
103 const static http_parser_settings settings_{
114 int nparsed = http_parser_execute(
this, &settings_, buffer, length);
115 return nparsed == length;
120 return feed(
nullptr, 0);
127 header_building_state = 0;
128 header_field.clear();
129 header_value.clear();
135 void process_header()
140 void process_message()
148 return request{
static_cast<HTTPMethod
>(method), std::move(raw_url), std::move(url), std::move(
url_params), std::move(headers), std::move(body)};
151 bool is_upgrade()
const
156 bool check_version(
int major,
int minor)
const
158 return http_major == major && http_minor == minor;
164 int header_building_state = 0;
165 std::string header_field;
166 std::string header_value;
A class to represent any data coming after the ? in the request URL into key-value pairs.
Definition: query_string.h:292
A wrapper for nodejs/http-parser.
Definition: parser.h:19
request to_request() const
Take the parsed HTTP request data and convert it to a crow::request.
Definition: parser.h:146
Handler * handler_
This is currently an HTTP connection object (crow::Connection).
Definition: parser.h:171
bool feed(const char *buffer, int length)
Parse a buffer into the different sections of an HTTP request.
Definition: parser.h:101
query_string url_params
What comes after the ? in the URL.
Definition: parser.h:168
An HTTP request.
Definition: http_request.h:27