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
|
|
|
#include "asio_server_http2_handler.h"
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2015-03-01 02:02:49 +01:00
|
|
|
#include "asio_common.h"
|
2015-03-04 17:52:12 +01:00
|
|
|
#include "asio_server_serve_mux.h"
|
2015-03-05 13:42:48 +01:00
|
|
|
#include "asio_server_stream.h"
|
|
|
|
#include "asio_server_request_impl.h"
|
|
|
|
#include "asio_server_response_impl.h"
|
2014-09-21 11:28:06 +02:00
|
|
|
#include "http2.h"
|
|
|
|
#include "util.h"
|
2015-02-05 15:21:53 +01:00
|
|
|
#include "template.h"
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
|
|
|
namespace asio_http2 {
|
|
|
|
|
|
|
|
namespace server {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int stream_error(nghttp2_session *session, int32_t stream_id,
|
2014-11-27 15:39:04 +01:00
|
|
|
uint32_t error_code) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id,
|
|
|
|
error_code);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int on_begin_headers_callback(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
|
|
|
auto handler = static_cast<http2_handler *>(user_data);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
handler->create_stream(frame->hd.stream_id);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
2014-09-21 11:28:06 +02:00
|
|
|
const uint8_t *name, size_t namelen,
|
2014-11-27 15:39:04 +01:00
|
|
|
const uint8_t *value, size_t valuelen, uint8_t flags,
|
|
|
|
void *user_data) {
|
|
|
|
auto handler = static_cast<http2_handler *>(user_data);
|
2014-09-21 11:28:06 +02:00
|
|
|
auto stream_id = frame->hd.stream_id;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
auto strm = handler->find_stream(stream_id);
|
|
|
|
if (!strm) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
auto &req = strm->request().impl();
|
2015-03-04 13:49:53 +01:00
|
|
|
auto &uref = req.uri();
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-02-20 16:06:05 +01:00
|
|
|
switch (nghttp2::http2::lookup_token(name, namelen)) {
|
|
|
|
case nghttp2::http2::HD__METHOD:
|
2014-09-21 11:28:06 +02:00
|
|
|
req.method(std::string(value, value + valuelen));
|
2015-02-20 16:06:05 +01:00
|
|
|
break;
|
|
|
|
case nghttp2::http2::HD__SCHEME:
|
2015-03-04 13:49:53 +01:00
|
|
|
uref.scheme.assign(value, value + valuelen);
|
2015-02-20 16:06:05 +01:00
|
|
|
break;
|
|
|
|
case nghttp2::http2::HD__AUTHORITY:
|
2015-03-04 13:49:53 +01:00
|
|
|
uref.host.assign(value, value + valuelen);
|
2015-02-20 16:06:05 +01:00
|
|
|
break;
|
|
|
|
case nghttp2::http2::HD__PATH:
|
2015-03-04 13:49:53 +01:00
|
|
|
split_path(uref, value, value + valuelen);
|
2015-02-20 16:06:05 +01:00
|
|
|
break;
|
|
|
|
case nghttp2::http2::HD_HOST:
|
2015-03-04 13:49:53 +01:00
|
|
|
if (uref.host.empty()) {
|
|
|
|
uref.host.assign(value, value + valuelen);
|
|
|
|
}
|
2015-02-20 16:06:05 +01:00
|
|
|
// fall through
|
|
|
|
default:
|
2015-03-03 19:05:57 +01:00
|
|
|
req.header().emplace(std::string(name, name + namelen),
|
2015-03-04 13:53:11 +01:00
|
|
|
header_value{std::string(value, value + valuelen),
|
|
|
|
(flags & NGHTTP2_NV_FLAG_NO_INDEX) != 0});
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
|
|
|
void *user_data) {
|
|
|
|
auto handler = static_cast<http2_handler *>(user_data);
|
2015-03-05 13:03:03 +01:00
|
|
|
auto strm = handler->find_stream(frame->hd.stream_id);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (frame->hd.type) {
|
2014-09-21 11:28:06 +02:00
|
|
|
case NGHTTP2_DATA:
|
2015-03-05 13:03:03 +01:00
|
|
|
if (!strm) {
|
2014-09-21 11:28:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
2015-03-05 13:03:03 +01:00
|
|
|
strm->request().impl().call_on_data(nullptr, 0);
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NGHTTP2_HEADERS: {
|
2015-03-05 13:03:03 +01:00
|
|
|
if (!strm || frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
2014-09-21 11:28:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
handler->call_on_request(*strm);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
2015-03-05 13:03:03 +01:00
|
|
|
strm->request().impl().call_on_data(nullptr, 0);
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t stream_id, const uint8_t *data,
|
|
|
|
size_t len, void *user_data) {
|
|
|
|
auto handler = static_cast<http2_handler *>(user_data);
|
2015-03-05 13:03:03 +01:00
|
|
|
auto strm = handler->find_stream(stream_id);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
if (!strm) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
strm->request().impl().call_on_data(data, len);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
|
|
|
|
uint32_t error_code, void *user_data) {
|
|
|
|
auto handler = static_cast<http2_handler *>(user_data);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
auto strm = handler->find_stream(stream_id);
|
|
|
|
if (!strm) {
|
2015-03-04 15:48:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
strm->response().impl().call_on_close(error_code);
|
2015-03-04 15:48:00 +01:00
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
handler->close_stream(stream_id);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_frame_send_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
|
|
|
void *user_data) {
|
|
|
|
auto handler = static_cast<http2_handler *>(user_data);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.type != NGHTTP2_PUSH_PROMISE) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
auto strm = handler->find_stream(frame->push_promise.promised_stream_id);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
if (!strm) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
auto &res = strm->response().impl();
|
2015-03-05 16:06:53 +01:00
|
|
|
res.push_promise_sent();
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_frame_not_send_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, int lib_error_code,
|
|
|
|
void *user_data) {
|
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue RST_STREAM so that stream does not hang around.
|
|
|
|
nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, frame->hd.stream_id,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
http2_handler::http2_handler(boost::asio::io_service &io_service,
|
2015-03-04 17:52:12 +01:00
|
|
|
connection_write writefun, serve_mux &mux)
|
|
|
|
: writefun_(writefun), mux_(mux), io_service_(io_service),
|
2015-03-06 16:20:53 +01:00
|
|
|
session_(nullptr), buf_(nullptr), buflen_(0), inside_callback_(false),
|
|
|
|
tstamp_cached_(time(nullptr)),
|
|
|
|
formatted_date_(util::http_date(tstamp_cached_)) {}
|
2014-11-27 15:39:04 +01:00
|
|
|
|
|
|
|
http2_handler::~http2_handler() { nghttp2_session_del(session_); }
|
|
|
|
|
2015-03-06 16:20:53 +01:00
|
|
|
const std::string &http2_handler::http_date() {
|
|
|
|
auto t = time(nullptr);
|
|
|
|
if (t != tstamp_cached_) {
|
|
|
|
tstamp_cached_ = t;
|
|
|
|
formatted_date_ = util::http_date(t);
|
|
|
|
}
|
|
|
|
return formatted_date_;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int http2_handler::start() {
|
2014-09-21 11:28:06 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
nghttp2_session_callbacks *callbacks;
|
|
|
|
rv = nghttp2_session_callbacks_new(&callbacks);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-06 15:27:15 +01:00
|
|
|
auto cb_del = defer(nghttp2_session_callbacks_del, callbacks);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_begin_headers_callback(
|
|
|
|
callbacks, on_begin_headers_callback);
|
|
|
|
nghttp2_session_callbacks_set_on_header_callback(callbacks,
|
|
|
|
on_header_callback);
|
|
|
|
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
|
|
|
|
on_frame_recv_callback);
|
|
|
|
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
|
|
|
|
callbacks, on_data_chunk_recv_callback);
|
|
|
|
nghttp2_session_callbacks_set_on_stream_close_callback(
|
|
|
|
callbacks, on_stream_close_callback);
|
|
|
|
nghttp2_session_callbacks_set_on_frame_send_callback(callbacks,
|
|
|
|
on_frame_send_callback);
|
|
|
|
nghttp2_session_callbacks_set_on_frame_not_send_callback(
|
|
|
|
callbacks, on_frame_not_send_callback);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
nghttp2_option *option;
|
|
|
|
rv = nghttp2_option_new(&option);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-06 15:27:15 +01:00
|
|
|
auto opt_del = defer(nghttp2_option_del, option);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
nghttp2_option_set_recv_client_preface(option, 1);
|
|
|
|
|
|
|
|
rv = nghttp2_session_server_new2(&session_, callbacks, this, option);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_settings_entry ent{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 100};
|
2014-09-21 11:28:06 +02:00
|
|
|
nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, &ent, 1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
stream *http2_handler::create_stream(int32_t stream_id) {
|
|
|
|
auto p = streams_.emplace(stream_id, make_unique<stream>(this, stream_id));
|
2015-03-03 18:19:38 +01:00
|
|
|
assert(p.second);
|
|
|
|
return (*p.first).second.get();
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void http2_handler::close_stream(int32_t stream_id) {
|
2014-09-21 11:28:06 +02:00
|
|
|
streams_.erase(stream_id);
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
stream *http2_handler::find_stream(int32_t stream_id) {
|
2014-09-21 11:28:06 +02:00
|
|
|
auto i = streams_.find(stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (i == std::end(streams_)) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-03 18:19:38 +01:00
|
|
|
return (*i).second.get();
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
void http2_handler::call_on_request(stream &strm) {
|
|
|
|
auto cb = mux_.handler(strm.request().impl());
|
|
|
|
cb(strm.request(), strm.response());
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
bool http2_handler::should_stop() const {
|
|
|
|
return !nghttp2_session_want_read(session_) &&
|
|
|
|
!nghttp2_session_want_write(session_);
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
int http2_handler::start_response(stream &strm) {
|
2014-09-21 11:28:06 +02:00
|
|
|
int rv;
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
auto &res = strm.response().impl();
|
2015-03-03 19:05:57 +01:00
|
|
|
auto &header = res.header();
|
2014-09-21 11:28:06 +02:00
|
|
|
auto nva = std::vector<nghttp2_nv>();
|
2015-03-03 19:05:57 +01:00
|
|
|
nva.reserve(2 + header.size());
|
2014-11-10 14:39:20 +01:00
|
|
|
auto status = util::utos(res.status_code());
|
2015-03-06 16:20:53 +01:00
|
|
|
auto date = http_date();
|
2014-09-21 11:28:06 +02:00
|
|
|
nva.push_back(nghttp2::http2::make_nv_ls(":status", status));
|
2015-03-06 16:20:53 +01:00
|
|
|
nva.push_back(nghttp2::http2::make_nv_ls("date", date));
|
2015-03-03 19:05:57 +01:00
|
|
|
for (auto &hd : header) {
|
|
|
|
nva.push_back(nghttp2::http2::make_nv(hd.first, hd.second.value,
|
|
|
|
hd.second.sensitive));
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2015-03-06 13:36:40 +01:00
|
|
|
nghttp2_data_provider *prd_ptr = nullptr, prd;
|
|
|
|
auto &req = strm.request().impl();
|
|
|
|
if (::nghttp2::http2::expect_response_body(req.method(), res.status_code())) {
|
|
|
|
prd.source.ptr = &strm;
|
|
|
|
prd.read_callback =
|
|
|
|
[](nghttp2_session *session, int32_t stream_id, uint8_t *buf,
|
|
|
|
size_t length, uint32_t *data_flags, nghttp2_data_source *source,
|
|
|
|
void *user_data) -> ssize_t {
|
|
|
|
auto &strm = *static_cast<stream *>(source->ptr);
|
|
|
|
return strm.response().impl().call_read(buf, length, data_flags);
|
|
|
|
};
|
|
|
|
prd_ptr = &prd;
|
|
|
|
}
|
2015-03-05 13:03:03 +01:00
|
|
|
rv = nghttp2_submit_response(session_, strm.get_stream_id(), nva.data(),
|
2015-03-06 13:36:40 +01:00
|
|
|
nva.size(), prd_ptr);
|
2014-11-27 15:39:04 +01:00
|
|
|
|
|
|
|
if (rv != 0) {
|
2014-09-21 11:28:06 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-03-05 14:00:14 +01:00
|
|
|
signal_write();
|
|
|
|
|
2014-09-21 11:28:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void http2_handler::enter_callback() {
|
2014-09-21 11:28:06 +02:00
|
|
|
assert(!inside_callback_);
|
|
|
|
inside_callback_ = true;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void http2_handler::leave_callback() {
|
2014-09-21 11:28:06 +02:00
|
|
|
assert(inside_callback_);
|
|
|
|
inside_callback_ = false;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void http2_handler::stream_error(int32_t stream_id, uint32_t error_code) {
|
2014-09-21 11:28:06 +02:00
|
|
|
::nghttp2::asio_http2::server::stream_error(session_, stream_id, error_code);
|
2015-03-05 14:00:14 +01:00
|
|
|
signal_write();
|
|
|
|
}
|
|
|
|
|
|
|
|
void http2_handler::signal_write() {
|
|
|
|
if (!inside_callback_) {
|
|
|
|
initiate_write();
|
|
|
|
}
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void http2_handler::initiate_write() { writefun_(); }
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
void http2_handler::resume(stream &strm) {
|
|
|
|
nghttp2_session_resume_data(session_, strm.get_stream_id());
|
2015-03-05 14:00:14 +01:00
|
|
|
signal_write();
|
2014-09-28 09:17:43 +02:00
|
|
|
}
|
|
|
|
|
2015-03-04 15:28:32 +01:00
|
|
|
response *http2_handler::push_promise(boost::system::error_code &ec,
|
2015-03-05 13:03:03 +01:00
|
|
|
stream &strm, std::string method,
|
2015-03-04 15:28:32 +01:00
|
|
|
std::string raw_path_query,
|
|
|
|
header_map h) {
|
2014-09-21 11:28:06 +02:00
|
|
|
int rv;
|
|
|
|
|
2015-03-04 15:28:32 +01:00
|
|
|
ec.clear();
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
auto &req = strm.request().impl();
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
auto nva = std::vector<nghttp2_nv>();
|
2015-03-04 13:49:53 +01:00
|
|
|
nva.reserve(4 + h.size());
|
2014-09-21 11:28:06 +02:00
|
|
|
nva.push_back(nghttp2::http2::make_nv_ls(":method", method));
|
2015-03-04 13:49:53 +01:00
|
|
|
nva.push_back(nghttp2::http2::make_nv_ls(":scheme", req.uri().scheme));
|
|
|
|
nva.push_back(nghttp2::http2::make_nv_ls(":authority", req.uri().host));
|
|
|
|
nva.push_back(nghttp2::http2::make_nv_ls(":path", raw_path_query));
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-03 19:05:57 +01:00
|
|
|
for (auto &hd : h) {
|
|
|
|
nva.push_back(nghttp2::http2::make_nv(hd.first, hd.second.value,
|
|
|
|
hd.second.sensitive));
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_submit_push_promise(session_, NGHTTP2_FLAG_NONE,
|
2015-03-05 13:03:03 +01:00
|
|
|
strm.get_stream_id(), nva.data(), nva.size(),
|
|
|
|
nullptr);
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2015-03-04 15:28:32 +01:00
|
|
|
ec = make_error_code(static_cast<nghttp2_error>(rv));
|
|
|
|
return nullptr;
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
auto promised_strm = create_stream(rv);
|
|
|
|
auto &promised_req = promised_strm->request().impl();
|
2015-03-03 19:05:57 +01:00
|
|
|
promised_req.header(std::move(h));
|
2015-03-04 13:49:53 +01:00
|
|
|
promised_req.method(std::move(method));
|
2015-03-04 15:28:32 +01:00
|
|
|
|
2015-03-04 13:49:53 +01:00
|
|
|
auto &uref = promised_req.uri();
|
|
|
|
uref.scheme = req.uri().scheme;
|
|
|
|
uref.host = req.uri().host;
|
|
|
|
split_path(uref, std::begin(raw_path_query), std::end(raw_path_query));
|
2014-09-21 11:28:06 +02:00
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
auto &promised_res = promised_strm->response().impl();
|
2015-03-04 15:28:32 +01:00
|
|
|
promised_res.pushed(true);
|
|
|
|
|
2015-03-05 14:00:14 +01:00
|
|
|
signal_write();
|
|
|
|
|
2015-03-05 13:03:03 +01:00
|
|
|
return &promised_strm->response();
|
2014-09-21 11:28:06 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
boost::asio::io_service &http2_handler::io_service() { return io_service_; }
|
2014-09-23 17:57:43 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
callback_guard::callback_guard(http2_handler &h) : handler(h) {
|
2014-09-21 11:28:06 +02:00
|
|
|
handler.enter_callback();
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
callback_guard::~callback_guard() { handler.leave_callback(); }
|
2014-09-21 11:28:06 +02:00
|
|
|
|
|
|
|
} // namespace server
|
|
|
|
|
|
|
|
} // namespace asio_http2
|
|
|
|
|
|
|
|
} // namespace nghttp2
|