Callback shared_ptr parameters are now shared_ptr&
This commit is contained in:
parent
0c9cb000b4
commit
b3a1d48c0a
7 changed files with 104 additions and 98 deletions
|
|
@ -21,7 +21,7 @@ typedef SimpleWeb::Server<SimpleWeb::HTTPS> HttpsServer;
|
|||
typedef SimpleWeb::Client<SimpleWeb::HTTPS> HttpsClient;
|
||||
|
||||
//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() {
|
||||
//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
|
||||
//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);
|
||||
|
|
@ -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
|
||||
};
|
||||
|
||||
|
|
@ -216,7 +216,7 @@ int main() {
|
|||
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;
|
||||
});
|
||||
|
|
@ -226,14 +226,14 @@ int main() {
|
|||
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
|
||||
static vector<char> buffer(131072); // Safe when server is running on one thread
|
||||
streamsize read_length;
|
||||
if((read_length = ifs->read(&buffer[0], buffer.size()).gcount()) > 0) {
|
||||
response->write(&buffer[0], read_length);
|
||||
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)
|
||||
default_resource_send(server, response, ifs);
|
||||
else
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue