Reverted resource/response function parameter types shared_ptr& to shared_ptr. No real value added here, just more complicated code. Compiler might also optimise the now const lambda captures.

This commit is contained in:
eidheim 2017-07-04 12:04:07 +02:00
commit 68f1e8413a
8 changed files with 75 additions and 75 deletions

View file

@ -181,7 +181,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);
@ -198,7 +198,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);
@ -212,14 +212,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>(this->shared_from_this(), get_connection(), create_request_header(method, path, header)); auto session = std::make_shared<Session>(this->shared_from_this(), 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) mutable { session->callback = [connection, response, request_callback, connections, connections_mutex](const error_code &ec) {
{ {
std::lock_guard<std::mutex> lock(*connections_mutex); std::lock_guard<std::mutex> lock(*connections_mutex);
connection->in_use = false; connection->in_use = false;
@ -254,31 +254,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>(this->shared_from_this(), get_connection(), create_request_header(method, path, header)); auto session = std::make_shared<Session>(this->shared_from_this(), 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) mutable { session->callback = [connection, response, request_callback, connections, connections_mutex](const error_code &ec) {
{ {
std::lock_guard<std::mutex> lock(*connections_mutex); std::lock_guard<std::mutex> lock(*connections_mutex);
connection->in_use = false; connection->in_use = false;
@ -317,7 +317,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));
} }
@ -366,7 +366,7 @@ namespace SimpleWeb {
} }
virtual std::shared_ptr<Connection> create_connection() = 0; virtual std::shared_ptr<Connection> create_connection() = 0;
virtual void connect(std::shared_ptr<Session> &) = 0; virtual void connect(const std::shared_ptr<Session> &) = 0;
std::unique_ptr<asio::streambuf> create_request_header(const std::string &method, const std::string &path, const CaseInsensitiveMultimap &header) const { std::unique_ptr<asio::streambuf> create_request_header(const std::string &method, const std::string &path, const CaseInsensitiveMultimap &header) const {
auto corrected_path = path; auto corrected_path = path;
@ -398,9 +398,9 @@ namespace SimpleWeb {
return parsed_host_port; return parsed_host_port;
} }
void write(std::shared_ptr<Session> &session) { void write(const std::shared_ptr<Session> &session) {
session->set_timeout(); session->set_timeout();
asio::async_write(*session->connection->socket, session->request_buffer->data(), [this, session](const error_code &ec, size_t /*bytes_transferred*/) mutable { asio::async_write(*session->connection->socket, session->request_buffer->data(), [this, session](const error_code &ec, size_t /*bytes_transferred*/) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) if(!ec)
this->read(session); this->read(session);
@ -411,9 +411,9 @@ namespace SimpleWeb {
}); });
} }
void read(std::shared_ptr<Session> &session) { void read(const std::shared_ptr<Session> &session) {
session->set_timeout(); session->set_timeout();
asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n\r\n", [this, session](const error_code &ec, size_t bytes_transferred) mutable { asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n\r\n", [this, session](const error_code &ec, size_t bytes_transferred) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) { if(!ec) {
session->connection->reconnecting = false; session->connection->reconnecting = false;
@ -427,7 +427,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) {
session->set_timeout(); session->set_timeout();
asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(content_length - num_additional_bytes), [this, session](const error_code &ec, size_t /*bytes_transferred*/) mutable { asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(content_length - num_additional_bytes), [this, session](const error_code &ec, size_t /*bytes_transferred*/) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) if(!ec)
session->callback(ec); session->callback(ec);
@ -446,7 +446,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")) {
session->set_timeout(); session->set_timeout();
asio::async_read(*session->connection->socket, session->response->content_buffer, [this, session](const error_code &ec, size_t /*bytes_transferred*/) mutable { asio::async_read(*session->connection->socket, session->response->content_buffer, [this, session](const error_code &ec, size_t /*bytes_transferred*/) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) if(!ec)
session->callback(ec); session->callback(ec);
@ -478,9 +478,9 @@ namespace SimpleWeb {
}); });
} }
void read_chunked(std::shared_ptr<Session> &session, std::shared_ptr<asio::streambuf> &tmp_streambuf) { void read_chunked(const std::shared_ptr<Session> &session, const std::shared_ptr<asio::streambuf> &tmp_streambuf) {
session->set_timeout(); session->set_timeout();
asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n", [this, session, tmp_streambuf](const error_code &ec, size_t bytes_transferred) mutable { asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n", [this, session, tmp_streambuf](const error_code &ec, size_t bytes_transferred) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) { if(!ec) {
std::string line; std::string line;
@ -491,7 +491,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 = [this, session, tmp_streambuf, length]() mutable { auto post_process = [this, session, tmp_streambuf, length]() {
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));
@ -515,7 +515,7 @@ namespace SimpleWeb {
if((2 + length) > num_additional_bytes) { if((2 + length) > num_additional_bytes) {
session->set_timeout(); session->set_timeout();
asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(2 + length - num_additional_bytes), [this, session, post_process](const error_code &ec, size_t /*bytes_transferred*/) mutable { asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(2 + length - num_additional_bytes), [this, session, post_process](const error_code &ec, size_t /*bytes_transferred*/) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) if(!ec)
post_process(); post_process();
@ -559,15 +559,15 @@ namespace SimpleWeb {
return std::make_shared<Connection>(std::unique_ptr<HTTP>(new HTTP(*io_service))); return std::make_shared<Connection>(std::unique_ptr<HTTP>(new HTTP(*io_service)));
} }
void connect(std::shared_ptr<Session> &session) override { void connect(const std::shared_ptr<Session> &session) override {
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>(*io_service); auto resolver = std::make_shared<asio::ip::tcp::resolver>(*io_service);
session->set_timeout(config.timeout_connect); session->set_timeout(config.timeout_connect);
resolver->async_resolve(*query, [this, session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) mutable { resolver->async_resolve(*query, [this, session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) { if(!ec) {
session->set_timeout(config.timeout_connect); session->set_timeout(config.timeout_connect);
asio::async_connect(*session->connection->socket, it, [this, session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) mutable { asio::async_connect(*session->connection->socket, it, [this, session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) { if(!ec) {
asio::ip::tcp::no_delay option(true); asio::ip::tcp::no_delay option(true);

View file

@ -53,13 +53,13 @@ namespace SimpleWeb {
return std::make_shared<Connection>(std::unique_ptr<HTTPS>(new HTTPS(*io_service, context))); return std::make_shared<Connection>(std::unique_ptr<HTTPS>(new HTTPS(*io_service, context)));
} }
void connect(std::shared_ptr<Session> &session) override { void connect(const std::shared_ptr<Session> &session) override {
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>(*io_service); auto resolver = std::make_shared<asio::ip::tcp::resolver>(*io_service);
resolver->async_resolve(*query, [this, session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) mutable { resolver->async_resolve(*query, [this, session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) {
if(!ec) { if(!ec) {
session->set_timeout(this->config.timeout_connect); session->set_timeout(this->config.timeout_connect);
asio::async_connect(session->connection->socket->lowest_layer(), it, [this, session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) mutable { asio::async_connect(session->connection->socket->lowest_layer(), it, [this, session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) { if(!ec) {
asio::ip::tcp::no_delay option(true); asio::ip::tcp::no_delay option(true);
@ -72,12 +72,12 @@ 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";
session->set_timeout(this->config.timeout_connect); session->set_timeout(this->config.timeout_connect);
asio::async_write(session->connection->socket->next_layer(), *write_buffer, [this, session, write_buffer](const error_code &ec, size_t /*bytes_transferred*/) mutable { asio::async_write(session->connection->socket->next_layer(), *write_buffer, [this, session, write_buffer](const error_code &ec, size_t /*bytes_transferred*/) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) { if(!ec) {
std::shared_ptr<Response> response(new Response()); std::shared_ptr<Response> response(new Response());
session->set_timeout(this->config.timeout_connect); session->set_timeout(this->config.timeout_connect);
asio::async_read_until(session->connection->socket->next_layer(), response->content_buffer, "\r\n\r\n", [this, session, response](const error_code &ec, size_t /*bytes_transferred*/) mutable { asio::async_read_until(session->connection->socket->next_layer(), response->content_buffer, "\r\n\r\n", [this, session, response](const error_code &ec, size_t /*bytes_transferred*/) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) { if(!ec) {
response->parse_header(); response->parse_header();
@ -119,9 +119,9 @@ namespace SimpleWeb {
write(session); write(session);
} }
void handshake(std::shared_ptr<Session> &session) { void handshake(const std::shared_ptr<Session> &session) {
session->set_timeout(this->config.timeout_connect); session->set_timeout(this->config.timeout_connect);
session->connection->socket->async_handshake(asio::ssl::stream_base::client, [this, session](const error_code &ec) mutable { session->connection->socket->async_handshake(asio::ssl::stream_base::client, [this, session](const error_code &ec) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) if(!ec)
this->write(session); this->write(session);

View file

@ -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"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) { server->default_resource["GET"] = [](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 = [](shared_ptr<HttpServer::Request> & /*request*/, const SimpleWeb::error_code & /*ec*/) { server->on_error = [](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, [](shared_ptr<HttpClient::Response> &response, const SimpleWeb::error_code &ec) { client->request("POST", "/json", json_string, [](shared_ptr<HttpClient::Response> response, const SimpleWeb::error_code &ec) {
if(!ec) if(!ec)
cout << response->content.rdbuf() << endl; cout << response->content.rdbuf() << endl;
}); });

View file

@ -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"] = [](shared_ptr<HttpsServer::Response> &response, shared_ptr<HttpsServer::Request> &request) { server->default_resource["GET"] = [](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 = [](shared_ptr<HttpsServer::Request> & /*request*/, const SimpleWeb::error_code & /*ec*/) { server->on_error = [](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, [](shared_ptr<HttpsClient::Response> &response, const SimpleWeb::error_code &ec) { client->request("POST", "/json", json_string, [](shared_ptr<HttpsClient::Response> response, const SimpleWeb::error_code &ec) {
if(!ec) if(!ec)
cout << response->content.rdbuf() << endl; cout << response->content.rdbuf() << endl;
}); });

View file

@ -333,13 +333,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> &, 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) {
@ -403,9 +403,9 @@ namespace SimpleWeb {
virtual void accept() = 0; virtual void accept() = 0;
void read_request_and_content(std::shared_ptr<Session> &session) { void read_request_and_content(const std::shared_ptr<Session> &session) {
session->set_timeout(config.timeout_request); session->set_timeout(config.timeout_request);
asio::async_read_until(*session->socket, session->request->streambuf, "\r\n\r\n", [this, session](const error_code &ec, size_t bytes_transferred) mutable { asio::async_read_until(*session->socket, session->request->streambuf, "\r\n\r\n", [this, session](const error_code &ec, size_t bytes_transferred) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) { if(!ec) {
//request->streambuf.size() is not necessarily the same as bytes_transferred, from Boost-docs: //request->streambuf.size() is not necessarily the same as bytes_transferred, from Boost-docs:
@ -431,7 +431,7 @@ namespace SimpleWeb {
} }
if(content_length > num_additional_bytes) { if(content_length > num_additional_bytes) {
session->set_timeout(config.timeout_content); session->set_timeout(config.timeout_content);
asio::async_read(*session->socket, session->request->streambuf, asio::transfer_exactly(content_length - num_additional_bytes), [this, session](const error_code &ec, size_t /*bytes_transferred*/) mutable { asio::async_read(*session->socket, session->request->streambuf, asio::transfer_exactly(content_length - num_additional_bytes), [this, session](const error_code &ec, size_t /*bytes_transferred*/) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) if(!ec)
this->find_resource(session); this->find_resource(session);
@ -450,7 +450,7 @@ namespace SimpleWeb {
}); });
} }
void find_resource(std::shared_ptr<Session> &session) { void find_resource(const std::shared_ptr<Session> &session) {
//Upgrade connection //Upgrade connection
if(on_upgrade) { if(on_upgrade) {
auto it = session->request->header.find("Upgrade"); auto it = session->request->header.find("Upgrade");
@ -476,12 +476,12 @@ namespace SimpleWeb {
write_response(session, it->second); write_response(session, it->second);
} }
void write_response(std::shared_ptr<Session> &session, void write_response(const std::shared_ptr<Session> &session,
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) {
session->set_timeout(config.timeout_content); session->set_timeout(config.timeout_content);
auto response = std::shared_ptr<Response>(new Response(session), [this](Response *response_ptr) mutable { auto response = std::shared_ptr<Response>(new Response(session), [this](Response *response_ptr) {
auto response = std::shared_ptr<Response>(response_ptr); auto response = std::shared_ptr<Response>(response_ptr);
response->send([this, response](const error_code &ec) mutable { response->send([this, response](const error_code &ec) {
response->session->cancel_timeout(); response->session->cancel_timeout();
if(!ec) { if(!ec) {
if(response->close_connection_after_response) if(response->close_connection_after_response)
@ -542,7 +542,7 @@ namespace SimpleWeb {
//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 session = std::make_shared<Session>(this->shared_from_this(), std::make_shared<HTTP>(*io_service)); auto session = std::make_shared<Session>(this->shared_from_this(), std::make_shared<HTTP>(*io_service));
acceptor->async_accept(*session->socket, [this, session](const error_code &ec) mutable { acceptor->async_accept(*session->socket, [this, session](const error_code &ec) {
//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)
this->accept(); this->accept();

View file

@ -59,7 +59,7 @@ namespace SimpleWeb {
//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 session = std::make_shared<Session>(this->shared_from_this(), std::make_shared<HTTPS>(*io_service, context)); auto session = std::make_shared<Session>(this->shared_from_this(), std::make_shared<HTTPS>(*io_service, context));
acceptor->async_accept(session->socket->lowest_layer(), [this, session](const error_code &ec) mutable { acceptor->async_accept(session->socket->lowest_layer(), [this, session](const error_code &ec) {
//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)
this->accept(); this->accept();
@ -70,7 +70,7 @@ namespace SimpleWeb {
session->socket->lowest_layer().set_option(option); session->socket->lowest_layer().set_option(option);
session->set_timeout(config.timeout_request); session->set_timeout(config.timeout_request);
session->socket->async_handshake(asio::ssl::stream_base::server, [this, session](const error_code &ec) mutable { session->socket->async_handshake(asio::ssl::stream_base::server, [this, session](const error_code &ec) {
session->cancel_timeout(); session->cancel_timeout();
if(!ec) if(!ec)
this->read_request_and_content(session); this->read_request_and_content(session);

View file

@ -12,28 +12,28 @@ int main() {
auto server = HttpServer::create(); auto server = HttpServer::create();
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"] = [](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() {
{ {
auto client = HttpClient::create("localhost:8080"); auto client = HttpClient::create("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();

View file

@ -58,7 +58,7 @@ public:
return nullptr; return nullptr;
} }
void connect(std::shared_ptr<Session> &) override {} void connect(const std::shared_ptr<Session> &) override {}
void constructor_parse_test1() { void constructor_parse_test1() {
assert(host == "test.org"); assert(host == "test.org");