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();
105 http_parser_init(
this);
110 bool feed(
const char* buffer,
int length)
112 if (message_complete)
126 int nparsed = http_parser_execute(
this, &settings_, buffer, length);
127 if (http_errno != CHPE_OK)
131 return nparsed == length;
136 return feed(
nullptr, 0);
142 header_field.clear();
143 header_value.clear();
144 header_building_state = 0;
146 message_complete =
false;
147 state = CROW_NEW_MESSAGE();
150 inline void process_url()
152 handler_->handle_url();
155 inline void process_header()
157 handler_->handle_header();
160 inline void process_message()
165 inline void set_connection_parameters()
168 req.http_ver_minor = http_minor;
174 ((
flags & F_CONNECTION_KEEP_ALIVE) ? true :
false) :
175 ((
http_major == 1 && http_minor == 1) ? true :
false);
179 ((
flags & F_CONNECTION_KEEP_ALIVE) ? false :
true) :
180 ((
http_major == 1 && http_minor == 1) ? ((
flags & F_CONNECTION_CLOSE) ?
true :
false) :
false);
190 int header_building_state = 0;
191 bool message_complete =
false;
192 std::string header_field;
193 std::string header_value;
199 #undef CROW_NEW_MESSAGE
200 #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:187
bool feed(const char *buffer, int length)
Parse a buffer into the different sections of an HTTP request.
Definition: parser.h:110
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