2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2021 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.
25 #ifndef SHRPX_HTTP3_UPSTREAM_H
26 #define SHRPX_HTTP3_UPSTREAM_H
30 #include <ngtcp2/ngtcp2.h>
31 #include <nghttp3/nghttp3.h>
33 #include "shrpx_upstream.h"
34 #include "shrpx_downstream_queue.h"
38 using namespace nghttp2;
44 class Http3Upstream : public Upstream {
46 Http3Upstream(ClientHandler *handler);
47 virtual ~Http3Upstream();
49 virtual int on_read();
50 virtual int on_write();
51 virtual int on_timeout(Downstream *downstream);
52 virtual int on_downstream_abort_request(Downstream *downstream,
53 unsigned int status_code);
55 on_downstream_abort_request_with_https_redirect(Downstream *downstream);
56 virtual int downstream_read(DownstreamConnection *dconn);
57 virtual int downstream_write(DownstreamConnection *dconn);
58 virtual int downstream_eof(DownstreamConnection *dconn);
59 virtual int downstream_error(DownstreamConnection *dconn, int events);
60 virtual ClientHandler *get_client_handler() const;
62 virtual int on_downstream_header_complete(Downstream *downstream);
63 virtual int on_downstream_body(Downstream *downstream, const uint8_t *data,
64 size_t len, bool flush);
65 virtual int on_downstream_body_complete(Downstream *downstream);
67 virtual void on_handler_delete();
68 virtual int on_downstream_reset(Downstream *downstream, bool no_retry);
70 virtual void pause_read(IOCtrlReason reason);
71 virtual int resume_read(IOCtrlReason reason, Downstream *downstream,
73 virtual int send_reply(Downstream *downstream, const uint8_t *body,
76 virtual int initiate_push(Downstream *downstream, const StringRef &uri);
78 virtual int response_riovec(struct iovec *iov, int iovcnt) const;
79 virtual void response_drain(size_t n);
80 virtual bool response_empty() const;
82 virtual Downstream *on_downstream_push_promise(Downstream *downstream,
83 int32_t promised_stream_id);
85 on_downstream_push_promise_complete(Downstream *downstream,
86 Downstream *promised_downstream);
87 virtual bool push_enabled() const;
88 virtual void cancel_premature_downstream(Downstream *promised_downstream);
90 int init(const UpstreamAddr *faddr, const Address &remote_addr,
91 const Address &local_addr, const ngtcp2_pkt_hd &initial_hd,
92 const ngtcp2_cid *odcid, const uint8_t *token, size_t tokenlen);
94 int on_read(const UpstreamAddr *faddr, const Address &remote_addr,
95 const Address &local_addr, const uint8_t *data, size_t datalen);
99 int on_rx_secret(ngtcp2_crypto_level level, const uint8_t *secret,
101 int on_tx_secret(ngtcp2_crypto_level level, const uint8_t *secret,
104 int add_crypto_data(ngtcp2_crypto_level level, const uint8_t *data,
107 void set_tls_alert(uint8_t alert);
112 void reset_idle_timer();
115 int setup_httpconn();
116 void add_pending_downstream(std::unique_ptr<Downstream> downstream);
117 int recv_stream_data(uint32_t flags, int64_t stream_id, const uint8_t *data,
119 int acked_stream_data_offset(int64_t stream_id, uint64_t datalen);
120 int extend_max_stream_data(int64_t stream_id);
121 void extend_max_remote_streams_bidi(uint64_t max_streams);
122 int error_reply(Downstream *downstream, unsigned int status_code);
123 void http_begin_request_headers(int64_t stream_id);
124 int http_recv_request_header(Downstream *downstream, int32_t token,
125 nghttp3_rcbuf *name, nghttp3_rcbuf *value,
127 int http_end_request_headers(Downstream *downstream);
128 int http_end_stream(Downstream *downstream);
129 void start_downstream(Downstream *downstream);
130 void initiate_downstream(Downstream *downstream);
131 int shutdown_stream(Downstream *downstream, uint64_t app_error_code);
132 int shutdown_stream_read(int64_t stream_id, uint64_t app_error_code);
133 int redirect_to_https(Downstream *downstream);
134 int http_stream_close(Downstream *downstream, uint64_t app_error_code);
135 void consume(int64_t stream_id, size_t nconsumed);
136 void remove_downstream(Downstream *downstream);
137 int stream_close(int64_t stream_id, uint64_t app_error_code);
138 void log_response_headers(Downstream *downstream,
139 const std::vector<nghttp3_nv> &nva) const;
140 int http_acked_stream_data(Downstream *downstream, uint64_t datalen);
141 int http_shutdown_stream_read(int64_t stream_id);
142 int http_reset_stream(int64_t stream_id, uint64_t app_error_code);
143 int http_send_stop_sending(int64_t stream_id, uint64_t app_error_code);
144 int http_recv_data(Downstream *downstream, const uint8_t *data,
146 int handshake_completed();
147 int check_shutdown();
148 int start_graceful_shutdown();
151 int send_packet(const UpstreamAddr *faddr, const sockaddr *remote_sa,
152 size_t remote_salen, const sockaddr *local_sa,
153 size_t local_salen, const uint8_t *data, size_t datalen,
156 void qlog_write(const void *data, size_t datalen, bool fin);
157 int open_qlog_file(const StringRef &dir, const ngtcp2_cid &scid) const;
160 ClientHandler *handler_;
162 ev_timer idle_timer_;
163 ev_timer shutdown_timer_;
165 size_t max_udp_payload_size_;
167 ngtcp2_cid hashed_scid_;
169 quic::Error last_error_;
171 nghttp3_conn *httpconn_;
172 DownstreamQueue downstream_queue_;
175 std::vector<uint8_t> conn_close_;
180 #endif // SHRPX_HTTP3_UPSTREAM_H