tizen 2.4 release
[external/nghttp2.git] / src / HttpServer.h
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2013 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef HTTP_SERVER_H
26 #define HTTP_SERVER_H
27
28 #include "nghttp2_config.h"
29
30 #include <stdint.h>
31 #include <sys/types.h>
32
33 #include <cstdlib>
34
35 #include <string>
36 #include <vector>
37 #include <map>
38 #include <memory>
39
40 #include <openssl/ssl.h>
41
42 #include <ev.h>
43
44 #include <nghttp2/nghttp2.h>
45
46 #include "http2.h"
47 #include "buffer.h"
48
49 namespace nghttp2 {
50
51 struct Config {
52   std::map<std::string, std::vector<std::string>> push;
53   std::string htdocs;
54   std::string host;
55   std::string private_key_file;
56   std::string cert_file;
57   std::string dh_param_file;
58   ev_tstamp stream_read_timeout;
59   ev_tstamp stream_write_timeout;
60   nghttp2_option *session_option;
61   void *data_ptr;
62   size_t padding;
63   size_t num_worker;
64   ssize_t header_table_size;
65   uint16_t port;
66   bool verbose;
67   bool daemon;
68   bool verify_client;
69   bool no_tls;
70   bool error_gzip;
71   bool early_response;
72   Config();
73   ~Config();
74 };
75
76 class Http2Handler;
77
78 struct Stream {
79   Headers headers;
80   Http2Handler *handler;
81   ev_timer rtimer;
82   ev_timer wtimer;
83   int64_t body_left;
84   int64_t upload_left;
85   int32_t stream_id;
86   int file;
87   http2::HeaderIndex hdidx;
88   Stream(Http2Handler *handler, int32_t stream_id);
89   ~Stream();
90 };
91
92 class Sessions;
93
94 class Http2Handler {
95 public:
96   Http2Handler(Sessions *sessions, int fd, SSL *ssl, int64_t session_id);
97   ~Http2Handler();
98
99   void remove_self();
100   int setup_bev();
101   int on_read();
102   int on_write();
103   int on_connect();
104   int verify_npn_result();
105
106   int submit_file_response(const std::string &status, Stream *stream,
107                            time_t last_modified, off_t file_length,
108                            nghttp2_data_provider *data_prd);
109
110   int submit_response(const std::string &status, int32_t stream_id,
111                       nghttp2_data_provider *data_prd);
112
113   int submit_response(const std::string &status, int32_t stream_id,
114                       const Headers &headers, nghttp2_data_provider *data_prd);
115
116   int submit_non_final_response(const std::string &status, int32_t stream_id);
117
118   int submit_push_promise(Stream *stream, const std::string &push_path);
119
120   int submit_rst_stream(Stream *stream, uint32_t error_code);
121
122   void add_stream(int32_t stream_id, std::unique_ptr<Stream> stream);
123   void remove_stream(int32_t stream_id);
124   Stream *get_stream(int32_t stream_id);
125   int64_t session_id() const;
126   Sessions *get_sessions() const;
127   const Config *get_config() const;
128   void remove_settings_timer();
129   void terminate_session(uint32_t error_code);
130
131   int fill_wb();
132
133   int read_clear();
134   int write_clear();
135   int tls_handshake();
136   int read_tls();
137   int write_tls();
138
139   struct ev_loop *get_loop() const;
140
141 private:
142   ev_io wev_;
143   ev_io rev_;
144   ev_timer settings_timerev_;
145   std::map<int32_t, std::unique_ptr<Stream>> id2stream_;
146   Buffer<65536> wb_;
147   std::function<int(Http2Handler &)> read_, write_;
148   int64_t session_id_;
149   nghttp2_session *session_;
150   Sessions *sessions_;
151   SSL *ssl_;
152   const uint8_t *data_pending_;
153   size_t data_pendinglen_;
154   int fd_;
155 };
156
157 class HttpServer {
158 public:
159   HttpServer(const Config *config);
160   int listen();
161   int run();
162   const Config *get_config() const;
163
164 private:
165   const Config *config_;
166 };
167
168 ssize_t file_read_callback(nghttp2_session *session, int32_t stream_id,
169                            uint8_t *buf, size_t length, int *eof,
170                            nghttp2_data_source *source, void *user_data);
171
172 } // namespace nghttp2
173
174 #endif // HTTP_SERVER_H