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

Unified Diff: third_party/WebKit/Source/platform/network/ParsedParameterizedContentHeaderField.cpp

Issue 2880543003: Implement a parser for Content-Disposition header fields (Closed)
Patch Set: 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/ParsedParameterizedContentHeaderField.cpp
diff --git a/third_party/WebKit/Source/platform/network/ParsedParameterizedContentHeaderField.cpp b/third_party/WebKit/Source/platform/network/ParsedParameterizedContentHeaderField.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..a8470875d751066e9ff74a606345dff0cf3f61f0
--- /dev/null
+++ b/third_party/WebKit/Source/platform/network/ParsedParameterizedContentHeaderField.cpp
@@ -0,0 +1,251 @@
+/*
+ * 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/ParsedParameterizedContentHeaderField.h"
+
+#include "platform/wtf/text/StringBuilder.h"
+#include "platform/wtf/text/StringView.h"
+
+namespace blink {
+
+using Mode = ParsedParameterizedContentHeaderField::Mode;
+
+namespace {
+
+bool IsTokenCharacter(Mode mode, UChar c) {
+ 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
+
+ParsedParameterizedContentHeaderField::Input::Input(const String& header_field)
+ : index_(0u), input_(header_field) {}
+
+bool ParsedParameterizedContentHeaderField::Input::Consume(char c) {
+ DCHECK_NE(c, ' ');
+
+ SkipSpaces();
+ if (IsConsumed())
+ return false;
+
+ ++index_;
horo 2017/05/15 05:57:54 Why you don't use |c|?
e_hakkinen 2017/05/18 09:35:48 Due to too much refactoring. Fixed.
+ return true;
+}
+
+bool ParsedParameterizedContentHeaderField::Input::ConsumeQuotedString(
+ String& output) {
+ StringBuilder builder;
+
+ DCHECK_EQ('"', input_[index_]);
+ ++index_;
+
+ while (!IsConsumed()) {
+ if (input_[index_] == '"') {
+ output = builder.ToString();
+ ++index_;
+ return true;
+ }
+ if (input_[index_] == '\\') {
+ ++index_;
+ if (IsConsumed())
+ return false;
+ }
+ builder.Append(input_[index_]);
+ ++index_;
+ }
+ return false;
+}
+
+bool ParsedParameterizedContentHeaderField::Input::ConsumeToken(
+ Mode mode,
+ StringView& output) {
+ DCHECK(output.IsNull());
+
+ SkipSpaces();
+
+ auto start = index_;
+ while (!IsConsumed() && IsTokenCharacter(mode, input_[index_]))
+ ++index_;
+
+ if (start == index_)
+ return false;
+
+ output = StringView(input_, start, index_ - start);
+ return true;
+}
+
+bool ParsedParameterizedContentHeaderField::Input::ConsumeTokenOrQuotedString(
+ Mode mode,
+ String& output) {
+ SkipSpaces();
+ if (IsConsumed())
+ return false;
+
+ if (input_[index_] == '"')
+ return ConsumeQuotedString(output);
+
+ StringView view;
+ if (!ConsumeToken(mode, view))
+ return false;
+ output = view.ToString();
+ return true;
+}
+
+void ParsedParameterizedContentHeaderField::Input::SkipSpaces() {
+ while (!IsConsumed() && input_[index_] == ' ')
+ ++index_;
+}
+
+ParsedParameterizedContentHeaderField::ParsedParameterizedContentHeaderField()
+ : is_valid_(false) {}
+
+ParsedParameterizedContentHeaderField::ParsedParameterizedContentHeaderField(
horo 2017/05/15 05:57:54 Who call this? If you want to use it to parse Cont
e_hakkinen 2017/05/18 09:35:48 Anyone wanting to parse any content header field o
+ const String& header_field,
+ Mode mode)
+ : ParsedParameterizedContentHeaderField() {
+ is_valid_ = Parse(header_field, mode);
+}
+
+String ParsedParameterizedContentHeaderField::ParameterValueForName(
+ const String& name) const {
+ if (!name.ContainsOnlyASCII())
+ return String();
+ return parameters_.at(name.DeprecatedLower());
+}
+
+size_t ParsedParameterizedContentHeaderField::ParameterCount() const {
+ return parameters_.size();
+}
+
+bool ParsedParameterizedContentHeaderField::Parse(const String& header_field,
+ Mode mode) {
+ Input input(header_field);
+
+ StringView type;
+ if (!input.ConsumeToken(Mode::kNormal, type)) {
+ DVLOG(1) << "Failed to find `type' in '" << header_field << "'";
+ return false;
+ }
+ SetType(type.ToString());
+
+ return ParseParameters(input, mode);
+}
+
+// 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
+bool ParsedParameterizedContentHeaderField::ParseParameters(Input& input,
+ Mode mode) {
+ KeyValuePairs map;
+ for (;;) {
+ input.SkipSpaces();
+ if (input.IsConsumed())
+ break;
+
+ if (!input.Consume(';')) {
+ DVLOG(1) << "Failed to find ';'";
+ return false;
+ }
+
+ StringView key;
+ String value;
+ if (!input.ConsumeToken(Mode::kNormal, key)) {
+ DVLOG(1) << "Invalid content parameter name. (at " << input.Index()
+ << ")";
+ return false;
+ }
+ if (!input.Consume('=')) {
+ DVLOG(1) << "Failed to find '='";
+ return false;
+ }
+ if (!input.ConsumeTokenOrQuotedString(mode, value)) {
+ DVLOG(1) << "Invalid content parameter value (at " << input.Index()
+ << ", for '" << key.ToString() << "').";
+ return false;
+ }
+ // 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 false;
+ }
+ map.Set(key_string, value);
+ }
+ parameters_ = std::move(map);
+ return true;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698