Some fixes to nghttp2 to provide basic functionality. Added the missing main header. Removed the asio headers which require Boost libraries. Moved http_parser into the expected location. (#2068)

This commit is contained in:
lbernstone 2018-11-19 08:09:38 -08:00 committed by Me No Dev
parent 0d564d7b1d
commit c700e5694f
5 changed files with 5296 additions and 634 deletions

View File

@ -1,151 +0,0 @@
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2014 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_H
#define ASIO_HTTP2_H
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include <functional>
#include <map>
#include <boost/system/error_code.hpp>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <nghttp2/nghttp2.h>
namespace nghttp2 {
namespace asio_http2 {
struct header_value {
// header field value
std::string value;
// true if the header field value is sensitive information, such as
// authorization information or short length secret cookies. If
// true, those header fields are not indexed by HPACK (but still
// huffman-encoded), which results in lesser compression.
bool sensitive;
};
// header fields. The header field name must be lower-cased.
using header_map = std::multimap<std::string, header_value>;
const boost::system::error_category &nghttp2_category() noexcept;
struct uri_ref {
std::string scheme;
std::string host;
// form after percent-encoding decoded
std::string path;
// original path, percent-encoded
std::string raw_path;
// original query, percent-encoded
std::string raw_query;
std::string fragment;
};
// Callback function when data is arrived. EOF is indicated by
// passing 0 to the second parameter.
typedef std::function<void(const uint8_t *, std::size_t)> data_cb;
typedef std::function<void(void)> void_cb;
typedef std::function<void(const boost::system::error_code &ec)> error_cb;
// Callback function when request and response are finished. The
// parameter indicates the cause of closure.
typedef std::function<void(uint32_t)> close_cb;
// Callback function to generate response body. This function has the
// same semantics with nghttp2_data_source_read_callback. Just source
// and user_data parameters are removed.
//
// Basically, write at most |len| bytes to |data| and returns the
// number of bytes written. If there is no data left to send, set
// NGHTTP2_DATA_FLAG_EOF to *data_flags (e.g., *data_flags |=
// NGHTTP2_DATA_FLAG_EOF). If there is still data to send but they
// are not available right now, return NGHTTP2_ERR_DEFERRED. In case
// of the error and request/response must be closed, return
// NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE.
typedef std::function<ssize_t(uint8_t *buf, std::size_t len,
uint32_t *data_flags)>
generator_cb;
// Convenient function to create function to read file denoted by
// |path|. This can be passed to response::end().
generator_cb file_generator(const std::string &path);
// Like file_generator(const std::string&), but it takes opened file
// descriptor. The passed descriptor will be closed when returned
// function object is destroyed.
generator_cb file_generator_from_fd(int fd);
// Validates path so that it does not contain directory traversal
// vector. Returns true if path is safe. The |path| must start with
// "/" otherwise returns false. This function should be called after
// percent-decode was performed.
bool check_path(const std::string &path);
// Performs percent-decode against string |s|.
std::string percent_decode(const std::string &s);
// Returns HTTP date representation of current posix time |t|.
std::string http_date(int64_t t);
// Parses |uri| and extract scheme, host and service. The service is
// port component of URI (e.g., "8443") if available, otherwise it is
// scheme (e.g., "https").
boost::system::error_code host_service_from_uri(boost::system::error_code &ec,
std::string &scheme,
std::string &host,
std::string &service,
const std::string &uri);
enum nghttp2_asio_error {
NGHTTP2_ASIO_ERR_NO_ERROR = 0,
NGHTTP2_ASIO_ERR_TLS_NO_APP_PROTO_NEGOTIATED = 1,
};
} // namespace asio_http2
} // namespace nghttp2
namespace boost {
namespace system {
template <> struct is_error_code_enum<nghttp2_error> {
BOOST_STATIC_CONSTANT(bool, value = true);
};
template <> struct is_error_code_enum<nghttp2::asio_http2::nghttp2_asio_error> {
BOOST_STATIC_CONSTANT(bool, value = true);
};
} // namespace system
} // namespace boost
#endif // ASIO_HTTP2_H

View File

@ -1,241 +0,0 @@
/*
* 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_CLIENT_H
#define ASIO_HTTP2_CLIENT_H
#include <nghttp2/asio_http2.h>
namespace nghttp2 {
namespace asio_http2 {
namespace client {
class response_impl;
class response {
public:
// Application must not call this directly.
response();
~response();
// Sets callback which is invoked when chunk of response body is
// received.
void on_data(data_cb cb) const;
// Returns status code.
int status_code() const;
// Returns content-length. -1 if it is unknown.
int64_t content_length() const;
// Returns the response header fields. The pusedo header fields,
// which start with colon (:), are exluced from this list.
const header_map &header() const;
// Application must not call this directly.
response_impl &impl() const;
private:
std::unique_ptr<response_impl> impl_;
};
class request;
using response_cb = std::function<void(const response &)>;
using request_cb = std::function<void(const request &)>;
using connect_cb =
std::function<void(boost::asio::ip::tcp::resolver::iterator)>;
class request_impl;
class request {
public:
// Application must not call this directly.
request();
~request();
// Sets callback which is invoked when response header is received.
void on_response(response_cb cb) const;
// Sets callback which is invoked when push request header is
// received.
void on_push(request_cb cb) const;
// Sets callback which is invoked when this request and response are
// finished. After the invocation of this callback, the application
// must not access request and response object.
void on_close(close_cb cb) const;
// Write trailer part. This must be called after setting both
// NGHTTP2_DATA_FLAG_EOF and NGHTTP2_DATA_FLAG_NO_END_STREAM set in
// *data_flag parameter in generator_cb passed to session::submit()
// function.
void write_trailer(header_map h) const;
// Cancels this request and response with given error code.
void cancel(uint32_t error_code = NGHTTP2_INTERNAL_ERROR) const;
// Resumes deferred uploading.
void resume() const;
// Returns method (e.g., GET).
const std::string &method() const;
// Returns request URI, split into components.
const uri_ref &uri() const;
// Returns request header fields. The pusedo header fields, which
// start with colon (:), are exluced from this list.
const header_map &header() const;
// Application must not call this directly.
request_impl &impl() const;
private:
std::unique_ptr<request_impl> impl_;
};
// Wrapper around an nghttp2_priority_spec.
class priority_spec {
public:
// The default ctor is used only by sentinel values.
priority_spec() = default;
// Create a priority spec with the given priority settings.
explicit priority_spec(const int32_t stream_id, const int32_t weight,
const bool exclusive = false);
// Return a pointer to a valid nghttp2 priority spec, or null.
const nghttp2_priority_spec *get() const;
// Indicates whether or not this spec is valid (i.e. was constructed with
// values).
const bool valid() const;
private:
nghttp2_priority_spec spec_;
bool valid_ = false;
};
class session_impl;
class session {
public:
// Starts HTTP/2 session by connecting to |host| and |service|
// (e.g., "80") using clear text TCP connection with connect timeout
// 60 seconds.
session(boost::asio::io_service &io_service, const std::string &host,
const std::string &service);
// Starts HTTP/2 session by connecting to |host| and |service|
// (e.g., "80") using clear text TCP connection with given connect
// timeout.
session(boost::asio::io_service &io_service, const std::string &host,
const std::string &service,
const boost::posix_time::time_duration &connect_timeout);
// Starts HTTP/2 session by connecting to |host| and |service|
// (e.g., "443") using encrypted SSL/TLS connection with connect
// timeout 60 seconds.
session(boost::asio::io_service &io_service,
boost::asio::ssl::context &tls_context, const std::string &host,
const std::string &service);
// Starts HTTP/2 session by connecting to |host| and |service|
// (e.g., "443") using encrypted SSL/TLS connection with given
// connect timeout.
session(boost::asio::io_service &io_service,
boost::asio::ssl::context &tls_context, const std::string &host,
const std::string &service,
const boost::posix_time::time_duration &connect_timeout);
~session();
session(session &&other) noexcept;
session &operator=(session &&other) noexcept;
// Sets callback which is invoked after connection is established.
void on_connect(connect_cb cb) const;
// Sets callback which is invoked there is connection level error
// and session is terminated.
void on_error(error_cb cb) const;
// Sets read timeout, which defaults to 60 seconds.
void read_timeout(const boost::posix_time::time_duration &t);
// Shutdowns connection.
void shutdown() const;
// Returns underlying io_service object.
boost::asio::io_service &io_service() const;
// Submits request to server using |method| (e.g., "GET"), |uri|
// (e.g., "http://localhost/") and optionally additional header
// fields. This function returns pointer to request object if it
// succeeds, or nullptr and |ec| contains error message.
const request *submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri,
header_map h = header_map{},
priority_spec prio = priority_spec()) const;
// Submits request to server using |method| (e.g., "GET"), |uri|
// (e.g., "http://localhost/") and optionally additional header
// fields. The |data| is request body. This function returns
// pointer to request object if it succeeds, or nullptr and |ec|
// contains error message.
const request *submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri,
std::string data, header_map h = header_map{},
priority_spec prio = priority_spec()) const;
// Submits request to server using |method| (e.g., "GET"), |uri|
// (e.g., "http://localhost/") and optionally additional header
// fields. The |cb| is used to generate request body. This
// function returns pointer to request object if it succeeds, or
// nullptr and |ec| contains error message.
const request *submit(boost::system::error_code &ec,
const std::string &method, const std::string &uri,
generator_cb cb, header_map h = header_map{},
priority_spec prio = priority_spec()) const;
private:
std::shared_ptr<session_impl> impl_;
};
// configure |tls_ctx| for client use. Currently, we just set NPN
// callback for HTTP/2.
boost::system::error_code
configure_tls_context(boost::system::error_code &ec,
boost::asio::ssl::context &tls_ctx);
} // namespace client
} // namespace asio_http2
} // namespace nghttp2
#endif // ASIO_HTTP2_CLIENT_H

View File

@ -1,242 +0,0 @@
/*
* 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 <nghttp2/asio_http2.h>
namespace nghttp2 {
namespace asio_http2 {
namespace server {
class request_impl;
class response_impl;
class request {
public:
// Application must not call this directly.
request();
~request();
// Returns request header fields. The pusedo header fields, which
// start with colon (:), are exluced from this list.
const header_map &header() const;
// Returns method (e.g., GET).
const std::string &method() const;
// Returns request URI, split into components.
const uri_ref &uri() const;
// Sets callback which is invoked when chunk of request body is
// received.
void on_data(data_cb cb) const;
// Application must not call this directly.
request_impl &impl() const;
// Returns the remote endpoint of the request
const boost::asio::ip::tcp::endpoint &remote_endpoint() const;
private:
std::unique_ptr<request_impl> impl_;
};
class response {
public:
// Application must not call this directly.
response();
~response();
// Write response header using |status_code| (e.g., 200) and
// additional header fields in |h|.
void write_head(unsigned int status_code, header_map h = header_map{}) const;
// Sends |data| as request body. No further call of end() is
// allowed.
void end(std::string data = "") const;
// Sets callback as a generator of the response body. No further
// call of end() is allowed.
void end(generator_cb cb) const;
// Write trailer part. This must be called after setting both
// NGHTTP2_DATA_FLAG_EOF and NGHTTP2_DATA_FLAG_NO_END_STREAM set in
// *data_flag parameter in generator_cb passed to end() function.
void write_trailer(header_map h) const;
// Sets callback which is invoked when this request and response are
// finished. After the invocation of this callback, the application
// must not access request and response object.
void on_close(close_cb cb) const;
// Cancels this request and response with given error code.
void cancel(uint32_t error_code = NGHTTP2_INTERNAL_ERROR) const;
// Resumes deferred response.
void resume() const;
// Pushes resource denoted by |raw_path_query| using |method|. The
// additional header fields can be given in |h|. This function
// returns pointer to response object for promised stream, otherwise
// nullptr and error code is filled in |ec|. Be aware that the
// header field name given in |h| must be lower-cased.
const response *push(boost::system::error_code &ec, std::string method,
std::string raw_path_query,
header_map h = header_map{}) const;
// Returns status code.
unsigned int status_code() const;
// Returns boost::asio::io_service this response is running on.
boost::asio::io_service &io_service() const;
// Application must not call this directly.
response_impl &impl() const;
private:
std::unique_ptr<response_impl> impl_;
};
// This is so called request callback. Called every time request is
// received. The life time of |request| and |response| objects end
// when callback set by response::on_close() is called. After that,
// the application must not access to those objects.
typedef std::function<void(const request &, const response &)> request_cb;
class http2_impl;
class http2 {
public:
http2();
~http2();
http2(http2 &&other) noexcept;
http2 &operator=(http2 &&other) noexcept;
// Starts listening connection on given address and port and serves
// incoming requests in cleartext TCP connection. If |asynchronous|
// is false, this function blocks forever unless there is an error.
// If it is true, after server has started, this function returns
// immediately, and the caller should call join() to shutdown server
// gracefully.
boost::system::error_code listen_and_serve(boost::system::error_code &ec,
const std::string &address,
const std::string &port,
bool asynchronous = false);
// Starts listening connection on given address and port and serves
// incoming requests in SSL/TLS encrypted connection. For
// |asynchronous| parameter, see cleartext version
// |listen_and_serve|.
boost::system::error_code
listen_and_serve(boost::system::error_code &ec,
boost::asio::ssl::context &tls_context,
const std::string &address, const std::string &port,
bool asynchronous = false);
// Registers request handler |cb| with path pattern |pattern|. This
// function will fail and returns false if same pattern has been
// already registered or |pattern| is empty string. Otherwise
// returns true. The pattern match rule is the same as
// net/http/ServeMux in golang. Quoted from golang manual
// (http://golang.org/pkg/net/http/#ServeMux):
//
// Patterns name fixed, rooted paths, like "/favicon.ico", or
// rooted subtrees, like "/images/" (note the trailing
// slash). Longer patterns take precedence over shorter ones, so
// that if there are handlers registered for both "/images/" and
// "/images/thumbnails/", the latter handler will be called for
// paths beginning "/images/thumbnails/" and the former will
// receive requests for any other paths in the "/images/" subtree.
//
// Note that since a pattern ending in a slash names a rooted
// subtree, the pattern "/" matches all paths not matched by other
// registered patterns, not just the URL with Path == "/".
//
// Patterns may optionally begin with a host name, restricting
// matches to URLs on that host only. Host-specific patterns take
// precedence over general patterns, so that a handler might
// register for the two patterns "/codesearch" and
// "codesearch.google.com/" without also taking over requests for
// "http://www.google.com/".
//
// Just like ServeMux in golang, URL request path is sanitized and
// if they contains . or .. elements, they are redirected to an
// equivalent .- and ..-free URL.
bool handle(std::string pattern, 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 the maximum length to which the queue of pending
// connections.
void backlog(int backlog);
// Sets TLS handshake timeout, which defaults to 60 seconds.
void tls_handshake_timeout(const boost::posix_time::time_duration &t);
// Sets read timeout, which defaults to 60 seconds.
void read_timeout(const boost::posix_time::time_duration &t);
// Gracefully stop http2 server
void stop();
// Join on http2 server and wait for it to fully stop
void join();
// Get access to the io_service objects.
const std::vector<std::shared_ptr<boost::asio::io_service>> &
io_services() const;
private:
std::unique_ptr<http2_impl> impl_;
};
// Configures |tls_context| for server use. This function sets couple
// of OpenSSL options (disables SSLv2 and SSLv3 and compression) and
// enables ECDHE ciphers. NPN callback is also configured.
boost::system::error_code
configure_tls_context_easy(boost::system::error_code &ec,
boost::asio::ssl::context &tls_context);
// 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
} // namespace nghttp2
#endif // ASIO_HTTP2_SERVER_H

File diff suppressed because it is too large Load Diff