2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2013 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
28 #include "nghttp2_config.h"
30 #include <sys/types.h>
40 #include <openssl/ssl.h>
44 #include <nghttp2/nghttp2.h>
49 #include "allocator.h"
54 std::map<std::string, std::vector<std::string>> push;
55 std::map<std::string, std::string> mime_types;
57 std::string trailer_names;
60 std::string private_key_file;
61 std::string cert_file;
62 std::string dh_param_file;
64 std::string mime_types_file;
65 ev_tstamp stream_read_timeout;
66 ev_tstamp stream_write_timeout;
70 size_t max_concurrent_streams;
71 ssize_t header_table_size;
72 ssize_t encoder_header_table_size;
74 int connection_window_bits;
84 bool no_content_length;
92 FileEntry(std::string path, int64_t length, int64_t mtime, int fd,
93 const std::string *content_type, ev_tstamp last_valid,
95 : path(std::move(path)),
98 last_valid(last_valid),
99 content_type(content_type),
106 std::multimap<std::string, std::unique_ptr<FileEntry>>::iterator it;
109 ev_tstamp last_valid;
110 const std::string *content_type;
111 FileEntry *dlnext, *dlprev;
117 struct RequestHeader {
127 nghttp2_rcbuf *method;
128 nghttp2_rcbuf *scheme;
129 nghttp2_rcbuf *authority;
133 nghttp2_rcbuf *expect;
138 BlockAllocator balloc;
139 RequestHeader header;
140 Http2Handler *handler;
146 // Total amount of bytes (sum of name and value length) used in
148 size_t header_buffer_size;
151 Stream(Http2Handler *handler, int32_t stream_id);
159 Http2Handler(Sessions *sessions, int fd, SSL *ssl, int64_t session_id);
163 void start_settings_timer();
166 int connection_made();
167 int verify_npn_result();
169 int submit_file_response(const StringRef &status, Stream *stream,
170 time_t last_modified, off_t file_length,
171 const std::string *content_type,
172 nghttp2_data_provider *data_prd);
174 int submit_response(const StringRef &status, int32_t stream_id,
175 nghttp2_data_provider *data_prd);
177 int submit_response(const StringRef &status, int32_t stream_id,
178 const HeaderRefs &headers,
179 nghttp2_data_provider *data_prd);
181 int submit_non_final_response(const std::string &status, int32_t stream_id);
183 int submit_push_promise(Stream *stream, const StringRef &push_path);
185 int submit_rst_stream(Stream *stream, uint32_t error_code);
187 void add_stream(int32_t stream_id, std::unique_ptr<Stream> stream);
188 void remove_stream(int32_t stream_id);
189 Stream *get_stream(int32_t stream_id);
190 int64_t session_id() const;
191 Sessions *get_sessions() const;
192 const Config *get_config() const;
193 void remove_settings_timer();
194 void terminate_session(uint32_t error_code);
204 struct ev_loop *get_loop() const;
206 using WriteBuf = Buffer<64_k>;
213 ev_timer settings_timerev_;
214 std::map<int32_t, std::unique_ptr<Stream>> id2stream_;
216 std::function<int(Http2Handler &)> read_, write_;
218 nghttp2_session *session_;
221 const uint8_t *data_pending_;
222 size_t data_pendinglen_;
233 HttpServer(const Config *config);
236 const Config *get_config() const;
237 const StatusPage *get_status_page(int status) const;
240 std::vector<StatusPage> status_pages_;
241 const Config *config_;
244 ssize_t file_read_callback(nghttp2_session *session, int32_t stream_id,
245 uint8_t *buf, size_t length, uint32_t *data_flags,
246 nghttp2_data_source *source, void *user_data);
248 } // namespace nghttp2
250 #endif // HTTP_SERVER_H