2013-07-22 14:30:40 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2013-07-22 14:30:40 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013 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.
|
|
|
|
*/
|
|
|
|
#include "HttpServer.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2013-07-22 14:30:40 +02:00
|
|
|
#include <sys/socket.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_SYS_SOCKET_H
|
|
|
|
#ifdef HAVE_NETDB_H
|
2013-07-22 14:30:40 +02:00
|
|
|
#include <netdb.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_NETDB_H
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2013-07-22 14:30:40 +02:00
|
|
|
#include <unistd.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_UNISTD_H
|
|
|
|
#ifdef HAVE_FCNTL_H
|
2013-07-22 14:30:40 +02:00
|
|
|
#include <fcntl.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_FCNTL_H
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
2013-07-22 14:30:40 +02:00
|
|
|
#include <netinet/in.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_NETINET_IN_H
|
2013-07-22 14:30:40 +02:00
|
|
|
#include <netinet/tcp.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
2015-01-24 21:26:49 +01:00
|
|
|
#include <arpa/inet.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_ARPA_INET_H
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <set>
|
|
|
|
#include <iostream>
|
2014-03-04 15:14:26 +01:00
|
|
|
#include <thread>
|
2014-12-23 16:05:45 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <deque>
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
#include <openssl/err.h>
|
2015-12-11 15:14:52 +01:00
|
|
|
#include <openssl/dh.h>
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
2013-07-22 15:12:54 +02:00
|
|
|
#include "app_helper.h"
|
2013-08-27 20:14:19 +02:00
|
|
|
#include "http2.h"
|
2013-07-22 14:30:40 +02:00
|
|
|
#include "util.h"
|
2014-06-28 08:28:19 +02:00
|
|
|
#include "ssl.h"
|
2015-02-05 15:21:53 +01:00
|
|
|
#include "template.h"
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
#ifndef O_BINARY
|
2014-11-27 15:39:04 +01:00
|
|
|
#define O_BINARY (0)
|
2013-07-22 14:30:40 +02:00
|
|
|
#endif // O_BINARY
|
|
|
|
|
|
|
|
namespace nghttp2 {
|
|
|
|
|
|
|
|
namespace {
|
2015-05-29 15:31:02 +02:00
|
|
|
// TODO could be constexpr
|
|
|
|
constexpr char DEFAULT_HTML[] = "index.html";
|
|
|
|
constexpr char NGHTTPD_SERVER[] = "nghttpd nghttp2/" NGHTTP2_VERSION;
|
2013-07-22 14:30:40 +02:00
|
|
|
} // namespace
|
|
|
|
|
2014-03-21 15:07:20 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void delete_handler(Http2Handler *handler) {
|
2014-03-21 15:07:20 +01:00
|
|
|
handler->remove_self();
|
|
|
|
delete handler;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void print_session_id(int64_t id) { std::cout << "[id=" << id << "] "; }
|
2014-03-21 15:07:20 +01:00
|
|
|
} // namespace
|
|
|
|
|
2014-05-07 16:24:07 +02:00
|
|
|
namespace {
|
2015-02-05 15:39:36 +01:00
|
|
|
template <typename Array> void append_nv(Stream *stream, const Array &nva) {
|
2015-01-04 15:22:39 +01:00
|
|
|
for (size_t i = 0; i < nva.size(); ++i) {
|
|
|
|
auto &nv = nva[i];
|
|
|
|
auto token = http2::lookup_token(nv.name, nv.namelen);
|
|
|
|
if (token != -1) {
|
|
|
|
http2::index_header(stream->hdidx, token, i);
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
http2::add_header(stream->headers, nv.name, nv.namelen, nv.value,
|
2015-02-08 06:07:01 +01:00
|
|
|
nv.valuelen, nv.flags & NGHTTP2_NV_FLAG_NO_INDEX, token);
|
2014-05-07 16:24:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
Config::Config()
|
2016-01-27 13:14:07 +01:00
|
|
|
: mime_types_file("/etc/mime.types"),
|
|
|
|
stream_read_timeout(1_min),
|
|
|
|
stream_write_timeout(1_min),
|
|
|
|
data_ptr(nullptr),
|
|
|
|
padding(0),
|
|
|
|
num_worker(1),
|
|
|
|
max_concurrent_streams(100),
|
|
|
|
header_table_size(-1),
|
2016-01-27 14:34:17 +01:00
|
|
|
window_bits(-1),
|
|
|
|
connection_window_bits(-1),
|
2016-01-27 13:14:07 +01:00
|
|
|
port(0),
|
|
|
|
verbose(false),
|
|
|
|
daemon(false),
|
|
|
|
verify_client(false),
|
|
|
|
no_tls(false),
|
|
|
|
error_gzip(false),
|
|
|
|
early_response(false),
|
|
|
|
hexdump(false),
|
|
|
|
echo_upload(false),
|
|
|
|
no_content_length(false) {}
|
2013-07-22 14:30:40 +02:00
|
|
|
|
2015-03-22 16:12:27 +01:00
|
|
|
Config::~Config() {}
|
2014-10-10 15:50:35 +02:00
|
|
|
|
2014-03-21 15:07:20 +01:00
|
|
|
namespace {
|
2014-12-23 16:05:45 +01:00
|
|
|
void stream_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
2014-03-21 15:07:20 +01:00
|
|
|
int rv;
|
2014-12-23 16:05:45 +01:00
|
|
|
auto stream = static_cast<Stream *>(w->data);
|
2014-03-21 15:07:20 +01:00
|
|
|
auto hd = stream->handler;
|
|
|
|
auto config = hd->get_config();
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_timer_stop(hd->get_loop(), &stream->rtimer);
|
|
|
|
ev_timer_stop(hd->get_loop(), &stream->wtimer);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config->verbose) {
|
2014-03-21 15:07:20 +01:00
|
|
|
print_session_id(hd->session_id());
|
|
|
|
print_timer();
|
|
|
|
std::cout << " timeout stream_id=" << stream->stream_id << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
hd->submit_rst_stream(stream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
|
|
|
|
rv = hd->on_write();
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == -1) {
|
2014-03-21 15:07:20 +01:00
|
|
|
delete_handler(hd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void add_stream_read_timeout(Stream *stream) {
|
2014-03-21 15:26:53 +01:00
|
|
|
auto hd = stream->handler;
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_timer_again(hd->get_loop(), &stream->rtimer);
|
2014-03-21 15:07:20 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-03-21 16:36:39 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void add_stream_read_timeout_if_pending(Stream *stream) {
|
2014-03-21 16:36:39 +01:00
|
|
|
auto hd = stream->handler;
|
2014-12-23 16:05:45 +01:00
|
|
|
if (ev_is_active(&stream->rtimer)) {
|
|
|
|
ev_timer_again(hd->get_loop(), &stream->rtimer);
|
2014-03-21 16:36:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-03-21 15:07:20 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void add_stream_write_timeout(Stream *stream) {
|
2014-03-21 15:26:53 +01:00
|
|
|
auto hd = stream->handler;
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_timer_again(hd->get_loop(), &stream->wtimer);
|
2014-03-21 15:07:20 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void remove_stream_read_timeout(Stream *stream) {
|
2014-12-23 16:05:45 +01:00
|
|
|
auto hd = stream->handler;
|
|
|
|
ev_timer_stop(hd->get_loop(), &stream->rtimer);
|
2014-03-21 15:07:20 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void remove_stream_write_timeout(Stream *stream) {
|
2014-12-23 16:05:45 +01:00
|
|
|
auto hd = stream->handler;
|
|
|
|
ev_timer_stop(hd->get_loop(), &stream->wtimer);
|
2014-03-21 15:07:20 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-08-22 13:59:50 +02:00
|
|
|
namespace {
|
|
|
|
void fill_callback(nghttp2_session_callbacks *callbacks, const Config *config);
|
|
|
|
} // namespace
|
|
|
|
|
2015-11-18 15:21:57 +01:00
|
|
|
namespace {
|
|
|
|
constexpr ev_tstamp RELEASE_FD_TIMEOUT = 2.;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void release_fd_cb(struct ev_loop *loop, ev_timer *w, int revents);
|
|
|
|
} // namespace
|
|
|
|
|
2015-11-20 15:29:12 +01:00
|
|
|
namespace {
|
|
|
|
constexpr ev_tstamp FILE_ENTRY_MAX_AGE = 10.;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
constexpr size_t FILE_ENTRY_EVICT_THRES = 2048;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
bool need_validation_file_entry(const FileEntry *ent, ev_tstamp now) {
|
|
|
|
return ent->last_valid + FILE_ENTRY_MAX_AGE < now;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
bool validate_file_entry(FileEntry *ent, ev_tstamp now) {
|
|
|
|
struct stat stbuf;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = fstat(ent->fd, &stbuf);
|
|
|
|
if (rv != 0) {
|
|
|
|
ent->stale = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stbuf.st_nlink == 0 || ent->mtime != stbuf.st_mtime) {
|
|
|
|
ent->stale = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ent->mtime = stbuf.st_mtime;
|
|
|
|
ent->last_valid = now;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
class Sessions {
|
|
|
|
public:
|
2015-04-19 09:26:47 +02:00
|
|
|
Sessions(HttpServer *sv, struct ev_loop *loop, const Config *config,
|
|
|
|
SSL_CTX *ssl_ctx)
|
2016-01-27 13:14:07 +01:00
|
|
|
: sv_(sv),
|
|
|
|
loop_(loop),
|
|
|
|
config_(config),
|
|
|
|
ssl_ctx_(ssl_ctx),
|
|
|
|
callbacks_(nullptr),
|
|
|
|
next_session_id_(1),
|
|
|
|
tstamp_cached_(ev_now(loop)),
|
2015-03-06 16:48:13 +01:00
|
|
|
cached_date_(util::http_date(tstamp_cached_)) {
|
2014-08-22 13:59:50 +02:00
|
|
|
nghttp2_session_callbacks_new(&callbacks_);
|
|
|
|
|
|
|
|
fill_callback(callbacks_, config_);
|
2015-11-18 15:21:57 +01:00
|
|
|
|
|
|
|
ev_timer_init(&release_fd_timer_, release_fd_cb, 0., RELEASE_FD_TIMEOUT);
|
|
|
|
release_fd_timer_.data = this;
|
2014-08-22 13:59:50 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
~Sessions() {
|
2015-11-18 15:21:57 +01:00
|
|
|
ev_timer_stop(loop_, &release_fd_timer_);
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto handler : handlers_) {
|
2013-07-22 14:30:40 +02:00
|
|
|
delete handler;
|
|
|
|
}
|
2014-08-22 13:59:50 +02:00
|
|
|
nghttp2_session_callbacks_del(callbacks_);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
void add_handler(Http2Handler *handler) { handlers_.insert(handler); }
|
2015-11-18 15:21:57 +01:00
|
|
|
void remove_handler(Http2Handler *handler) {
|
|
|
|
handlers_.erase(handler);
|
|
|
|
if (handlers_.empty() && !fd_cache_.empty()) {
|
|
|
|
ev_timer_again(loop_, &release_fd_timer_);
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
SSL_CTX *get_ssl_ctx() const { return ssl_ctx_; }
|
|
|
|
SSL *ssl_session_new(int fd) {
|
2013-07-22 14:30:40 +02:00
|
|
|
SSL *ssl = SSL_new(ssl_ctx_);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!ssl) {
|
2013-07-22 14:30:40 +02:00
|
|
|
std::cerr << "SSL_new() failed" << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (SSL_set_fd(ssl, fd) == 0) {
|
2013-07-22 14:30:40 +02:00
|
|
|
std::cerr << "SSL_set_fd() failed" << std::endl;
|
|
|
|
SSL_free(ssl);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ssl;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
const Config *get_config() const { return config_; }
|
2014-12-23 16:05:45 +01:00
|
|
|
struct ev_loop *get_loop() const {
|
|
|
|
return loop_;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
int64_t get_next_session_id() {
|
2014-03-04 15:14:26 +01:00
|
|
|
auto session_id = next_session_id_;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (next_session_id_ == std::numeric_limits<int64_t>::max()) {
|
2014-03-04 15:14:26 +01:00
|
|
|
next_session_id_ = 1;
|
2014-12-09 17:21:12 +01:00
|
|
|
} else {
|
|
|
|
++next_session_id_;
|
2014-03-04 15:14:26 +01:00
|
|
|
}
|
|
|
|
return session_id;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
const nghttp2_session_callbacks *get_callbacks() const { return callbacks_; }
|
|
|
|
void accept_connection(int fd) {
|
2015-01-05 07:59:51 +01:00
|
|
|
util::make_socket_nodelay(fd);
|
2014-03-04 15:14:26 +01:00
|
|
|
SSL *ssl = nullptr;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (ssl_ctx_) {
|
2014-03-04 15:14:26 +01:00
|
|
|
ssl = ssl_session_new(fd);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!ssl) {
|
2014-03-04 15:14:26 +01:00
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
auto handler =
|
2015-02-05 15:21:53 +01:00
|
|
|
make_unique<Http2Handler>(this, fd, ssl, get_next_session_id());
|
2014-03-04 15:14:26 +01:00
|
|
|
handler->setup_bev();
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!ssl) {
|
2015-03-28 12:21:12 +01:00
|
|
|
if (handler->connection_made() != 0) {
|
2014-03-04 15:14:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_handler(handler.release());
|
|
|
|
}
|
2015-03-06 16:48:13 +01:00
|
|
|
void update_cached_date() { cached_date_ = util::http_date(tstamp_cached_); }
|
|
|
|
const std::string &get_cached_date() {
|
|
|
|
auto t = ev_now(loop_);
|
|
|
|
if (t != tstamp_cached_) {
|
|
|
|
tstamp_cached_ = t;
|
|
|
|
update_cached_date();
|
|
|
|
}
|
|
|
|
return cached_date_;
|
|
|
|
}
|
2015-04-19 09:26:47 +02:00
|
|
|
FileEntry *get_cached_fd(const std::string &path) {
|
2015-11-20 15:29:12 +01:00
|
|
|
auto range = fd_cache_.equal_range(path);
|
|
|
|
if (range.first == range.second) {
|
2015-04-19 09:26:47 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-11-20 15:29:12 +01:00
|
|
|
|
|
|
|
auto now = ev_now(loop_);
|
|
|
|
|
|
|
|
for (auto it = range.first; it != range.second;) {
|
|
|
|
auto &ent = (*it).second;
|
2015-11-23 13:25:12 +01:00
|
|
|
if (ent->stale) {
|
2015-11-20 15:29:12 +01:00
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-23 13:25:12 +01:00
|
|
|
if (need_validation_file_entry(ent.get(), now) &&
|
|
|
|
!validate_file_entry(ent.get(), now)) {
|
|
|
|
if (ent->usecount == 0) {
|
|
|
|
fd_cache_lru_.remove(ent.get());
|
|
|
|
close(ent->fd);
|
2015-11-20 15:29:12 +01:00
|
|
|
it = fd_cache_.erase(it);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-23 13:25:12 +01:00
|
|
|
fd_cache_lru_.remove(ent.get());
|
|
|
|
fd_cache_lru_.append(ent.get());
|
2015-11-20 15:29:12 +01:00
|
|
|
|
2015-11-23 13:25:12 +01:00
|
|
|
++ent->usecount;
|
|
|
|
return ent.get();
|
2015-11-20 15:29:12 +01:00
|
|
|
}
|
|
|
|
return nullptr;
|
2015-04-19 09:26:47 +02:00
|
|
|
}
|
|
|
|
FileEntry *cache_fd(const std::string &path, const FileEntry &ent) {
|
2015-06-27 04:19:10 +02:00
|
|
|
#ifdef HAVE_STD_MAP_EMPLACE
|
2015-11-23 13:25:12 +01:00
|
|
|
auto rv = fd_cache_.emplace(path, make_unique<FileEntry>(ent));
|
2015-06-27 04:19:10 +02:00
|
|
|
#else // !HAVE_STD_MAP_EMPLACE
|
|
|
|
// for gcc-4.7
|
2015-11-23 13:25:12 +01:00
|
|
|
auto rv =
|
|
|
|
fd_cache_.insert(std::make_pair(path, make_unique<FileEntry>(ent)));
|
2015-06-27 04:19:10 +02:00
|
|
|
#endif // !HAVE_STD_MAP_EMPLACE
|
2015-11-20 15:29:12 +01:00
|
|
|
auto &res = (*rv).second;
|
2015-11-23 13:25:12 +01:00
|
|
|
res->it = rv;
|
|
|
|
fd_cache_lru_.append(res.get());
|
2015-11-20 15:29:12 +01:00
|
|
|
|
|
|
|
while (fd_cache_.size() > FILE_ENTRY_EVICT_THRES) {
|
|
|
|
auto ent = fd_cache_lru_.head;
|
|
|
|
if (ent->usecount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fd_cache_lru_.remove(ent);
|
|
|
|
close(ent->fd);
|
|
|
|
fd_cache_.erase(ent->it);
|
2015-04-19 09:26:47 +02:00
|
|
|
}
|
2015-11-18 15:21:57 +01:00
|
|
|
|
2015-11-23 13:25:12 +01:00
|
|
|
return res.get();
|
2015-11-20 15:29:12 +01:00
|
|
|
}
|
|
|
|
void release_fd(FileEntry *target) {
|
|
|
|
--target->usecount;
|
|
|
|
|
|
|
|
if (target->usecount == 0 && target->stale) {
|
|
|
|
fd_cache_lru_.remove(target);
|
|
|
|
close(target->fd);
|
|
|
|
fd_cache_.erase(target->it);
|
2015-11-18 15:21:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use timer to close file descriptor and delete the entry from
|
|
|
|
// cache. The timer will be started when there is no handler.
|
|
|
|
}
|
|
|
|
void release_unused_fd() {
|
|
|
|
for (auto i = std::begin(fd_cache_); i != std::end(fd_cache_);) {
|
|
|
|
auto &ent = (*i).second;
|
2015-11-23 13:25:12 +01:00
|
|
|
if (ent->usecount != 0) {
|
2015-11-18 15:21:57 +01:00
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-23 13:25:12 +01:00
|
|
|
fd_cache_lru_.remove(ent.get());
|
|
|
|
close(ent->fd);
|
2015-11-18 15:21:57 +01:00
|
|
|
i = fd_cache_.erase(i);
|
2015-04-19 09:26:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const HttpServer *get_server() const { return sv_; }
|
2015-11-18 15:21:57 +01:00
|
|
|
bool handlers_empty() const { return handlers_.empty(); }
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
private:
|
2014-11-27 15:39:04 +01:00
|
|
|
std::set<Http2Handler *> handlers_;
|
2015-04-19 09:26:47 +02:00
|
|
|
// cache for file descriptors to read file.
|
2015-11-23 13:25:12 +01:00
|
|
|
std::multimap<std::string, std::unique_ptr<FileEntry>> fd_cache_;
|
2015-11-20 15:29:12 +01:00
|
|
|
DList<FileEntry> fd_cache_lru_;
|
2015-04-19 09:26:47 +02:00
|
|
|
HttpServer *sv_;
|
2014-12-23 16:05:45 +01:00
|
|
|
struct ev_loop *loop_;
|
2013-07-22 14:30:40 +02:00
|
|
|
const Config *config_;
|
|
|
|
SSL_CTX *ssl_ctx_;
|
2014-08-22 13:59:50 +02:00
|
|
|
nghttp2_session_callbacks *callbacks_;
|
2015-11-18 15:21:57 +01:00
|
|
|
ev_timer release_fd_timer_;
|
2014-03-04 15:14:26 +01:00
|
|
|
int64_t next_session_id_;
|
2015-03-06 16:48:13 +01:00
|
|
|
ev_tstamp tstamp_cached_;
|
|
|
|
std::string cached_date_;
|
2013-07-22 14:30:40 +02:00
|
|
|
};
|
|
|
|
|
2015-11-18 15:21:57 +01:00
|
|
|
namespace {
|
|
|
|
void release_fd_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto sessions = static_cast<Sessions *>(w->data);
|
|
|
|
|
|
|
|
ev_timer_stop(loop, w);
|
|
|
|
|
|
|
|
if (!sessions->handlers_empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sessions->release_unused_fd();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
Stream::Stream(Http2Handler *handler, int32_t stream_id)
|
2016-01-27 13:14:07 +01:00
|
|
|
: handler(handler),
|
|
|
|
file_ent(nullptr),
|
|
|
|
body_length(0),
|
|
|
|
body_offset(0),
|
|
|
|
stream_id(stream_id),
|
|
|
|
echo_upload(false) {
|
2014-12-23 16:05:45 +01:00
|
|
|
auto config = handler->get_config();
|
|
|
|
ev_timer_init(&rtimer, stream_timeout_cb, 0., config->stream_read_timeout);
|
|
|
|
ev_timer_init(&wtimer, stream_timeout_cb, 0., config->stream_write_timeout);
|
|
|
|
rtimer.data = this;
|
|
|
|
wtimer.data = this;
|
|
|
|
|
|
|
|
headers.reserve(10);
|
2015-01-02 16:12:26 +01:00
|
|
|
|
|
|
|
http2::init_hdidx(hdidx);
|
2014-12-23 16:05:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream::~Stream() {
|
2015-04-19 09:26:47 +02:00
|
|
|
if (file_ent != nullptr) {
|
|
|
|
auto sessions = handler->get_sessions();
|
2015-11-20 15:29:12 +01:00
|
|
|
sessions->release_fd(file_ent);
|
2014-12-23 16:05:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto loop = handler->get_loop();
|
|
|
|
ev_timer_stop(loop, &rtimer);
|
|
|
|
ev_timer_stop(loop, &wtimer);
|
|
|
|
}
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void on_session_closed(Http2Handler *hd, int64_t session_id) {
|
|
|
|
if (hd->get_config()->verbose) {
|
2013-07-22 14:30:40 +02:00
|
|
|
print_session_id(session_id);
|
|
|
|
print_timer();
|
|
|
|
std::cout << " closed" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
namespace {
|
|
|
|
void settings_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
2015-05-18 14:38:12 +02:00
|
|
|
int rv;
|
2014-12-23 16:05:45 +01:00
|
|
|
auto hd = static_cast<Http2Handler *>(w->data);
|
|
|
|
hd->terminate_session(NGHTTP2_SETTINGS_TIMEOUT);
|
2015-05-18 14:38:12 +02:00
|
|
|
rv = hd->on_write();
|
|
|
|
if (rv == -1) {
|
|
|
|
delete_handler(hd);
|
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void readcb(struct ev_loop *loop, ev_io *w, int revents) {
|
|
|
|
int rv;
|
|
|
|
auto handler = static_cast<Http2Handler *>(w->data);
|
|
|
|
|
|
|
|
rv = handler->on_read();
|
|
|
|
if (rv == -1) {
|
|
|
|
delete_handler(handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void writecb(struct ev_loop *loop, ev_io *w, int revents) {
|
|
|
|
int rv;
|
|
|
|
auto handler = static_cast<Http2Handler *>(w->data);
|
|
|
|
|
|
|
|
rv = handler->on_write();
|
|
|
|
if (rv == -1) {
|
|
|
|
delete_handler(handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Http2Handler::Http2Handler(Sessions *sessions, int fd, SSL *ssl,
|
|
|
|
int64_t session_id)
|
2016-01-27 13:14:07 +01:00
|
|
|
: session_id_(session_id),
|
|
|
|
session_(nullptr),
|
|
|
|
sessions_(sessions),
|
|
|
|
ssl_(ssl),
|
|
|
|
data_pending_(nullptr),
|
|
|
|
data_pendinglen_(0),
|
|
|
|
fd_(fd) {
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_timer_init(&settings_timerev_, settings_timeout_cb, 10., 0.);
|
|
|
|
ev_io_init(&wev_, writecb, fd, EV_WRITE);
|
|
|
|
ev_io_init(&rev_, readcb, fd, EV_READ);
|
|
|
|
|
|
|
|
settings_timerev_.data = this;
|
|
|
|
wev_.data = this;
|
|
|
|
rev_.data = this;
|
|
|
|
|
|
|
|
auto loop = sessions_->get_loop();
|
|
|
|
ev_io_start(loop, &rev_);
|
|
|
|
|
|
|
|
if (ssl) {
|
|
|
|
SSL_set_accept_state(ssl);
|
|
|
|
read_ = &Http2Handler::tls_handshake;
|
|
|
|
write_ = &Http2Handler::tls_handshake;
|
|
|
|
} else {
|
|
|
|
read_ = &Http2Handler::read_clear;
|
|
|
|
write_ = &Http2Handler::write_clear;
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
|
|
|
|
Http2Handler::~Http2Handler() {
|
2013-07-22 14:30:40 +02:00
|
|
|
on_session_closed(this, session_id_);
|
|
|
|
nghttp2_session_del(session_);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (ssl_) {
|
2014-01-08 15:32:47 +01:00
|
|
|
SSL_set_shutdown(ssl_, SSL_RECEIVED_SHUTDOWN);
|
2015-01-09 01:15:01 +01:00
|
|
|
ERR_clear_error();
|
2013-07-22 14:30:40 +02:00
|
|
|
SSL_shutdown(ssl_);
|
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
auto loop = sessions_->get_loop();
|
|
|
|
ev_timer_stop(loop, &settings_timerev_);
|
|
|
|
ev_io_stop(loop, &rev_);
|
|
|
|
ev_io_stop(loop, &wev_);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (ssl_) {
|
2013-07-22 14:30:40 +02:00
|
|
|
SSL_free(ssl_);
|
|
|
|
}
|
|
|
|
shutdown(fd_, SHUT_WR);
|
|
|
|
close(fd_);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Handler::remove_self() { sessions_->remove_handler(this); }
|
2013-07-22 14:30:40 +02:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
struct ev_loop *Http2Handler::get_loop() const {
|
|
|
|
return sessions_->get_loop();
|
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
Http2Handler::WriteBuf *Http2Handler::get_wb() { return &wb_; }
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
int Http2Handler::setup_bev() { return 0; }
|
|
|
|
|
2015-01-02 05:42:57 +01:00
|
|
|
int Http2Handler::fill_wb() {
|
2014-12-23 16:05:45 +01:00
|
|
|
if (data_pending_) {
|
2015-01-02 05:42:57 +01:00
|
|
|
auto n = std::min(wb_.wleft(), data_pendinglen_);
|
|
|
|
wb_.write(data_pending_, n);
|
|
|
|
if (n < data_pendinglen_) {
|
|
|
|
data_pending_ += n;
|
|
|
|
data_pendinglen_ -= n;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
|
|
|
|
data_pending_ = nullptr;
|
|
|
|
data_pendinglen_ = 0;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const uint8_t *data;
|
|
|
|
auto datalen = nghttp2_session_mem_send(session_, &data);
|
|
|
|
|
|
|
|
if (datalen < 0) {
|
|
|
|
std::cerr << "nghttp2_session_mem_send() returned error: "
|
|
|
|
<< nghttp2_strerror(datalen) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (datalen == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2015-01-02 05:42:57 +01:00
|
|
|
auto n = wb_.write(data, datalen);
|
2014-12-23 16:05:45 +01:00
|
|
|
if (n < static_cast<decltype(n)>(datalen)) {
|
|
|
|
data_pending_ = data + n;
|
|
|
|
data_pendinglen_ = datalen - n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
int Http2Handler::read_clear() {
|
2013-07-22 14:30:40 +02:00
|
|
|
int rv;
|
2015-06-21 07:32:47 +02:00
|
|
|
std::array<uint8_t, 8_k> buf;
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
for (;;) {
|
|
|
|
ssize_t nread;
|
2015-02-05 16:06:01 +01:00
|
|
|
while ((nread = read(fd_, buf.data(), buf.size())) == -1 && errno == EINTR)
|
2014-12-23 16:05:45 +01:00
|
|
|
;
|
|
|
|
if (nread == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nread == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-23 18:30:51 +01:00
|
|
|
|
|
|
|
if (get_config()->hexdump) {
|
|
|
|
util::hexdump(stdout, buf.data(), nread);
|
|
|
|
}
|
|
|
|
|
2015-02-05 16:06:01 +01:00
|
|
|
rv = nghttp2_session_mem_recv(session_, buf.data(), nread);
|
2014-12-23 16:05:45 +01:00
|
|
|
if (rv < 0) {
|
2015-04-05 15:35:40 +02:00
|
|
|
if (rv != NGHTTP2_ERR_BAD_CLIENT_MAGIC) {
|
2015-02-01 16:20:44 +01:00
|
|
|
std::cerr << "nghttp2_session_mem_recv() returned error: "
|
|
|
|
<< nghttp2_strerror(rv) << std::endl;
|
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
|
|
|
|
return write_(*this);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
int Http2Handler::write_clear() {
|
|
|
|
auto loop = sessions_->get_loop();
|
|
|
|
for (;;) {
|
2015-01-02 05:42:57 +01:00
|
|
|
if (wb_.rleft() > 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
ssize_t nwrite;
|
2015-02-06 15:40:34 +01:00
|
|
|
while ((nwrite = write(fd_, wb_.pos, wb_.rleft())) == -1 &&
|
|
|
|
errno == EINTR)
|
2014-12-23 16:05:45 +01:00
|
|
|
;
|
|
|
|
if (nwrite == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
ev_io_start(loop, &wev_);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-02 05:42:57 +01:00
|
|
|
wb_.drain(nwrite);
|
2014-12-23 16:05:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-02 05:42:57 +01:00
|
|
|
wb_.reset();
|
|
|
|
if (fill_wb() != 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-02 05:42:57 +01:00
|
|
|
if (wb_.rleft() == 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2015-01-02 05:42:57 +01:00
|
|
|
if (wb_.rleft() == 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_io_stop(loop, &wev_);
|
|
|
|
} else {
|
|
|
|
ev_io_start(loop, &wev_);
|
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
if (nghttp2_session_want_read(session_) == 0 &&
|
2015-01-02 05:42:57 +01:00
|
|
|
nghttp2_session_want_write(session_) == 0 && wb_.rleft() == 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
return -1;
|
2014-03-16 11:37:06 +01:00
|
|
|
}
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
int Http2Handler::tls_handshake() {
|
|
|
|
ev_io_stop(sessions_->get_loop(), &wev_);
|
2014-09-18 17:58:32 +02:00
|
|
|
|
2015-01-09 01:10:59 +01:00
|
|
|
ERR_clear_error();
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
auto rv = SSL_do_handshake(ssl_);
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2015-09-29 16:31:50 +02:00
|
|
|
if (rv <= 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
auto err = SSL_get_error(ssl_, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
ev_io_start(sessions_->get_loop(), &wev_);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
if (sessions_->get_config()->verbose) {
|
|
|
|
std::cerr << "SSL/TLS handshake completed" << std::endl;
|
|
|
|
}
|
2014-09-18 17:58:32 +02:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
if (verify_npn_result() != 0) {
|
|
|
|
return -1;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
read_ = &Http2Handler::read_tls;
|
|
|
|
write_ = &Http2Handler::write_tls;
|
|
|
|
|
2015-03-28 12:21:12 +01:00
|
|
|
if (connection_made() != 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2015-07-24 16:40:44 +02:00
|
|
|
if (sessions_->get_config()->verbose) {
|
|
|
|
if (SSL_session_reused(ssl_)) {
|
|
|
|
std::cerr << "SSL/TLS session reused" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
int Http2Handler::read_tls() {
|
2015-06-21 07:32:47 +02:00
|
|
|
std::array<uint8_t, 8_k> buf;
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2015-01-09 01:10:59 +01:00
|
|
|
ERR_clear_error();
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
for (;;) {
|
2015-02-05 16:06:01 +01:00
|
|
|
auto rv = SSL_read(ssl_, buf.data(), buf.size());
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2015-09-29 16:31:50 +02:00
|
|
|
if (rv <= 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
auto err = SSL_get_error(ssl_, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
goto fin;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2015-01-09 01:10:59 +01:00
|
|
|
// renegotiation started
|
|
|
|
return -1;
|
2014-12-23 16:05:45 +01:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
|
|
|
|
auto nread = rv;
|
2015-03-23 18:30:51 +01:00
|
|
|
|
|
|
|
if (get_config()->hexdump) {
|
|
|
|
util::hexdump(stdout, buf.data(), nread);
|
|
|
|
}
|
|
|
|
|
2015-02-05 16:06:01 +01:00
|
|
|
rv = nghttp2_session_mem_recv(session_, buf.data(), nread);
|
2014-12-23 16:05:45 +01:00
|
|
|
if (rv < 0) {
|
2015-04-05 15:35:40 +02:00
|
|
|
if (rv != NGHTTP2_ERR_BAD_CLIENT_MAGIC) {
|
2015-02-01 16:20:44 +01:00
|
|
|
std::cerr << "nghttp2_session_mem_recv() returned error: "
|
|
|
|
<< nghttp2_strerror(rv) << std::endl;
|
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
fin:
|
|
|
|
return write_(*this);
|
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
int Http2Handler::write_tls() {
|
|
|
|
auto loop = sessions_->get_loop();
|
2015-01-09 01:10:59 +01:00
|
|
|
|
|
|
|
ERR_clear_error();
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
for (;;) {
|
2015-01-02 05:42:57 +01:00
|
|
|
if (wb_.rleft() > 0) {
|
2015-02-06 15:40:34 +01:00
|
|
|
auto rv = SSL_write(ssl_, wb_.pos, wb_.rleft());
|
2013-07-22 14:30:40 +02:00
|
|
|
|
2015-09-29 16:31:50 +02:00
|
|
|
if (rv <= 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
auto err = SSL_get_error(ssl_, rv);
|
|
|
|
switch (err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2015-01-09 01:10:59 +01:00
|
|
|
// renegotiation started
|
|
|
|
return -1;
|
2014-12-23 16:05:45 +01:00
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
ev_io_start(sessions_->get_loop(), &wev_);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 05:42:57 +01:00
|
|
|
wb_.drain(rv);
|
2014-12-23 16:05:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-02 05:42:57 +01:00
|
|
|
wb_.reset();
|
|
|
|
if (fill_wb() != 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-02 05:42:57 +01:00
|
|
|
if (wb_.rleft() == 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-12-23 16:46:04 +01:00
|
|
|
}
|
2014-02-19 15:27:21 +01:00
|
|
|
|
2015-01-02 05:42:57 +01:00
|
|
|
if (wb_.rleft() == 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_io_stop(loop, &wev_);
|
|
|
|
} else {
|
|
|
|
ev_io_start(loop, &wev_);
|
2014-12-23 16:46:04 +01:00
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
if (nghttp2_session_want_read(session_) == 0 &&
|
2015-01-02 05:42:57 +01:00
|
|
|
nghttp2_session_want_write(session_) == 0 && wb_.rleft() == 0) {
|
2014-12-23 16:05:45 +01:00
|
|
|
return -1;
|
2014-02-19 15:27:21 +01:00
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
return 0;
|
2014-09-18 17:58:32 +02:00
|
|
|
}
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
int Http2Handler::on_read() { return read_(*this); }
|
2013-07-22 14:30:40 +02:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
int Http2Handler::on_write() { return write_(*this); }
|
2013-10-27 15:02:39 +01:00
|
|
|
|
2015-03-28 12:21:12 +01:00
|
|
|
int Http2Handler::connection_made() {
|
2013-07-22 14:30:40 +02:00
|
|
|
int r;
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2015-03-22 16:12:27 +01:00
|
|
|
r = nghttp2_session_server_new(&session_, sessions_->get_callbacks(), this);
|
2015-05-06 03:42:43 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (r != 0) {
|
2013-07-22 14:30:40 +02:00
|
|
|
return r;
|
|
|
|
}
|
2015-05-08 12:21:51 +02:00
|
|
|
|
|
|
|
auto config = sessions_->get_config();
|
2015-02-05 16:06:01 +01:00
|
|
|
std::array<nghttp2_settings_entry, 4> entry;
|
2014-06-07 09:04:43 +02:00
|
|
|
size_t niv = 1;
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2013-07-28 12:28:41 +02:00
|
|
|
entry[0].settings_id = NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS;
|
2015-05-06 03:42:43 +02:00
|
|
|
entry[0].value = config->max_concurrent_streams;
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2015-05-06 03:42:43 +02:00
|
|
|
if (config->header_table_size >= 0) {
|
2013-11-05 15:44:20 +01:00
|
|
|
entry[niv].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
|
2015-05-06 03:42:43 +02:00
|
|
|
entry[niv].value = config->header_table_size;
|
2013-11-05 15:44:20 +01:00
|
|
|
++niv;
|
|
|
|
}
|
2016-01-27 14:34:17 +01:00
|
|
|
|
|
|
|
if (config->window_bits != -1) {
|
|
|
|
entry[niv].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
|
|
|
|
entry[niv].value = (1 << config->window_bits) - 1;
|
|
|
|
++niv;
|
|
|
|
}
|
|
|
|
|
2015-02-05 16:06:01 +01:00
|
|
|
r = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, entry.data(), niv);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (r != 0) {
|
2013-07-22 14:30:40 +02:00
|
|
|
return r;
|
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
|
2016-01-27 14:34:17 +01:00
|
|
|
if (config->connection_window_bits != -1) {
|
|
|
|
r = nghttp2_submit_window_update(
|
|
|
|
session_, NGHTTP2_FLAG_NONE, 0,
|
|
|
|
(1 << config->connection_window_bits) - 1 -
|
|
|
|
NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE);
|
|
|
|
if (r != 0) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_timer_start(sessions_->get_loop(), &settings_timerev_);
|
2013-10-27 15:02:39 +01:00
|
|
|
|
2015-08-21 15:50:15 +02:00
|
|
|
if (ssl_ && !nghttp2::ssl::check_http2_requirement(ssl_)) {
|
2015-08-20 14:21:56 +02:00
|
|
|
terminate_session(NGHTTP2_INADEQUATE_SECURITY);
|
|
|
|
}
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
return on_write();
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Handler::verify_npn_result() {
|
2013-07-22 14:30:40 +02:00
|
|
|
const unsigned char *next_proto = nullptr;
|
|
|
|
unsigned int next_proto_len;
|
2014-01-01 15:54:28 +01:00
|
|
|
// Check the negotiated protocol in NPN or ALPN
|
2013-07-22 14:30:40 +02:00
|
|
|
SSL_get0_next_proto_negotiated(ssl_, &next_proto, &next_proto_len);
|
2014-11-27 15:39:04 +01:00
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
if (next_proto) {
|
|
|
|
if (sessions_->get_config()->verbose) {
|
|
|
|
std::string proto(next_proto, next_proto + next_proto_len);
|
2014-01-01 15:54:28 +01:00
|
|
|
std::cout << "The negotiated protocol: " << proto << std::endl;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (util::check_h2_is_selected(next_proto, next_proto_len)) {
|
2014-01-01 15:54:28 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
SSL_get0_alpn_selected(ssl_, &next_proto, &next_proto_len);
|
2014-11-27 15:39:04 +01:00
|
|
|
#else // OPENSSL_VERSION_NUMBER < 0x10002000L
|
2014-01-01 15:54:28 +01:00
|
|
|
break;
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER < 0x10002000L
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (sessions_->get_config()->verbose) {
|
2014-03-30 12:09:21 +02:00
|
|
|
std::cerr << "Client did not advertise HTTP/2 protocol."
|
2014-03-03 15:51:46 +01:00
|
|
|
<< " (nghttp2 expects " << NGHTTP2_PROTO_VERSION_ID << ")"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2014-01-01 15:54:28 +01:00
|
|
|
return -1;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
|
2016-01-27 14:47:30 +01:00
|
|
|
namespace {
|
|
|
|
std::string make_trailer_header_value(const Headers &trailer) {
|
|
|
|
if (trailer.empty()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
auto trailer_names = trailer[0].name;
|
|
|
|
for (size_t i = 1; i < trailer.size(); ++i) {
|
|
|
|
trailer_names += ", ";
|
|
|
|
trailer_names += trailer[i].name;
|
|
|
|
}
|
|
|
|
return trailer_names;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Handler::submit_file_response(const std::string &status,
|
|
|
|
Stream *stream, time_t last_modified,
|
2013-07-22 14:30:40 +02:00
|
|
|
off_t file_length,
|
2015-10-28 16:21:36 +01:00
|
|
|
const std::string *content_type,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_data_provider *data_prd) {
|
2014-11-10 13:23:26 +01:00
|
|
|
std::string content_length = util::utos(file_length);
|
2013-07-22 14:30:40 +02:00
|
|
|
std::string last_modified_str;
|
2015-02-05 16:06:01 +01:00
|
|
|
auto nva = make_array(http2::make_nv_ls(":status", status),
|
2015-05-29 15:31:02 +02:00
|
|
|
http2::make_nv_ll("server", NGHTTPD_SERVER),
|
2015-02-05 16:06:01 +01:00
|
|
|
http2::make_nv_ll("cache-control", "max-age=3600"),
|
|
|
|
http2::make_nv_ls("date", sessions_->get_cached_date()),
|
2015-10-28 16:21:36 +01:00
|
|
|
http2::make_nv_ll("", ""), http2::make_nv_ll("", ""),
|
2016-01-07 14:51:47 +01:00
|
|
|
http2::make_nv_ll("", ""), http2::make_nv_ll("", ""));
|
|
|
|
size_t nvlen = 4;
|
|
|
|
if (!get_config()->no_content_length) {
|
|
|
|
nva[nvlen++] = http2::make_nv_ls("content-length", content_length);
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (last_modified != 0) {
|
2013-07-22 14:30:40 +02:00
|
|
|
last_modified_str = util::http_date(last_modified);
|
2014-12-23 16:56:25 +01:00
|
|
|
nva[nvlen++] = http2::make_nv_ls("last-modified", last_modified_str);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2015-10-28 16:21:36 +01:00
|
|
|
if (content_type) {
|
|
|
|
nva[nvlen++] = http2::make_nv_ls("content-type", *content_type);
|
|
|
|
}
|
2016-01-27 14:47:30 +01:00
|
|
|
auto trailer_names = make_trailer_header_value(get_config()->trailer);
|
|
|
|
if (!trailer_names.empty()) {
|
2015-03-08 09:52:36 +01:00
|
|
|
nva[nvlen++] = http2::make_nv_ls("trailer", trailer_names);
|
|
|
|
}
|
2015-02-05 16:06:01 +01:00
|
|
|
return nghttp2_submit_response(session_, stream->stream_id, nva.data(), nvlen,
|
2014-12-23 16:56:25 +01:00
|
|
|
data_prd);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Handler::submit_response(const std::string &status, int32_t stream_id,
|
|
|
|
const Headers &headers,
|
|
|
|
nghttp2_data_provider *data_prd) {
|
2015-02-05 15:39:36 +01:00
|
|
|
auto nva = std::vector<nghttp2_nv>();
|
2016-01-27 14:47:30 +01:00
|
|
|
nva.reserve(4 + headers.size());
|
2015-02-05 15:39:36 +01:00
|
|
|
nva.push_back(http2::make_nv_ls(":status", status));
|
2015-05-29 15:31:02 +02:00
|
|
|
nva.push_back(http2::make_nv_ll("server", NGHTTPD_SERVER));
|
2015-02-05 15:39:36 +01:00
|
|
|
nva.push_back(http2::make_nv_ls("date", sessions_->get_cached_date()));
|
2016-01-27 14:47:30 +01:00
|
|
|
|
|
|
|
std::string trailer_names;
|
|
|
|
if (data_prd) {
|
|
|
|
trailer_names = make_trailer_header_value(get_config()->trailer);
|
|
|
|
if (!trailer_names.empty()) {
|
|
|
|
nva.push_back(http2::make_nv_ls("trailer", trailer_names));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto &nv : headers) {
|
2014-11-02 15:33:04 +01:00
|
|
|
nva.push_back(http2::make_nv(nv.name, nv.value, nv.no_index));
|
2013-10-23 16:18:24 +02:00
|
|
|
}
|
2013-12-08 13:19:33 +01:00
|
|
|
int r = nghttp2_submit_response(session_, stream_id, nva.data(), nva.size(),
|
|
|
|
data_prd);
|
2013-07-22 14:30:40 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Handler::submit_response(const std::string &status, int32_t stream_id,
|
|
|
|
nghttp2_data_provider *data_prd) {
|
2016-01-27 14:47:30 +01:00
|
|
|
auto nva = make_array(http2::make_nv_ls(":status", status),
|
|
|
|
http2::make_nv_ll("server", NGHTTPD_SERVER),
|
|
|
|
http2::make_nv_ls("date", sessions_->get_cached_date()),
|
|
|
|
http2::make_nv_ll("", ""));
|
|
|
|
size_t nvlen = 3;
|
|
|
|
|
|
|
|
std::string trailer_names;
|
|
|
|
if (data_prd) {
|
|
|
|
trailer_names = make_trailer_header_value(get_config()->trailer);
|
|
|
|
if (!trailer_names.empty()) {
|
|
|
|
nva[nvlen++] = http2::make_nv_ls("trailer", trailer_names);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nghttp2_submit_response(session_, stream_id, nva.data(), nvlen,
|
2013-12-08 13:19:33 +01:00
|
|
|
data_prd);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Handler::submit_non_final_response(const std::string &status,
|
|
|
|
int32_t stream_id) {
|
2015-02-05 15:39:36 +01:00
|
|
|
auto nva = make_array(http2::make_nv_ls(":status", status));
|
2014-11-27 15:39:04 +01:00
|
|
|
return nghttp2_submit_headers(session_, NGHTTP2_FLAG_NONE, stream_id, nullptr,
|
|
|
|
nva.data(), nva.size(), nullptr);
|
2014-07-23 16:32:57 +02:00
|
|
|
}
|
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
int Http2Handler::submit_push_promise(Stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
const std::string &push_path) {
|
2015-01-02 16:12:26 +01:00
|
|
|
auto authority =
|
2015-01-04 15:22:39 +01:00
|
|
|
http2::get_header(stream->hdidx, http2::HD__AUTHORITY, stream->headers);
|
2014-04-03 04:22:11 +02:00
|
|
|
|
2015-01-02 16:12:26 +01:00
|
|
|
if (!authority) {
|
|
|
|
authority =
|
|
|
|
http2::get_header(stream->hdidx, http2::HD_HOST, stream->headers);
|
2013-12-08 16:00:12 +01:00
|
|
|
}
|
2014-04-03 04:22:11 +02:00
|
|
|
|
2015-02-05 15:39:36 +01:00
|
|
|
auto nva =
|
|
|
|
make_array(http2::make_nv_ll(":method", "GET"),
|
|
|
|
http2::make_nv_ls(":path", push_path),
|
|
|
|
get_config()->no_tls ? http2::make_nv_ll(":scheme", "http")
|
|
|
|
: http2::make_nv_ll(":scheme", "https"),
|
|
|
|
http2::make_nv_ls(":authority", authority->value));
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
auto promised_stream_id = nghttp2_submit_push_promise(
|
|
|
|
session_, NGHTTP2_FLAG_END_HEADERS, stream->stream_id, nva.data(),
|
|
|
|
nva.size(), nullptr);
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (promised_stream_id < 0) {
|
2014-05-07 16:24:07 +02:00
|
|
|
return promised_stream_id;
|
|
|
|
}
|
|
|
|
|
2015-02-05 15:21:53 +01:00
|
|
|
auto promised_stream = make_unique<Stream>(this, promised_stream_id);
|
2014-05-07 16:24:07 +02:00
|
|
|
|
2014-08-07 14:55:30 +02:00
|
|
|
append_nv(promised_stream.get(), nva);
|
2014-05-07 16:24:07 +02:00
|
|
|
add_stream(promised_stream_id, std::move(promised_stream));
|
|
|
|
|
|
|
|
return 0;
|
2013-12-08 16:00:12 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Handler::submit_rst_stream(Stream *stream, uint32_t error_code) {
|
2014-03-21 15:26:53 +01:00
|
|
|
remove_stream_read_timeout(stream);
|
|
|
|
remove_stream_write_timeout(stream);
|
2014-03-21 15:07:20 +01:00
|
|
|
|
|
|
|
return nghttp2_submit_rst_stream(session_, NGHTTP2_FLAG_NONE,
|
2014-03-21 15:26:53 +01:00
|
|
|
stream->stream_id, error_code);
|
2014-03-21 15:07:20 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Handler::add_stream(int32_t stream_id,
|
|
|
|
std::unique_ptr<Stream> stream) {
|
2014-03-21 15:26:53 +01:00
|
|
|
id2stream_[stream_id] = std::move(stream);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Handler::remove_stream(int32_t stream_id) {
|
2014-03-21 15:26:53 +01:00
|
|
|
id2stream_.erase(stream_id);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Stream *Http2Handler::get_stream(int32_t stream_id) {
|
2014-03-21 15:26:53 +01:00
|
|
|
auto itr = id2stream_.find(stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (itr == std::end(id2stream_)) {
|
2013-07-22 14:30:40 +02:00
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return (*itr).second.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int64_t Http2Handler::session_id() const { return session_id_; }
|
2013-07-22 14:30:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Sessions *Http2Handler::get_sessions() const { return sessions_; }
|
2013-07-22 14:30:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const Config *Http2Handler::get_config() const {
|
2013-07-22 14:30:40 +02:00
|
|
|
return sessions_->get_config();
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Handler::remove_settings_timer() {
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_timer_stop(sessions_->get_loop(), &settings_timerev_);
|
2013-10-27 15:02:39 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Handler::terminate_session(uint32_t error_code) {
|
2013-12-25 16:23:07 +01:00
|
|
|
nghttp2_session_terminate_session(session_, error_code);
|
2013-10-27 15:02:39 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t file_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) {
|
2015-03-07 09:50:03 +01:00
|
|
|
int rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto hd = static_cast<Http2Handler *>(user_data);
|
2014-03-21 16:36:39 +01:00
|
|
|
auto stream = hd->get_stream(stream_id);
|
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
auto nread = std::min(stream->body_length - stream->body_offset,
|
|
|
|
static_cast<int64_t>(length));
|
2014-05-02 16:34:57 +02:00
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
*data_flags |= NGHTTP2_DATA_FLAG_NO_COPY;
|
2014-03-21 16:36:39 +01:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
if (nread == 0 || stream->body_length == stream->body_offset + nread) {
|
2014-04-05 10:59:24 +02:00
|
|
|
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
2014-07-03 15:44:27 +02:00
|
|
|
|
2015-03-07 09:50:03 +01:00
|
|
|
auto config = hd->get_config();
|
|
|
|
if (!config->trailer.empty()) {
|
|
|
|
std::vector<nghttp2_nv> nva;
|
|
|
|
nva.reserve(config->trailer.size());
|
|
|
|
for (auto &kv : config->trailer) {
|
|
|
|
nva.push_back(http2::make_nv(kv.name, kv.value, kv.no_index));
|
|
|
|
}
|
|
|
|
rv = nghttp2_submit_trailer(session, stream_id, nva.data(), nva.size());
|
|
|
|
if (rv != 0) {
|
2015-03-08 08:28:23 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*data_flags |= NGHTTP2_DATA_FLAG_NO_END_STREAM;
|
2015-03-07 09:50:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_get_stream_remote_close(session, stream_id) == 0) {
|
2014-07-03 15:44:27 +02:00
|
|
|
remove_stream_read_timeout(stream);
|
|
|
|
remove_stream_write_timeout(stream);
|
|
|
|
|
|
|
|
hd->submit_rst_stream(stream, NGHTTP2_NO_ERROR);
|
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-03-21 16:36:39 +01:00
|
|
|
|
2014-05-02 16:34:57 +02:00
|
|
|
return nread;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2015-04-19 09:26:47 +02:00
|
|
|
void prepare_status_response(Stream *stream, Http2Handler *hd, int status) {
|
|
|
|
auto sessions = hd->get_sessions();
|
|
|
|
auto status_page = sessions->get_server()->get_status_page(status);
|
|
|
|
auto file_ent = &status_page->file_ent;
|
2014-07-17 16:41:54 +02:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
// we don't set stream->file_ent since we don't want to expire it.
|
|
|
|
stream->body_length = file_ent->length;
|
2014-03-04 15:29:30 +01:00
|
|
|
nghttp2_data_provider data_prd;
|
2015-04-19 09:26:47 +02:00
|
|
|
data_prd.source.fd = file_ent->fd;
|
2014-03-04 15:29:30 +01:00
|
|
|
data_prd.read_callback = file_read_callback;
|
2015-04-19 09:26:47 +02:00
|
|
|
|
|
|
|
Headers headers;
|
2014-03-04 15:29:30 +01:00
|
|
|
headers.emplace_back("content-type", "text/html; charset=UTF-8");
|
2015-04-19 09:26:47 +02:00
|
|
|
hd->submit_response(status_page->status, stream->stream_id, headers,
|
|
|
|
&data_prd);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-05-12 17:38:28 +02:00
|
|
|
namespace {
|
|
|
|
void prepare_echo_response(Stream *stream, Http2Handler *hd) {
|
|
|
|
auto length = lseek(stream->file_ent->fd, 0, SEEK_END);
|
|
|
|
if (length == -1) {
|
|
|
|
hd->submit_rst_stream(stream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stream->body_length = length;
|
|
|
|
if (lseek(stream->file_ent->fd, 0, SEEK_SET) == -1) {
|
|
|
|
hd->submit_rst_stream(stream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nghttp2_data_provider data_prd;
|
|
|
|
data_prd.source.fd = stream->file_ent->fd;
|
|
|
|
data_prd.read_callback = file_read_callback;
|
|
|
|
|
|
|
|
Headers headers;
|
|
|
|
headers.emplace_back("nghttpd-response", "echo");
|
2016-01-07 14:51:47 +01:00
|
|
|
if (!hd->get_config()->no_content_length) {
|
|
|
|
headers.emplace_back("content-length", util::utos(length));
|
|
|
|
}
|
2015-05-12 17:38:28 +02:00
|
|
|
|
|
|
|
hd->submit_response("200", stream->stream_id, headers, &data_prd);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
bool prepare_upload_temp_store(Stream *stream, Http2Handler *hd) {
|
|
|
|
auto sessions = hd->get_sessions();
|
|
|
|
|
|
|
|
char tempfn[] = "/tmp/nghttpd.temp.XXXXXX";
|
|
|
|
auto fd = mkstemp(tempfn);
|
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unlink(tempfn);
|
|
|
|
// Ordinary request never start with "echo:". The length is 0 for
|
|
|
|
// now. We will update it when we get whole request body.
|
2015-11-18 15:21:57 +01:00
|
|
|
auto path = std::string("echo:") + tempfn;
|
|
|
|
stream->file_ent =
|
2015-11-20 15:29:12 +01:00
|
|
|
sessions->cache_fd(path, FileEntry(path, 0, 0, fd, nullptr, 0, true));
|
2015-05-12 17:38:28 +02:00
|
|
|
stream->echo_upload = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-02 15:27:38 +01:00
|
|
|
namespace {
|
|
|
|
void prepare_redirect_response(Stream *stream, Http2Handler *hd,
|
2015-04-19 09:26:47 +02:00
|
|
|
const std::string &path, int status) {
|
2015-01-02 16:12:26 +01:00
|
|
|
auto scheme =
|
2015-01-04 15:22:39 +01:00
|
|
|
http2::get_header(stream->hdidx, http2::HD__SCHEME, stream->headers);
|
2015-01-02 16:12:26 +01:00
|
|
|
auto authority =
|
2015-01-04 15:22:39 +01:00
|
|
|
http2::get_header(stream->hdidx, http2::HD__AUTHORITY, stream->headers);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!authority) {
|
2015-01-02 16:12:26 +01:00
|
|
|
authority =
|
|
|
|
http2::get_header(stream->hdidx, http2::HD_HOST, stream->headers);
|
2014-11-02 15:27:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto redirect_url = scheme->value;
|
|
|
|
redirect_url += "://";
|
|
|
|
redirect_url += authority->value;
|
|
|
|
redirect_url += path;
|
|
|
|
|
2014-11-02 15:33:04 +01:00
|
|
|
auto headers = Headers{{"location", redirect_url}};
|
2014-11-02 15:27:38 +01:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
auto sessions = hd->get_sessions();
|
|
|
|
auto status_page = sessions->get_server()->get_status_page(status);
|
|
|
|
|
|
|
|
hd->submit_response(status_page->status, stream->stream_id, headers, nullptr);
|
2014-11-02 15:27:38 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void prepare_response(Stream *stream, Http2Handler *hd,
|
|
|
|
bool allow_push = true) {
|
2013-12-08 16:00:12 +01:00
|
|
|
int rv;
|
2015-11-21 04:54:04 +01:00
|
|
|
auto pathhdr =
|
|
|
|
http2::get_header(stream->hdidx, http2::HD__PATH, stream->headers);
|
|
|
|
if (!pathhdr) {
|
|
|
|
prepare_status_response(stream, hd, 405);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto reqpath = pathhdr->value;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto ims =
|
2015-01-02 16:12:26 +01:00
|
|
|
get_header(stream->hdidx, http2::HD_IF_MODIFIED_SINCE, stream->headers);
|
2014-04-03 04:22:11 +02:00
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
time_t last_mod = 0;
|
|
|
|
bool last_mod_found = false;
|
2015-01-02 16:12:26 +01:00
|
|
|
if (ims) {
|
2014-11-27 15:39:04 +01:00
|
|
|
last_mod_found = true;
|
2015-01-02 16:12:26 +01:00
|
|
|
last_mod = util::parse_http_date(ims->value);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-11-02 15:27:38 +01:00
|
|
|
auto query_pos = reqpath.find("?");
|
|
|
|
std::string url;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (query_pos != std::string::npos) {
|
2013-07-22 14:30:40 +02:00
|
|
|
// Do not response to this request to allow clients to test timeouts.
|
2014-11-27 15:39:04 +01:00
|
|
|
if (reqpath.find("nghttpd_do_not_respond_to_req=yes", query_pos) !=
|
|
|
|
std::string::npos) {
|
2013-07-22 14:30:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-11-02 15:27:38 +01:00
|
|
|
url = reqpath.substr(0, query_pos);
|
|
|
|
} else {
|
|
|
|
url = reqpath;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-11-02 15:27:38 +01:00
|
|
|
|
2015-05-12 17:38:28 +02:00
|
|
|
auto sessions = hd->get_sessions();
|
|
|
|
|
2015-11-27 16:14:11 +01:00
|
|
|
url = util::percent_decode(std::begin(url), std::end(url));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!util::check_path(url)) {
|
2015-05-12 17:38:28 +02:00
|
|
|
if (stream->file_ent) {
|
2015-11-20 15:29:12 +01:00
|
|
|
sessions->release_fd(stream->file_ent);
|
2015-05-12 17:38:28 +02:00
|
|
|
stream->file_ent = nullptr;
|
|
|
|
}
|
2015-04-19 09:26:47 +02:00
|
|
|
prepare_status_response(stream, hd, 404);
|
2013-07-22 14:30:40 +02:00
|
|
|
return;
|
|
|
|
}
|
2013-12-08 16:00:12 +01:00
|
|
|
auto push_itr = hd->get_config()->push.find(url);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (allow_push && push_itr != std::end(hd->get_config()->push)) {
|
|
|
|
for (auto &push_path : (*push_itr).second) {
|
2014-03-21 15:26:53 +01:00
|
|
|
rv = hd->submit_push_promise(stream, push_path);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-12-08 16:00:12 +01:00
|
|
|
std::cerr << "nghttp2_submit_push_promise() returned error: "
|
|
|
|
<< nghttp2_strerror(rv) << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 17:38:28 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
std::string path = hd->get_config()->htdocs + url;
|
|
|
|
if (path[path.size() - 1] == '/') {
|
2013-07-22 14:30:40 +02:00
|
|
|
path += DEFAULT_HTML;
|
|
|
|
}
|
2014-06-07 09:04:43 +02:00
|
|
|
|
2015-05-12 17:38:28 +02:00
|
|
|
if (stream->echo_upload) {
|
|
|
|
assert(stream->file_ent);
|
|
|
|
prepare_echo_response(stream, hd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
auto file_ent = sessions->get_cached_fd(path);
|
2014-06-07 09:04:43 +02:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
if (file_ent == nullptr) {
|
|
|
|
int file = open(path.c_str(), O_RDONLY | O_BINARY);
|
|
|
|
if (file == -1) {
|
|
|
|
prepare_status_response(stream, hd, 404);
|
2014-06-07 09:04:43 +02:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-06-07 09:04:43 +02:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
struct stat buf;
|
2014-06-07 09:04:43 +02:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
if (fstat(file, &buf) == -1) {
|
|
|
|
close(file);
|
|
|
|
prepare_status_response(stream, hd, 404);
|
2014-11-08 15:07:40 +01:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
return;
|
2014-11-02 15:27:38 +01:00
|
|
|
}
|
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
if (buf.st_mode & S_IFDIR) {
|
|
|
|
close(file);
|
2014-11-02 15:27:38 +01:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
if (query_pos == std::string::npos) {
|
2015-11-27 16:03:16 +01:00
|
|
|
reqpath += '/';
|
2015-04-19 09:26:47 +02:00
|
|
|
} else {
|
|
|
|
reqpath.insert(query_pos, "/");
|
|
|
|
}
|
2014-11-02 15:27:38 +01:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
prepare_redirect_response(stream, hd, reqpath, 301);
|
2014-06-07 09:04:43 +02:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-06-07 09:04:43 +02:00
|
|
|
|
2015-10-28 16:21:36 +01:00
|
|
|
const std::string *content_type = nullptr;
|
|
|
|
|
|
|
|
if (path[path.size() - 1] == '/') {
|
|
|
|
static const std::string TEXT_HTML = "text/html";
|
|
|
|
content_type = &TEXT_HTML;
|
|
|
|
} else {
|
|
|
|
auto ext = path.c_str() + path.size() - 1;
|
|
|
|
for (; path.c_str() < ext && *ext != '.' && *ext != '/'; --ext)
|
|
|
|
;
|
|
|
|
if (*ext == '.') {
|
|
|
|
++ext;
|
|
|
|
|
|
|
|
const auto &mime_types = hd->get_config()->mime_types;
|
|
|
|
auto content_type_itr = mime_types.find(ext);
|
|
|
|
if (content_type_itr != std::end(mime_types)) {
|
|
|
|
content_type = &(*content_type_itr).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
file_ent = sessions->cache_fd(
|
2015-11-20 15:29:12 +01:00
|
|
|
path, FileEntry(path, buf.st_size, buf.st_mtime, file, content_type,
|
|
|
|
ev_now(sessions->get_loop())));
|
2015-08-17 16:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->file_ent = file_ent;
|
|
|
|
|
|
|
|
if (last_mod_found && file_ent->mtime <= last_mod) {
|
2015-07-24 16:25:10 +02:00
|
|
|
hd->submit_response("304", stream->stream_id, nullptr);
|
2014-06-07 09:04:43 +02:00
|
|
|
|
|
|
|
return;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-06-07 09:04:43 +02:00
|
|
|
|
2015-08-17 16:47:35 +02:00
|
|
|
auto &method = http2::get_header(stream->hdidx, http2::HD__METHOD,
|
|
|
|
stream->headers)->value;
|
|
|
|
if (method == "HEAD") {
|
|
|
|
hd->submit_file_response("200", stream, file_ent->mtime, file_ent->length,
|
2015-10-28 16:21:36 +01:00
|
|
|
file_ent->content_type, nullptr);
|
2015-08-17 16:47:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
stream->body_length = file_ent->length;
|
|
|
|
|
|
|
|
nghttp2_data_provider data_prd;
|
|
|
|
|
|
|
|
data_prd.source.fd = file_ent->fd;
|
|
|
|
data_prd.read_callback = file_read_callback;
|
|
|
|
|
|
|
|
hd->submit_file_response("200", stream, file_ent->mtime, file_ent->length,
|
2015-10-28 16:21:36 +01:00
|
|
|
file_ent->content_type, &data_prd);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
2014-01-16 15:41:13 +01: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 hd = static_cast<Http2Handler *>(user_data);
|
|
|
|
if (hd->get_config()->verbose) {
|
2014-01-27 16:20:48 +01:00
|
|
|
print_session_id(hd->session_id());
|
2014-01-16 15:41:13 +01:00
|
|
|
verbose_on_header_callback(session, frame, name, namelen, value, valuelen,
|
2014-04-01 19:10:35 +02:00
|
|
|
flags, user_data);
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
auto stream = hd->get_stream(frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-01-15 14:45:18 +01:00
|
|
|
|
2015-01-04 15:22:39 +01:00
|
|
|
auto token = http2::lookup_token(name, namelen);
|
|
|
|
|
|
|
|
http2::index_header(stream->hdidx, token, stream->headers.size());
|
2014-07-12 11:55:08 +02:00
|
|
|
http2::add_header(stream->headers, name, namelen, value, valuelen,
|
2015-02-08 06:07:01 +01:00
|
|
|
flags & NGHTTP2_NV_FLAG_NO_INDEX, token);
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-01-29 13:23:13 +01:00
|
|
|
int on_begin_headers_callback(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
|
|
|
auto hd = static_cast<Http2Handler *>(user_data);
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2015-02-05 15:21:53 +01:00
|
|
|
auto stream = make_unique<Stream>(hd, frame->hd.stream_id);
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
add_stream_read_timeout(stream.get());
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
hd->add_stream(frame->hd.stream_id, std::move(stream));
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int hd_on_frame_recv_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
|
|
|
auto hd = static_cast<Http2Handler *>(user_data);
|
|
|
|
if (hd->get_config()->verbose) {
|
2013-07-22 14:30:40 +02:00
|
|
|
print_session_id(hd->session_id());
|
2013-12-20 15:48:56 +01:00
|
|
|
verbose_on_frame_recv_callback(session, frame, user_data);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (frame->hd.type) {
|
2014-03-21 16:36:39 +01:00
|
|
|
case NGHTTP2_DATA: {
|
2014-01-27 14:13:41 +01:00
|
|
|
// TODO Handle POST
|
2014-03-21 16:36:39 +01:00
|
|
|
auto stream = hd->get_stream(frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2014-03-21 16:36:39 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
2014-03-21 16:36:39 +01:00
|
|
|
remove_stream_read_timeout(stream);
|
2015-05-12 17:38:28 +02:00
|
|
|
if (stream->echo_upload || !hd->get_config()->early_response) {
|
2014-07-03 15:44:27 +02:00
|
|
|
prepare_response(stream, hd);
|
|
|
|
}
|
2014-03-21 16:36:39 +01:00
|
|
|
} else {
|
|
|
|
add_stream_read_timeout(stream);
|
2014-01-29 13:23:13 +01:00
|
|
|
}
|
2014-03-21 16:36:39 +01:00
|
|
|
|
2014-01-27 14:13:41 +01:00
|
|
|
break;
|
2014-03-21 16:36:39 +01:00
|
|
|
}
|
2014-05-24 08:02:46 +02:00
|
|
|
case NGHTTP2_HEADERS: {
|
|
|
|
auto stream = hd->get_stream(frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2014-05-24 08:02:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2015-01-02 16:12:26 +01:00
|
|
|
auto expect100 =
|
|
|
|
http2::get_header(stream->hdidx, http2::HD_EXPECT, stream->headers);
|
2014-07-23 16:32:57 +02:00
|
|
|
|
2015-02-22 07:32:48 +01:00
|
|
|
if (expect100 && util::strieq_l("100-continue", expect100->value)) {
|
2014-07-23 16:32:57 +02:00
|
|
|
hd->submit_non_final_response("100", frame->hd.stream_id);
|
|
|
|
}
|
|
|
|
|
2015-05-12 17:38:28 +02:00
|
|
|
auto &method = http2::get_header(stream->hdidx, http2::HD__METHOD,
|
|
|
|
stream->headers)->value;
|
|
|
|
if (hd->get_config()->echo_upload &&
|
|
|
|
(method == "POST" || method == "PUT")) {
|
|
|
|
if (!prepare_upload_temp_store(stream, hd)) {
|
|
|
|
hd->submit_rst_stream(stream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (hd->get_config()->early_response) {
|
2014-07-03 15:44:27 +02:00
|
|
|
prepare_response(stream, hd);
|
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-05-24 08:02:46 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
2014-05-24 08:02:46 +02:00
|
|
|
remove_stream_read_timeout(stream);
|
2015-05-12 17:38:28 +02:00
|
|
|
if (stream->echo_upload || !hd->get_config()->early_response) {
|
2014-07-03 15:44:27 +02:00
|
|
|
prepare_response(stream, hd);
|
|
|
|
}
|
2014-05-24 08:02:46 +02:00
|
|
|
} else {
|
|
|
|
add_stream_read_timeout(stream);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-05-24 08:02:46 +02:00
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
break;
|
2014-05-24 08:02:46 +02:00
|
|
|
}
|
2013-10-27 15:02:39 +01:00
|
|
|
case NGHTTP2_SETTINGS:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
|
2013-10-27 15:02:39 +01:00
|
|
|
hd->remove_settings_timer();
|
|
|
|
}
|
|
|
|
break;
|
2013-07-22 14:30:40 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-08-29 14:03:39 +02:00
|
|
|
return 0;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-12-08 16:22:01 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int hd_on_frame_send_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
|
|
|
auto hd = static_cast<Http2Handler *>(user_data);
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (hd->get_config()->verbose) {
|
2013-07-22 14:30:40 +02:00
|
|
|
print_session_id(hd->session_id());
|
2013-12-20 15:48:56 +01:00
|
|
|
verbose_on_frame_send_callback(session, frame, user_data);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (frame->hd.type) {
|
2014-03-21 15:07:20 +01:00
|
|
|
case NGHTTP2_DATA:
|
|
|
|
case NGHTTP2_HEADERS: {
|
|
|
|
auto stream = hd->get_stream(frame->hd.stream_id);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2014-03-21 15:07:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
2014-03-21 16:36:39 +01:00
|
|
|
remove_stream_write_timeout(stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (std::min(nghttp2_session_get_stream_remote_window_size(
|
|
|
|
session, frame->hd.stream_id),
|
|
|
|
nghttp2_session_get_remote_window_size(session)) <= 0) {
|
2014-03-21 16:36:39 +01:00
|
|
|
// If stream is blocked by flow control, enable write timeout.
|
|
|
|
add_stream_read_timeout_if_pending(stream);
|
2014-03-21 15:07:20 +01:00
|
|
|
add_stream_write_timeout(stream);
|
2014-03-21 16:36:39 +01:00
|
|
|
} else {
|
|
|
|
add_stream_read_timeout_if_pending(stream);
|
|
|
|
remove_stream_write_timeout(stream);
|
2014-03-21 15:07:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
|
|
|
auto promised_stream_id = frame->push_promise.promised_stream_id;
|
2014-03-21 16:36:39 +01:00
|
|
|
auto promised_stream = hd->get_stream(promised_stream_id);
|
|
|
|
auto stream = hd->get_stream(frame->hd.stream_id);
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream || !promised_stream) {
|
2014-03-21 15:07:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-21 16:36:39 +01:00
|
|
|
add_stream_read_timeout_if_pending(stream);
|
|
|
|
add_stream_write_timeout(stream);
|
|
|
|
|
|
|
|
prepare_response(promised_stream, hd, /*allow_push */ false);
|
2014-03-21 15:07:20 +01:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 14:48:34 +02:00
|
|
|
return 0;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
namespace {
|
|
|
|
int send_data_callback(nghttp2_session *session, nghttp2_frame *frame,
|
|
|
|
const uint8_t *framehd, size_t length,
|
|
|
|
nghttp2_data_source *source, void *user_data) {
|
|
|
|
auto hd = static_cast<Http2Handler *>(user_data);
|
|
|
|
auto wb = hd->get_wb();
|
|
|
|
auto padlen = frame->data.padlen;
|
2015-04-19 09:26:47 +02:00
|
|
|
auto stream = hd->get_stream(frame->hd.stream_id);
|
2015-04-03 16:10:51 +02:00
|
|
|
|
|
|
|
if (wb->wleft() < 9 + length + padlen) {
|
|
|
|
return NGHTTP2_ERR_WOULDBLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = source->fd;
|
|
|
|
|
|
|
|
auto p = wb->last;
|
|
|
|
|
|
|
|
p = std::copy_n(framehd, 9, p);
|
|
|
|
|
|
|
|
if (padlen) {
|
|
|
|
*p++ = padlen - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (length) {
|
|
|
|
ssize_t nread;
|
2015-04-19 09:26:47 +02:00
|
|
|
while ((nread = pread(fd, p, length, stream->body_offset)) == -1 &&
|
|
|
|
errno == EINTR)
|
2015-04-03 16:10:51 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
if (nread == -1) {
|
|
|
|
remove_stream_read_timeout(stream);
|
|
|
|
remove_stream_write_timeout(stream);
|
|
|
|
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
stream->body_offset += nread;
|
2015-04-03 16:10:51 +02:00
|
|
|
length -= nread;
|
|
|
|
p += nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (padlen) {
|
|
|
|
std::fill(p, p + padlen - 1, 0);
|
|
|
|
p += padlen - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
wb->last = p;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-02-11 07:28:44 +01:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t select_padding_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, size_t max_payload,
|
|
|
|
void *user_data) {
|
|
|
|
auto hd = static_cast<Http2Handler *>(user_data);
|
2014-02-15 08:40:32 +01:00
|
|
|
return std::min(max_payload, frame->hd.length + hd->get_config()->padding);
|
2014-02-11 07:28:44 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id, const uint8_t *data,
|
|
|
|
size_t len, void *user_data) {
|
|
|
|
auto hd = static_cast<Http2Handler *>(user_data);
|
2014-03-21 15:26:53 +01:00
|
|
|
auto stream = hd->get_stream(stream_id);
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2014-03-21 15:07:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-12 17:38:28 +02:00
|
|
|
if (stream->echo_upload) {
|
|
|
|
assert(stream->file_ent);
|
|
|
|
while (len) {
|
|
|
|
ssize_t n;
|
|
|
|
while ((n = write(stream->file_ent->fd, data, len)) == -1 &&
|
|
|
|
errno == EINTR)
|
|
|
|
;
|
|
|
|
if (n == -1) {
|
|
|
|
hd->submit_rst_stream(stream, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len -= n;
|
|
|
|
data += n;
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
// TODO Handle POST
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
add_stream_read_timeout(stream);
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2013-08-29 14:18:40 +02:00
|
|
|
return 0;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
} // 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 hd = static_cast<Http2Handler *>(user_data);
|
2013-07-22 14:30:40 +02:00
|
|
|
hd->remove_stream(stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (hd->get_config()->verbose) {
|
2013-07-22 14:30:40 +02:00
|
|
|
print_session_id(hd->session_id());
|
|
|
|
print_timer();
|
|
|
|
printf(" stream_id=%d closed\n", stream_id);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2013-08-29 15:58:05 +02:00
|
|
|
return 0;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void fill_callback(nghttp2_session_callbacks *callbacks, const Config *config) {
|
|
|
|
nghttp2_session_callbacks_set_on_stream_close_callback(
|
|
|
|
callbacks, on_stream_close_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_frame_recv_callback(
|
|
|
|
callbacks, hd_on_frame_recv_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_frame_send_callback(
|
|
|
|
callbacks, hd_on_frame_send_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config->verbose) {
|
|
|
|
nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(
|
|
|
|
callbacks, verbose_on_invalid_frame_recv_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
|
|
|
|
callbacks, on_data_chunk_recv_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_header_callback(callbacks,
|
|
|
|
on_header_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_begin_headers_callback(
|
|
|
|
callbacks, on_begin_headers_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
nghttp2_session_callbacks_set_send_data_callback(callbacks,
|
|
|
|
send_data_callback);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config->padding) {
|
|
|
|
nghttp2_session_callbacks_set_select_padding_callback(
|
|
|
|
callbacks, select_padding_callback);
|
2014-02-11 08:48:27 +01:00
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-03-04 15:14:26 +01:00
|
|
|
struct ClientInfo {
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
struct Worker {
|
|
|
|
std::unique_ptr<Sessions> sessions;
|
|
|
|
ev_async w;
|
|
|
|
// protectes q
|
|
|
|
std::mutex m;
|
|
|
|
std::deque<ClientInfo> q;
|
|
|
|
};
|
|
|
|
|
2014-03-04 15:14:26 +01:00
|
|
|
namespace {
|
2014-12-23 16:05:45 +01:00
|
|
|
void worker_acceptcb(struct ev_loop *loop, ev_async *w, int revents) {
|
|
|
|
auto worker = static_cast<Worker *>(w->data);
|
|
|
|
auto &sessions = worker->sessions;
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
std::deque<ClientInfo> q;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(worker->m);
|
|
|
|
q.swap(worker->q);
|
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
for (auto c : q) {
|
2014-12-23 16:05:45 +01:00
|
|
|
sessions->accept_connection(c.fd);
|
2014-03-04 15:14:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-01-08 13:41:26 +01:00
|
|
|
namespace {
|
|
|
|
void run_worker(Worker *worker) {
|
|
|
|
auto loop = worker->sessions->get_loop();
|
|
|
|
|
|
|
|
ev_run(loop, 0);
|
|
|
|
}
|
2014-03-04 15:14:26 +01:00
|
|
|
} // namespace
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
class AcceptHandler {
|
2013-07-22 14:30:40 +02:00
|
|
|
public:
|
2015-04-19 09:26:47 +02:00
|
|
|
AcceptHandler(HttpServer *sv, Sessions *sessions, const Config *config)
|
2014-11-27 15:39:04 +01:00
|
|
|
: sessions_(sessions), config_(config), next_worker_(0) {
|
|
|
|
if (config_->num_worker == 1) {
|
2014-03-04 15:14:26 +01:00
|
|
|
return;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
for (size_t i = 0; i < config_->num_worker; ++i) {
|
|
|
|
if (config_->verbose) {
|
2014-03-04 15:14:26 +01:00
|
|
|
std::cerr << "spawning thread #" << i << std::endl;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2015-02-05 15:21:53 +01:00
|
|
|
auto worker = make_unique<Worker>();
|
2014-12-23 16:05:45 +01:00
|
|
|
auto loop = ev_loop_new(0);
|
|
|
|
worker->sessions =
|
2015-04-19 09:26:47 +02:00
|
|
|
make_unique<Sessions>(sv, loop, config_, sessions_->get_ssl_ctx());
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_async_init(&worker->w, worker_acceptcb);
|
|
|
|
worker->w.data = worker.get();
|
|
|
|
ev_async_start(loop, &worker->w);
|
|
|
|
|
|
|
|
auto t = std::thread(run_worker, worker.get());
|
2014-03-04 15:14:26 +01:00
|
|
|
t.detach();
|
2014-12-23 16:05:45 +01:00
|
|
|
workers_.push_back(std::move(worker));
|
2014-03-04 15:14:26 +01:00
|
|
|
}
|
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
void accept_connection(int fd) {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config_->num_worker == 1) {
|
2014-03-04 15:14:26 +01:00
|
|
|
sessions_->accept_connection(fd);
|
|
|
|
return;
|
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
|
2014-03-04 15:14:26 +01:00
|
|
|
// Dispatch client to the one of the worker threads, in a round
|
|
|
|
// robin manner.
|
2014-12-23 16:05:45 +01:00
|
|
|
auto &worker = workers_[next_worker_];
|
2014-11-27 15:39:04 +01:00
|
|
|
if (next_worker_ == config_->num_worker - 1) {
|
2014-03-04 15:14:26 +01:00
|
|
|
next_worker_ = 0;
|
|
|
|
} else {
|
|
|
|
++next_worker_;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(worker->m);
|
|
|
|
worker->q.push_back({fd});
|
|
|
|
}
|
|
|
|
ev_async_send(worker->sessions->get_loop(), &worker->w);
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
private:
|
2014-12-23 16:05:45 +01:00
|
|
|
std::vector<std::unique_ptr<Worker>> workers_;
|
2013-07-22 14:30:40 +02:00
|
|
|
Sessions *sessions_;
|
2014-03-04 15:14:26 +01:00
|
|
|
const Config *config_;
|
|
|
|
// In multi threading mode, this points to the next thread that
|
|
|
|
// client will be dispatched.
|
|
|
|
size_t next_worker_;
|
2013-07-22 14:30:40 +02:00
|
|
|
};
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
namespace {
|
|
|
|
void acceptcb(struct ev_loop *loop, ev_io *w, int revents);
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class ListenEventHandler {
|
|
|
|
public:
|
|
|
|
ListenEventHandler(Sessions *sessions, int fd,
|
|
|
|
std::shared_ptr<AcceptHandler> acceptor)
|
|
|
|
: acceptor_(acceptor), sessions_(sessions), fd_(fd) {
|
|
|
|
ev_io_init(&w_, acceptcb, fd, EV_READ);
|
|
|
|
w_.data = this;
|
|
|
|
ev_io_start(sessions_->get_loop(), &w_);
|
|
|
|
}
|
|
|
|
void accept_connection() {
|
|
|
|
for (;;) {
|
2015-01-05 07:59:51 +01:00
|
|
|
#ifdef HAVE_ACCEPT4
|
|
|
|
auto fd = accept4(fd_, nullptr, nullptr, SOCK_NONBLOCK);
|
|
|
|
#else // !HAVE_ACCEPT4
|
2014-12-23 16:05:45 +01:00
|
|
|
auto fd = accept(fd_, nullptr, nullptr);
|
2015-01-05 07:59:51 +01:00
|
|
|
#endif // !HAVE_ACCEPT4
|
2014-12-23 16:05:45 +01:00
|
|
|
if (fd == -1) {
|
2015-03-10 16:11:51 +01:00
|
|
|
break;
|
2014-12-23 16:05:45 +01:00
|
|
|
}
|
2015-01-05 07:59:51 +01:00
|
|
|
#ifndef HAVE_ACCEPT4
|
|
|
|
util::make_socket_nonblocking(fd);
|
|
|
|
#endif // !HAVE_ACCEPT4
|
2014-12-23 16:05:45 +01:00
|
|
|
acceptor_->accept_connection(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ev_io w_;
|
|
|
|
std::shared_ptr<AcceptHandler> acceptor_;
|
|
|
|
Sessions *sessions_;
|
|
|
|
int fd_;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
void acceptcb(struct ev_loop *loop, ev_io *w, int revents) {
|
|
|
|
auto handler = static_cast<ListenEventHandler *>(w->data);
|
|
|
|
handler->accept_connection();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
namespace {
|
|
|
|
FileEntry make_status_body(int status, uint16_t port) {
|
|
|
|
std::string body;
|
|
|
|
body = "<html><head><title>";
|
|
|
|
body += http2::get_status_string(status);
|
|
|
|
body += "</title></head><body><h1>";
|
|
|
|
body += http2::get_status_string(status);
|
|
|
|
body += "</h1><hr><address>";
|
|
|
|
body += NGHTTPD_SERVER;
|
|
|
|
body += " at port ";
|
|
|
|
body += util::utos(port);
|
|
|
|
body += "</address>";
|
|
|
|
body += "</body></html>";
|
|
|
|
|
|
|
|
char tempfn[] = "/tmp/nghttpd.temp.XXXXXX";
|
|
|
|
int fd = mkstemp(tempfn);
|
|
|
|
if (fd == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
std::cerr << "Could not open status response body file: errno=" << error;
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
unlink(tempfn);
|
|
|
|
ssize_t nwrite;
|
|
|
|
while ((nwrite = write(fd, body.c_str(), body.size())) == -1 &&
|
|
|
|
errno == EINTR)
|
|
|
|
;
|
|
|
|
if (nwrite == -1) {
|
|
|
|
auto error = errno;
|
|
|
|
std::cerr << "Could not write status response body into file: errno="
|
|
|
|
<< error;
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:29:12 +01:00
|
|
|
return FileEntry(util::utos(status), nwrite, 0, fd, nullptr, 0);
|
2015-04-19 09:26:47 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// index into HttpServer::status_pages_
|
|
|
|
enum {
|
|
|
|
IDX_200,
|
|
|
|
IDX_301,
|
|
|
|
IDX_400,
|
|
|
|
IDX_404,
|
2015-11-21 04:54:04 +01:00
|
|
|
IDX_405,
|
2015-04-19 09:26:47 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
HttpServer::HttpServer(const Config *config) : config_(config) {
|
|
|
|
status_pages_ = std::vector<StatusPage>{
|
|
|
|
{"200", make_status_body(200, config_->port)},
|
|
|
|
{"301", make_status_body(301, config_->port)},
|
|
|
|
{"400", make_status_body(400, config_->port)},
|
|
|
|
{"404", make_status_body(404, config_->port)},
|
2015-11-21 04:54:04 +01:00
|
|
|
{"405", make_status_body(405, config_->port)},
|
2015-04-19 09:26:47 +02:00
|
|
|
};
|
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
int next_proto_cb(SSL *s, const unsigned char **data, unsigned int *len,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *arg) {
|
|
|
|
auto next_proto = static_cast<std::vector<unsigned char> *>(arg);
|
2014-11-17 11:01:06 +01:00
|
|
|
*data = next_proto->data();
|
|
|
|
*len = next_proto->size();
|
2013-07-22 14:30:40 +02:00
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) {
|
2013-07-22 14:30:40 +02:00
|
|
|
// We don't verify the client certificate. Just request it for the
|
|
|
|
// testing purpose.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2015-04-19 09:26:47 +02:00
|
|
|
int start_listen(HttpServer *sv, struct ev_loop *loop, Sessions *sessions,
|
2014-12-23 16:05:45 +01:00
|
|
|
const Config *config) {
|
2013-07-22 14:30:40 +02:00
|
|
|
int r;
|
2014-12-23 16:05:45 +01:00
|
|
|
bool ok = false;
|
2015-01-24 21:26:49 +01:00
|
|
|
const char *addr = nullptr;
|
2014-05-14 15:39:28 +02:00
|
|
|
|
2015-10-21 15:27:15 +02:00
|
|
|
std::shared_ptr<AcceptHandler> acceptor;
|
2014-05-14 15:39:28 +02:00
|
|
|
auto service = util::utos(config->port);
|
|
|
|
|
2015-07-19 10:55:37 +02:00
|
|
|
addrinfo hints{};
|
2013-07-22 14:30:40 +02:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
#ifdef AI_ADDRCONFIG
|
|
|
|
hints.ai_flags |= AI_ADDRCONFIG;
|
|
|
|
#endif // AI_ADDRCONFIG
|
|
|
|
|
2015-01-24 21:26:49 +01:00
|
|
|
if (!config->address.empty()) {
|
|
|
|
addr = config->address.c_str();
|
|
|
|
}
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
addrinfo *res, *rp;
|
2015-01-24 21:26:49 +01:00
|
|
|
r = getaddrinfo(addr, service.c_str(), &hints, &res);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (r != 0) {
|
2013-07-22 14:30:40 +02:00
|
|
|
std::cerr << "getaddrinfo() failed: " << gai_strerror(r) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-24 21:26:49 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (rp = res; rp; rp = rp->ai_next) {
|
2013-07-22 14:30:40 +02:00
|
|
|
int fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (fd == -1) {
|
2013-07-22 14:30:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int val = 1;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
2013-07-22 14:30:40 +02:00
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-06 16:26:00 +01:00
|
|
|
(void)util::make_socket_nonblocking(fd);
|
2013-07-22 14:30:40 +02:00
|
|
|
#ifdef IPV6_V6ONLY
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rp->ai_family == AF_INET6) {
|
|
|
|
if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val,
|
|
|
|
static_cast<socklen_t>(sizeof(val))) == -1) {
|
2013-07-22 14:30:40 +02:00
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // IPV6_V6ONLY
|
2014-12-23 16:05:45 +01:00
|
|
|
if (bind(fd, rp->ai_addr, rp->ai_addrlen) == 0 && listen(fd, 1000) == 0) {
|
2015-10-21 15:27:15 +02:00
|
|
|
if (!acceptor) {
|
|
|
|
acceptor = std::make_shared<AcceptHandler>(sv, sessions, config);
|
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
new ListenEventHandler(sessions, fd, acceptor);
|
2014-04-05 13:04:09 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config->verbose) {
|
2015-02-26 14:59:25 +01:00
|
|
|
std::string s = util::numeric_name(rp->ai_addr, rp->ai_addrlen);
|
2015-01-24 21:26:49 +01:00
|
|
|
std::cout << (rp->ai_family == AF_INET ? "IPv4" : "IPv6") << ": listen "
|
|
|
|
<< s << ":" << config->port << std::endl;
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
2014-12-23 16:05:45 +01:00
|
|
|
ok = true;
|
2013-07-22 14:30:40 +02:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
std::cerr << strerror(errno) << std::endl;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
freeaddrinfo(res);
|
2014-12-15 15:17:50 +01:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
if (!ok) {
|
2014-12-15 15:17:50 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-01-01 15:54:28 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int alpn_select_proto_cb(SSL *ssl, const unsigned char **out,
|
|
|
|
unsigned char *outlen, const unsigned char *in,
|
|
|
|
unsigned int inlen, void *arg) {
|
|
|
|
auto config = static_cast<HttpServer *>(arg)->get_config();
|
|
|
|
if (config->verbose) {
|
2014-01-01 15:54:28 +01:00
|
|
|
std::cout << "[ALPN] client offers:" << std::endl;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config->verbose) {
|
|
|
|
for (unsigned int i = 0; i < inlen; i += in [i] + 1) {
|
2014-01-01 15:54:28 +01:00
|
|
|
std::cout << " * ";
|
2014-11-27 15:39:04 +01:00
|
|
|
std::cout.write(reinterpret_cast<const char *>(&in[i + 1]), in[i]);
|
2014-01-01 15:54:28 +01:00
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2014-12-15 14:51:34 +01:00
|
|
|
if (!util::select_h2(out, outlen, in, inlen)) {
|
2014-01-01 15:54:28 +01:00
|
|
|
return SSL_TLSEXT_ERR_NOACK;
|
|
|
|
}
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int HttpServer::run() {
|
2013-07-22 14:30:40 +02:00
|
|
|
SSL_CTX *ssl_ctx = nullptr;
|
2014-11-17 11:01:06 +01:00
|
|
|
std::vector<unsigned char> next_proto;
|
2014-11-14 15:14:39 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!config_->no_tls) {
|
2013-07-22 14:30:40 +02:00
|
|
|
ssl_ctx = SSL_CTX_new(SSLv23_server_method());
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!ssl_ctx) {
|
2013-07-22 14:30:40 +02:00
|
|
|
std::cerr << ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-03 15:49:13 +01:00
|
|
|
|
2015-06-22 16:26:45 +02:00
|
|
|
auto ssl_opts = (SSL_OP_ALL & ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) |
|
|
|
|
SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION |
|
|
|
|
SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
|
|
|
|
SSL_OP_SINGLE_ECDH_USE | SSL_OP_NO_TICKET |
|
|
|
|
SSL_OP_CIPHER_SERVER_PREFERENCE;
|
|
|
|
|
|
|
|
SSL_CTX_set_options(ssl_ctx, ssl_opts);
|
2013-07-22 14:30:40 +02:00
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_AUTO_RETRY);
|
|
|
|
SSL_CTX_set_mode(ssl_ctx, SSL_MODE_RELEASE_BUFFERS);
|
2014-03-03 15:49:13 +01:00
|
|
|
|
2015-01-06 16:26:00 +01:00
|
|
|
if (SSL_CTX_set_cipher_list(ssl_ctx, ssl::DEFAULT_CIPHER_LIST) == 0) {
|
|
|
|
std::cerr << ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2014-03-03 15:49:13 +01:00
|
|
|
const unsigned char sid_ctx[] = "nghttpd";
|
2014-11-27 15:39:04 +01:00
|
|
|
SSL_CTX_set_session_id_context(ssl_ctx, sid_ctx, sizeof(sid_ctx) - 1);
|
2014-03-03 15:49:13 +01:00
|
|
|
SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_SERVER);
|
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
// Disabled SSL_CTX_set_ecdh_auto, because computational cost of
|
|
|
|
// chosen curve is much higher than P-256.
|
2014-06-28 08:35:10 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
// #if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
// SSL_CTX_set_ecdh_auto(ssl_ctx, 1);
|
|
|
|
// #else // OPENSSL_VERSION_NUBMER < 0x10002000L
|
2014-03-03 15:49:13 +01:00
|
|
|
// Use P-256, which is sufficiently secure at the time of this
|
|
|
|
// writing.
|
|
|
|
auto ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (ecdh == nullptr) {
|
2014-03-03 15:49:13 +01:00
|
|
|
std::cerr << "EC_KEY_new_by_curv_name failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
SSL_CTX_set_tmp_ecdh(ssl_ctx, ecdh);
|
|
|
|
EC_KEY_free(ecdh);
|
2014-06-28 08:35:10 +02:00
|
|
|
// #endif // OPENSSL_VERSION_NUBMER < 0x10002000L
|
2014-03-03 15:49:13 +01:00
|
|
|
|
2014-05-14 16:22:23 +02:00
|
|
|
#endif // OPENSSL_NO_EC
|
2014-03-03 15:49:13 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!config_->dh_param_file.empty()) {
|
2014-06-28 08:43:06 +02:00
|
|
|
// Read DH parameters from file
|
|
|
|
auto bio = BIO_new_file(config_->dh_param_file.c_str(), "r");
|
2014-11-27 15:39:04 +01:00
|
|
|
if (bio == nullptr) {
|
2014-06-28 08:43:06 +02:00
|
|
|
std::cerr << "BIO_new_file() failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dh = PEM_read_bio_DHparams(bio, nullptr, nullptr, nullptr);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (dh == nullptr) {
|
2014-06-28 08:43:06 +02:00
|
|
|
std::cerr << "PEM_read_bio_DHparams() failed: "
|
|
|
|
<< ERR_error_string(ERR_get_error(), nullptr) << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX_set_tmp_dh(ssl_ctx, dh);
|
|
|
|
DH_free(dh);
|
|
|
|
BIO_free(bio);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (SSL_CTX_use_PrivateKey_file(ssl_ctx, config_->private_key_file.c_str(),
|
|
|
|
SSL_FILETYPE_PEM) != 1) {
|
2013-07-22 14:30:40 +02:00
|
|
|
std::cerr << "SSL_CTX_use_PrivateKey_file failed." << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (SSL_CTX_use_certificate_chain_file(ssl_ctx,
|
|
|
|
config_->cert_file.c_str()) != 1) {
|
2013-07-22 14:30:40 +02:00
|
|
|
std::cerr << "SSL_CTX_use_certificate_file failed." << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (SSL_CTX_check_private_key(ssl_ctx) != 1) {
|
2013-07-22 14:30:40 +02:00
|
|
|
std::cerr << "SSL_CTX_check_private_key failed." << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config_->verify_client) {
|
|
|
|
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE |
|
|
|
|
SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
|
2013-07-22 14:30:40 +02:00
|
|
|
verify_callback);
|
|
|
|
}
|
|
|
|
|
2014-11-17 11:01:06 +01:00
|
|
|
next_proto = util::get_default_alpn();
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
SSL_CTX_set_next_protos_advertised_cb(ssl_ctx, next_proto_cb, &next_proto);
|
2014-01-01 15:54:28 +01:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
|
|
|
// ALPN selection callback
|
|
|
|
SSL_CTX_set_alpn_select_cb(ssl_ctx, alpn_select_proto_cb, this);
|
|
|
|
#endif // OPENSSL_VERSION_NUMBER >= 0x10002000L
|
2013-07-22 14:30:40 +02:00
|
|
|
}
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
auto loop = EV_DEFAULT;
|
2014-03-16 11:37:06 +01:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
Sessions sessions(this, loop, config_, ssl_ctx);
|
|
|
|
if (start_listen(this, loop, &sessions, config_) != 0) {
|
2013-07-22 14:30:40 +02:00
|
|
|
std::cerr << "Could not listen" << std::endl;
|
2015-10-21 15:27:15 +02:00
|
|
|
if (ssl_ctx) {
|
|
|
|
SSL_CTX_free(ssl_ctx);
|
|
|
|
}
|
2013-07-22 14:30:40 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-10 14:35:08 +01:00
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_run(loop, 0);
|
2013-07-22 14:30:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
const Config *HttpServer::get_config() const { return config_; }
|
2014-01-01 15:54:28 +01:00
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
const StatusPage *HttpServer::get_status_page(int status) const {
|
|
|
|
switch (status) {
|
|
|
|
case 200:
|
|
|
|
return &status_pages_[IDX_200];
|
|
|
|
case 301:
|
|
|
|
return &status_pages_[IDX_301];
|
|
|
|
case 400:
|
|
|
|
return &status_pages_[IDX_400];
|
|
|
|
case 404:
|
|
|
|
return &status_pages_[IDX_404];
|
2015-11-21 04:54:04 +01:00
|
|
|
case 405:
|
|
|
|
return &status_pages_[IDX_405];
|
2015-04-19 09:26:47 +02:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
} // namespace nghttp2
|