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.
|
|
|
|
*/
|
2015-03-05 13:42:48 +01:00
|
|
|
#ifndef ASIO_SERVER_HTTP2_HANDLER_H
|
|
|
|
#define ASIO_SERVER_HTTP2_HANDLER_H
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
2015-03-03 18:43:13 +01:00
|
|
|
#include <boost/array.hpp>
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-03 18:43:13 +01:00
|
|
|
#include <nghttp2/asio_http2_server.h>
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
namespace asio_http2 {
|
|
|
|
namespace server {
|
|
|
|
|
|
|
|
class http2_handler;
|
2015-03-05 13:03:03 +01:00
|
|
|
class stream;
|
2015-03-04 17:52:12 +01:00
|
|
|
class serve_mux;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
struct callback_guard {
|
2014-11-27 15:39:04 +01:00
|
|
|
callback_guard(http2_handler &h);
|
2014-09-21 11:28:06 +02:00
|
|
|
~callback_guard();
|
2014-11-27 15:39:04 +01:00
|
|
|
http2_handler &handler;
|
2014-09-21 11:28:06 +02:00
|
|
|
};
|
|
|
|
|
2015-03-05 13:42:48 +01:00
|
|
|
using connection_write = std::function<void(void)>;
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
class http2_handler : public std::enable_shared_from_this<http2_handler> {
|
|
|
|
public:
|
2015-03-03 17:46:59 +01:00
|
|
|
http2_handler(boost::asio::io_service &io_service, connection_write writefun,
|
2015-03-04 17:52:12 +01:00
|
|
|
serve_mux &mux);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
~http2_handler();
|
|
|
|
|
|
|
|
int start();
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
stream *create_stream(int32_t stream_id);
|
2014-09-21 11:28:06 +02:00
|
|
|
void close_stream(int32_t stream_id);
|
2015-03-05 13:03:03 +01:00
|
|
|
stream *find_stream(int32_t stream_id);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
void call_on_request(stream &s);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
bool should_stop() const;
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
int start_response(stream &s);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-07 11:26:42 +01:00
|
|
|
int submit_trailer(stream &s, header_map h);
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
void stream_error(int32_t stream_id, uint32_t error_code);
|
|
|
|
|
|
|
|
void initiate_write();
|
|
|
|
|
|
|
|
void enter_callback();
|
|
|
|
void leave_callback();
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
void resume(stream &s);
|
2014-09-28 09:17:43 +02:00
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
response *push_promise(boost::system::error_code &ec, stream &s,
|
2015-03-04 15:28:32 +01:00
|
|
|
std::string method, std::string raw_path_query,
|
|
|
|
header_map h);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-05 14:00:14 +01:00
|
|
|
void signal_write();
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
boost::asio::io_service &io_service();
|
2014-09-23 17:57:43 +02:00
|
|
|
|
2015-03-06 16:20:53 +01:00
|
|
|
const std::string &http_date();
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
template <size_t N>
|
|
|
|
int on_read(const boost::array<uint8_t, N> &buffer, std::size_t len) {
|
2014-09-21 11:28:06 +02:00
|
|
|
callback_guard cg(*this);
|
|
|
|
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = nghttp2_session_mem_recv(session_, buffer.data(), len);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
template <size_t N>
|
|
|
|
int on_write(boost::array<uint8_t, N> &buffer, std::size_t &len) {
|
2014-09-21 11:28:06 +02:00
|
|
|
callback_guard cg(*this);
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (buf_) {
|
2015-02-06 13:35:03 +01:00
|
|
|
std::copy_n(buf_, buflen_, std::begin(buffer));
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
len += buflen_;
|
|
|
|
|
|
|
|
buf_ = nullptr;
|
|
|
|
buflen_ = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2014-09-21 11:28:06 +02:00
|
|
|
const uint8_t *data;
|
|
|
|
auto nread = nghttp2_session_mem_send(session_, &data);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread < 0) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nread == 0) {
|
2014-09-21 11:28:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (len + nread > buffer.size()) {
|
2014-09-21 11:28:06 +02:00
|
|
|
buf_ = data;
|
|
|
|
buflen_ = nread;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:35:03 +01:00
|
|
|
std::copy_n(data, nread, std::begin(buffer) + len);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
len += nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-03-05 13:03:03 +01:00
|
|
|
std::map<int32_t, std::shared_ptr<stream>> streams_;
|
2014-09-21 11:28:06 +02:00
|
|
|
connection_write writefun_;
|
2015-03-04 17:52:12 +01:00
|
|
|
serve_mux &mux_;
|
2014-11-27 15:39:04 +01:00
|
|
|
boost::asio::io_service &io_service_;
|
2014-09-21 11:28:06 +02:00
|
|
|
nghttp2_session *session_;
|
|
|
|
const uint8_t *buf_;
|
|
|
|
std::size_t buflen_;
|
|
|
|
bool inside_callback_;
|
2015-03-06 16:20:53 +01:00
|
|
|
time_t tstamp_cached_;
|
|
|
|
std::string formatted_date_;
|
2014-09-21 11:28:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace server
|
|
|
|
} // namespace asio_http2
|
|
|
|
} // namespace nghttp
|
|
|
|
|
2015-03-05 13:42:48 +01:00
|
|
|
#endif // ASIO_SERVER_HTTP2_HANDLER_H
|