blob: 64255b1ef2d8d8037cfe6f6f653a0c8d16fb680c [file] [log] [blame]
QUICHE teamfd50a402018-12-07 22:54:05 -05001// 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 team5be974e2020-12-29 18:35:24 -05005#include "http2/hpack/tools/hpack_example.h"
QUICHE teamfd50a402018-12-07 22:54:05 -05006
7#include <ctype.h>
8
bnc3bf526d2021-04-19 05:01:31 -07009#include "absl/strings/escaping.h"
vasilvvbca9deb2020-12-03 13:42:00 -080010#include "absl/strings/str_cat.h"
QUICHE team5be974e2020-12-29 18:35:24 -050011#include "http2/platform/api/http2_bug_tracker.h"
12#include "http2/platform/api/http2_logging.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050013
14namespace http2 {
15namespace test {
16namespace {
17
vasilvv999985c2020-10-12 19:15:19 -070018void HpackExampleToStringOrDie(absl::string_view example, std::string* output) {
QUICHE teamfd50a402018-12-07 22:54:05 -050019 while (!example.empty()) {
20 const char c0 = example[0];
21 if (isxdigit(c0)) {
vasilvvafcc3172021-02-02 12:01:07 -080022 QUICHE_CHECK_GT(example.size(), 1u) << "Truncated hex byte?";
QUICHE teamfd50a402018-12-07 22:54:05 -050023 const char c1 = example[1];
vasilvvafcc3172021-02-02 12:01:07 -080024 QUICHE_CHECK(isxdigit(c1)) << "Found half a byte?";
bnc3bf526d2021-04-19 05:01:31 -070025 *output += absl::HexStringToBytes(example.substr(0, 2));
QUICHE teamfd50a402018-12-07 22:54:05 -050026 example.remove_prefix(2);
27 continue;
28 }
29 if (isspace(c0)) {
30 example.remove_prefix(1);
31 continue;
32 }
33 if (!example.empty() && example[0] == '|') {
34 // Start of a comment. Skip to end of line or of input.
35 auto pos = example.find('\n');
vasilvv999985c2020-10-12 19:15:19 -070036 if (pos == absl::string_view::npos) {
QUICHE teamfd50a402018-12-07 22:54:05 -050037 // End of input.
38 break;
39 }
40 example.remove_prefix(pos + 1);
41 continue;
42 }
QUICHE teambe191282021-03-18 09:44:39 -070043 HTTP2_BUG(http2_bug_107_1)
QUICHE team0ec81ca2021-03-11 09:29:06 -080044 << "Can't parse byte " << static_cast<int>(c0)
bnc760e7232021-03-25 11:14:30 -070045 << absl::StrCat(" (0x", absl::Hex(c0), ")") << "\nExample: " << example;
QUICHE teamfd50a402018-12-07 22:54:05 -050046 }
vasilvvafcc3172021-02-02 12:01:07 -080047 QUICHE_CHECK_LT(0u, output->size()) << "Example is empty.";
QUICHE teamfd50a402018-12-07 22:54:05 -050048}
49
50} // namespace
51
vasilvv999985c2020-10-12 19:15:19 -070052std::string HpackExampleToStringOrDie(absl::string_view example) {
bnc47904002019-08-16 11:49:48 -070053 std::string output;
QUICHE teamfd50a402018-12-07 22:54:05 -050054 HpackExampleToStringOrDie(example, &output);
55 return output;
56}
57
58} // namespace test
59} // namespace http2