Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
|
horo
2017/05/22 02:31:19
Nit: Please use this license header for new files.
e_hakkinen
2017/05/22 07:17:08
Done.
| |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | |
| 3 * Copyright (C) 2012 Intel Corporation. All rights reserved. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are | |
| 7 * met: | |
| 8 * | |
| 9 * * Redistributions of source code must retain the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer. | |
| 11 * * Redistributions in binary form must reproduce the above | |
| 12 * copyright notice, this list of conditions and the following disclaimer | |
| 13 * in the documentation and/or other materials provided with the | |
| 14 * distribution. | |
| 15 * * Neither the name of Google Inc. nor the names of its | |
| 16 * contributors may be used to endorse or promote products derived from | |
| 17 * this software without specific prior written permission. | |
| 18 * | |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 */ | |
| 31 | |
| 32 #include "platform/network/ContentHeaderFieldInput.h" | |
| 33 | |
| 34 #include "platform/wtf/text/StringBuilder.h" | |
| 35 #include "platform/wtf/text/StringView.h" | |
| 36 | |
| 37 namespace blink { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 using Mode = ContentHeaderFieldInput::Mode; | |
| 42 | |
| 43 bool IsTokenCharacter(Mode mode, UChar c) { | |
| 44 if (c >= 128) | |
| 45 return false; | |
| 46 if (c < 0x20) | |
| 47 return false; | |
| 48 | |
| 49 switch (c) { | |
| 50 case ' ': | |
| 51 case ';': | |
| 52 case '"': | |
| 53 return false; | |
| 54 case '(': | |
| 55 case ')': | |
| 56 case '<': | |
| 57 case '>': | |
| 58 case '@': | |
| 59 case ',': | |
| 60 case ':': | |
| 61 case '\\': | |
| 62 case '/': | |
| 63 case '[': | |
| 64 case ']': | |
| 65 case '?': | |
| 66 case '=': | |
| 67 return mode == Mode::kRelaxed; | |
| 68 default: | |
| 69 return true; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 } // namespace | |
| 74 | |
| 75 ContentHeaderFieldInput::ContentHeaderFieldInput(const String& header_field) | |
| 76 : index_(0u), input_(header_field) {} | |
| 77 | |
| 78 ContentHeaderFieldInput::ContentHeaderFieldInput(ContentHeaderFieldInput&&) = | |
| 79 default; | |
| 80 | |
| 81 bool ContentHeaderFieldInput::Consume(char c) { | |
| 82 DCHECK_NE(c, ' '); | |
| 83 | |
| 84 SkipSpaces(); | |
| 85 if (IsConsumed() || input_[index_] != c) | |
| 86 return false; | |
| 87 | |
| 88 ++index_; | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 bool ContentHeaderFieldInput::ConsumeQuotedString(String& output) { | |
| 93 StringBuilder builder; | |
| 94 | |
| 95 DCHECK_EQ('"', input_[index_]); | |
| 96 ++index_; | |
| 97 | |
| 98 while (!IsConsumed()) { | |
| 99 if (input_[index_] == '"') { | |
| 100 output = builder.ToString(); | |
| 101 ++index_; | |
| 102 return true; | |
| 103 } | |
| 104 if (input_[index_] == '\\') { | |
| 105 ++index_; | |
| 106 if (IsConsumed()) | |
| 107 return false; | |
| 108 } | |
| 109 builder.Append(input_[index_]); | |
| 110 ++index_; | |
| 111 } | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 bool ContentHeaderFieldInput::ConsumeToken(Mode mode, StringView& output) { | |
| 116 DCHECK(output.IsNull()); | |
| 117 | |
| 118 SkipSpaces(); | |
| 119 | |
| 120 auto start = index_; | |
| 121 while (!IsConsumed() && IsTokenCharacter(mode, input_[index_])) | |
| 122 ++index_; | |
| 123 | |
| 124 if (start == index_) | |
| 125 return false; | |
| 126 | |
| 127 output = StringView(input_, start, index_ - start); | |
| 128 return true; | |
| 129 } | |
| 130 | |
| 131 bool ContentHeaderFieldInput::ConsumeTokenOrQuotedString(Mode mode, | |
| 132 String& output) { | |
| 133 SkipSpaces(); | |
| 134 if (IsConsumed()) | |
| 135 return false; | |
| 136 | |
| 137 if (input_[index_] == '"') | |
| 138 return ConsumeQuotedString(output); | |
| 139 | |
| 140 StringView view; | |
| 141 if (!ConsumeToken(mode, view)) | |
| 142 return false; | |
| 143 output = view.ToString(); | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 void ContentHeaderFieldInput::SkipSpaces() { | |
| 148 while (!IsConsumed() && input_[index_] == ' ') | |
| 149 ++index_; | |
| 150 } | |
| 151 | |
| 152 } // namespace blink | |
| OLD | NEW |