From b9d4be229f41e3eda72394451d479f5245d2a600 Mon Sep 17 00:00:00 2001 From: vitor-alves Date: Sat, 28 Oct 2017 16:11:56 -0200 Subject: [PATCH] Added a parse_query_string() example --- http_examples.cpp | 35 ++++++++++++----------------------- https_examples.cpp | 35 ++++++++++++----------------------- 2 files changed, 24 insertions(+), 46 deletions(-) diff --git a/http_examples.cpp b/http_examples.cpp index ed05bfb..a8a1047 100644 --- a/http_examples.cpp +++ b/http_examples.cpp @@ -89,40 +89,29 @@ int main() { // Responds with request-information server.resource["^/info$"]["GET"] = [](shared_ptr response, shared_ptr request) { stringstream stream; - stream << "

Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")

"; - stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; - for(auto &header : request->header) - stream << header.first << ": " << header.second << "
"; + stream << "

Request from " << request->remote_endpoint_address << ":" << request->remote_endpoint_port << "

"; - // Find length of content_stream (length received using content_stream.tellp()) - stream.seekp(0, ios::end); + stream << request->method << " " << request->path << " HTTP/" << request->http_version; - *response << "HTTP/1.1 200 OK\r\nContent-Length: " << stream.tellp() << "\r\n\r\n" - << stream.rdbuf(); + stream << "

Query Fields

"; + auto query_fields = request->parse_query_string(); + for(auto &field : query_fields) + stream << field.first << ": " << field.second << "
"; + stream << "

Header Fields

"; + for(auto &field : request->header) + stream << field.first << ": " << field.second << "
"; - // Alternatively, using a convenience function: - // stringstream stream; - // stream << "

Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")

"; - // stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; - // for(auto &header: request->header) - // stream << header.first << ": " << header.second << "
"; - // response->write(stream); + response->write(stream); }; // GET-example for the path /match/[number], responds with the matched string in path (number) // For instance a request GET /match/123 will receive: 123 server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr response, shared_ptr request) { - string number = request->path_match[1]; - *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" - << number; - - - // Alternatively, using a convenience function: - // response->write(request->path_match[1]); + response->write(request->path_match[1]); }; - // Get example simulating heavy work in a separate thread + // GET-example simulating heavy work in a separate thread server.resource["^/work$"]["GET"] = [](shared_ptr response, shared_ptr /*request*/) { thread work_thread([response] { this_thread::sleep_for(chrono::seconds(5)); diff --git a/https_examples.cpp b/https_examples.cpp index d86ee7f..7d6e987 100644 --- a/https_examples.cpp +++ b/https_examples.cpp @@ -87,40 +87,29 @@ int main() { // Responds with request-information server.resource["^/info$"]["GET"] = [](shared_ptr response, shared_ptr request) { stringstream stream; - stream << "

Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")

"; - stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; - for(auto &header : request->header) - stream << header.first << ": " << header.second << "
"; + stream << "

Request from " << request->remote_endpoint_address << ":" << request->remote_endpoint_port << "

"; - // Find length of content_stream (length received using content_stream.tellp()) - stream.seekp(0, ios::end); + stream << request->method << " " << request->path << " HTTP/" << request->http_version; - *response << "HTTP/1.1 200 OK\r\nContent-Length: " << stream.tellp() << "\r\n\r\n" - << stream.rdbuf(); + stream << "

Query Fields

"; + auto query_fields = request->parse_query_string(); + for(auto &field : query_fields) + stream << field.first << ": " << field.second << "
"; + stream << "

Header Fields

"; + for(auto &field : request->header) + stream << field.first << ": " << field.second << "
"; - // Alternatively, using a convenience function: - // stringstream stream; - // stream << "

Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")

"; - // stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; - // for(auto &header: request->header) - // stream << header.first << ": " << header.second << "
"; - // response->write(stream); + response->write(stream); }; // GET-example for the path /match/[number], responds with the matched string in path (number) // For instance a request GET /match/123 will receive: 123 server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr response, shared_ptr request) { - string number = request->path_match[1]; - *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" - << number; - - - // Alternatively, using a convenience function: - // response->write(request->path_match[1]); + response->write(request->path_match[1]); }; - // Get example simulating heavy work in a separate thread + // GET-example simulating heavy work in a separate thread server.resource["^/work$"]["GET"] = [](shared_ptr response, shared_ptr /*request*/) { thread work_thread([response] { this_thread::sleep_for(chrono::seconds(5));