1 // Copyright (c) 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef QUICHE_QUIC_CORE_HTTP_HTTP_FRAMES_H_ 6 #define QUICHE_QUIC_CORE_HTTP_HTTP_FRAMES_H_ 7 8 #include <algorithm> 9 #include <cstdint> 10 #include <map> 11 #include <ostream> 12 #include <sstream> 13 14 #include "absl/container/flat_hash_map.h" 15 #include "absl/strings/str_cat.h" 16 #include "absl/strings/string_view.h" 17 #include "quiche/quic/core/http/http_constants.h" 18 #include "quiche/quic/core/quic_types.h" 19 #include "quiche/spdy/core/spdy_protocol.h" 20 21 namespace quic { 22 23 enum class HttpFrameType { 24 DATA = 0x0, 25 HEADERS = 0x1, 26 CANCEL_PUSH = 0X3, 27 SETTINGS = 0x4, 28 PUSH_PROMISE = 0x5, 29 GOAWAY = 0x7, 30 MAX_PUSH_ID = 0xD, 31 // https://tools.ietf.org/html/draft-davidben-http-client-hint-reliability-02 32 ACCEPT_CH = 0x89, 33 // https://tools.ietf.org/html/draft-ietf-httpbis-priority-03 34 PRIORITY_UPDATE_REQUEST_STREAM = 0xF0700, 35 // https://www.ietf.org/archive/id/draft-ietf-webtrans-http3-00.html 36 WEBTRANSPORT_STREAM = 0x41, 37 METADATA = 0x4d, 38 }; 39 40 // 7.2.1. DATA 41 // 42 // DATA frames (type=0x0) convey arbitrary, variable-length sequences of 43 // octets associated with an HTTP request or response payload. 44 struct QUIC_EXPORT_PRIVATE DataFrame { 45 absl::string_view data; 46 }; 47 48 // 7.2.2. HEADERS 49 // 50 // The HEADERS frame (type=0x1) is used to carry a header block, 51 // compressed using QPACK. 52 struct QUIC_EXPORT_PRIVATE HeadersFrame { 53 absl::string_view headers; 54 }; 55 56 // 7.2.4. SETTINGS 57 // 58 // The SETTINGS frame (type=0x4) conveys configuration parameters that 59 // affect how endpoints communicate, such as preferences and constraints 60 // on peer behavior 61 62 using SettingsMap = absl::flat_hash_map<uint64_t, uint64_t>; 63 64 struct QUIC_EXPORT_PRIVATE SettingsFrame { 65 SettingsMap values; 66 67 bool operator==(const SettingsFrame& rhs) const { 68 return values == rhs.values; 69 } 70 ToStringSettingsFrame71 std::string ToString() const { 72 std::string s; 73 for (auto it : values) { 74 std::string setting = absl::StrCat( 75 H3SettingsToString( 76 static_cast<Http3AndQpackSettingsIdentifiers>(it.first)), 77 " = ", it.second, "; "); 78 absl::StrAppend(&s, setting); 79 } 80 return s; 81 } 82 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, 83 const SettingsFrame& s) { 84 os << s.ToString(); 85 return os; 86 } 87 }; 88 89 // 7.2.6. GOAWAY 90 // 91 // The GOAWAY frame (type=0x7) is used to initiate shutdown of a connection by 92 // either endpoint. 93 struct QUIC_EXPORT_PRIVATE GoAwayFrame { 94 // When sent from server to client, |id| is a stream ID that should refer to 95 // a client-initiated bidirectional stream. 96 // When sent from client to server, |id| is a push ID. 97 uint64_t id; 98 99 bool operator==(const GoAwayFrame& rhs) const { return id == rhs.id; } 100 }; 101 102 // https://httpwg.org/http-extensions/draft-ietf-httpbis-priority.html 103 // 104 // The PRIORITY_UPDATE frame specifies the sender-advised priority of a stream. 105 // Frame type 0xf0700 (called PRIORITY_UPDATE_REQUEST_STREAM in the 106 // implementation) is used for for request streams. 107 // Frame type 0xf0701 would be used for push streams but it is not implemented; 108 // incoming 0xf0701 frames are treated as frames of unknown type. 109 110 // Length of a priority frame's first byte. 111 const QuicByteCount kPriorityFirstByteLength = 1; 112 113 struct QUIC_EXPORT_PRIVATE PriorityUpdateFrame { 114 uint64_t prioritized_element_id = 0; 115 std::string priority_field_value; 116 117 bool operator==(const PriorityUpdateFrame& rhs) const { 118 return std::tie(prioritized_element_id, priority_field_value) == 119 std::tie(rhs.prioritized_element_id, rhs.priority_field_value); 120 } ToStringPriorityUpdateFrame121 std::string ToString() const { 122 return absl::StrCat( 123 "Priority Frame : {prioritized_element_id: ", prioritized_element_id, 124 ", priority_field_value: ", priority_field_value, "}"); 125 } 126 127 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<( 128 std::ostream& os, const PriorityUpdateFrame& s) { 129 os << s.ToString(); 130 return os; 131 } 132 }; 133 134 // ACCEPT_CH 135 // https://tools.ietf.org/html/draft-davidben-http-client-hint-reliability-02 136 // 137 struct QUIC_EXPORT_PRIVATE AcceptChFrame { 138 std::vector<spdy::AcceptChOriginValuePair> entries; 139 140 bool operator==(const AcceptChFrame& rhs) const { 141 return entries.size() == rhs.entries.size() && 142 std::equal(entries.begin(), entries.end(), rhs.entries.begin()); 143 } 144 ToStringAcceptChFrame145 std::string ToString() const { 146 std::stringstream s; 147 s << *this; 148 return s.str(); 149 } 150 151 friend QUIC_EXPORT_PRIVATE std::ostream& operator<<( 152 std::ostream& os, const AcceptChFrame& frame) { 153 os << "ACCEPT_CH frame with " << frame.entries.size() << " entries: "; 154 for (auto& entry : frame.entries) { 155 os << "origin: " << entry.origin << "; value: " << entry.value; 156 } 157 return os; 158 } 159 }; 160 161 } // namespace quic 162 163 #endif // QUICHE_QUIC_CORE_HTTP_HTTP_FRAMES_H_ 164