Preparation for std::enable_shared_from_this: Server- and Client-constructors are no longer public, use ::create instead

This commit is contained in:
eidheim 2017-07-03 15:02:35 +02:00
commit 1992bbb4b4
8 changed files with 147 additions and 114 deletions

View file

@ -27,12 +27,12 @@ int main() {
//HTTPS-server at port 8080 using 1 thread
//Unless you do more heavy non-threaded processing in the resources,
//1 thread is usually faster than several threads
HttpsServer server("server.crt", "server.key");
server.config.port = 8080;
auto server = HttpsServer::create("server.crt", "server.key");
server->config.port = 8080;
//Add resources using path-regex and method-string, and an anonymous function
//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:
auto content = request->content.string();
//request->content.string() is a convenience function for:
@ -56,7 +56,7 @@ int main() {
// "lastName": "Smith",
// "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 {
ptree pt;
read_json(request->content, pt);
@ -88,7 +88,7 @@ int main() {
//GET-example for the path /info
//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;
stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>";
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)
//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];
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n"
<< number;
@ -124,7 +124,7 @@ int main() {
};
//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] {
this_thread::sleep_for(chrono::seconds(5));
response->write("Work done");
@ -136,7 +136,7 @@ int main() {
//Will respond with content in the web/-directory, and its subdirectories.
//Default file: index.html
//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 {
auto web_root_path = boost::filesystem::canonical("web");
auto path = boost::filesystem::canonical(web_root_path / request->path);
@ -181,7 +181,7 @@ int main() {
header.emplace("Content-Length", to_string(length));
response->write(header);
default_resource_send(server, response, ifs);
default_resource_send(*server, response, ifs);
}
else
throw invalid_argument("could not read file");
@ -191,37 +191,37 @@ 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
};
thread server_thread([&server]() {
//Start server
server.start();
server->start();
});
//Wait for server to start so that the client can connect
this_thread::sleep_for(chrono::seconds(1));
//Client examples
//Second Client() parameter set to false: no certificate verification
HttpsClient client("localhost:8080", false);
//Second create() parameter set to false: no certificate verification
auto client = HttpsClient::create("localhost:8080", false);
// synchronous request examples
auto r1 = client.request("GET", "/match/123");
auto r1 = client->request("GET", "/match/123");
cout << r1->content.rdbuf() << endl; // Alternatively, use the convenience function r1->content.string()
string json_string = "{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
auto r2 = client.request("POST", "/string", json_string);
auto r2 = client->request("POST", "/string", json_string);
cout << r2->content.rdbuf() << endl;
// 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)
cout << response->content.rdbuf() << endl;
});
client.io_service->reset(); // needed because the io_service has been run already in the synchronous examples
client.io_service->run();
client->io_service->reset(); // needed because the io_service has been run already in the synchronous examples
client->io_service->run();
server_thread.join();
}