aboutsummaryrefslogtreecommitdiffstats
path: root/include/parser.h
diff options
context:
space:
mode:
authorAntony Woods <acron1@gmail.com>2014-10-14 09:48:35 +0100
committerAntony Woods <acron1@gmail.com>2014-10-14 09:48:35 +0100
commit06842721d7da53a2235e6e4071760588ec285f90 (patch)
treec72f69e9fe4154be039ba89e474395e21b6d5fdf /include/parser.h
parenta8f6c5a92fea5439b4a0372ccc3b367e3c3bed3b (diff)
downloadcrow-06842721d7da53a2235e6e4071760588ec285f90.tar.gz
crow-06842721d7da53a2235e6e4071760588ec285f90.zip
Wrapped qs_parse as query_string and added tests
Diffstat (limited to 'include/parser.h')
-rw-r--r--include/parser.h73
1 files changed, 10 insertions, 63 deletions
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 <typename Handler>
struct HTTPParser : public http_parser
{
- template<const char delimiter>
- struct tokenize_by_char
- {
- template<typename It>
- 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<tokenize_by_char<url_delimiter>>(params)) {
- std::string key, value;
- auto parts = boost::tokenizer<tokenize_by_char<param_delimiter>>(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<HTTPParser*>(self_);
@@ -78,11 +22,7 @@ namespace crow
static int on_url(http_parser* self_, const char* at, size_t length)
{
HTTPParser* self = static_cast<HTTPParser*>(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<HTTPParser*>(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_;