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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/network/ParsedContentHeaderFieldParameters.cpp
diff --git a/third_party/WebKit/Source/platform/network/ParsedContentHeaderFieldParameters.cpp b/third_party/WebKit/Source/platform/network/ParsedContentHeaderFieldParameters.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..79ea35f71acf2c6917611e45a8b7f8c2ef90ba48
--- /dev/null
+++ b/third_party/WebKit/Source/platform/network/ParsedContentHeaderFieldParameters.cpp
@@ -0,0 +1,130 @@
+/*
horo 2017/05/22 02:31:19 nit: ditto
e_hakkinen 2017/05/22 07:17:08 Done.
+ * Copyright (C) 2011 Google Inc. All rights reserved.
+ * Copyright (C) 2012 Intel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "platform/network/ParsedContentHeaderFieldParameters.h"
+
+#include "platform/network/ContentHeaderFieldInput.h"
+#include "platform/wtf/text/StringBuilder.h"
+#include "platform/wtf/text/StringView.h"
+
+namespace blink {
+
+ParsedContentHeaderFieldParameters::ParsedContentHeaderFieldParameters()
+ : is_valid_(false) {}
+
+// static
+ParsedContentHeaderFieldParameters
+ParsedContentHeaderFieldParameters::CreateForTesting(const String& input,
+ Mode mode) {
+ ParsedContentHeaderFieldParameters parameters;
+ parameters.ParseParameters(ContentHeaderFieldInput(input), mode);
+ return parameters;
+}
+
+String ParsedContentHeaderFieldParameters::ParameterValueForName(
+ const String& name) const {
+ if (!name.ContainsOnlyASCII())
+ return String();
+ return parameters_.at(name.DeprecatedLower());
+}
+
+size_t ParsedContentHeaderFieldParameters::ParameterCount() const {
+ return parameters_.size();
+}
+
+// parameters := *(";" parameter)
+//
+// From http://tools.ietf.org/html/rfc2045#section-5.1:
+//
+// parameter := attribute "=" value
+//
+// attribute := token
+// ; Matching of attributes
+// ; is ALWAYS case-insensitive.
+//
+// value := token / quoted-string
+//
+// token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
+// or tspecials>
+//
+// tspecials := "(" / ")" / "<" / ">" / "@" /
+// "," / ";" / ":" / "\" / <">
+// "/" / "[" / "]" / "?" / "="
+// ; Must be in quoted-string,
+// ; to use within parameter values
+void ParsedContentHeaderFieldParameters::ParseParameters(
+ ContentHeaderFieldInput input,
+ Mode mode) {
+ DCHECK(!is_valid_);
+ DCHECK(parameters_.IsEmpty());
+
+ KeyValuePairs map;
+ for (;;) {
+ input.SkipSpaces();
+ if (input.IsConsumed())
+ break;
+
+ if (!input.Consume(';')) {
+ DVLOG(1) << "Failed to find ';'";
+ return;
+ }
+
+ StringView key;
+ String value;
+ if (!input.ConsumeToken(Mode::kNormal, key)) {
+ DVLOG(1) << "Invalid content parameter name. (at " << input.Index()
+ << ")";
+ return;
+ }
+ if (!input.Consume('=')) {
+ DVLOG(1) << "Failed to find '='";
+ return;
+ }
+ if (!input.ConsumeTokenOrQuotedString(mode, value)) {
+ DVLOG(1) << "Invalid content parameter value (at " << input.Index()
+ << ", for '" << key.ToString() << "').";
+ return;
+ }
+ // As |key| is parsed as a token, it consists of ascii characters
+ // and hence we don't need to care about non-ascii lowercasing.
+ DCHECK(key.ToString().ContainsOnlyASCII());
+ String key_string = key.ToString().DeprecatedLower();
+ if (mode == Mode::kStrict && map.find(key_string) != map.end()) {
+ DVLOG(1) << "Parameter " << key_string << " is defined multiple times.";
+ return;
+ }
+ map.Set(key_string, value);
+ }
+ parameters_ = std::move(map);
+ is_valid_ = true;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698