1 #ifndef QUICHE_HTTP2_ADAPTER_NGHTTP2_ADAPTER_H_ 2 #define QUICHE_HTTP2_ADAPTER_NGHTTP2_ADAPTER_H_ 3 4 #include <cstdint> 5 6 #include "absl/container/flat_hash_map.h" 7 #include "quiche/http2/adapter/http2_adapter.h" 8 #include "quiche/http2/adapter/http2_protocol.h" 9 #include "quiche/http2/adapter/nghttp2_session.h" 10 #include "quiche/http2/adapter/nghttp2_util.h" 11 #include "quiche/common/platform/api/quiche_export.h" 12 13 namespace http2 { 14 namespace adapter { 15 16 class QUICHE_EXPORT NgHttp2Adapter : public Http2Adapter { 17 public: 18 ~NgHttp2Adapter() override; 19 20 // Creates an adapter that functions as a client. Does not take ownership of 21 // |options|. 22 static std::unique_ptr<NgHttp2Adapter> CreateClientAdapter( 23 Http2VisitorInterface& visitor, const nghttp2_option* options = nullptr); 24 25 // Creates an adapter that functions as a server. Does not take ownership of 26 // |options|. 27 static std::unique_ptr<NgHttp2Adapter> CreateServerAdapter( 28 Http2VisitorInterface& visitor, const nghttp2_option* options = nullptr); 29 30 bool IsServerSession() const override; want_read()31 bool want_read() const override { return session_->want_read(); } want_write()32 bool want_write() const override { return session_->want_write(); } 33 34 int64_t ProcessBytes(absl::string_view bytes) override; 35 void SubmitSettings(absl::Span<const Http2Setting> settings) override; 36 void SubmitPriorityForStream(Http2StreamId stream_id, 37 Http2StreamId parent_stream_id, int weight, 38 bool exclusive) override; 39 40 // Submits a PING on the connection. Note that nghttp2 automatically submits 41 // PING acks upon receiving non-ack PINGs from the peer, so callers only use 42 // this method to originate PINGs. See nghttp2_option_set_no_auto_ping_ack(). 43 void SubmitPing(Http2PingId ping_id) override; 44 45 void SubmitShutdownNotice() override; 46 void SubmitGoAway(Http2StreamId last_accepted_stream_id, 47 Http2ErrorCode error_code, 48 absl::string_view opaque_data) override; 49 50 void SubmitWindowUpdate(Http2StreamId stream_id, 51 int window_increment) override; 52 53 void SubmitRst(Http2StreamId stream_id, Http2ErrorCode error_code) override; 54 55 void SubmitMetadata(Http2StreamId stream_id, size_t max_frame_size, 56 std::unique_ptr<MetadataSource> source) override; 57 58 int Send() override; 59 60 int GetSendWindowSize() const override; 61 int GetStreamSendWindowSize(Http2StreamId stream_id) const override; 62 63 int GetStreamReceiveWindowLimit(Http2StreamId stream_id) const override; 64 int GetStreamReceiveWindowSize(Http2StreamId stream_id) const override; 65 int GetReceiveWindowSize() const override; 66 67 int GetHpackEncoderDynamicTableSize() const override; 68 int GetHpackDecoderDynamicTableSize() const override; 69 70 Http2StreamId GetHighestReceivedStreamId() const override; 71 72 void MarkDataConsumedForStream(Http2StreamId stream_id, 73 size_t num_bytes) override; 74 75 int32_t SubmitRequest(absl::Span<const Header> headers, 76 std::unique_ptr<DataFrameSource> data_source, 77 void* user_data) override; 78 79 int SubmitResponse(Http2StreamId stream_id, absl::Span<const Header> headers, 80 std::unique_ptr<DataFrameSource> data_source) override; 81 82 int SubmitTrailer(Http2StreamId stream_id, 83 absl::Span<const Header> trailers) override; 84 85 void SetStreamUserData(Http2StreamId stream_id, void* user_data) override; 86 void* GetStreamUserData(Http2StreamId stream_id) override; 87 88 bool ResumeStream(Http2StreamId stream_id) override; 89 90 // Removes references to the `stream_id` from this adapter. 91 void RemoveStream(Http2StreamId stream_id); 92 93 // Accessor for testing. sources_size()94 size_t sources_size() const { return sources_.size(); } 95 96 private: 97 NgHttp2Adapter(Http2VisitorInterface& visitor, Perspective perspective, 98 const nghttp2_option* options); 99 100 // Performs any necessary initialization of the underlying HTTP/2 session, 101 // such as preparing initial SETTINGS. 102 void Initialize(); 103 104 std::unique_ptr<NgHttp2Session> session_; 105 Http2VisitorInterface& visitor_; 106 const nghttp2_option* options_; 107 Perspective perspective_; 108 109 absl::flat_hash_map<int32_t, std::unique_ptr<DataFrameSource>> sources_; 110 }; 111 112 } // namespace adapter 113 } // namespace http2 114 115 #endif // QUICHE_HTTP2_ADAPTER_NGHTTP2_ADAPTER_H_ 116