blob: b2783e3b277f1f3bfeeb2c2b558d220c04fa89b4 [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"
13#include "http2/platform/api/http2_string_utils.h"
QUICHE teamfd50a402018-12-07 22:54:05 -050014
15namespace http2 {
16namespace test {
17namespace {
18
vasilvv999985c2020-10-12 19:15:19 -070019void HpackExampleToStringOrDie(absl::string_view example, std::string* output) {
QUICHE teamfd50a402018-12-07 22:54:05 -050020 while (!example.empty()) {
21 const char c0 = example[0];
22 if (isxdigit(c0)) {
vasilvvafcc3172021-02-02 12:01:07 -080023 QUICHE_CHECK_GT(example.size(), 1u) << "Truncated hex byte?";
QUICHE teamfd50a402018-12-07 22:54:05 -050024 const char c1 = example[1];
vasilvvafcc3172021-02-02 12:01:07 -080025 QUICHE_CHECK(isxdigit(c1)) << "Found half a byte?";
bnc3bf526d2021-04-19 05:01:31 -070026 *output += absl::HexStringToBytes(example.substr(0, 2));
QUICHE teamfd50a402018-12-07 22:54:05 -050027 example.remove_prefix(2);
28 continue;
29 }
30 if (isspace(c0)) {
31 example.remove_prefix(1);
32 continue;
33 }
34 if (!example.empty() && example[0] == '|') {
35 // Start of a comment. Skip to end of line or of input.
36 auto pos = example.find('\n');
vasilvv999985c2020-10-12 19:15:19 -070037 if (pos == absl::string_view::npos) {
QUICHE teamfd50a402018-12-07 22:54:05 -050038 // End of input.
39 break;
40 }
41 example.remove_prefix(pos + 1);
42 continue;
43 }
QUICHE teambe191282021-03-18 09:44:39 -070044 HTTP2_BUG(http2_bug_107_1)
QUICHE team0ec81ca2021-03-11 09:29:06 -080045 << "Can't parse byte " << static_cast<int>(c0)
bnc760e7232021-03-25 11:14:30 -070046 << absl::StrCat(" (0x", absl::Hex(c0), ")") << "\nExample: " << example;
QUICHE teamfd50a402018-12-07 22:54:05 -050047 }
vasilvvafcc3172021-02-02 12:01:07 -080048 QUICHE_CHECK_LT(0u, output->size()) << "Example is empty.";
QUICHE teamfd50a402018-12-07 22:54:05 -050049}
50
51} // namespace
52
vasilvv999985c2020-10-12 19:15:19 -070053std::string HpackExampleToStringOrDie(absl::string_view example) {
bnc47904002019-08-16 11:49:48 -070054 std::string output;
QUICHE teamfd50a402018-12-07 22:54:05 -050055 HpackExampleToStringOrDie(example, &output);
56 return output;
57}
58
59} // namespace test
60} // namespace http2