Preparation for std::enable_shared_from_this: Server- and Client-constructors are no longer public, use ::create instead
This commit is contained in:
parent
80ab651fca
commit
1992bbb4b4
8 changed files with 147 additions and 114 deletions
|
|
@ -29,12 +29,12 @@ int main() {
|
|||
//HTTP-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
|
||||
HttpServer server;
|
||||
server.config.port = 8080;
|
||||
auto server = HttpServer::create();
|
||||
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<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
|
||||
server->resource["^/string$"]["POST"] = [](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
|
||||
//Retrieve string:
|
||||
auto content = request->content.string();
|
||||
//request->content.string() is a convenience function for:
|
||||
|
|
@ -58,7 +58,7 @@ int main() {
|
|||
// "lastName": "Smith",
|
||||
// "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 {
|
||||
ptree pt;
|
||||
read_json(request->content, pt);
|
||||
|
|
@ -90,7 +90,7 @@ int main() {
|
|||
|
||||
//GET-example for the path /info
|
||||
//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;
|
||||
stream << "<h1>Request from " << request->remote_endpoint_address << " (" << request->remote_endpoint_port << ")</h1>";
|
||||
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)
|
||||
//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];
|
||||
*response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n"
|
||||
<< number;
|
||||
|
|
@ -126,7 +126,7 @@ int main() {
|
|||
};
|
||||
|
||||
//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] {
|
||||
this_thread::sleep_for(chrono::seconds(5));
|
||||
response->write("Work done");
|
||||
|
|
@ -138,7 +138,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<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
|
||||
server->default_resource["GET"] = [&server](shared_ptr<HttpServer::Response> &response, shared_ptr<HttpServer::Request> &request) {
|
||||
try {
|
||||
auto web_root_path = boost::filesystem::canonical("web");
|
||||
auto path = boost::filesystem::canonical(web_root_path / request->path);
|
||||
|
|
@ -183,7 +183,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");
|
||||
|
|
@ -193,36 +193,36 @@ 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
|
||||
};
|
||||
|
||||
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
|
||||
HttpClient client("localhost:8080");
|
||||
auto client = HttpClient::create("localhost:8080");
|
||||
|
||||
// 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<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)
|
||||
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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue