nghttp2/src/HttpServer.h

173 lines
4.4 KiB
C
Raw Normal View History

/*
2014-03-30 12:09:21 +02:00
* nghttp2 - HTTP/2 C Library
*
* 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>
#include <ev.h>
#include <nghttp2/nghttp2.h>
#include "http2.h"
#include "ringbuf.h"
namespace nghttp2 {
struct Config {
std::map<std::string, std::vector<std::string>> push;
std::string htdocs;
std::string host;
std::string private_key_file;
std::string cert_file;
std::string dh_param_file;
ev_tstamp stream_read_timeout;
ev_tstamp stream_write_timeout;
nghttp2_option *session_option;
void *data_ptr;
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;
bool verify_client;
bool no_tls;
bool error_gzip;
bool early_response;
Config();
2014-10-10 15:50:35 +02:00
~Config();
};
class Http2Handler;
2014-03-21 15:26:53 +01:00
struct Stream {
Headers headers;
Http2Handler *handler;
ev_timer rtimer;
ev_timer wtimer;
int64_t body_left;
2013-12-06 15:17:38 +01:00
int32_t stream_id;
int file;
2014-03-21 15:26:53 +01:00
Stream(Http2Handler *handler, int32_t stream_id);
~Stream();
};
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();
2014-11-27 15:39:04 +01:00
int submit_file_response(const std::string &status, Stream *stream,
time_t last_modified, off_t file_length,
nghttp2_data_provider *data_prd);
2014-11-27 15:39:04 +01:00
int submit_response(const std::string &status, int32_t stream_id,
nghttp2_data_provider *data_prd);
2014-11-27 15:39:04 +01:00
int submit_response(const std::string &status, int32_t stream_id,
const Headers &headers, nghttp2_data_provider *data_prd);
2014-11-27 15:39:04 +01:00
int submit_non_final_response(const std::string &status, int32_t stream_id);
2014-11-27 15:39:04 +01:00
int submit_push_promise(Stream *stream, const std::string &push_path);
int submit_rst_stream(Stream *stream, uint32_t error_code);
2014-03-21 15:26:53 +01:00
void add_stream(int32_t stream_id, std::unique_ptr<Stream> stream);
void remove_stream(int32_t stream_id);
2014-11-27 15:39:04 +01:00
Stream *get_stream(int32_t stream_id);
int64_t session_id() const;
2014-11-27 15:39:04 +01:00
Sessions *get_sessions() const;
const Config *get_config() const;
void remove_settings_timer();
void terminate_session(uint32_t error_code);
2014-11-27 15:39:04 +01:00
int fill_wb();
int read_clear();
int write_clear();
int tls_handshake();
int read_tls();
int write_tls();
struct ev_loop *get_loop() const;
private:
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-01-02 05:45:51 +01:00
RingBuf<65536> wb_;
std::function<int(Http2Handler &)> read_, write_;
2013-12-06 15:17:38 +01:00
int64_t session_id_;
nghttp2_session *session_;
Sessions *sessions_;
SSL *ssl_;
const uint8_t *data_pending_;
size_t data_pendinglen_;
2013-12-06 15:17:38 +01:00
int fd_;
};
class HttpServer {
public:
2014-11-27 15:39:04 +01:00
HttpServer(const Config *config);
int listen();
int run();
2014-11-27 15:39:04 +01:00
const Config *get_config() const;
private:
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);
} // namespace nghttp2
#endif // HTTP_SERVER_H