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

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

Issue 2880543003: Implement a parser for Content-Disposition header fields (Closed)
Patch Set: Inheritance to delegation, tests 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 /*
horo 2017/05/22 02:31:19 nit: ditto
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/ParsedContentHeaderFieldParameters.h"
33
34 #include "platform/network/ContentHeaderFieldInput.h"
35 #include "platform/wtf/text/StringBuilder.h"
36 #include "platform/wtf/text/StringView.h"
37
38 namespace blink {
39
40 ParsedContentHeaderFieldParameters::ParsedContentHeaderFieldParameters()
41 : is_valid_(false) {}
42
43 // static
44 ParsedContentHeaderFieldParameters
45 ParsedContentHeaderFieldParameters::CreateForTesting(const String& input,
46 Mode mode) {
47 ParsedContentHeaderFieldParameters parameters;
48 parameters.ParseParameters(ContentHeaderFieldInput(input), mode);
49 return parameters;
50 }
51
52 String ParsedContentHeaderFieldParameters::ParameterValueForName(
53 const String& name) const {
54 if (!name.ContainsOnlyASCII())
55 return String();
56 return parameters_.at(name.DeprecatedLower());
57 }
58
59 size_t ParsedContentHeaderFieldParameters::ParameterCount() const {
60 return parameters_.size();
61 }
62
63 // parameters := *(";" parameter)
64 //
65 // From http://tools.ietf.org/html/rfc2045#section-5.1:
66 //
67 // parameter := attribute "=" value
68 //
69 // attribute := token
70 // ; Matching of attributes
71 // ; is ALWAYS case-insensitive.
72 //
73 // value := token / quoted-string
74 //
75 // token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
76 // or tspecials>
77 //
78 // tspecials := "(" / ")" / "<" / ">" / "@" /
79 // "," / ";" / ":" / "\" / <">
80 // "/" / "[" / "]" / "?" / "="
81 // ; Must be in quoted-string,
82 // ; to use within parameter values
83 void ParsedContentHeaderFieldParameters::ParseParameters(
84 ContentHeaderFieldInput input,
85 Mode mode) {
86 DCHECK(!is_valid_);
87 DCHECK(parameters_.IsEmpty());
88
89 KeyValuePairs map;
90 for (;;) {
91 input.SkipSpaces();
92 if (input.IsConsumed())
93 break;
94
95 if (!input.Consume(';')) {
96 DVLOG(1) << "Failed to find ';'";
97 return;
98 }
99
100 StringView key;
101 String value;
102 if (!input.ConsumeToken(Mode::kNormal, key)) {
103 DVLOG(1) << "Invalid content parameter name. (at " << input.Index()
104 << ")";
105 return;
106 }
107 if (!input.Consume('=')) {
108 DVLOG(1) << "Failed to find '='";
109 return;
110 }
111 if (!input.ConsumeTokenOrQuotedString(mode, value)) {
112 DVLOG(1) << "Invalid content parameter value (at " << input.Index()
113 << ", for '" << key.ToString() << "').";
114 return;
115 }
116 // As |key| is parsed as a token, it consists of ascii characters
117 // and hence we don't need to care about non-ascii lowercasing.
118 DCHECK(key.ToString().ContainsOnlyASCII());
119 String key_string = key.ToString().DeprecatedLower();
120 if (mode == Mode::kStrict && map.find(key_string) != map.end()) {
121 DVLOG(1) << "Parameter " << key_string << " is defined multiple times.";
122 return;
123 }
124 map.Set(key_string, value);
125 }
126 parameters_ = std::move(map);
127 is_valid_ = true;
128 }
129
130 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698