4 #include <unordered_map>
7 #include "crow/http_request.h"
8 #include "crow/http_parser_merged.h"
16 template<
typename Handler>
26 self->
req.method =
static_cast<HTTPMethod
>(
self->method);
30 static int on_url(
http_parser* self_,
const char* at,
size_t length)
33 self->
req.
raw_url.insert(self->req.raw_url.end(), at, at + length);
35 self->req.url =
self->req.raw_url.substr(0, self->qs_point != 0 ? self->qs_point : std::string::npos);
41 static int on_header_field(
http_parser* self_,
const char* at,
size_t length)
44 switch (self->header_building_state)
47 if (!self->header_value.empty())
49 self->
req.headers.emplace(std::move(self->header_field), std::move(self->header_value));
51 self->header_field.assign(at, at + length);
52 self->header_building_state = 1;
55 self->header_field.insert(self->header_field.end(), at, at + length);
60 static int on_header_value(
http_parser* self_,
const char* at,
size_t length)
63 switch (self->header_building_state)
66 self->header_value.insert(self->header_value.end(), at, at + length);
69 self->header_building_state = 0;
70 self->header_value.assign(at, at + length);
78 if (!self->header_field.empty())
80 self->
req.headers.emplace(std::move(self->header_field), std::move(self->header_value));
83 self->set_connection_parameters();
85 self->process_header();
88 static int on_body(
http_parser* self_,
const char* at,
size_t length)
91 self->
req.body.insert(self->req.body.end(), at, at + length);
98 self->message_complete =
true;
99 self->process_message();
106 http_parser_init(
this);
111 bool feed(
const char* buffer,
int length)
113 if (message_complete)
127 int nparsed = http_parser_execute(
this, &settings_, buffer, length);
128 if (http_errno != CHPE_OK)
132 return nparsed == length;
137 return feed(
nullptr, 0);
143 header_field.clear();
144 header_value.clear();
145 header_building_state = 0;
147 message_complete =
false;
148 state = CROW_NEW_MESSAGE();
151 inline void process_url()
153 handler_->handle_url();
156 inline void process_header()
158 handler_->handle_header();
161 inline void process_message()
166 inline void set_connection_parameters()
169 req.http_ver_minor = http_minor;
175 ((
flags & F_CONNECTION_KEEP_ALIVE) ? true :
false) :
176 ((
http_major == 1 && http_minor == 1) ? true :
false);
180 ((
flags & F_CONNECTION_KEEP_ALIVE) ? false :
true) :
181 ((
http_major == 1 && http_minor == 1) ? ((
flags & F_CONNECTION_CLOSE) ?
true :
false) :
false);
191 int header_building_state = 0;
192 bool message_complete =
false;
193 std::string header_field;
194 std::string header_value;
200 #undef CROW_NEW_MESSAGE
201 #undef CROW_start_state
A class to represent any data coming after the ? in the request URL into key-value pairs.
Definition: query_string.h:294
The main namespace of the library. In this namespace is defined the most important classes and functi...
A wrapper for nodejs/http-parser.
Definition: parser.h:18
request req
Definition: parser.h:188
bool feed(const char *buffer, int length)
Parse a buffer into the different sections of an HTTP request.
Definition: parser.h:111
Definition: http_parser_merged.h:181
Definition: http_parser_merged.h:148
unsigned char http_major
Definition: http_parser_merged.h:163
unsigned int flags
Definition: http_parser_merged.h:150
An HTTP request.
Definition: http_request.h:36
bool close_connection
Whether or not the server should shut down the TCP connection once a response is sent.
Definition: http_request.h:46
bool keep_alive
Whether or not the server should send a connection: Keep-Alive header to the client.
Definition: http_request.h:45
std::string raw_url
The full URL containing the ? and URL parameters.
Definition: http_request.h:38
bool upgrade
Whether or noth the server should change the HTTP connection to a different connection.
Definition: http_request.h:47