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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/network/header_field_tokenizer.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/string_view.h"
namespace blink {
namespace {
using Mode = HeaderFieldTokenizer::Mode;
bool IsTokenCharacter(Mode mode, UChar c) {
// TODO(cvazac) change this to use LChar
// TODO(cvazac) Check HTTPArchive for usage and possible deprecation.
// According to https://tools.ietf.org/html/rfc7230#appendix-B, the
// following characters (ASCII decimal) should not be included in a TOKEN:
// 123 ('{')
// 125 ('}')
// 127 (delete)
if (c >= 128)
return false;
if (c < 0x20)
return false;
switch (c) {
case ' ':
case ';':
case '"':
return false;
case '(':
case ')':
case '<':
case '>':
case '@':
case ',':
case ':':
case '\\':
case '/':
case '[':
case ']':
case '?':
case '=':
return mode == Mode::kRelaxed;
default:
return true;
}
}
} // namespace
HeaderFieldTokenizer::HeaderFieldTokenizer(const String& header_field)
: index_(0u), input_(header_field) {
SkipOptionalWhitespace();
}
HeaderFieldTokenizer::HeaderFieldTokenizer(HeaderFieldTokenizer&&) = default;
bool HeaderFieldTokenizer::Consume(char c) {
// TODO(cvazac) change this to use LChar
DCHECK_NE(c, ' ');
DCHECK_NE(c, '\t');
if (IsConsumed() || input_[index_] != c)
return false;
++index_;
SkipOptionalWhitespace();
return true;
}
bool HeaderFieldTokenizer::ConsumeQuotedString(String& output) {
StringBuilder builder;
DCHECK_EQ('"', input_[index_]);
++index_;
while (!IsConsumed()) {
if (input_[index_] == '"') {
output = builder.ToString();
++index_;
SkipOptionalWhitespace();
return true;
}
if (input_[index_] == '\\') {
++index_;
if (IsConsumed())
return false;
}
builder.Append(input_[index_]);
++index_;
}
return false;
}
bool HeaderFieldTokenizer::ConsumeToken(Mode mode, StringView& output) {
DCHECK(output.IsNull());
auto start = index_;
while (!IsConsumed() && IsTokenCharacter(mode, input_[index_]))
++index_;
if (start == index_)
return false;
output = StringView(input_, start, index_ - start);
SkipOptionalWhitespace();
return true;
}
bool HeaderFieldTokenizer::ConsumeTokenOrQuotedString(Mode mode,
String& output) {
if (IsConsumed())
return false;
if (input_[index_] == '"')
return ConsumeQuotedString(output);
StringView view;
if (!ConsumeToken(mode, view))
return false;
output = view.ToString();
return true;
}
void HeaderFieldTokenizer::SkipOptionalWhitespace() {
while (!IsConsumed() && (input_[index_] == ' ' || input_[index_] == '\t'))
++index_;
}
void HeaderFieldTokenizer::ConsumeBeforeAnyCharMatch(Vector<LChar> chars) {
// TODO(cvazac) move this to HeaderFieldTokenizer c'tor
DCHECK(input_.Is8Bit());
DCHECK_GT(chars.size(), 0U);
DCHECK_LT(chars.size(), 3U);
while (!IsConsumed()) {
for (const auto& c : chars) {
if (c == input_[index_]) {
return;
}
}
++index_;
}
}
} // namespace blink
|