Replaced Server/Client::shared_from_this with asio callback checks if the Server/Client has been destroyed. Also added SharedMutex to utility.hpp.
This commit is contained in:
parent
53c9ff64c0
commit
9b5063f422
9 changed files with 500 additions and 163 deletions
|
|
@ -26,12 +26,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
|
||||
auto server = HttpServer::create();
|
||||
server->config.port = 8080;
|
||||
HttpServer server;
|
||||
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:
|
||||
|
|
@ -55,7 +55,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);
|
||||
|
|
@ -87,7 +87,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>";
|
||||
|
|
@ -112,7 +112,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;
|
||||
|
|
@ -123,7 +123,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");
|
||||
|
|
@ -135,7 +135,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"] = [](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 {
|
||||
auto web_root_path = boost::filesystem::canonical("web");
|
||||
auto path = boost::filesystem::canonical(web_root_path / request->path);
|
||||
|
|
@ -210,36 +210,36 @@ 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
|
||||
};
|
||||
|
||||
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
|
||||
auto client = HttpClient::create("localhost:8080");
|
||||
HttpClient client("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, [](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)
|
||||
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