xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/http2/adapter/nghttp2_util.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Various utility/conversion functions for compatibility with the nghttp2 API.
2 
3 #ifndef QUICHE_HTTP2_ADAPTER_NGHTTP2_UTIL_H_
4 #define QUICHE_HTTP2_ADAPTER_NGHTTP2_UTIL_H_
5 
6 #include <cstdint>
7 #include <vector>
8 
9 #include "absl/strings/string_view.h"
10 #include "absl/types/span.h"
11 #include "quiche/http2/adapter/data_source.h"
12 #include "quiche/http2/adapter/http2_protocol.h"
13 #include "quiche/http2/adapter/http2_visitor_interface.h"
14 #include "quiche/http2/adapter/nghttp2.h"
15 #include "quiche/spdy/core/http2_header_block.h"
16 
17 namespace http2 {
18 namespace adapter {
19 
20 // Return codes to represent various errors.
21 inline constexpr int kStreamCallbackFailureStatus =
22     NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
23 inline constexpr int kCancelStatus = NGHTTP2_ERR_CANCEL;
24 
25 using CallbacksDeleter = void (*)(nghttp2_session_callbacks*);
26 using SessionDeleter = void (*)(nghttp2_session*);
27 
28 using nghttp2_session_callbacks_unique_ptr =
29     std::unique_ptr<nghttp2_session_callbacks, CallbacksDeleter>;
30 using nghttp2_session_unique_ptr =
31     std::unique_ptr<nghttp2_session, SessionDeleter>;
32 
33 nghttp2_session_callbacks_unique_ptr MakeCallbacksPtr(
34     nghttp2_session_callbacks* callbacks);
35 nghttp2_session_unique_ptr MakeSessionPtr(nghttp2_session* session);
36 
37 uint8_t* ToUint8Ptr(char* str);
38 uint8_t* ToUint8Ptr(const char* str);
39 
40 absl::string_view ToStringView(nghttp2_rcbuf* rc_buffer);
41 absl::string_view ToStringView(uint8_t* pointer, size_t length);
42 absl::string_view ToStringView(const uint8_t* pointer, size_t length);
43 
44 // Returns the nghttp2 header structure from the given |headers|, which
45 // must have the correct pseudoheaders preceding other headers.
46 std::vector<nghttp2_nv> GetNghttp2Nvs(absl::Span<const Header> headers);
47 
48 // Returns the nghttp2 header structure from the given response |headers|, with
49 // the :status pseudoheader first based on the given |response_code|. The
50 // |response_code| is passed in separately from |headers| for lifetime reasons.
51 std::vector<nghttp2_nv> GetResponseNghttp2Nvs(
52     const spdy::Http2HeaderBlock& headers, absl::string_view response_code);
53 
54 // Returns the HTTP/2 error code corresponding to the raw wire value, as defined
55 // in RFC 7540 Section 7. Unrecognized error codes are treated as INTERNAL_ERROR
56 // based on the RFC 7540 Section 7 suggestion.
57 Http2ErrorCode ToHttp2ErrorCode(uint32_t wire_error_code);
58 
59 // Converts between the integer error code used by nghttp2 and the corresponding
60 // InvalidFrameError value.
61 int ToNgHttp2ErrorCode(Http2VisitorInterface::InvalidFrameError error);
62 Http2VisitorInterface::InvalidFrameError ToInvalidFrameError(int error);
63 
64 // Transforms a nghttp2_data_provider into a DataFrameSource. Assumes that
65 // |provider| uses the zero-copy nghttp2_data_source_read_callback API. Unsafe
66 // otherwise.
67 std::unique_ptr<DataFrameSource> MakeZeroCopyDataFrameSource(
68     nghttp2_data_provider provider, void* user_data,
69     nghttp2_send_data_callback send_data);
70 
71 void LogBeforeSend(const nghttp2_frame& frame);
72 
73 }  // namespace adapter
74 }  // namespace http2
75 
76 #endif  // QUICHE_HTTP2_ADAPTER_NGHTTP2_UTIL_H_
77