QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 1 | // Copyright 2016 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 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "http2/http2_structures.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 6 | |
| 7 | #include <cstring> // For std::memcmp |
| 8 | #include <sstream> |
| 9 | |
bnc | 3bf526d | 2021-04-19 05:01:31 -0700 | [diff] [blame] | 10 | #include "absl/strings/escaping.h" |
vasilvv | 13aa6a0 | 2020-12-03 13:02:33 -0800 | [diff] [blame] | 11 | #include "absl/strings/str_cat.h" |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 12 | |
| 13 | namespace http2 { |
| 14 | |
| 15 | // Http2FrameHeader: |
| 16 | |
| 17 | bool Http2FrameHeader::IsProbableHttpResponse() const { |
| 18 | return (payload_length == 0x485454 && // "HTT" |
| 19 | static_cast<char>(type) == 'P' && // "P" |
| 20 | flags == '/'); // "/" |
| 21 | } |
| 22 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 23 | std::string Http2FrameHeader::ToString() const { |
vasilvv | 13aa6a0 | 2020-12-03 13:02:33 -0800 | [diff] [blame] | 24 | return absl::StrCat("length=", payload_length, |
| 25 | ", type=", Http2FrameTypeToString(type), |
| 26 | ", flags=", FlagsToString(), ", stream=", stream_id); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 27 | } |
| 28 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 29 | std::string Http2FrameHeader::FlagsToString() const { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 30 | return Http2FrameFlagsToString(type, flags); |
| 31 | } |
| 32 | |
| 33 | bool operator==(const Http2FrameHeader& a, const Http2FrameHeader& b) { |
| 34 | return a.payload_length == b.payload_length && a.stream_id == b.stream_id && |
| 35 | a.type == b.type && a.flags == b.flags; |
| 36 | } |
| 37 | |
| 38 | std::ostream& operator<<(std::ostream& out, const Http2FrameHeader& v) { |
| 39 | return out << v.ToString(); |
| 40 | } |
| 41 | |
| 42 | // Http2PriorityFields: |
| 43 | |
| 44 | bool operator==(const Http2PriorityFields& a, const Http2PriorityFields& b) { |
| 45 | return a.stream_dependency == b.stream_dependency && a.weight == b.weight; |
| 46 | } |
| 47 | |
bnc | 4790400 | 2019-08-16 11:49:48 -0700 | [diff] [blame] | 48 | std::string Http2PriorityFields::ToString() const { |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 49 | std::stringstream ss; |
| 50 | ss << "E=" << (is_exclusive ? "true" : "false") |
| 51 | << ", stream=" << stream_dependency |
| 52 | << ", weight=" << static_cast<uint32_t>(weight); |
| 53 | return ss.str(); |
| 54 | } |
| 55 | |
| 56 | std::ostream& operator<<(std::ostream& out, const Http2PriorityFields& v) { |
| 57 | return out << v.ToString(); |
| 58 | } |
| 59 | |
| 60 | // Http2RstStreamFields: |
| 61 | |
| 62 | bool operator==(const Http2RstStreamFields& a, const Http2RstStreamFields& b) { |
| 63 | return a.error_code == b.error_code; |
| 64 | } |
| 65 | |
| 66 | std::ostream& operator<<(std::ostream& out, const Http2RstStreamFields& v) { |
| 67 | return out << "error_code=" << v.error_code; |
| 68 | } |
| 69 | |
| 70 | // Http2SettingFields: |
| 71 | |
| 72 | bool operator==(const Http2SettingFields& a, const Http2SettingFields& b) { |
| 73 | return a.parameter == b.parameter && a.value == b.value; |
| 74 | } |
| 75 | std::ostream& operator<<(std::ostream& out, const Http2SettingFields& v) { |
| 76 | return out << "parameter=" << v.parameter << ", value=" << v.value; |
| 77 | } |
| 78 | |
| 79 | // Http2PushPromiseFields: |
| 80 | |
| 81 | bool operator==(const Http2PushPromiseFields& a, |
| 82 | const Http2PushPromiseFields& b) { |
| 83 | return a.promised_stream_id == b.promised_stream_id; |
| 84 | } |
| 85 | |
| 86 | std::ostream& operator<<(std::ostream& out, const Http2PushPromiseFields& v) { |
| 87 | return out << "promised_stream_id=" << v.promised_stream_id; |
| 88 | } |
| 89 | |
| 90 | // Http2PingFields: |
| 91 | |
| 92 | bool operator==(const Http2PingFields& a, const Http2PingFields& b) { |
| 93 | static_assert((sizeof a.opaque_bytes) == Http2PingFields::EncodedSize(), |
| 94 | "Why not the same size?"); |
| 95 | return 0 == |
| 96 | std::memcmp(a.opaque_bytes, b.opaque_bytes, sizeof a.opaque_bytes); |
| 97 | } |
| 98 | |
| 99 | std::ostream& operator<<(std::ostream& out, const Http2PingFields& v) { |
| 100 | return out << "opaque_bytes=0x" |
bnc | 3bf526d | 2021-04-19 05:01:31 -0700 | [diff] [blame] | 101 | << absl::BytesToHexString(absl::string_view( |
| 102 | reinterpret_cast<const char*>(v.opaque_bytes), |
| 103 | sizeof v.opaque_bytes)); |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Http2GoAwayFields: |
| 107 | |
| 108 | bool operator==(const Http2GoAwayFields& a, const Http2GoAwayFields& b) { |
| 109 | return a.last_stream_id == b.last_stream_id && a.error_code == b.error_code; |
| 110 | } |
| 111 | std::ostream& operator<<(std::ostream& out, const Http2GoAwayFields& v) { |
| 112 | return out << "last_stream_id=" << v.last_stream_id |
| 113 | << ", error_code=" << v.error_code; |
| 114 | } |
| 115 | |
| 116 | // Http2WindowUpdateFields: |
| 117 | |
| 118 | bool operator==(const Http2WindowUpdateFields& a, |
| 119 | const Http2WindowUpdateFields& b) { |
| 120 | return a.window_size_increment == b.window_size_increment; |
| 121 | } |
| 122 | std::ostream& operator<<(std::ostream& out, const Http2WindowUpdateFields& v) { |
| 123 | return out << "window_size_increment=" << v.window_size_increment; |
| 124 | } |
| 125 | |
| 126 | // Http2AltSvcFields: |
| 127 | |
| 128 | bool operator==(const Http2AltSvcFields& a, const Http2AltSvcFields& b) { |
| 129 | return a.origin_length == b.origin_length; |
| 130 | } |
| 131 | std::ostream& operator<<(std::ostream& out, const Http2AltSvcFields& v) { |
| 132 | return out << "origin_length=" << v.origin_length; |
| 133 | } |
| 134 | |
bnc | bea13b8 | 2021-01-08 08:46:09 -0800 | [diff] [blame] | 135 | // Http2PriorityUpdateFields: |
| 136 | |
| 137 | bool operator==(const Http2PriorityUpdateFields& a, |
| 138 | const Http2PriorityUpdateFields& b) { |
| 139 | return a.prioritized_stream_id == b.prioritized_stream_id; |
| 140 | } |
| 141 | |
| 142 | std::string Http2PriorityUpdateFields::ToString() const { |
| 143 | std::stringstream ss; |
| 144 | ss << "prioritized_stream_id=" << prioritized_stream_id; |
| 145 | return ss.str(); |
| 146 | } |
| 147 | |
| 148 | std::ostream& operator<<(std::ostream& out, |
| 149 | const Http2PriorityUpdateFields& v) { |
| 150 | return out << v.ToString(); |
| 151 | } |
| 152 | |
QUICHE team | fd50a40 | 2018-12-07 22:54:05 -0500 | [diff] [blame] | 153 | } // namespace http2 |