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.
|
|
|
|
*/
|
|
|
|
#ifndef HTTP_SERVER_H
|
|
|
|
#define HTTP_SERVER_H
|
|
|
|
|
|
|
|
#include "nghttp2_config.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2015-05-13 17:17:45 +02:00
|
|
|
#include <cinttypes>
|
2013-07-22 14:30:40 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
2014-12-23 16:05:45 +01:00
|
|
|
#include <ev.h>
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
#include "http2.h"
|
2015-02-06 15:40:34 +01:00
|
|
|
#include "buffer.h"
|
2015-06-21 07:32:47 +02:00
|
|
|
#include "template.h"
|
2016-03-19 10:33:36 +01:00
|
|
|
#include "allocator.h"
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
namespace nghttp2 {
|
|
|
|
|
|
|
|
struct Config {
|
2013-12-08 16:00:12 +01:00
|
|
|
std::map<std::string, std::vector<std::string>> push;
|
2015-10-28 16:21:36 +01:00
|
|
|
std::map<std::string, std::string> mime_types;
|
2015-03-07 09:50:03 +01:00
|
|
|
Headers trailer;
|
2016-03-19 10:33:36 +01:00
|
|
|
std::string trailer_names;
|
2013-07-22 14:30:40 +02:00
|
|
|
std::string htdocs;
|
|
|
|
std::string host;
|
|
|
|
std::string private_key_file;
|
|
|
|
std::string cert_file;
|
2014-06-28 08:43:06 +02:00
|
|
|
std::string dh_param_file;
|
2015-01-24 21:26:49 +01:00
|
|
|
std::string address;
|
2015-10-28 16:21:36 +01:00
|
|
|
std::string mime_types_file;
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_tstamp stream_read_timeout;
|
|
|
|
ev_tstamp stream_write_timeout;
|
2013-07-22 14:30:40 +02:00
|
|
|
void *data_ptr;
|
2014-02-15 08:40:32 +01:00
|
|
|
size_t padding;
|
2014-03-04 15:14:26 +01:00
|
|
|
size_t num_worker;
|
2015-05-06 03:42:43 +02:00
|
|
|
size_t max_concurrent_streams;
|
2013-12-06 15:17:38 +01:00
|
|
|
ssize_t header_table_size;
|
2016-09-17 15:33:21 +02:00
|
|
|
ssize_t encoder_header_table_size;
|
2016-01-27 14:34:17 +01:00
|
|
|
int window_bits;
|
|
|
|
int connection_window_bits;
|
2013-12-06 15:17:38 +01:00
|
|
|
uint16_t port;
|
|
|
|
bool verbose;
|
|
|
|
bool daemon;
|
2013-07-22 14:30:40 +02:00
|
|
|
bool verify_client;
|
|
|
|
bool no_tls;
|
2014-03-04 15:29:30 +01:00
|
|
|
bool error_gzip;
|
2014-07-03 15:44:27 +02:00
|
|
|
bool early_response;
|
2015-03-23 18:30:51 +01:00
|
|
|
bool hexdump;
|
2015-05-12 17:38:28 +02:00
|
|
|
bool echo_upload;
|
2016-01-07 14:51:47 +01:00
|
|
|
bool no_content_length;
|
2013-07-22 14:30:40 +02:00
|
|
|
Config();
|
2014-10-10 15:50:35 +02:00
|
|
|
~Config();
|
2013-07-22 14:30:40 +02:00
|
|
|
};
|
|
|
|
|
2014-03-21 15:07:20 +01:00
|
|
|
class Http2Handler;
|
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
struct FileEntry {
|
2015-10-28 16:21:36 +01:00
|
|
|
FileEntry(std::string path, int64_t length, int64_t mtime, int fd,
|
2015-11-20 15:29:12 +01:00
|
|
|
const std::string *content_type, ev_tstamp last_valid,
|
|
|
|
bool stale = false)
|
2016-01-27 13:14:07 +01:00
|
|
|
: path(std::move(path)),
|
|
|
|
length(length),
|
|
|
|
mtime(mtime),
|
|
|
|
last_valid(last_valid),
|
|
|
|
content_type(content_type),
|
|
|
|
dlnext(nullptr),
|
|
|
|
dlprev(nullptr),
|
|
|
|
fd(fd),
|
|
|
|
usecount(1),
|
|
|
|
stale(stale) {}
|
2015-04-19 09:26:47 +02:00
|
|
|
std::string path;
|
2015-11-23 13:25:12 +01:00
|
|
|
std::multimap<std::string, std::unique_ptr<FileEntry>>::iterator it;
|
2015-04-19 09:26:47 +02:00
|
|
|
int64_t length;
|
|
|
|
int64_t mtime;
|
2015-11-20 15:29:12 +01:00
|
|
|
ev_tstamp last_valid;
|
2015-10-28 16:21:36 +01:00
|
|
|
const std::string *content_type;
|
2015-11-20 15:29:12 +01:00
|
|
|
FileEntry *dlnext, *dlprev;
|
2015-04-19 09:26:47 +02:00
|
|
|
int fd;
|
|
|
|
int usecount;
|
2015-11-20 15:29:12 +01:00
|
|
|
bool stale;
|
2015-04-19 09:26:47 +02:00
|
|
|
};
|
|
|
|
|
2016-03-19 10:33:36 +01:00
|
|
|
struct RequestHeader {
|
|
|
|
StringRef method;
|
|
|
|
StringRef scheme;
|
|
|
|
StringRef authority;
|
|
|
|
StringRef host;
|
|
|
|
StringRef path;
|
|
|
|
StringRef ims;
|
|
|
|
StringRef expect;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
nghttp2_rcbuf *method;
|
|
|
|
nghttp2_rcbuf *scheme;
|
|
|
|
nghttp2_rcbuf *authority;
|
|
|
|
nghttp2_rcbuf *host;
|
|
|
|
nghttp2_rcbuf *path;
|
|
|
|
nghttp2_rcbuf *ims;
|
|
|
|
nghttp2_rcbuf *expect;
|
|
|
|
} rcbuf;
|
|
|
|
};
|
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
struct Stream {
|
2016-03-19 10:33:36 +01:00
|
|
|
BlockAllocator balloc;
|
|
|
|
RequestHeader header;
|
2014-03-21 15:07:20 +01:00
|
|
|
Http2Handler *handler;
|
2015-04-19 09:26:47 +02:00
|
|
|
FileEntry *file_ent;
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_timer rtimer;
|
|
|
|
ev_timer wtimer;
|
2015-04-19 09:26:47 +02:00
|
|
|
int64_t body_length;
|
|
|
|
int64_t body_offset;
|
2016-02-04 14:51:06 +01:00
|
|
|
// Total amount of bytes (sum of name and value length) used in
|
|
|
|
// headers.
|
|
|
|
size_t header_buffer_size;
|
2013-12-06 15:17:38 +01:00
|
|
|
int32_t stream_id;
|
2015-05-12 17:38:28 +02:00
|
|
|
bool echo_upload;
|
2014-03-21 15:26:53 +01:00
|
|
|
Stream(Http2Handler *handler, int32_t stream_id);
|
|
|
|
~Stream();
|
2013-07-22 14:30:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Sessions;
|
|
|
|
|
|
|
|
class Http2Handler {
|
|
|
|
public:
|
|
|
|
Http2Handler(Sessions *sessions, int fd, SSL *ssl, int64_t session_id);
|
|
|
|
~Http2Handler();
|
|
|
|
|
|
|
|
void remove_self();
|
2016-02-22 17:18:07 +01:00
|
|
|
void start_settings_timer();
|
2013-07-22 14:30:40 +02:00
|
|
|
int on_read();
|
|
|
|
int on_write();
|
2015-03-28 12:21:12 +01:00
|
|
|
int connection_made();
|
2013-07-22 14:30:40 +02:00
|
|
|
int verify_npn_result();
|
|
|
|
|
2016-03-19 10:33:36 +01:00
|
|
|
int submit_file_response(const StringRef &status, Stream *stream,
|
2014-11-27 15:39:04 +01:00
|
|
|
time_t last_modified, off_t file_length,
|
2015-10-28 16:21:36 +01:00
|
|
|
const std::string *content_type,
|
2013-07-22 14:30:40 +02:00
|
|
|
nghttp2_data_provider *data_prd);
|
|
|
|
|
2016-03-19 10:33:36 +01:00
|
|
|
int submit_response(const StringRef &status, int32_t stream_id,
|
2013-07-22 14:30:40 +02:00
|
|
|
nghttp2_data_provider *data_prd);
|
|
|
|
|
2016-03-19 10:33:36 +01:00
|
|
|
int submit_response(const StringRef &status, int32_t stream_id,
|
|
|
|
const HeaderRefs &headers,
|
|
|
|
nghttp2_data_provider *data_prd);
|
2013-07-22 14:30:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int submit_non_final_response(const std::string &status, int32_t stream_id);
|
2014-07-23 16:32:57 +02:00
|
|
|
|
2016-03-19 10:33:36 +01:00
|
|
|
int submit_push_promise(Stream *stream, const StringRef &push_path);
|
2013-12-08 16:00:12 +01:00
|
|
|
|
2014-08-23 10:34:56 +02:00
|
|
|
int submit_rst_stream(Stream *stream, uint32_t error_code);
|
2014-03-21 15:07:20 +01:00
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
void add_stream(int32_t stream_id, std::unique_ptr<Stream> stream);
|
2013-07-22 14:30:40 +02:00
|
|
|
void remove_stream(int32_t stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
Stream *get_stream(int32_t stream_id);
|
2013-07-22 14:30:40 +02:00
|
|
|
int64_t session_id() const;
|
2014-11-27 15:39:04 +01:00
|
|
|
Sessions *get_sessions() const;
|
|
|
|
const Config *get_config() const;
|
2013-10-27 15:02:39 +01:00
|
|
|
void remove_settings_timer();
|
2014-08-23 10:34:56 +02:00
|
|
|
void terminate_session(uint32_t error_code);
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2015-01-02 05:42:57 +01:00
|
|
|
int fill_wb();
|
2014-12-23 16:05:45 +01:00
|
|
|
|
|
|
|
int read_clear();
|
|
|
|
int write_clear();
|
|
|
|
int tls_handshake();
|
|
|
|
int read_tls();
|
|
|
|
int write_tls();
|
|
|
|
|
|
|
|
struct ev_loop *get_loop() const;
|
|
|
|
|
2015-06-21 07:32:47 +02:00
|
|
|
using WriteBuf = Buffer<64_k>;
|
2015-04-03 16:10:51 +02:00
|
|
|
|
|
|
|
WriteBuf *get_wb();
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
private:
|
2014-12-23 16:05:45 +01:00
|
|
|
ev_io wev_;
|
|
|
|
ev_io rev_;
|
|
|
|
ev_timer settings_timerev_;
|
2014-03-21 15:26:53 +01:00
|
|
|
std::map<int32_t, std::unique_ptr<Stream>> id2stream_;
|
2015-04-03 16:10:51 +02:00
|
|
|
WriteBuf wb_;
|
2014-12-23 16:05:45 +01:00
|
|
|
std::function<int(Http2Handler &)> read_, write_;
|
2013-12-06 15:17:38 +01:00
|
|
|
int64_t session_id_;
|
2013-07-22 14:30:40 +02:00
|
|
|
nghttp2_session *session_;
|
|
|
|
Sessions *sessions_;
|
2014-09-18 17:58:32 +02:00
|
|
|
SSL *ssl_;
|
2014-12-23 16:05:45 +01:00
|
|
|
const uint8_t *data_pending_;
|
|
|
|
size_t data_pendinglen_;
|
2013-12-06 15:17:38 +01:00
|
|
|
int fd_;
|
2013-07-22 14:30:40 +02:00
|
|
|
};
|
|
|
|
|
2015-04-19 09:26:47 +02:00
|
|
|
struct StatusPage {
|
|
|
|
std::string status;
|
|
|
|
FileEntry file_ent;
|
|
|
|
};
|
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
class HttpServer {
|
|
|
|
public:
|
2014-11-27 15:39:04 +01:00
|
|
|
HttpServer(const Config *config);
|
2013-07-22 14:30:40 +02:00
|
|
|
int listen();
|
|
|
|
int run();
|
2014-11-27 15:39:04 +01:00
|
|
|
const Config *get_config() const;
|
2015-04-19 09:26:47 +02:00
|
|
|
const StatusPage *get_status_page(int status) const;
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2013-07-22 14:30:40 +02:00
|
|
|
private:
|
2015-04-19 09:26:47 +02:00
|
|
|
std::vector<StatusPage> status_pages_;
|
2013-07-22 14:30:40 +02:00
|
|
|
const Config *config_;
|
|
|
|
};
|
|
|
|
|
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, int *eof,
|
|
|
|
nghttp2_data_source *source, void *user_data);
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
#endif // HTTP_SERVER_H
|