From 30499005f8a1a44006ca5812a6ad09f86bf7ca68 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 30 Nov 2014 21:22:06 +0900 Subject: [PATCH] Reformat source code in libnghttp2_asio.rst --- doc/sources/libnghttp2_asio.rst | 126 ++++++++++++++------------------ 1 file changed, 53 insertions(+), 73 deletions(-) diff --git a/doc/sources/libnghttp2_asio.rst b/doc/sources/libnghttp2_asio.rst index bea4c536..bf544b17 100644 --- a/doc/sources/libnghttp2_asio.rst +++ b/doc/sources/libnghttp2_asio.rst @@ -36,18 +36,14 @@ HTTP/2 server looks like this: using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2::server; - int main(int argc, char* argv[]) - { + int main(int argc, char *argv[]) { http2 server; - server.listen - ("*", 3000, - [](const std::shared_ptr& req, - const std::shared_ptr& res) - { - res->write_head(200); - res->end("hello, world"); - }); + server.listen("*", 3000, [](const std::shared_ptr &req, + const std::shared_ptr &res) { + res->write_head(200); + res->end("hello, world"); + }); } First we instantiate ``nghttp2::asio_http2::server::http2`` object. @@ -78,26 +74,22 @@ SSL/TLS. using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2::server; - int main(int argc, char* argv[]) - { + int main(int argc, char *argv[]) { http2 server; server.tls("server.key", "server.crt"); - server.listen - ("*", 3000, - [](const std::shared_ptr& req, - const std::shared_ptr& res) - { - if(req->path() == "/" || req->path() == "/index.html") { - res->write_head(200); - res->end(file_reader("index.html")); - } else { - res->write_head(404); - res->end("404" - "404 Not Found"); - } - }); + server.listen("*", 3000, [](const std::shared_ptr &req, + const std::shared_ptr &res) { + if (req->path() == "/" || req->path() == "/index.html") { + res->write_head(200); + res->end(file_reader("index.html")); + } else { + res->write_head(404); + res->end("404" + "404 Not Found"); + } + }); } Specifying path to private key file and certificate file in @@ -124,37 +116,33 @@ Server push is also supported. using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2::server; - int main(int argc, char* argv[]) - { + int main(int argc, char *argv[]) { http2 server; server.tls("server.key", "server.crt"); - server.listen - ("*", 3000, - [](const std::shared_ptr& req, - const std::shared_ptr& res) - { - if(req->path() == "/") { - req->push("GET", "/my.css"); + server.listen("*", 3000, [](const std::shared_ptr &req, + const std::shared_ptr &res) { + if (req->path() == "/") { + req->push("GET", "/my.css"); - res->write_head(200); - res->end(file_reader("index.html")); + res->write_head(200); + res->end(file_reader("index.html")); - return; - } + return; + } - if(req->path() == "/my.css") { - res->write_head(200); - res->end(file_reader("my.css")); + if (req->path() == "/my.css") { + res->write_head(200); + res->end(file_reader("my.css")); - return; - } + return; + } - res->write_head(404); - res->end("404" - "404 Not Found"); - }); + res->write_head(404); + res->end("404" + "404 Not Found"); + }); } When client requested "/", we push "/my.css". To push resource, call @@ -197,38 +185,30 @@ blocking task there. The example follows: using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2::server; - int main(int argc, char* argv[]) - { + int main(int argc, char *argv[]) { http2 server; server.num_concurrent_tasks(16); - server.listen - ("*", 3000, - [](const std::shared_ptr& req, - const std::shared_ptr& res) - { - req->run_task - ([res](channel& channel) - { - // executed in different thread than the thread where - // request callback was executed. + server.listen("*", 3000, [](const std::shared_ptr &req, + const std::shared_ptr &res) { + req->run_task([res](channel &channel) { + // executed in different thread than the thread where + // request callback was executed. - // using res directly here is not safe. Capturing it by - // value is safe because it is std::shared_ptr. + // using res directly here is not safe. Capturing it by + // value is safe because it is std::shared_ptr. - sleep(1); + sleep(1); - channel.post - ([res]() - { - // executed in the same thread where request callback - // was executed. - res->write_head(200); - res->end("hello, world"); - }); - }); - }); + channel.post([res]() { + // executed in the same thread where request callback + // was executed. + res->write_head(200); + res->end("hello, world"); + }); + }); + }); } First we set the number of background threads which run tasks. By