diff --git a/src/Makefile.am b/src/Makefile.am index bdda06d8..54140e36 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -187,6 +187,7 @@ libnghttp2_asio_la_SOURCES = \ asio_server_response_impl.cc asio_server_response_impl.h \ asio_server_stream.cc asio_server_stream.h \ asio_server_serve_mux.cc asio_server_serve_mux.h \ + asio_server_request_handler.cc asio_server_request_handler.h \ asio_client_session.cc \ asio_client_session_impl.cc asio_client_session_impl.h \ asio_client_session_tcp_impl.cc asio_client_session_tcp_impl.h \ diff --git a/src/asio_server_request_handler.cc b/src/asio_server_request_handler.cc new file mode 100644 index 00000000..52e56e59 --- /dev/null +++ b/src/asio_server_request_handler.cc @@ -0,0 +1,77 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2015 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#include "asio_server_request_handler.h" + +#include "util.h" +#include "http2.h" + +namespace nghttp2 { +namespace asio_http2 { +namespace server { + +namespace { +std::string create_html(int status_code) { + std::string res; + res.reserve(512); + auto status = ::nghttp2::http2::get_status_string(status_code); + res += ""; + res += status; + res += "

"; + res += status; + res += "

"; + return res; +} +} // namespace + +request_cb redirect_handler(int status_code, std::string uri) { + return [status_code, uri](const request &req, const response &res) { + header_map h; + h.emplace("location", header_value{std::move(uri)}); + std::string html; + if (req.method() == "GET") { + html = create_html(status_code); + } + h.emplace("content-length", header_value{util::utos(html.size())}); + + res.write_head(status_code, std::move(h)); + res.end(std::move(html)); + }; +} + +request_cb status_handler(int status_code) { + return [status_code](const request &req, const response &res) { + auto html = create_html(status_code); + header_map h; + h.emplace("content-length", header_value{util::utos(html.size())}); + h.emplace("content-type", header_value{"text/html; charset=utf-8"}); + + res.write_head(status_code, std::move(h)); + res.end(std::move(html)); + }; +} + +} // namespace server +} // namespace asio_http2 +} // namespace nghttp2 diff --git a/src/asio_server_request_handler.h b/src/asio_server_request_handler.h new file mode 100644 index 00000000..5eefcfda --- /dev/null +++ b/src/asio_server_request_handler.h @@ -0,0 +1,32 @@ +/* + * nghttp2 - HTTP/2 C Library + * + * Copyright (c) 2015 Tatsuhiro Tsujikawa + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef ASIO_SERVER_REQUEST_HANDLER_H +#define ASIO_SERVER_REQUEST_HANDLER_H + +#include "nghttp2_config.h" + +#include + +#endif // ASIO_SERVER_REQUEST_HANDLER_H diff --git a/src/asio_server_serve_mux.cc b/src/asio_server_serve_mux.cc index 94a0a322..1959ea10 100644 --- a/src/asio_server_serve_mux.cc +++ b/src/asio_server_serve_mux.cc @@ -25,6 +25,7 @@ #include "asio_server_serve_mux.h" #include "asio_server_request_impl.h" +#include "asio_server_request_handler.h" #include "util.h" #include "http2.h" @@ -34,51 +35,6 @@ namespace asio_http2 { namespace server { -namespace { -std::string create_html(int status_code) { - std::string res; - res.reserve(512); - auto status = ::nghttp2::http2::get_status_string(status_code); - res += ""; - res += status; - res += "

"; - res += status; - res += "

"; - return res; -} -} // namespace - -namespace { -request_cb redirect_handler(int status_code, std::string uri) { - return [status_code, uri](const request &req, const response &res) { - header_map h; - h.emplace("location", header_value{std::move(uri)}); - std::string html; - if (req.method() == "GET") { - html = create_html(status_code); - } - h.emplace("content-length", header_value{util::utos(html.size())}); - - res.write_head(status_code, std::move(h)); - res.end(std::move(html)); - }; -} -} // namespace - -namespace { -request_cb status_handler(int status_code) { - return [status_code](const request &req, const response &res) { - auto html = create_html(status_code); - header_map h; - h.emplace("content-length", header_value{util::utos(html.size())}); - h.emplace("content-type", header_value{"text/html; charset=utf-8"}); - - res.write_head(status_code, std::move(h)); - res.end(std::move(html)); - }; -} -} // namespace - bool serve_mux::handle(std::string pattern, request_cb cb) { if (pattern.empty() || !cb) { return false; diff --git a/src/includes/nghttp2/asio_http2_server.h b/src/includes/nghttp2/asio_http2_server.h index 41721dd2..35bab796 100644 --- a/src/includes/nghttp2/asio_http2_server.h +++ b/src/includes/nghttp2/asio_http2_server.h @@ -146,6 +146,14 @@ private: std::unique_ptr impl_; }; +// Returns request handler to do redirect to |uri| using +// |status_code|. The |uri| appears in "location" header field as is. +request_cb redirect_handler(int status_code, std::string uri); + +// Returns request handler to reply with given |status_code| and HTML +// including message about status code. +request_cb status_handler(int status_code); + } // namespace server } // namespace asio_http2