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 <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
2013-10-27 15:02:39 +01:00
|
|
|
#include <event2/event.h>
|
2013-07-22 14:30:40 +02:00
|
|
|
|
|
|
|
#include <nghttp2/nghttp2.h>
|
|
|
|
|
2014-03-16 11:37:06 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nghttp2_buf.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
#include "http2.h"
|
|
|
|
|
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;
|
2013-07-22 14:30:40 +02:00
|
|
|
std::string htdocs;
|
|
|
|
std::string host;
|
|
|
|
std::string private_key_file;
|
|
|
|
std::string cert_file;
|
2014-03-21 15:07:20 +01:00
|
|
|
timeval stream_read_timeout;
|
|
|
|
timeval 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;
|
2013-12-06 15:17:38 +01:00
|
|
|
ssize_t header_table_size;
|
|
|
|
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;
|
2013-07-22 14:30:40 +02:00
|
|
|
Config();
|
|
|
|
};
|
|
|
|
|
2014-03-21 15:07:20 +01:00
|
|
|
class Http2Handler;
|
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
struct Stream {
|
2014-01-16 15:41:13 +01:00
|
|
|
Headers headers;
|
2013-07-22 14:30:40 +02:00
|
|
|
std::pair<std::string, size_t> response_body;
|
2014-03-21 15:07:20 +01:00
|
|
|
Http2Handler *handler;
|
|
|
|
event *rtimer;
|
|
|
|
event *wtimer;
|
2013-12-06 15:17:38 +01:00
|
|
|
int32_t stream_id;
|
|
|
|
int file;
|
2014-05-02 16:34:57 +02:00
|
|
|
bool enable_compression;
|
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();
|
|
|
|
int setup_bev();
|
|
|
|
int on_read();
|
|
|
|
int on_write();
|
|
|
|
int on_connect();
|
|
|
|
int verify_npn_result();
|
|
|
|
int sendcb(const uint8_t *data, size_t len);
|
|
|
|
int recvcb(uint8_t *buf, size_t len);
|
|
|
|
|
|
|
|
int submit_file_response(const std::string& status,
|
2014-05-02 16:34:57 +02:00
|
|
|
Stream *stream,
|
2013-07-22 14:30:40 +02:00
|
|
|
time_t last_modified,
|
|
|
|
off_t file_length,
|
|
|
|
nghttp2_data_provider *data_prd);
|
|
|
|
|
|
|
|
int submit_response(const std::string& status,
|
|
|
|
int32_t stream_id,
|
|
|
|
nghttp2_data_provider *data_prd);
|
|
|
|
|
|
|
|
int submit_response
|
|
|
|
(const std::string& status,
|
|
|
|
int32_t stream_id,
|
|
|
|
const std::vector<std::pair<std::string, std::string>>& headers,
|
|
|
|
nghttp2_data_provider *data_prd);
|
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
int submit_push_promise(Stream *stream, const std::string& push_path);
|
2013-12-08 16:00:12 +01:00
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
int submit_rst_stream(Stream *stream, nghttp2_error_code 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-03-21 15:26:53 +01:00
|
|
|
Stream* get_stream(int32_t stream_id);
|
2013-07-22 14:30:40 +02:00
|
|
|
int64_t session_id() const;
|
|
|
|
Sessions* get_sessions() const;
|
|
|
|
const Config* get_config() const;
|
|
|
|
size_t get_left_connhd_len() const;
|
2013-07-26 14:34:39 +02:00
|
|
|
void set_left_connhd_len(size_t left);
|
2013-10-27 15:02:39 +01:00
|
|
|
void remove_settings_timer();
|
2013-12-25 16:23:07 +01:00
|
|
|
void terminate_session(nghttp2_error_code error_code);
|
2014-03-16 11:37:06 +01:00
|
|
|
int tls_handshake();
|
2014-05-02 16:34:57 +02:00
|
|
|
void decide_compression(const std::string& path, Stream *stream);
|
2013-07-22 14:30:40 +02:00
|
|
|
private:
|
2014-03-16 11:37:06 +01:00
|
|
|
int handle_ssl_temporal_error(int err);
|
|
|
|
int tls_write(const uint8_t *data, size_t datalen);
|
|
|
|
int tls_write_pending();
|
|
|
|
int wait_events();
|
|
|
|
|
2014-03-21 15:26:53 +01:00
|
|
|
std::map<int32_t, std::unique_ptr<Stream>> id2stream_;
|
2014-03-16 11:37:06 +01:00
|
|
|
nghttp2_buf sendbuf_;
|
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_;
|
|
|
|
SSL* ssl_;
|
2014-03-16 11:37:06 +01:00
|
|
|
event *rev_, *wev_;
|
2013-10-27 15:02:39 +01:00
|
|
|
event *settings_timerev_;
|
2014-03-16 11:37:06 +01:00
|
|
|
const uint8_t *pending_data_;
|
|
|
|
size_t pending_datalen_;
|
2013-12-06 15:17:38 +01:00
|
|
|
size_t left_connhd_len_;
|
|
|
|
int fd_;
|
2014-03-16 11:37:06 +01:00
|
|
|
uint8_t sendbufarray_[65536];
|
2013-07-22 14:30:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class HttpServer {
|
|
|
|
public:
|
|
|
|
HttpServer(const Config* config);
|
|
|
|
int listen();
|
|
|
|
int run();
|
2014-01-01 15:54:28 +01:00
|
|
|
const Config* get_config() const;
|
2013-07-22 14:30:40 +02:00
|
|
|
private:
|
|
|
|
const Config *config_;
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
} // namespace nghttp2
|
|
|
|
|
|
|
|
#endif // HTTP_SERVER_H
|