Skip to content
This repository was archived by the owner on Jun 12, 2018. It is now read-only.

Commit 0b397bc

Browse files
committed
Added heavy work example
1 parent 3c3359f commit 0b397bc

4 files changed

Lines changed: 24 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required (VERSION 2.8.8)
22
project (Simple-Web-Server)
3-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O3 -Wall -Wextra")
3+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra")
44

55
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
66
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)

http_examples.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ int main() {
8787
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
8888
};
8989

90+
//Get example simulating heavy work in a separate thread
91+
server.resource["^/work$"]["GET"]=[&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) {
92+
*response << "HTTP/1.1 200 OK\r\n";
93+
thread work_thread([response] {
94+
this_thread::sleep_for(chrono::seconds(5));
95+
string message="Work done";
96+
*response << "Content-Length: " << message.length() << "\r\n\r\n" << message;
97+
});
98+
work_thread.detach();
99+
};
100+
90101
//Default GET-example. If no other matches, this anonymous function will be called.
91102
//Will respond with content in the web/-directory, and its subdirectories.
92103
//Default file: index.html

https_examples.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ int main() {
8787
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
8888
};
8989

90+
//Get example simulating heavy work in a separate thread
91+
server.resource["^/work$"]["GET"]=[&server](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> /*request*/) {
92+
*response << "HTTP/1.1 200 OK\r\n";
93+
thread work_thread([response] {
94+
this_thread::sleep_for(chrono::seconds(5));
95+
string message="Work done";
96+
*response << "Content-Length: " << message.length() << "\r\n\r\n" << message;
97+
});
98+
work_thread.detach();
99+
};
100+
90101
//Default GET-example. If no other matches, this anonymous function will be called.
91102
//Will respond with content in the web/-directory, and its subdirectories.
92103
//Default file: index.html

server_http.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ namespace SimpleWeb {
181181
io_service.stop();
182182
}
183183

184+
///Use this function if you need to recursively send parts of a longer message
184185
void send(std::shared_ptr<Response> response, const std::function<void(const boost::system::error_code&)>& callback=nullptr) const {
185186
boost::asio::async_write(*response->socket, response->streambuf, [this, response, callback](const boost::system::error_code& ec, size_t /*bytes_transferred*/) {
186187
if(callback)

0 commit comments

Comments
 (0)