From 4b8c67e2300205200f4f846400d73a03cb3da854 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Tue, 1 Apr 2014 01:51:50 +0900 Subject: accept connections, print http request parsed information --- parser.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 parser.h (limited to 'parser.h') diff --git a/parser.h b/parser.h new file mode 100644 index 0000000..2f2626d --- /dev/null +++ b/parser.h @@ -0,0 +1,79 @@ +#include +#include +namespace flask +{ + struct HTTPParser : public http_parser + { + static int on_message_begin(http_parser* self_) + { + HTTPParser* self = static_cast(self_); + return 0; + } + static int on_url(http_parser* self_, const char* at, size_t length) + { + HTTPParser* self = static_cast(self_); + std::cout << std::string(at, at+length) << std::endl; + return 0; + } + static int on_status(http_parser* self_, const char* at, size_t length) + { + HTTPParser* self = static_cast(self_); + std::cout << std::string(at, at+length) << std::endl; + return 0; + } + static int on_header_field(http_parser* self_, const char* at, size_t length) + { + HTTPParser* self = static_cast(self_); + std::cout << std::string(at, at+length) << std::endl; + return 0; + } + static int on_header_value(http_parser* self_, const char* at, size_t length) + { + HTTPParser* self = static_cast(self_); + std::cout << std::string(at, at+length) << std::endl; + return 0; + } + static int on_headers_complete(http_parser* self_) + { + HTTPParser* self = static_cast(self_); + return 0; + } + static int on_body(http_parser* self_, const char* at, size_t length) + { + HTTPParser* self = static_cast(self_); + std::cout << std::string(at, at+length) << std::endl; + return 0; + } + static int on_message_complete(http_parser* self_) + { + HTTPParser* self = static_cast(self_); + return 0; + } + HTTPParser() : + settings_ { + on_message_begin, + on_url, + on_status, + on_header_field, + on_header_value, + on_headers_complete, + on_body, + on_message_complete, + } + { + http_parser_init(this, HTTP_REQUEST); + } + + void feed(const char* buffer, int length) + { + int nparsed = http_parser_execute(this, &settings_, buffer, length); + } + + void done() + { + int nparsed = http_parser_execute(this, &settings_, nullptr, 0); + } + + http_parser_settings settings_; + }; +} -- cgit v1.2.3-54-g00ecf