xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/http2/test_tools/http2_trace_printer.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 #include "quiche/http2/test_tools/http2_trace_printer.h"
2 
3 #include <algorithm>
4 #include <cstddef>
5 
6 #include "absl/strings/escaping.h"
7 #include "absl/strings/match.h"
8 #include "absl/strings/string_view.h"
9 #include "quiche/http2/core/http2_trace_logging.h"
10 #include "quiche/spdy/core/spdy_protocol.h"
11 
12 namespace http2 {
13 namespace test {
14 namespace {
15 
IsLoggingEnabled()16 bool IsLoggingEnabled() { return true; }
17 
18 }  // namespace
19 
Http2TracePrinter(absl::string_view perspective,const void * connection_id,bool consume_connection_preface)20 Http2TracePrinter::Http2TracePrinter(absl::string_view perspective,
21                                      const void* connection_id,
22                                      bool consume_connection_preface)
23     : logger_(&visitor_, perspective, IsLoggingEnabled, connection_id),
24       perspective_(perspective) {
25   decoder_.set_visitor(&logger_);
26   if (consume_connection_preface) {
27     remaining_preface_ =
28         absl::string_view(spdy::kHttp2ConnectionHeaderPrefix,
29                           spdy::kHttp2ConnectionHeaderPrefixSize);
30   }
31 }
32 
ProcessInput(absl::string_view bytes)33 void Http2TracePrinter::ProcessInput(absl::string_view bytes) {
34   if (preface_error_) {
35     HTTP2_TRACE_LOG(perspective_, IsLoggingEnabled)
36         << "Earlier connection preface error, ignoring " << bytes.size()
37         << " bytes";
38     return;
39   }
40   if (!remaining_preface_.empty()) {
41     const size_t consumed = std::min(remaining_preface_.size(), bytes.size());
42 
43     const absl::string_view preface = bytes.substr(0, consumed);
44     HTTP2_TRACE_LOG(perspective_, IsLoggingEnabled)
45         << "Received connection preface: " << absl::CEscape(preface);
46 
47     if (!absl::StartsWith(remaining_preface_, preface)) {
48       HTTP2_TRACE_LOG(perspective_, IsLoggingEnabled)
49           << "Received preface does not match expected remaining preface: "
50           << absl::CEscape(remaining_preface_);
51       preface_error_ = true;
52       return;
53     }
54     bytes.remove_prefix(consumed);
55     remaining_preface_.remove_prefix(consumed);
56   }
57   decoder_.ProcessInput(bytes.data(), bytes.size());
58 }
59 
60 }  // namespace test
61 }  // namespace http2
62