From 06842721d7da53a2235e6e4071760588ec285f90 Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Tue, 14 Oct 2014 09:48:35 +0100 Subject: Wrapped qs_parse as query_string and added tests --- include/parser.h | 73 ++++++++------------------------------------------------ 1 file changed, 10 insertions(+), 63 deletions(-) (limited to 'include/parser.h') diff --git a/include/parser.h b/include/parser.h index 00bb155..6ead8fd 100644 --- a/include/parser.h +++ b/include/parser.h @@ -13,62 +13,6 @@ namespace crow template struct HTTPParser : public http_parser { - template - struct tokenize_by_char - { - template - bool operator()(It& next, It end, std::string & tok) - { - if (next == end) - return false; - const char dels = delimiter; - const char* del = &dels; - auto pos = std::search(next, end, del, del + 1); - tok.assign(next, pos); - next = pos; - if (next != end) - std::advance(next, 1); - return true; - } - - void reset() {} - }; - - static ci_map get_url_params(std::string url) - { - const char url_delimiter = '&'; - const char param_delimiter = '='; - ci_map ret; - - unsigned int qMarkPos = url.find("?"); - if(!(qMarkPos >=0 && qMarkPos != (url.length()-1))) { - return ret; - } - - auto params = url.substr(qMarkPos+1); - - // substitute ';' for '&' for recommended process of delimintation - // (http://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.2.2) - std::replace(params.begin(), params.end(), ';', url_delimiter); - - // url tokenizer - for (auto i : boost::tokenizer>(params)) { - std::string key, value; - auto parts = boost::tokenizer>(i); - int count = 0; - for(auto p = parts.begin(); p != parts.end(); ++p, ++count) { - if(count == 0){ - key = *p; - } else { - value = *p; - } - } - ret.insert(std::make_pair(key, value)); - } - - return ret; - } - static int on_message_begin(http_parser* self_) { HTTPParser* self = static_cast(self_); @@ -78,11 +22,7 @@ namespace crow static int on_url(http_parser* self_, const char* at, size_t length) { HTTPParser* self = static_cast(self_); - self->url.insert(self->url.end(), at, at+length); - - // url params - self->url_params = get_url_params(self->url); - + self->raw_url.insert(self->raw_url.end(), at, at+length); return 0; } static int on_header_field(http_parser* self_, const char* at, size_t length) @@ -138,6 +78,11 @@ namespace crow static int on_message_complete(http_parser* self_) { HTTPParser* self = static_cast(self_); + + // url params + self->url = self->raw_url.substr(0, self->raw_url.find("?")); + self->url_params = query_string(self->raw_url); + self->process_message(); return 0; } @@ -173,6 +118,7 @@ namespace crow void clear() { url.clear(); + raw_url.clear(); header_building_state = 0; header_field.clear(); header_value.clear(); @@ -193,7 +139,7 @@ namespace crow request to_request() const { - return request{(HTTPMethod)method, std::move(url), std::move(url_params), std::move(headers), std::move(body)}; + return request{(HTTPMethod)method, std::move(raw_url), std::move(url), std::move(url_params), std::move(headers), std::move(body)}; } bool check_version(int major, int minor) const @@ -201,13 +147,14 @@ namespace crow return http_major == major && http_minor == minor; } + std::string raw_url; std::string url; int header_building_state = 0; std::string header_field; std::string header_value; ci_map headers; - ci_map url_params; + query_string url_params; std::string body; Handler* handler_; -- cgit v1.2.3-54-g00ecf