summaryrefslogtreecommitdiff
path: root/chromium/net/third_party/quiche/src/http2/hpack/tools/hpack_example.cc
blob: 00664b27f1485fda9f854384d5f4d5e79471e510 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "http2/hpack/tools/hpack_example.h"

#include <ctype.h>

#include "absl/strings/str_cat.h"
#include "http2/platform/api/http2_bug_tracker.h"
#include "http2/platform/api/http2_logging.h"
#include "http2/platform/api/http2_string_utils.h"

namespace http2 {
namespace test {
namespace {

void HpackExampleToStringOrDie(absl::string_view example, std::string* output) {
  while (!example.empty()) {
    const char c0 = example[0];
    if (isxdigit(c0)) {
      QUICHE_CHECK_GT(example.size(), 1u) << "Truncated hex byte?";
      const char c1 = example[1];
      QUICHE_CHECK(isxdigit(c1)) << "Found half a byte?";
      *output += Http2HexDecode(example.substr(0, 2));
      example.remove_prefix(2);
      continue;
    }
    if (isspace(c0)) {
      example.remove_prefix(1);
      continue;
    }
    if (!example.empty() && example[0] == '|') {
      // Start of a comment. Skip to end of line or of input.
      auto pos = example.find('\n');
      if (pos == absl::string_view::npos) {
        // End of input.
        break;
      }
      example.remove_prefix(pos + 1);
      continue;
    }
    HTTP2_BUG(http2_bug_107_1)
        << "Can't parse byte " << static_cast<int>(c0)
        << absl::StrCat(" (0x", absl::Hex(c0), ")") << "\nExample: " << example;
  }
  QUICHE_CHECK_LT(0u, output->size()) << "Example is empty.";
}

}  // namespace

std::string HpackExampleToStringOrDie(absl::string_view example) {
  std::string output;
  HpackExampleToStringOrDie(example, &output);
  return output;
}

}  // namespace test
}  // namespace http2