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

Side by Side Diff: Source/modules/serviceworkers/FetchHeaderList.cpp

Issue 358573002: [ServiceWorker] Implement Headers class. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebase Created 6 years, 6 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 2014 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 "config.h"
6 #include "FetchHeaderList.h"
7
8 #include "core/fetch/CrossOriginAccessControl.h"
9 #include "core/xml/XMLHttpRequest.h"
10 #include "platform/network/HTTPParsers.h"
11 #include "wtf/PassOwnPtr.h"
12
13 namespace WebCore {
14
15 PassRefPtr<FetchHeaderList> FetchHeaderList::create()
16 {
17 return adoptRef(new FetchHeaderList());
18 }
19
20 FetchHeaderList::FetchHeaderList()
21 {
22 }
23
24 FetchHeaderList::~FetchHeaderList()
25 {
26 }
27
28 void FetchHeaderList::append(const String& name, const String& value)
29 {
30 // "To append a name/value (|name|/|value|) pair to a header list (|list|),
31 // append a new header whose name is |name| and value is |value|, to
32 // |list|."
33 m_headerList.append(adoptPtr(new Header(name, value)));
34 }
35
36 void FetchHeaderList::set(const String& name, const String& value)
37 {
38 // "To set a name/value (|name|/|value|) pair in a header list (|list|), run
39 // these steps:
40 // 1. If there are any headers in |list| whose name is |name|, set the value
41 // of the first such header to |value| and remove the others.
42 // 2. Otherwise, append a new header whose name is |name| and value is
43 // |value|, to |list|."
44 for (size_t i = 0; i < m_headerList.size(); ++i) {
45 if (m_headerList[i]->first == name) {
46 m_headerList[i]->second = value;
47 for (size_t j = i + 1; j < m_headerList.size(); ) {
48 if (m_headerList[j]->first == name)
49 m_headerList.remove(j);
50 else
51 ++j;
52 }
53 return;
54 }
55 }
56 m_headerList.append(adoptPtr(new Header(name, value)));
57 }
58
59 size_t FetchHeaderList::size() const
60 {
61 return m_headerList.size();
62 }
63
64 void FetchHeaderList::remove(const String& name)
65 {
66 for (size_t i = 0; i < m_headerList.size(); ) {
67 if (m_headerList[i]->first == name)
68 m_headerList.remove(i);
69 else
70 ++i;
71 }
72 }
73
74 bool FetchHeaderList::get(const String& name, String& result) const
75 {
76 for (size_t i = 0; i < m_headerList.size(); ++i) {
77 if (m_headerList[i]->first == name) {
78 result = m_headerList[i]->second;
79 return true;
80 }
81 }
82 return false;
83 }
84
85 void FetchHeaderList::getAll(const String& name, Vector<String>& result) const
86 {
87 result.clear();
88 for (size_t i = 0; i < m_headerList.size(); ++i) {
89 if (m_headerList[i]->first == name)
90 result.append(m_headerList[i]->second);
91 }
92 }
93
94 bool FetchHeaderList::has(const String& name) const
95 {
96 for (size_t i = 0; i < m_headerList.size(); ++i) {
97 if (m_headerList[i]->first == name)
98 return true;
99 }
100 return false;
101 }
102
103 void FetchHeaderList::clearList()
104 {
105 m_headerList.clear();
106 }
107
108 bool FetchHeaderList::isValidHeaderName(const String& name)
109 {
110 // FIXME: According to the spec (http://fetch.spec.whatwg.org/) should we
111 // accept all characters in the range 0x00 to 0x7F?
112 return isValidHTTPToken(name);
113 }
114
115 bool FetchHeaderList::isValidHeaderValue(const String& value)
116 {
117 // "A value is a byte sequence that matches the field-value token production
118 // and contains no 0x0A or 0x0D bytes."
119 return (value.find('\r') == kNotFound) && (value.find('\n') == kNotFound);
120 }
121
122 bool FetchHeaderList::isSimpleHeader(const String& name, const String& value)
123 {
124 // "A simple header is a header whose name is either one of `Accept`,
125 // `Accept-Language`, and `Content-Language`, or whose name is
126 // `Content-Type` and value, once parsed, is one of
127 // `application/x-www-form-urlencoded`, `multipart/form-data`, and
128 // `text/plain`."
129 if (equalIgnoringCase(name, "accept")
130 || equalIgnoringCase(name, "accept-language")
131 || equalIgnoringCase(name, "content-language"))
132 return true;
133
134 if (equalIgnoringCase(name, "content-type")) {
135 AtomicString mimeType = extractMIMETypeFromMediaType(AtomicString(value) );
136 return equalIgnoringCase(mimeType, "application/x-www-form-urlencoded")
137 || equalIgnoringCase(mimeType, "multipart/form-data")
138 || equalIgnoringCase(mimeType, "text/plain");
139 }
140
141 return false;
142 }
143
144 bool FetchHeaderList::isForbiddenHeaderName(const String& name)
145 {
146 // "A forbidden header name is a header names that is one of:
147 // `Accept-Charset`, `Accept-Encoding`, `Access-Control-Request-Headers`,
148 // `Access-Control-Request-Method`, `Connection`,
149 // `Content-Length, Cookie`, `Cookie2`, `Date`, `DNT`, `Expect`, `Host`,
150 // `Keep-Alive`, `Origin`, `Referer`, `TE`, `Trailer`,
151 // `Transfer-Encoding`, `Upgrade`, `User-Agent`, `Via`
152 // or starts with `Proxy-` or `Sec-` (including when it is just `Proxy-` or
153 // `Sec-`)."
154 return !XMLHttpRequest::isAllowedHTTPHeader(name);
155 }
156
157 bool FetchHeaderList::isForbiddenResponseHeaderName(const String& name)
158 {
159 // "A forbidden response header name is a header name that is one of:
160 // `Set-Cookie`, `Set-Cookie2`"
161 return equalIgnoringCase(name, "set-cookie") || equalIgnoringCase(name, "set -cookie2");
162 }
163
164 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/serviceworkers/FetchHeaderList.h ('k') | Source/modules/serviceworkers/Headers.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698