From dcbb45175aba181f5f54965a2552e91f37edde52 Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 23 Jun 2017 08:05:48 +0200 Subject: [PATCH] Added convenience functions to examples --- http_examples.cpp | 43 ++++++++++++++++++++++++++++++++++--------- https_examples.cpp | 43 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/http_examples.cpp b/http_examples.cpp index 7aaa324..31e3242 100644 --- a/http_examples.cpp +++ b/http_examples.cpp @@ -70,25 +70,46 @@ int main() { << "Content-Length: " << name.length() << "\r\n\r\n" << name; } - catch(exception& e) { + catch(const exception &e) { *response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what(); } + + + // Alternatively, using a convenience function: + // try { + // ptree pt; + // read_json(request->content, pt); + + // string name=pt.get("firstName")+" "+pt.get("lastName"); + // response->write(name, {{"Content-Type", "application/json"}}); + // } + // catch(const exception &e) { + // response->write(SimpleWeb::StatusCode::client_error_bad_request, e.what()); + // } }; //GET-example for the path /info //Responds with request-information server.resource["^/info$"]["GET"]=[](shared_ptr response, shared_ptr request) { - stringstream content_stream; - content_stream << "

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

"; - content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; - for(auto& header: request->header) { - content_stream << header.first << ": " << header.second << "
"; - } + 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 << "
"; //find length of content_stream (length received using content_stream.tellp()) - content_stream.seekp(0, ios::end); + stream.seekp(0, ios::end); - *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content_stream.tellp() << "\r\n\r\n" << content_stream.rdbuf(); + *response << "HTTP/1.1 200 OK\r\nContent-Length: " << stream.tellp() << "\r\n\r\n" << stream.rdbuf(); + + + // 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); }; //GET-example for the path /match/[number], responds with the matched string in path (number) @@ -96,6 +117,10 @@ int main() { 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]); }; //Get example simulating heavy work in a separate thread diff --git a/https_examples.cpp b/https_examples.cpp index 4d7dd8d..a74496c 100644 --- a/https_examples.cpp +++ b/https_examples.cpp @@ -68,25 +68,46 @@ int main() { << "Content-Length: " << name.length() << "\r\n\r\n" << name; } - catch(exception& e) { + catch(const exception &e) { *response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what(); } + + + // Alternatively, using a convenience function: + // try { + // ptree pt; + // read_json(request->content, pt); + + // string name=pt.get("firstName")+" "+pt.get("lastName"); + // response->write(name, {{"Content-Type", "application/json"}}); + // } + // catch(const exception &e) { + // response->write(SimpleWeb::StatusCode::client_error_bad_request, e.what()); + // } }; //GET-example for the path /info //Responds with request-information server.resource["^/info$"]["GET"]=[](shared_ptr response, shared_ptr request) { - stringstream content_stream; - content_stream << "

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

"; - content_stream << request->method << " " << request->path << " HTTP/" << request->http_version << "
"; - for(auto& header: request->header) { - content_stream << header.first << ": " << header.second << "
"; - } + 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 << "
"; //find length of content_stream (length received using content_stream.tellp()) - content_stream.seekp(0, ios::end); + stream.seekp(0, ios::end); - *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content_stream.tellp() << "\r\n\r\n" << content_stream.rdbuf(); + *response << "HTTP/1.1 200 OK\r\nContent-Length: " << stream.tellp() << "\r\n\r\n" << stream.rdbuf(); + + + // 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); }; //GET-example for the path /match/[number], responds with the matched string in path (number) @@ -94,6 +115,10 @@ int main() { 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]); }; //Get example simulating heavy work in a separate thread