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

Unified Diff: third_party/WebKit/Source/platform/network/ContentHeaderFieldInput.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/ContentHeaderFieldInput.cpp
diff --git a/third_party/WebKit/Source/platform/network/ContentHeaderFieldInput.cpp b/third_party/WebKit/Source/platform/network/ContentHeaderFieldInput.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..038cd8fc7bba0bc57061997a3c1b7c9f278219c9
--- /dev/null
+++ b/third_party/WebKit/Source/platform/network/ContentHeaderFieldInput.cpp
@@ -0,0 +1,152 @@
+/*
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.
+ * 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/ContentHeaderFieldInput.h"
+
+#include "platform/wtf/text/StringBuilder.h"
+#include "platform/wtf/text/StringView.h"
+
+namespace blink {
+
+namespace {
+
+using Mode = ContentHeaderFieldInput::Mode;
+
+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
+
+ContentHeaderFieldInput::ContentHeaderFieldInput(const String& header_field)
+ : index_(0u), input_(header_field) {}
+
+ContentHeaderFieldInput::ContentHeaderFieldInput(ContentHeaderFieldInput&&) =
+ default;
+
+bool ContentHeaderFieldInput::Consume(char c) {
+ DCHECK_NE(c, ' ');
+
+ SkipSpaces();
+ if (IsConsumed() || input_[index_] != c)
+ return false;
+
+ ++index_;
+ return true;
+}
+
+bool ContentHeaderFieldInput::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 ContentHeaderFieldInput::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 ContentHeaderFieldInput::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 ContentHeaderFieldInput::SkipSpaces() {
+ while (!IsConsumed() && input_[index_] == ' ')
+ ++index_;
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698