From 0b397bc6975418de6e9da7ab87189271dd10516b Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 29 Jun 2016 10:10:34 +0200 Subject: [PATCH] Added heavy work example --- CMakeLists.txt | 2 +- http_examples.cpp | 11 +++++++++++ https_examples.cpp | 11 +++++++++++ server_http.hpp | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a38fb18..b65fb72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 2.8.8) project (Simple-Web-Server) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3 -Wall -Wextra") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra") if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8) diff --git a/http_examples.cpp b/http_examples.cpp index 259e1c5..c9c3683 100644 --- a/http_examples.cpp +++ b/http_examples.cpp @@ -87,6 +87,17 @@ int main() { *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number; }; + //Get example simulating heavy work in a separate thread + server.resource["^/work$"]["GET"]=[&server](shared_ptr response, shared_ptr /*request*/) { + *response << "HTTP/1.1 200 OK\r\n"; + thread work_thread([response] { + this_thread::sleep_for(chrono::seconds(5)); + string message="Work done"; + *response << "Content-Length: " << message.length() << "\r\n\r\n" << message; + }); + work_thread.detach(); + }; + //Default GET-example. If no other matches, this anonymous function will be called. //Will respond with content in the web/-directory, and its subdirectories. //Default file: index.html diff --git a/https_examples.cpp b/https_examples.cpp index 7c2af86..016a92e 100644 --- a/https_examples.cpp +++ b/https_examples.cpp @@ -87,6 +87,17 @@ int main() { *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number; }; + //Get example simulating heavy work in a separate thread + server.resource["^/work$"]["GET"]=[&server](shared_ptr response, shared_ptr /*request*/) { + *response << "HTTP/1.1 200 OK\r\n"; + thread work_thread([response] { + this_thread::sleep_for(chrono::seconds(5)); + string message="Work done"; + *response << "Content-Length: " << message.length() << "\r\n\r\n" << message; + }); + work_thread.detach(); + }; + //Default GET-example. If no other matches, this anonymous function will be called. //Will respond with content in the web/-directory, and its subdirectories. //Default file: index.html diff --git a/server_http.hpp b/server_http.hpp index 18dc0ee..2ffde4b 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -181,6 +181,7 @@ namespace SimpleWeb { io_service.stop(); } + ///Use this function if you need to recursively send parts of a longer message void send(std::shared_ptr response, const std::function& callback=nullptr) const { boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) { if(callback)