Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: third_party/WebKit/Source/platform/network/ContentHeaderFieldInput.cpp

Issue 2880543003: Implement a parser for Content-Disposition header fields (Closed)
Patch Set: License headers Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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
5 #include "platform/network/ContentHeaderFieldInput.h"
6
7 #include "platform/wtf/text/StringBuilder.h"
8 #include "platform/wtf/text/StringView.h"
9
10 namespace blink {
11
12 namespace {
13
14 using Mode = ContentHeaderFieldInput::Mode;
15
16 bool IsTokenCharacter(Mode mode, UChar c) {
17 if (c >= 128)
18 return false;
19 if (c < 0x20)
20 return false;
21
22 switch (c) {
23 case ' ':
24 case ';':
25 case '"':
26 return false;
27 case '(':
28 case ')':
29 case '<':
30 case '>':
31 case '@':
32 case ',':
33 case ':':
34 case '\\':
35 case '/':
36 case '[':
37 case ']':
38 case '?':
39 case '=':
40 return mode == Mode::kRelaxed;
41 default:
42 return true;
43 }
44 }
45
46 } // namespace
47
48 ContentHeaderFieldInput::ContentHeaderFieldInput(const String& header_field)
49 : index_(0u), input_(header_field) {}
50
51 ContentHeaderFieldInput::ContentHeaderFieldInput(ContentHeaderFieldInput&&) =
52 default;
53
54 bool ContentHeaderFieldInput::Consume(char c) {
55 DCHECK_NE(c, ' ');
56
57 SkipSpaces();
58 if (IsConsumed() || input_[index_] != c)
59 return false;
60
61 ++index_;
62 return true;
63 }
64
65 bool ContentHeaderFieldInput::ConsumeQuotedString(String& output) {
66 StringBuilder builder;
67
68 DCHECK_EQ('"', input_[index_]);
69 ++index_;
70
71 while (!IsConsumed()) {
72 if (input_[index_] == '"') {
73 output = builder.ToString();
74 ++index_;
75 return true;
76 }
77 if (input_[index_] == '\\') {
78 ++index_;
79 if (IsConsumed())
80 return false;
81 }
82 builder.Append(input_[index_]);
83 ++index_;
84 }
85 return false;
86 }
87
88 bool ContentHeaderFieldInput::ConsumeToken(Mode mode, StringView& output) {
89 DCHECK(output.IsNull());
90
91 SkipSpaces();
92
93 auto start = index_;
94 while (!IsConsumed() && IsTokenCharacter(mode, input_[index_]))
95 ++index_;
96
97 if (start == index_)
98 return false;
99
100 output = StringView(input_, start, index_ - start);
101 return true;
102 }
103
104 bool ContentHeaderFieldInput::ConsumeTokenOrQuotedString(Mode mode,
105 String& output) {
106 SkipSpaces();
107 if (IsConsumed())
108 return false;
109
110 if (input_[index_] == '"')
111 return ConsumeQuotedString(output);
112
113 StringView view;
114 if (!ConsumeToken(mode, view))
115 return false;
116 output = view.ToString();
117 return true;
118 }
119
120 void ContentHeaderFieldInput::SkipSpaces() {
121 while (!IsConsumed() && input_[index_] == ' ')
122 ++index_;
123 }
124
125 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698