diff --git a/examples/asio-sv.cc b/examples/asio-sv.cc index 06ce35ae..51f52c41 100644 --- a/examples/asio-sv.cc +++ b/examples/asio-sv.cc @@ -37,7 +37,7 @@ #include #include -#include +#include using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2::server; diff --git a/examples/asio-sv2.cc b/examples/asio-sv2.cc index aa246864..35269a82 100644 --- a/examples/asio-sv2.cc +++ b/examples/asio-sv2.cc @@ -40,7 +40,7 @@ #include #include -#include +#include using namespace nghttp2::asio_http2; using namespace nghttp2::asio_http2::server; diff --git a/src/asio_connection.h b/src/asio_connection.h index 03e8f43f..01643749 100644 --- a/src/asio_connection.h +++ b/src/asio_connection.h @@ -41,11 +41,11 @@ #include -#include #include #include -#include +#include + #include "asio_http2_handler.h" #include "util.h" diff --git a/src/asio_http2_handler.h b/src/asio_http2_handler.h index df61432f..9fe65dd3 100644 --- a/src/asio_http2_handler.h +++ b/src/asio_http2_handler.h @@ -31,12 +31,10 @@ #include #include #include + #include -#include -#include - -#include +#include namespace nghttp2 { namespace asio_http2 { diff --git a/src/asio_http2_impl.cc b/src/asio_http2_impl.cc index 4a823349..4d2c5ddf 100644 --- a/src/asio_http2_impl.cc +++ b/src/asio_http2_impl.cc @@ -24,12 +24,8 @@ */ #include "asio_http2_impl.h" -#include - #include -#include - #include "asio_server.h" #include "util.h" #include "ssl.h" diff --git a/src/asio_http2_impl.h b/src/asio_http2_impl.h index eb99f4a1..b41098ae 100644 --- a/src/asio_http2_impl.h +++ b/src/asio_http2_impl.h @@ -27,7 +27,7 @@ #include "nghttp2_config.h" -#include +#include namespace nghttp2 { diff --git a/src/asio_io_service_pool.h b/src/asio_io_service_pool.h index ac0a76c3..bbbae2a9 100644 --- a/src/asio_io_service_pool.h +++ b/src/asio_io_service_pool.h @@ -41,11 +41,11 @@ #include #include -#include + #include #include -#include +#include namespace nghttp2 { diff --git a/src/asio_server.h b/src/asio_server.h index ba109256..894b7f59 100644 --- a/src/asio_server.h +++ b/src/asio_server.h @@ -42,11 +42,10 @@ #include #include #include -#include -#include -#include -#include +#include + +#include #include "asio_connection.h" #include "asio_io_service_pool.h" diff --git a/src/includes/Makefile.am b/src/includes/Makefile.am index f662b4ca..d711dddb 100644 --- a/src/includes/Makefile.am +++ b/src/includes/Makefile.am @@ -20,4 +20,5 @@ # 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. -nobase_include_HEADERS = nghttp2/asio_http2.h nghttp2/asio_http2_client.h +nobase_include_HEADERS = nghttp2/asio_http2.h nghttp2/asio_http2_client.h \ + nghttp2/asio_http2_server.h diff --git a/src/includes/nghttp2/asio_http2.h b/src/includes/nghttp2/asio_http2.h index 778efec9..e38e8100 100644 --- a/src/includes/nghttp2/asio_http2.h +++ b/src/includes/nghttp2/asio_http2.h @@ -34,6 +34,7 @@ #include #include +#include #include @@ -100,128 +101,6 @@ typedef std::function close_cb; typedef std::function(uint8_t *buf, std::size_t len)> read_cb; -namespace server { - -class request_impl; -class response_impl; - -class request { -public: - // Application must not call this directly. - request(); - - // Returns request headers. The pusedo headers, which start with - // colon (;), are exluced from this list. - const std::vector
&headers() const; - - // Returns method (e.g., GET). - const std::string &method() const; - - // Returns scheme (e.g., https). - const std::string &scheme() const; - - // Returns authority (e.g., example.org). This could be empty - // string. In this case, check host(). - - const std::string &authority() const; - - // Returns host (e.g., example.org). If host header field is not - // present, this value is copied from authority(). - const std::string &host() const; - - // Returns path (e.g., /index.html). - const std::string &path() const; - - // Sets callback when chunk of request body is received. - void on_data(data_cb cb) const; - - // Sets callback when request was completed. - void on_end(void_cb cb) const; - - // Pushes resource denoted by |path| using |method|. The additional - // headers can be given in |headers|. request_cb will be called for - // pushed resource later on. This function returns true if it - // succeeds, or false. - bool push(std::string method, std::string path, - std::vector
headers = {}) const; - - // Returns true if this is pushed request. - bool pushed() const; - - // Application must not call this directly. - request_impl &impl() const; - -private: - std::unique_ptr impl_; -}; - -class response { -public: - // Application must not call this directly. - response(); - - // Write response header using |status_code| (e.g., 200) and - // additional headers in |headers|. - void write_head(unsigned int status_code, - std::vector
headers = {}) const; - - // Sends |data| as request body. No further call of end() is - // allowed. - void end(std::string data = "") const; - - // Sets callback |cb| as a generator of the response body. No - // further call of end() is allowed. - void end(read_cb cb) const; - - // Resumes deferred response. - void resume() const; - - // Returns status code. - unsigned int status_code() const; - - // Returns true if response has been started. - bool started() const; - - // Application must not call this directly. - response_impl &impl() const; - -private: - std::unique_ptr impl_; -}; - -// This is so called request callback. Called every time request is -// received. -typedef std::function request_cb; - -class http2_impl; - -class http2 { -public: - http2(); - ~http2(); - - // Starts listening connection on given address and port. The - // incoming requests are handled by given callback |cb|. - void listen(const std::string &address, uint16_t port, request_cb cb); - - // Sets number of native threads to handle incoming HTTP request. - // It defaults to 1. - void num_threads(size_t num_threads); - - // Sets TLS private key file and certificate file. Both files must - // be in PEM format. - void tls(std::string private_key_file, std::string certificate_file); - - // Sets the maximum length to which the queue of pending - // connections. - void backlog(int backlog); - -private: - std::unique_ptr impl_; -}; - -} // namespace server - // Convenient function to create function to read file denoted by // |path|. This can be passed to response::end(). read_cb file_reader(const std::string &path); diff --git a/src/includes/nghttp2/asio_http2_client.h b/src/includes/nghttp2/asio_http2_client.h index 2d391975..02bae8c6 100644 --- a/src/includes/nghttp2/asio_http2_client.h +++ b/src/includes/nghttp2/asio_http2_client.h @@ -27,8 +27,6 @@ #include -#include - namespace nghttp2 { namespace asio_http2 { diff --git a/src/includes/nghttp2/asio_http2_server.h b/src/includes/nghttp2/asio_http2_server.h new file mode 100644 index 00000000..f40d4919 --- /dev/null +++ b/src/includes/nghttp2/asio_http2_server.h @@ -0,0 +1,160 @@ +/* + * 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_HTTP2_SERVER_H +#define ASIO_HTTP2_SERVER_H + +#include + +namespace nghttp2 { + +namespace asio_http2 { + +namespace server { + +class request_impl; +class response_impl; + +class request { +public: + // Application must not call this directly. + request(); + + // Returns request headers. The pusedo headers, which start with + // colon (;), are exluced from this list. + const std::vector
&headers() const; + + // Returns method (e.g., GET). + const std::string &method() const; + + // Returns scheme (e.g., https). + const std::string &scheme() const; + + // Returns authority (e.g., example.org). This could be empty + // string. In this case, check host(). + + const std::string &authority() const; + + // Returns host (e.g., example.org). If host header field is not + // present, this value is copied from authority(). + const std::string &host() const; + + // Returns path (e.g., /index.html). + const std::string &path() const; + + // Sets callback when chunk of request body is received. + void on_data(data_cb cb) const; + + // Sets callback when request was completed. + void on_end(void_cb cb) const; + + // Pushes resource denoted by |path| using |method|. The additional + // headers can be given in |headers|. request_cb will be called for + // pushed resource later on. This function returns true if it + // succeeds, or false. + bool push(std::string method, std::string path, + std::vector
headers = {}) const; + + // Returns true if this is pushed request. + bool pushed() const; + + // Application must not call this directly. + request_impl &impl() const; + +private: + std::unique_ptr impl_; +}; + +class response { +public: + // Application must not call this directly. + response(); + + // Write response header using |status_code| (e.g., 200) and + // additional headers in |headers|. + void write_head(unsigned int status_code, + std::vector
headers = {}) const; + + // Sends |data| as request body. No further call of end() is + // allowed. + void end(std::string data = "") const; + + // Sets callback |cb| as a generator of the response body. No + // further call of end() is allowed. + void end(read_cb cb) const; + + // Resumes deferred response. + void resume() const; + + // Returns status code. + unsigned int status_code() const; + + // Returns true if response has been started. + bool started() const; + + // Application must not call this directly. + response_impl &impl() const; + +private: + std::unique_ptr impl_; +}; + +// This is so called request callback. Called every time request is +// received. +typedef std::function request_cb; + +class http2_impl; + +class http2 { +public: + http2(); + ~http2(); + + // Starts listening connection on given address and port. The + // incoming requests are handled by given callback |cb|. + void listen(const std::string &address, uint16_t port, request_cb cb); + + // Sets number of native threads to handle incoming HTTP request. + // It defaults to 1. + void num_threads(size_t num_threads); + + // Sets TLS private key file and certificate file. Both files must + // be in PEM format. + void tls(std::string private_key_file, std::string certificate_file); + + // Sets the maximum length to which the queue of pending + // connections. + void backlog(int backlog); + +private: + std::unique_ptr impl_; +}; + +} // namespace server + +} // namespace asio_http2 + +} // namespace nghttp2 + +#endif // ASIO_HTTP2_SERVER_H