mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
URL params are now present as a ci_map variable of request
This commit is contained in:
parent
c9e8bf3c64
commit
3b4bf01a7d
@ -68,8 +68,20 @@ int main()
|
|||||||
return crow::response{os.str()};
|
return crow::response{os.str()};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
CROW_ROUTE(app, "/params")
|
||||||
|
([](const crow::request& req){
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Params:\n";
|
||||||
|
for (auto& i : req.url_params) {
|
||||||
|
os << "key = " << i.first << ", val = " << i.second << '\n';
|
||||||
|
}
|
||||||
|
return crow::response{os.str()};
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ignore all log
|
// ignore all log
|
||||||
crow::logger::setLogLevel(crow::LogLevel::CRITICAL);
|
crow::logger::setLogLevel(crow::LogLevel::DEBUG);
|
||||||
//crow::logger::setHandler(std::make_shared<ExampleLogHandler>());
|
//crow::logger::setHandler(std::make_shared<ExampleLogHandler>());
|
||||||
|
|
||||||
app.port(18080)
|
app.port(18080)
|
||||||
|
@ -20,6 +20,7 @@ namespace crow
|
|||||||
{
|
{
|
||||||
HTTPMethod method;
|
HTTPMethod method;
|
||||||
std::string url;
|
std::string url;
|
||||||
|
ci_map url_params;
|
||||||
ci_map headers;
|
ci_map headers;
|
||||||
std::string body;
|
std::string body;
|
||||||
|
|
||||||
@ -30,8 +31,8 @@ namespace crow
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
request(HTTPMethod method, std::string url, ci_map headers, std::string body)
|
request(HTTPMethod method, std::string url, ci_map url_params, ci_map headers, std::string body)
|
||||||
: method(method), url(std::move(url)), headers(std::move(headers)), body(std::move(body))
|
: method(method), url(std::move(url)), url_params(std::move(url_params)), headers(std::move(headers)), body(std::move(body))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <boost/algorithm/string.hpp>
|
#include <boost/algorithm/string.hpp>
|
||||||
|
#include <boost/tokenizer.hpp>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include "http_request.h"
|
#include "http_request.h"
|
||||||
|
|
||||||
@ -11,6 +13,62 @@ namespace crow
|
|||||||
template <typename Handler>
|
template <typename Handler>
|
||||||
struct HTTPParser : public http_parser
|
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;
|
||||||
|
|
||||||
|
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_)
|
static int on_message_begin(http_parser* self_)
|
||||||
{
|
{
|
||||||
HTTPParser* self = static_cast<HTTPParser*>(self_);
|
HTTPParser* self = static_cast<HTTPParser*>(self_);
|
||||||
@ -21,6 +79,10 @@ namespace crow
|
|||||||
{
|
{
|
||||||
HTTPParser* self = static_cast<HTTPParser*>(self_);
|
HTTPParser* self = static_cast<HTTPParser*>(self_);
|
||||||
self->url.insert(self->url.end(), at, at+length);
|
self->url.insert(self->url.end(), at, at+length);
|
||||||
|
|
||||||
|
// url params
|
||||||
|
self->url_params = get_url_params(self->url);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
static int on_header_field(http_parser* self_, const char* at, size_t length)
|
static int on_header_field(http_parser* self_, const char* at, size_t length)
|
||||||
@ -115,6 +177,7 @@ namespace crow
|
|||||||
header_field.clear();
|
header_field.clear();
|
||||||
header_value.clear();
|
header_value.clear();
|
||||||
headers.clear();
|
headers.clear();
|
||||||
|
url_params.clear();
|
||||||
body.clear();
|
body.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,7 +193,7 @@ namespace crow
|
|||||||
|
|
||||||
request to_request() const
|
request to_request() const
|
||||||
{
|
{
|
||||||
return request{(HTTPMethod)method, std::move(url), std::move(headers), std::move(body)};
|
return request{(HTTPMethod)method, std::move(url), std::move(url_params), std::move(headers), std::move(body)};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool check_version(int major, int minor) const
|
bool check_version(int major, int minor) const
|
||||||
@ -139,10 +202,12 @@ namespace crow
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string url;
|
std::string url;
|
||||||
|
|
||||||
int header_building_state = 0;
|
int header_building_state = 0;
|
||||||
std::string header_field;
|
std::string header_field;
|
||||||
std::string header_value;
|
std::string header_value;
|
||||||
ci_map headers;
|
ci_map headers;
|
||||||
|
ci_map url_params;
|
||||||
std::string body;
|
std::string body;
|
||||||
|
|
||||||
Handler* handler_;
|
Handler* handler_;
|
||||||
|
Loading…
Reference in New Issue
Block a user