Callback shared_ptr parameters are now shared_ptr&

This commit is contained in:
eidheim 2017-07-03 13:27:47 +02:00
commit b3a1d48c0a
7 changed files with 104 additions and 98 deletions

View file

@ -136,7 +136,7 @@ namespace SimpleWeb {
std::shared_ptr<Response> request(const std::string &method, const std::string &path = std::string("/"), std::shared_ptr<Response> request(const std::string &method, const std::string &path = std::string("/"),
string_view content = "", const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) { string_view content = "", const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
std::shared_ptr<Response> response; std::shared_ptr<Response> response;
request(method, path, content, header, [&response](std::shared_ptr<Response> response_, const error_code &ec) { request(method, path, content, header, [&response](std::shared_ptr<Response> &response_, const error_code &ec) {
response = response_; response = response_;
if(ec) if(ec)
throw system_error(ec); throw system_error(ec);
@ -153,7 +153,7 @@ namespace SimpleWeb {
std::shared_ptr<Response> request(const std::string &method, const std::string &path, std::istream &content, std::shared_ptr<Response> request(const std::string &method, const std::string &path, std::istream &content,
const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) { const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) {
std::shared_ptr<Response> response; std::shared_ptr<Response> response;
request(method, path, content, header, [&response](std::shared_ptr<Response> response_, const error_code &ec) { request(method, path, content, header, [&response](std::shared_ptr<Response> &response_, const error_code &ec) {
response = response_; response = response_;
if(ec) if(ec)
throw system_error(ec); throw system_error(ec);
@ -167,14 +167,14 @@ namespace SimpleWeb {
/// Asynchronous request where setting and/or running Client's io_service is required. /// Asynchronous request where setting and/or running Client's io_service is required.
void request(const std::string &method, const std::string &path, string_view content, const CaseInsensitiveMultimap &header, void request(const std::string &method, const std::string &path, string_view content, const CaseInsensitiveMultimap &header,
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) { std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback_) {
auto session = std::make_shared<Session>(io_service, get_connection(), create_request_header(method, path, header)); auto session = std::make_shared<Session>(io_service, get_connection(), create_request_header(method, path, header));
auto connection = session->connection; auto connection = session->connection;
auto response = session->response; auto response = session->response;
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response>, const error_code &)>>(std::move(request_callback_)); auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response> &, const error_code &)>>(std::move(request_callback_));
auto connections = this->connections; auto connections = this->connections;
auto connections_mutex = this->connections_mutex; auto connections_mutex = this->connections_mutex;
session->callback = [connection, response, request_callback, connections, connections_mutex](const error_code &ec) { session->callback = [connection, response, request_callback, connections, connections_mutex](const error_code &ec) mutable {
{ {
std::lock_guard<std::mutex> lock(*connections_mutex); std::lock_guard<std::mutex> lock(*connections_mutex);
connection->in_use = false; connection->in_use = false;
@ -209,31 +209,31 @@ namespace SimpleWeb {
/// Asynchronous request where setting and/or running Client's io_service is required. /// Asynchronous request where setting and/or running Client's io_service is required.
void request(const std::string &method, const std::string &path, string_view content, void request(const std::string &method, const std::string &path, string_view content,
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback) { std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback) {
request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback)); request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback));
} }
/// Asynchronous request where setting and/or running Client's io_service is required. /// Asynchronous request where setting and/or running Client's io_service is required.
void request(const std::string &method, const std::string &path, void request(const std::string &method, const std::string &path,
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback) { std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback) {
request(method, path, std::string(), CaseInsensitiveMultimap(), std::move(request_callback)); request(method, path, std::string(), CaseInsensitiveMultimap(), std::move(request_callback));
} }
/// Asynchronous request where setting and/or running Client's io_service is required. /// Asynchronous request where setting and/or running Client's io_service is required.
void request(const std::string &method, std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback) { void request(const std::string &method, std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback) {
request(method, std::string("/"), std::string(), CaseInsensitiveMultimap(), std::move(request_callback)); request(method, std::string("/"), std::string(), CaseInsensitiveMultimap(), std::move(request_callback));
} }
/// Asynchronous request where setting and/or running Client's io_service is required. /// Asynchronous request where setting and/or running Client's io_service is required.
void request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header, void request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header,
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback_) { std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback_) {
auto session = std::make_shared<Session>(io_service, get_connection(), create_request_header(method, path, header)); auto session = std::make_shared<Session>(io_service, get_connection(), create_request_header(method, path, header));
auto connection = session->connection; auto connection = session->connection;
auto response = session->response; auto response = session->response;
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response>, const error_code &)>>(std::move(request_callback_)); auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response> &, const error_code &)>>(std::move(request_callback_));
auto connections = this->connections; auto connections = this->connections;
auto connections_mutex = this->connections_mutex; auto connections_mutex = this->connections_mutex;
session->callback = [connection, response, request_callback, connections, connections_mutex](const error_code &ec) { session->callback = [connection, response, request_callback, connections, connections_mutex](const error_code &ec) mutable {
{ {
std::lock_guard<std::mutex> lock(*connections_mutex); std::lock_guard<std::mutex> lock(*connections_mutex);
connection->in_use = false; connection->in_use = false;
@ -272,7 +272,7 @@ namespace SimpleWeb {
/// Asynchronous request where setting and/or running Client's io_service is required. /// Asynchronous request where setting and/or running Client's io_service is required.
void request(const std::string &method, const std::string &path, std::istream &content, void request(const std::string &method, const std::string &path, std::istream &content,
std::function<void(std::shared_ptr<Response>, const error_code &)> &&request_callback) { std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback) {
request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback)); request(method, path, content, CaseInsensitiveMultimap(), std::move(request_callback));
} }
@ -340,7 +340,7 @@ namespace SimpleWeb {
return parsed_host_port; return parsed_host_port;
} }
static std::shared_ptr<asio::deadline_timer> get_timeout_timer(const std::shared_ptr<Session> &session, size_t timeout = 0) { static std::shared_ptr<asio::deadline_timer> get_timeout_timer(std::shared_ptr<Session> &session, size_t timeout = 0) {
if(timeout == 0) if(timeout == 0)
timeout = session->connection->config.timeout; timeout = session->connection->config.timeout;
if(timeout == 0) if(timeout == 0)
@ -348,14 +348,14 @@ namespace SimpleWeb {
auto timer = std::make_shared<asio::deadline_timer>(*session->io_service); auto timer = std::make_shared<asio::deadline_timer>(*session->io_service);
timer->expires_from_now(boost::posix_time::seconds(timeout)); timer->expires_from_now(boost::posix_time::seconds(timeout));
timer->async_wait([session](const error_code &ec) { timer->async_wait([session](const error_code &ec) mutable {
if(!ec) if(!ec)
close(session); close(session);
}); });
return timer; return timer;
} }
static void parse_response_header(const std::shared_ptr<Response> &response) { static void parse_response_header(std::shared_ptr<Response> &response) {
std::string line; std::string line;
getline(response->content, line); getline(response->content, line);
size_t version_end = line.find(' '); size_t version_end = line.find(' ');
@ -381,9 +381,9 @@ namespace SimpleWeb {
} }
} }
static void write(const std::shared_ptr<Session> &session) { static void write(std::shared_ptr<Session> &session) {
auto timer = get_timeout_timer(session); auto timer = get_timeout_timer(session);
asio::async_write(*session->connection->socket, session->request_buffer->data(), [session, timer](const error_code &ec, size_t /*bytes_transferred*/) { asio::async_write(*session->connection->socket, session->request_buffer->data(), [session, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) if(!ec)
@ -395,9 +395,9 @@ namespace SimpleWeb {
}); });
} }
static void read(const std::shared_ptr<Session> &session) { static void read(std::shared_ptr<Session> &session) {
auto timer = get_timeout_timer(session); auto timer = get_timeout_timer(session);
asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n\r\n", [session, timer](const error_code &ec, size_t bytes_transferred) { asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n\r\n", [session, timer](const error_code &ec, size_t bytes_transferred) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -412,7 +412,7 @@ namespace SimpleWeb {
auto content_length = stoull(header_it->second); auto content_length = stoull(header_it->second);
if(content_length > num_additional_bytes) { if(content_length > num_additional_bytes) {
auto timer = get_timeout_timer(session); auto timer = get_timeout_timer(session);
asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(content_length - num_additional_bytes), [session, timer](const error_code &ec, size_t /*bytes_transferred*/) { asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(content_length - num_additional_bytes), [session, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) if(!ec)
@ -432,7 +432,7 @@ namespace SimpleWeb {
} }
else if(session->response->http_version < "1.1" || ((header_it = session->response->header.find("Session")) != session->response->header.end() && header_it->second == "close")) { else if(session->response->http_version < "1.1" || ((header_it = session->response->header.find("Session")) != session->response->header.end() && header_it->second == "close")) {
auto timer = get_timeout_timer(session); auto timer = get_timeout_timer(session);
asio::async_read(*session->connection->socket, session->response->content_buffer, [session, timer](const error_code &ec, size_t /*bytes_transferred*/) { asio::async_read(*session->connection->socket, session->response->content_buffer, [session, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) if(!ec)
@ -465,9 +465,9 @@ namespace SimpleWeb {
}); });
} }
static void read_chunked(const std::shared_ptr<Session> &session, const std::shared_ptr<asio::streambuf> &tmp_streambuf) { static void read_chunked(std::shared_ptr<Session> &session, std::shared_ptr<asio::streambuf> &tmp_streambuf) {
auto timer = get_timeout_timer(session); auto timer = get_timeout_timer(session);
asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n", [session, tmp_streambuf, timer](const error_code &ec, size_t bytes_transferred) { asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n", [session, tmp_streambuf, timer](const error_code &ec, size_t bytes_transferred) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -479,7 +479,7 @@ namespace SimpleWeb {
auto num_additional_bytes = static_cast<std::streamsize>(session->response->content_buffer.size() - bytes_transferred); auto num_additional_bytes = static_cast<std::streamsize>(session->response->content_buffer.size() - bytes_transferred);
auto post_process = [session, tmp_streambuf, length] { auto post_process = [session, tmp_streambuf, length]() mutable {
std::ostream tmp_stream(tmp_streambuf.get()); std::ostream tmp_stream(tmp_streambuf.get());
if(length > 0) { if(length > 0) {
std::vector<char> buffer(static_cast<size_t>(length)); std::vector<char> buffer(static_cast<size_t>(length));
@ -503,7 +503,7 @@ namespace SimpleWeb {
if((2 + length) > num_additional_bytes) { if((2 + length) > num_additional_bytes) {
auto timer = get_timeout_timer(session); auto timer = get_timeout_timer(session);
asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(2 + length - num_additional_bytes), [session, post_process, timer](const error_code &ec, size_t /*bytes_transferred*/) { asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(2 + length - num_additional_bytes), [session, post_process, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) if(!ec)
@ -524,7 +524,7 @@ namespace SimpleWeb {
}); });
} }
static void close(const std::shared_ptr<Session> &session) { static void close(std::shared_ptr<Session> &session) {
error_code ec; error_code ec;
session->connection->socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec); session->connection->socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
session->connection->socket->lowest_layer().close(ec); session->connection->socket->lowest_layer().close(ec);
@ -548,16 +548,16 @@ namespace SimpleWeb {
return std::make_shared<Connection>(host, port, config, std::unique_ptr<HTTP>(new HTTP(*io_service))); return std::make_shared<Connection>(host, port, config, std::unique_ptr<HTTP>(new HTTP(*io_service)));
} }
static void connect(const std::shared_ptr<Session> &session) { static void connect(std::shared_ptr<Session> &session) {
if(!session->connection->socket->lowest_layer().is_open()) { if(!session->connection->socket->lowest_layer().is_open()) {
auto resolver = std::make_shared<asio::ip::tcp::resolver>(*session->io_service); auto resolver = std::make_shared<asio::ip::tcp::resolver>(*session->io_service);
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect); auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
resolver->async_resolve(*session->connection->query, [session, timer, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) { resolver->async_resolve(*session->connection->query, [session, timer, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect); auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
asio::async_connect(*session->connection->socket, it, [session, timer, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) { asio::async_connect(*session->connection->socket, it, [session, timer, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {

View file

@ -46,13 +46,13 @@ namespace SimpleWeb {
return std::make_shared<Connection>(host, port, config, std::unique_ptr<HTTPS>(new HTTPS(*io_service, context))); return std::make_shared<Connection>(host, port, config, std::unique_ptr<HTTPS>(new HTTPS(*io_service, context)));
} }
static void connect(const std::shared_ptr<Session> &session) { static void connect(std::shared_ptr<Session> &session) {
if(!session->connection->socket->lowest_layer().is_open()) { if(!session->connection->socket->lowest_layer().is_open()) {
auto resolver = std::make_shared<asio::ip::tcp::resolver>(*session->io_service); auto resolver = std::make_shared<asio::ip::tcp::resolver>(*session->io_service);
resolver->async_resolve(*session->connection->query, [session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) { resolver->async_resolve(*session->connection->query, [session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) mutable {
if(!ec) { if(!ec) {
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect); auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
asio::async_connect(session->connection->socket->lowest_layer(), it, [session, resolver, timer](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) { asio::async_connect(session->connection->socket->lowest_layer(), it, [session, resolver, timer](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -66,13 +66,13 @@ namespace SimpleWeb {
write_stream << "CONNECT " + host_port + " HTTP/1.1\r\n" write_stream << "CONNECT " + host_port + " HTTP/1.1\r\n"
<< "Host: " << host_port << "\r\n\r\n"; << "Host: " << host_port << "\r\n\r\n";
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect); auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
asio::async_write(session->connection->socket->next_layer(), *write_buffer, [session, write_buffer, timer](const error_code &ec, size_t /*bytes_transferred*/) { asio::async_write(session->connection->socket->next_layer(), *write_buffer, [session, write_buffer, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
std::shared_ptr<Response> response(new Response()); std::shared_ptr<Response> response(new Response());
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect); auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
asio::async_read_until(session->connection->socket->next_layer(), response->content_buffer, "\r\n\r\n", [session, response, timer](const error_code &ec, size_t /*bytes_transferred*/) { asio::async_read_until(session->connection->socket->next_layer(), response->content_buffer, "\r\n\r\n", [session, response, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -115,9 +115,9 @@ namespace SimpleWeb {
write(session); write(session);
} }
static void handshake(const std::shared_ptr<Session> &session) { static void handshake(std::shared_ptr<Session> &session) {
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect); auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
session->connection->socket->async_handshake(asio::ssl::stream_base::client, [session, timer](const error_code &ec) { session->connection->socket->async_handshake(asio::ssl::stream_base::client, [session, timer](const error_code &ec) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) if(!ec)

View file

@ -23,7 +23,7 @@ typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient; typedef SimpleWeb::Client<SimpleWeb::HTTP> HttpClient;
//Added for the default_resource example //Added for the default_resource example
void default_resource_send(const HttpServer &server, const shared_ptr<HttpServer::Response> &response, const shared_ptr<ifstream> &ifs); void default_resource_send(const HttpServer &server, shared_ptr<HttpServer::Response> &response, shared_ptr<ifstream> &ifs);
int main() { int main() {
//HTTP-server at port 8080 using 1 thread //HTTP-server at port 8080 using 1 thread
@ -34,7 +34,7 @@ int main() {
//Add resources using path-regex and method-string, and an anonymous function //Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string //POST-example for the path /string, responds the posted string
server.resource["^/string$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/string$"]["POST"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
//Retrieve string: //Retrieve string:
auto content = request->content.string(); auto content = request->content.string();
//request->content.string() is a convenience function for: //request->content.string() is a convenience function for:
@ -58,7 +58,7 @@ int main() {
// "lastName": "Smith", // "lastName": "Smith",
// "age": 25 // "age": 25
//} //}
server.resource["^/json$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/json$"]["POST"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
try { try {
ptree pt; ptree pt;
read_json(request->content, pt); read_json(request->content, pt);
@ -90,7 +90,7 @@ int main() {
//GET-example for the path /info //GET-example for the path /info
//Responds with request-information //Responds with request-information
server.resource["^/info$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/info$"]["GET"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
stringstream stream; stringstream stream;
stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>"; stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>";
stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>"; stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>";
@ -115,7 +115,7 @@ int main() {
//GET-example for the path /match/[number], responds with the matched string in path (number) //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 //For instance a request GET /match/123 will receive: 123
server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
string number = request->path_match[1]; string number = request->path_match[1];
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n"
<< number; << number;
@ -126,7 +126,7 @@ int main() {
}; };
//Get example simulating heavy work in a separate thread //Get example simulating heavy work in a separate thread
server.resource["^/work$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) { server.resource["^/work$"]["GET"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> & /*request*/) {
thread work_thread([response] { thread work_thread([response] {
this_thread::sleep_for(chrono::seconds(5)); this_thread::sleep_for(chrono::seconds(5));
response->write("Work done"); response->write("Work done");
@ -138,7 +138,7 @@ int main() {
//Will respond with content in the web/-directory, and its subdirectories. //Will respond with content in the web/-directory, and its subdirectories.
//Default file: index.html //Default file: index.html
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server //Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
server.default_resource["GET"] = [&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.default_resource["GET"] = [&server](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
try { try {
auto web_root_path = boost::filesystem::canonical("web"); auto web_root_path = boost::filesystem::canonical("web");
auto path = boost::filesystem::canonical(web_root_path / request->path); auto path = boost::filesystem::canonical(web_root_path / request->path);
@ -193,7 +193,7 @@ int main() {
} }
}; };
server.on_error = [](std::shared_ptr<HttpServer::Request> /*request*/, const SimpleWeb::error_code & /*ec*/) { server.on_error = [](std::shared_ptr<HttpServer::Request> & /*request*/, const SimpleWeb::error_code & /*ec*/) {
// handle errors here // handle errors here
}; };
@ -217,7 +217,7 @@ int main() {
cout << r2->content.rdbuf() << endl; cout << r2->content.rdbuf() << endl;
// asynchronous request example // asynchronous request example
client.request("POST", "/json", json_string, [](std::shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) { client.request("POST", "/json", json_string, [](std::shared_ptr<HttpClient::Response> &response, const SimpleWeb::error_code &ec) {
if(!ec) if(!ec)
cout << response->content.rdbuf() << endl; cout << response->content.rdbuf() << endl;
}); });
@ -227,14 +227,14 @@ int main() {
server_thread.join(); server_thread.join();
} }
void default_resource_send(const HttpServer &server, const shared_ptr<HttpServer::Response> &response, const shared_ptr<ifstream> &ifs) { void default_resource_send(const HttpServer &server, shared_ptr<HttpServer::Response> &response, shared_ptr<ifstream> &ifs) {
//read and send 128 KB at a time //read and send 128 KB at a time
static vector<char> buffer(131072); // Safe when server is running on one thread static vector<char> buffer(131072); // Safe when server is running on one thread
streamsize read_length; streamsize read_length;
if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) { if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) {
response->write(&buffer[0], read_length); response->write(&buffer[0], read_length);
if(read_length == static_cast<streamsize>(buffer.size())) { if(read_length == static_cast<streamsize>(buffer.size())) {
server.send(response, [&server, response, ifs](const SimpleWeb::error_code &ec) { server.send(response, [&server, response, ifs](const SimpleWeb::error_code &ec) mutable {
if(!ec) if(!ec)
default_resource_send(server, response, ifs); default_resource_send(server, response, ifs);
else else

View file

@ -21,7 +21,7 @@ typedef SimpleWeb::Server<SimpleWeb::HTTPS> HttpsServer;
typedef SimpleWeb::Client<SimpleWeb::HTTPS> HttpsClient; typedef SimpleWeb::Client<SimpleWeb::HTTPS> HttpsClient;
//Added for the default_resource example //Added for the default_resource example
void default_resource_send(const HttpsServer &server, const shared_ptr<HttpsServer::Response> &response, const shared_ptr<ifstream> &ifs); void default_resource_send(const HttpsServer &server, shared_ptr<HttpsServer::Response> &response, shared_ptr<ifstream> &ifs);
int main() { int main() {
//HTTPS-server at port 8080 using 1 thread //HTTPS-server at port 8080 using 1 thread
@ -32,7 +32,7 @@ int main() {
//Add resources using path-regex and method-string, and an anonymous function //Add resources using path-regex and method-string, and an anonymous function
//POST-example for the path /string, responds the posted string //POST-example for the path /string, responds the posted string
server.resource["^/string$"]["POST"] = [](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) { server.resource["^/string$"]["POST"] = [](shared_ptr<HttpsServer::Response> &response, shared_ptr<HttpsServer::Request> &request) {
//Retrieve string: //Retrieve string:
auto content = request->content.string(); auto content = request->content.string();
//request->content.string() is a convenience function for: //request->content.string() is a convenience function for:
@ -56,7 +56,7 @@ int main() {
// "lastName": "Smith", // "lastName": "Smith",
// "age": 25 // "age": 25
//} //}
server.resource["^/json$"]["POST"] = [](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) { server.resource["^/json$"]["POST"] = [](shared_ptr<HttpsServer::Response> &response, shared_ptr<HttpsServer::Request> &request) {
try { try {
ptree pt; ptree pt;
read_json(request->content, pt); read_json(request->content, pt);
@ -88,7 +88,7 @@ int main() {
//GET-example for the path /info //GET-example for the path /info
//Responds with request-information //Responds with request-information
server.resource["^/info$"]["GET"] = [](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) { server.resource["^/info$"]["GET"] = [](shared_ptr<HttpsServer::Response> &response, shared_ptr<HttpsServer::Request> &request) {
stringstream stream; stringstream stream;
stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>"; stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>";
stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>"; stream << request->method << " " << request->path << " HTTP/" << request->http_version << "<br>";
@ -113,7 +113,7 @@ int main() {
//GET-example for the path /match/[number], responds with the matched string in path (number) //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 //For instance a request GET /match/123 will receive: 123
server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) { server.resource["^/match/([0-9]+)$"]["GET"] = [](shared_ptr<HttpsServer::Response> &response, shared_ptr<HttpsServer::Request> &request) {
string number = request->path_match[1]; string number = request->path_match[1];
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n"
<< number; << number;
@ -124,7 +124,7 @@ int main() {
}; };
//Get example simulating heavy work in a separate thread //Get example simulating heavy work in a separate thread
server.resource["^/work$"]["GET"] = [](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> /*request*/) { server.resource["^/work$"]["GET"] = [](shared_ptr<HttpsServer::Response> &response, shared_ptr<HttpsServer::Request> & /*request*/) {
thread work_thread([response] { thread work_thread([response] {
this_thread::sleep_for(chrono::seconds(5)); this_thread::sleep_for(chrono::seconds(5));
response->write("Work done"); response->write("Work done");
@ -136,7 +136,7 @@ int main() {
//Will respond with content in the web/-directory, and its subdirectories. //Will respond with content in the web/-directory, and its subdirectories.
//Default file: index.html //Default file: index.html
//Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server //Can for instance be used to retrieve an HTML 5 client that uses REST-resources on this server
server.default_resource["GET"] = [&server](shared_ptr<HttpsServer::Response> response, shared_ptr<HttpsServer::Request> request) { server.default_resource["GET"] = [&server](shared_ptr<HttpsServer::Response> &response, shared_ptr<HttpsServer::Request> &request) {
try { try {
auto web_root_path = boost::filesystem::canonical("web"); auto web_root_path = boost::filesystem::canonical("web");
auto path = boost::filesystem::canonical(web_root_path / request->path); auto path = boost::filesystem::canonical(web_root_path / request->path);
@ -191,7 +191,7 @@ int main() {
} }
}; };
server.on_error = [](std::shared_ptr<HttpsServer::Request> /*request*/, const SimpleWeb::error_code & /*ec*/) { server.on_error = [](std::shared_ptr<HttpsServer::Request> & /*request*/, const SimpleWeb::error_code & /*ec*/) {
// handle errors here // handle errors here
}; };
@ -216,7 +216,7 @@ int main() {
cout << r2->content.rdbuf() << endl; cout << r2->content.rdbuf() << endl;
// asynchronous request example // asynchronous request example
client.request("POST", "/json", json_string, [](std::shared_ptr<HttpsClient::Response> response, const SimpleWeb::error_code &ec) { client.request("POST", "/json", json_string, [](std::shared_ptr<HttpsClient::Response> &response, const SimpleWeb::error_code &ec) {
if(!ec) if(!ec)
cout << response->content.rdbuf() << endl; cout << response->content.rdbuf() << endl;
}); });
@ -226,14 +226,14 @@ int main() {
server_thread.join(); server_thread.join();
} }
void default_resource_send(const HttpsServer &server, const shared_ptr<HttpsServer::Response> &response, const shared_ptr<ifstream> &ifs) { void default_resource_send(const HttpsServer &server, shared_ptr<HttpsServer::Response> &response, shared_ptr<ifstream> &ifs) {
//read and send 128 KB at a time //read and send 128 KB at a time
static vector<char> buffer(131072); // Safe when server is running on one thread static vector<char> buffer(131072); // Safe when server is running on one thread
streamsize read_length; streamsize read_length;
if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) { if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) {
response->write(&buffer[0], read_length); response->write(&buffer[0], read_length);
if(read_length == static_cast<streamsize>(buffer.size())) { if(read_length == static_cast<streamsize>(buffer.size())) {
server.send(response, [&server, response, ifs](const SimpleWeb::error_code &ec) { server.send(response, [&server, response, ifs](const SimpleWeb::error_code &ec) mutable {
if(!ec) if(!ec)
default_resource_send(server, response, ifs); default_resource_send(server, response, ifs);
else else

View file

@ -54,7 +54,7 @@ namespace SimpleWeb {
std::shared_ptr<socket_type> socket; std::shared_ptr<socket_type> socket;
Response(const std::shared_ptr<socket_type> &socket) : std::ostream(&streambuf), socket(socket) {} Response(std::shared_ptr<socket_type> &socket) : std::ostream(&streambuf), socket(socket) {}
template <class size_type> template <class size_type>
void write_header(const CaseInsensitiveMultimap &header, size_type size) { void write_header(const CaseInsensitiveMultimap &header, size_type size) {
@ -222,13 +222,13 @@ namespace SimpleWeb {
public: public:
/// Warning: do not add or remove resources after start() is called /// Warning: do not add or remove resources after start() is called
std::map<regex_orderable, std::map<std::string, std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)>>> resource; std::map<regex_orderable, std::map<std::string, std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response> &, std::shared_ptr<typename ServerBase<socket_type>::Request> &)>>> resource;
std::map<std::string, std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)>> default_resource; std::map<std::string, std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response> &, std::shared_ptr<typename ServerBase<socket_type>::Request> &)>> default_resource;
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Request>, const error_code &)> on_error; std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Request> &, const error_code &)> on_error;
std::function<void(std::shared_ptr<socket_type> socket, std::shared_ptr<typename ServerBase<socket_type>::Request>)> on_upgrade; std::function<void(std::shared_ptr<socket_type> &, std::shared_ptr<typename ServerBase<socket_type>::Request> &)> on_upgrade;
virtual void start() { virtual void start() {
if(!io_service) { if(!io_service) {
@ -281,8 +281,8 @@ namespace SimpleWeb {
} }
///Use this function if you need to recursively send parts of a longer message ///Use this function if you need to recursively send parts of a longer message
void send(const std::shared_ptr<Response> &response, const std::function<void(const error_code &)> &callback = nullptr) const { void send(std::shared_ptr<Response> &response, const std::function<void(const error_code &)> &callback = nullptr) const {
asio::async_write(*response->socket, response->streambuf, [this, response, callback](const error_code &ec, size_t /*bytes_transferred*/) { asio::async_write(*response->socket, response->streambuf, [this, response, callback](const error_code &ec, size_t /*bytes_transferred*/) mutable {
if(callback) if(callback)
callback(ec); callback(ec);
}); });
@ -301,13 +301,13 @@ namespace SimpleWeb {
virtual void accept() = 0; virtual void accept() = 0;
std::shared_ptr<asio::deadline_timer> get_timeout_timer(const std::shared_ptr<socket_type> &socket, long seconds) { std::shared_ptr<asio::deadline_timer> get_timeout_timer(std::shared_ptr<socket_type> &socket, long seconds) {
if(seconds == 0) if(seconds == 0)
return nullptr; return nullptr;
auto timer = std::make_shared<asio::deadline_timer>(*io_service); auto timer = std::make_shared<asio::deadline_timer>(*io_service);
timer->expires_from_now(boost::posix_time::seconds(seconds)); timer->expires_from_now(boost::posix_time::seconds(seconds));
timer->async_wait([socket](const error_code &ec) { timer->async_wait([socket](const error_code &ec) mutable {
if(!ec) { if(!ec) {
error_code ec; error_code ec;
socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec); socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
@ -317,7 +317,7 @@ namespace SimpleWeb {
return timer; return timer;
} }
void read_request_and_content(const std::shared_ptr<socket_type> &socket) { void read_request_and_content(std::shared_ptr<socket_type> &socket) {
//Create new streambuf (Request::streambuf) for async_read_until() //Create new streambuf (Request::streambuf) for async_read_until()
//shared_ptr is used to pass temporary objects to the asynchronous functions //shared_ptr is used to pass temporary objects to the asynchronous functions
std::shared_ptr<Request> request(new Request(*socket)); std::shared_ptr<Request> request(new Request(*socket));
@ -325,7 +325,7 @@ namespace SimpleWeb {
//Set timeout on the following asio::async-read or write function //Set timeout on the following asio::async-read or write function
auto timer = this->get_timeout_timer(socket, config.timeout_request); auto timer = this->get_timeout_timer(socket, config.timeout_request);
asio::async_read_until(*socket, request->streambuf, "\r\n\r\n", [this, socket, request, timer](const error_code &ec, size_t bytes_transferred) { asio::async_read_until(*socket, request->streambuf, "\r\n\r\n", [this, socket, request, timer](const error_code &ec, size_t bytes_transferred) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -353,7 +353,7 @@ namespace SimpleWeb {
if(content_length > num_additional_bytes) { if(content_length > num_additional_bytes) {
//Set timeout on the following asio::async-read or write function //Set timeout on the following asio::async-read or write function
auto timer = this->get_timeout_timer(socket, config.timeout_content); auto timer = this->get_timeout_timer(socket, config.timeout_content);
asio::async_read(*socket, request->streambuf, asio::transfer_exactly(content_length - num_additional_bytes), [this, socket, request, timer](const error_code &ec, size_t /*bytes_transferred*/) { asio::async_read(*socket, request->streambuf, asio::transfer_exactly(content_length - num_additional_bytes), [this, socket, request, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) if(!ec)
@ -373,7 +373,7 @@ namespace SimpleWeb {
}); });
} }
bool parse_request(const std::shared_ptr<Request> &request) const { bool parse_request(std::shared_ptr<Request> &request) const {
std::string line; std::string line;
getline(request->content, line); getline(request->content, line);
size_t method_end; size_t method_end;
@ -430,7 +430,7 @@ namespace SimpleWeb {
} }
void void
find_resource(const std::shared_ptr<socket_type> &socket, const std::shared_ptr<Request> &request) { find_resource(std::shared_ptr<socket_type> &socket, std::shared_ptr<Request> &request) {
//Upgrade connection //Upgrade connection
if(on_upgrade) { if(on_upgrade) {
auto it = request->header.find("Upgrade"); auto it = request->header.find("Upgrade");
@ -457,14 +457,14 @@ namespace SimpleWeb {
} }
} }
void write_response(const std::shared_ptr<socket_type> &socket, const std::shared_ptr<Request> &request, void write_response(std::shared_ptr<socket_type> &socket, std::shared_ptr<Request> &request,
std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response>, std::shared_ptr<typename ServerBase<socket_type>::Request>)> &resource_function) { std::function<void(std::shared_ptr<typename ServerBase<socket_type>::Response> &, std::shared_ptr<typename ServerBase<socket_type>::Request> &)> &resource_function) {
//Set timeout on the following asio::async-read or write function //Set timeout on the following asio::async-read or write function
auto timer = this->get_timeout_timer(socket, config.timeout_content); auto timer = this->get_timeout_timer(socket, config.timeout_content);
auto response = std::shared_ptr<Response>(new Response(socket), [this, request, timer](Response *response_ptr) { auto response = std::shared_ptr<Response>(new Response(socket), [this, request, timer](Response *response_ptr) mutable {
auto response = std::shared_ptr<Response>(response_ptr); auto response = std::shared_ptr<Response>(response_ptr);
this->send(response, [this, response, request, timer](const error_code &ec) { this->send(response, [this, response, request, timer](const error_code &ec) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) { if(!ec) {
@ -518,12 +518,12 @@ namespace SimpleWeb {
Server() : ServerBase<HTTP>::ServerBase(80) {} Server() : ServerBase<HTTP>::ServerBase(80) {}
protected: protected:
void accept() { void accept() override {
//Create new socket for this connection //Create new socket for this connection
//Shared_ptr is used to pass temporary objects to the asynchronous functions //Shared_ptr is used to pass temporary objects to the asynchronous functions
auto socket = std::make_shared<HTTP>(*io_service); auto socket = std::make_shared<HTTP>(*io_service);
acceptor->async_accept(*socket, [this, socket](const error_code &ec) { acceptor->async_accept(*socket, [this, socket](const error_code &ec) mutable {
//Immediately start accepting a new connection (if io_service hasn't been stopped) //Immediately start accepting a new connection (if io_service hasn't been stopped)
if(ec != asio::error::operation_aborted) if(ec != asio::error::operation_aborted)
accept(); accept();
@ -534,8 +534,10 @@ namespace SimpleWeb {
this->read_request_and_content(socket); this->read_request_and_content(socket);
} }
else if(on_error) else if(on_error) {
on_error(std::shared_ptr<Request>(new Request(*socket)), ec); std::shared_ptr<Request> request(new Request(*socket));
on_error(request, ec);
}
}); });
} }
}; };

View file

@ -42,7 +42,7 @@ namespace SimpleWeb {
} }
} }
void start() { void start() override {
if(set_session_id_context) { if(set_session_id_context) {
// Creating session_id_context from address:port but reversed due to small SSL_MAX_SSL_SESSION_ID_LENGTH // Creating session_id_context from address:port but reversed due to small SSL_MAX_SSL_SESSION_ID_LENGTH
session_id_context = std::to_string(config.port) + ':'; session_id_context = std::to_string(config.port) + ':';
@ -56,12 +56,12 @@ namespace SimpleWeb {
protected: protected:
asio::ssl::context context; asio::ssl::context context;
void accept() { void accept() override {
//Create new socket for this connection //Create new socket for this connection
//Shared_ptr is used to pass temporary objects to the asynchronous functions //Shared_ptr is used to pass temporary objects to the asynchronous functions
auto socket = std::make_shared<HTTPS>(*io_service, context); auto socket = std::make_shared<HTTPS>(*io_service, context);
acceptor->async_accept((*socket).lowest_layer(), [this, socket](const error_code &ec) { acceptor->async_accept((*socket).lowest_layer(), [this, socket](const error_code &ec) mutable {
//Immediately start accepting a new connection (if io_service hasn't been stopped) //Immediately start accepting a new connection (if io_service hasn't been stopped)
if(ec != asio::error::operation_aborted) if(ec != asio::error::operation_aborted)
accept(); accept();
@ -73,17 +73,21 @@ namespace SimpleWeb {
//Set timeout on the following asio::ssl::stream::async_handshake //Set timeout on the following asio::ssl::stream::async_handshake
auto timer = get_timeout_timer(socket, config.timeout_request); auto timer = get_timeout_timer(socket, config.timeout_request);
socket->async_handshake(asio::ssl::stream_base::server, [this, socket, timer](const error_code &ec) { socket->async_handshake(asio::ssl::stream_base::server, [this, socket, timer](const error_code &ec) mutable {
if(timer) if(timer)
timer->cancel(); timer->cancel();
if(!ec) if(!ec)
read_request_and_content(socket); read_request_and_content(socket);
else if(on_error) else if(on_error) {
on_error(std::shared_ptr<Request>(new Request(*socket)), ec); std::shared_ptr<Request> request(new Request(*socket));
on_error(request, ec);
}
}); });
} }
else if(on_error) else if(on_error) {
on_error(std::shared_ptr<Request>(new Request(*socket)), ec); std::shared_ptr<Request> request(new Request(*socket));
on_error(request, ec);
}
}); });
} }
}; };

View file

@ -12,28 +12,28 @@ int main() {
HttpServer server; HttpServer server;
server.config.port = 8080; server.config.port = 8080;
server.resource["^/string$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/string$"]["POST"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
auto content = request->content.string(); auto content = request->content.string();
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n"
<< content; << content;
}; };
server.resource["^/string2$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/string2$"]["POST"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
response->write(request->content.string()); response->write(request->content.string());
}; };
server.resource["^/string3$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/string3$"]["POST"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
std::stringstream stream; std::stringstream stream;
stream << request->content.rdbuf(); stream << request->content.rdbuf();
response->write(stream); response->write(stream);
}; };
server.resource["^/string4$"]["POST"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> /*request*/) { server.resource["^/string4$"]["POST"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> & /*request*/) {
response->write(SimpleWeb::StatusCode::client_error_forbidden, {{"Test1", "test2"}, {"tesT3", "test4"}}); response->write(SimpleWeb::StatusCode::client_error_forbidden, {{"Test1", "test2"}, {"tesT3", "test4"}});
}; };
server.resource["^/info$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/info$"]["GET"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
stringstream content_stream; stringstream content_stream;
content_stream << request->method << " " << request->path << " " << request->http_version << " "; content_stream << request->method << " " << request->path << " " << request->http_version << " ";
content_stream << request->header.find("test parameter")->second; content_stream << request->header.find("test parameter")->second;
@ -44,20 +44,20 @@ int main() {
<< content_stream.rdbuf(); << content_stream.rdbuf();
}; };
server.resource["^/match/([0-9]+)$"]["GET"] = [&server](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/match/([0-9]+)$"]["GET"] = [&server](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
string number = request->path_match[1]; string number = request->path_match[1];
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" *response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n"
<< number; << number;
}; };
server.resource["^/header$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/header$"]["GET"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
auto content = request->header.find("test1")->second + request->header.find("test2")->second; auto content = request->header.find("test1")->second + request->header.find("test2")->second;
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" *response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n"
<< content; << content;
}; };
server.resource["^/query_string$"]["GET"] = [](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) { server.resource["^/query_string$"]["GET"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
assert(request->path == "/query_string"); assert(request->path == "/query_string");
assert(request->query_string == "testing"); assert(request->query_string == "testing");
auto queries = request->parse_query_string(); auto queries = request->parse_query_string();
@ -185,7 +185,7 @@ int main() {
{ {
HttpClient client("localhost:8080"); HttpClient client("localhost:8080");
bool call = false; bool call = false;
client.request("GET", "/match/123", [&call](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) { client.request("GET", "/match/123", [&call](shared_ptr<HttpClient::Response> &response, const SimpleWeb::error_code &ec) {
assert(!ec); assert(!ec);
stringstream output; stringstream output;
output << response->content.rdbuf(); output << response->content.rdbuf();
@ -201,7 +201,7 @@ int main() {
for(size_t c = 0; c < 100; ++c) { for(size_t c = 0; c < 100; ++c) {
calls[c] = 0; calls[c] = 0;
threads.emplace_back([c, &client, &calls] { threads.emplace_back([c, &client, &calls] {
client.request("GET", "/match/123", [c, &calls](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) { client.request("GET", "/match/123", [c, &calls](shared_ptr<HttpClient::Response> &response, const SimpleWeb::error_code &ec) {
assert(!ec); assert(!ec);
stringstream output; stringstream output;
output << response->content.rdbuf(); output << response->content.rdbuf();