2014-09-21 11:28:06 +02:00
|
|
|
/*
|
|
|
|
* 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>
|
2015-03-01 02:02:49 +01:00
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include <boost/system/error_code.hpp>
|
|
|
|
#include <boost/asio.hpp>
|
|
|
|
#include <boost/asio/ssl.hpp>
|
|
|
|
|
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
namespace system {
|
|
|
|
|
|
|
|
template <> struct is_error_code_enum<nghttp2_error> {
|
|
|
|
BOOST_STATIC_CONSTANT(bool, value = true);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace system
|
|
|
|
} // namespace boost
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
|
|
|
namespace asio_http2 {
|
|
|
|
|
|
|
|
struct header {
|
2015-03-01 02:02:49 +01:00
|
|
|
header() : sensitive(false) {}
|
|
|
|
header(std::string name, std::string value, bool sensitive = false)
|
|
|
|
: name(std::move(name)), value(std::move(value)), sensitive(sensitive) {}
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
std::string name;
|
|
|
|
std::string value;
|
2015-03-01 02:02:49 +01:00
|
|
|
bool sensitive;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct header_value {
|
|
|
|
header_value(std::string value, bool sensitive = false)
|
|
|
|
: value(std::move(value)), sensitive(sensitive) {}
|
|
|
|
|
|
|
|
std::string value;
|
|
|
|
bool sensitive;
|
|
|
|
};
|
|
|
|
|
|
|
|
using header_map = std::multimap<std::string, header_value>;
|
|
|
|
|
|
|
|
const boost::system::error_category &nghttp2_category() noexcept;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
typedef std::function<void(const uint8_t *, std::size_t)> data_cb;
|
2014-09-21 11:28:06 +02:00
|
|
|
typedef std::function<void(void)> void_cb;
|
2015-03-03 16:23:17 +01:00
|
|
|
typedef std::function<void(const boost::system::error_code &ec)> error_cb;
|
2015-03-01 02:02:49 +01:00
|
|
|
typedef std::function<void(uint32_t)> close_cb;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
// Callback function to generate response body. The implementation of
|
|
|
|
// this callback must fill at most |len| bytes data to |buf|. The
|
|
|
|
// return value is pair of written bytes and bool value indicating
|
|
|
|
// that this is the end of the body. If the end of the body was
|
|
|
|
// reached, return true. If there is error and application wants to
|
2014-09-28 09:17:43 +02:00
|
|
|
// terminate stream, return std::make_pair(-1, false). Returning
|
|
|
|
// std::make_pair(0, false) tells the library that don't call this
|
|
|
|
// callback until application calls response::resume(). This is
|
|
|
|
// useful when there is no data to send at the moment but there will
|
|
|
|
// be more to come in near future.
|
2014-11-27 15:39:04 +01:00
|
|
|
typedef std::function<std::pair<ssize_t, bool>(uint8_t *buf, std::size_t len)>
|
|
|
|
read_cb;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2014-09-28 09:17:43 +02:00
|
|
|
class channel_impl;
|
|
|
|
|
|
|
|
class channel {
|
|
|
|
public:
|
|
|
|
// Application must not call this directly.
|
|
|
|
channel();
|
|
|
|
|
|
|
|
// Schedules the execution of callback |cb| in the same thread where
|
|
|
|
// request callback is called. Therefore, it is same to use request
|
|
|
|
// or response object in |cb|. The callbacks are executed in the
|
|
|
|
// same order they are posted though same channel object if they are
|
|
|
|
// posted from the same thread.
|
|
|
|
void post(void_cb cb);
|
|
|
|
|
|
|
|
// Application must not call this directly.
|
2014-11-27 15:39:04 +01:00
|
|
|
channel_impl &impl();
|
|
|
|
|
2014-09-28 09:17:43 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<channel_impl> impl_;
|
|
|
|
};
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
typedef std::function<void(channel &)> thread_cb;
|
2014-09-28 09:17:43 +02:00
|
|
|
|
2014-09-28 09:54:00 +02:00
|
|
|
namespace server {
|
|
|
|
|
|
|
|
class request_impl;
|
|
|
|
class response_impl;
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
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.
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::vector<header> &headers() const;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
// Returns method (e.g., GET).
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &method() const;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
// Returns scheme (e.g., https).
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &scheme() const;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
// Returns authority (e.g., example.org). This could be empty
|
|
|
|
// string. In this case, check host().
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &authority() const;
|
2015-03-01 02:02:49 +01:00
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
// Returns host (e.g., example.org). If host header field is not
|
|
|
|
// present, this value is copied from authority().
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &host() const;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
// Returns path (e.g., /index.html).
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &path() const;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
// Sets callback when chunk of request body is received.
|
|
|
|
void on_data(data_cb cb);
|
|
|
|
|
|
|
|
// Sets callback when request was completed.
|
|
|
|
void on_end(void_cb cb);
|
|
|
|
|
|
|
|
// 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<header> headers = {});
|
|
|
|
|
|
|
|
// Returns true if this is pushed request.
|
|
|
|
bool pushed() const;
|
|
|
|
|
2014-09-24 17:15:52 +02:00
|
|
|
// Returns true if stream has been closed.
|
|
|
|
bool closed() const;
|
|
|
|
|
2014-09-28 09:17:43 +02:00
|
|
|
// Runs function |start| in one of background threads. Returns true
|
|
|
|
// if scheduling task was done successfully.
|
|
|
|
//
|
|
|
|
// Since |start| is called in different thread, calling any method
|
|
|
|
// of request or response object in the callback may cause undefined
|
|
|
|
// behavior. To safely use them, use channel::post(). A callback
|
|
|
|
// passed to channel::post() is executed in the same thread where
|
|
|
|
// request callback is called, so it is safe to use request or
|
|
|
|
// response object. Example::
|
|
|
|
bool run_task(thread_cb start);
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
// Application must not call this directly.
|
2014-11-27 15:39:04 +01:00
|
|
|
request_impl &impl();
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<request_impl> 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<header> headers = {});
|
|
|
|
|
|
|
|
// Sends |data| as request body. No further call of end() is
|
|
|
|
// allowed.
|
|
|
|
void end(std::string data = "");
|
|
|
|
|
|
|
|
// Sets callback |cb| as a generator of the response body. No
|
|
|
|
// further call of end() is allowed.
|
|
|
|
void end(read_cb cb);
|
|
|
|
|
2014-09-28 09:17:43 +02:00
|
|
|
// Resumes deferred response.
|
2014-09-21 11:28:06 +02:00
|
|
|
void resume();
|
|
|
|
|
|
|
|
// Returns status code.
|
|
|
|
unsigned int status_code() const;
|
|
|
|
|
|
|
|
// Returns true if response has been started.
|
|
|
|
bool started() const;
|
|
|
|
|
|
|
|
// Application must not call this directly.
|
2014-11-27 15:39:04 +01:00
|
|
|
response_impl &impl();
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<response_impl> impl_;
|
|
|
|
};
|
|
|
|
|
2014-09-28 09:17:43 +02:00
|
|
|
// This is so called request callback. Called every time request is
|
|
|
|
// received.
|
2014-11-27 15:39:04 +01:00
|
|
|
typedef std::function<void(const std::shared_ptr<request> &,
|
|
|
|
const std::shared_ptr<response> &)> request_cb;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
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|.
|
2014-11-27 15:39:04 +01:00
|
|
|
void listen(const std::string &address, uint16_t port, request_cb cb);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2014-09-28 09:17:43 +02:00
|
|
|
// Sets number of native threads to handle incoming HTTP request.
|
|
|
|
// It defaults to 1.
|
2014-09-21 11:28:06 +02:00
|
|
|
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);
|
2014-09-28 09:17:43 +02:00
|
|
|
|
|
|
|
// Sets number of background threads to run concurrent tasks (see
|
|
|
|
// request::run_task()). It defaults to 1. This is not the number
|
|
|
|
// of thread to handle incoming HTTP request. For this purpose, see
|
|
|
|
// num_threads().
|
|
|
|
void num_concurrent_tasks(size_t num_concurrent_tasks);
|
2014-11-10 15:26:01 +01:00
|
|
|
|
|
|
|
// Sets the maximum length to which the queue of pending
|
|
|
|
// connections.
|
|
|
|
void backlog(int backlog);
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<http2_impl> impl_;
|
|
|
|
};
|
|
|
|
|
2014-09-28 09:54:00 +02:00
|
|
|
} // namespace server
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
// Convenient function to create function to read file denoted by
|
|
|
|
// |path|. This can be passed to response::end().
|
2014-11-27 15:39:04 +01:00
|
|
|
read_cb file_reader(const std::string &path);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2014-09-24 16:05:13 +02:00
|
|
|
// Like file_reader(const std::string&), but it takes opened file
|
|
|
|
// descriptor. The passed descriptor will be closed when returned
|
|
|
|
// function object is destroyed.
|
|
|
|
read_cb file_reader_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.
|
2014-11-27 15:39:04 +01:00
|
|
|
bool check_path(const std::string &path);
|
2014-09-24 16:05:13 +02:00
|
|
|
|
|
|
|
// Performs percent-decode against string |s|.
|
2014-11-27 15:39:04 +01:00
|
|
|
std::string percent_decode(const std::string &s);
|
2014-09-24 16:05:13 +02:00
|
|
|
|
|
|
|
// Returns HTTP date representation of current posix time |t|.
|
2014-10-27 17:01:48 +01:00
|
|
|
std::string http_date(int64_t t);
|
2014-09-24 16:05:13 +02:00
|
|
|
|
2015-03-01 02:02:49 +01:00
|
|
|
namespace client {
|
|
|
|
|
|
|
|
class response_impl;
|
|
|
|
|
|
|
|
class response {
|
|
|
|
public:
|
|
|
|
response();
|
|
|
|
~response();
|
|
|
|
|
2015-03-03 16:20:09 +01:00
|
|
|
void on_data(data_cb cb) const;
|
2015-03-01 02:02:49 +01:00
|
|
|
|
|
|
|
int status_code() const;
|
|
|
|
|
|
|
|
int64_t content_length() const;
|
|
|
|
|
2015-03-03 15:37:08 +01:00
|
|
|
const header_map &header() const;
|
2015-03-01 02:02:49 +01:00
|
|
|
|
|
|
|
response_impl &impl();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<response_impl> impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class request;
|
|
|
|
|
2015-03-03 16:20:09 +01:00
|
|
|
using response_cb = std::function<void(const response &)>;
|
|
|
|
using request_cb = std::function<void(const request &)>;
|
2015-03-01 02:02:49 +01:00
|
|
|
|
|
|
|
class request_impl;
|
|
|
|
|
|
|
|
class request {
|
|
|
|
public:
|
|
|
|
request();
|
|
|
|
~request();
|
|
|
|
|
2015-03-03 16:20:09 +01:00
|
|
|
void on_response(response_cb cb) const;
|
|
|
|
void on_push(request_cb cb) const;
|
|
|
|
void on_close(close_cb cb) const;
|
2015-03-01 02:02:49 +01:00
|
|
|
|
2015-03-03 16:20:09 +01:00
|
|
|
void cancel() const;
|
2015-03-01 02:02:49 +01:00
|
|
|
|
|
|
|
const std::string &method() const;
|
|
|
|
const std::string &scheme() const;
|
|
|
|
const std::string &path() const;
|
|
|
|
const std::string &authority() const;
|
|
|
|
const std::string &host() const;
|
|
|
|
|
2015-03-03 15:37:08 +01:00
|
|
|
const header_map &header() const;
|
2015-03-01 02:02:49 +01:00
|
|
|
|
|
|
|
request_impl &impl();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<request_impl> impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class session_impl;
|
|
|
|
|
|
|
|
class session {
|
|
|
|
public:
|
2015-03-03 16:08:47 +01:00
|
|
|
session(boost::asio::io_service &io_service, const std::string &host,
|
|
|
|
const std::string &service);
|
2015-03-01 02:02:49 +01:00
|
|
|
session(boost::asio::io_service &io_service,
|
2015-03-03 16:08:47 +01:00
|
|
|
boost::asio::ssl::context &tls_context, const std::string &host,
|
|
|
|
const std::string &service);
|
2015-03-01 02:02:49 +01:00
|
|
|
~session();
|
|
|
|
|
|
|
|
void on_connect(void_cb cb);
|
|
|
|
void on_error(error_cb cb);
|
|
|
|
|
|
|
|
void shutdown();
|
|
|
|
|
2015-03-03 16:08:47 +01:00
|
|
|
boost::asio::io_service &io_service();
|
|
|
|
|
2015-03-03 16:20:09 +01:00
|
|
|
const request *submit(boost::system::error_code &ec,
|
|
|
|
const std::string &method, const std::string &uri,
|
|
|
|
header_map h = {});
|
|
|
|
const request *submit(boost::system::error_code &ec,
|
|
|
|
const std::string &method, const std::string &uri,
|
|
|
|
std::string data, header_map h = {});
|
|
|
|
const request *submit(boost::system::error_code &ec,
|
|
|
|
const std::string &method, const std::string &uri,
|
|
|
|
read_cb cb, header_map h = {});
|
2015-03-01 02:02:49 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<session_impl> impl_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void configure_tls_context(boost::asio::ssl::context &tls_ctx);
|
|
|
|
|
|
|
|
} // namespace client
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
} // namespace asio_http2
|
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
#endif // ASIO_HTTP2_H
|