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

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: 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), append
31 // a new header whose name is name and value is value, to list.
32 m_headerList.append(adoptPtr(new Header(name, value)));
33 }
34
35 void FetchHeaderList::set(const String& name, const String& value)
36 {
37 // To set a name/value (name/value) pair in a header list (list), run these
38 // steps:
39 // 1. If there are any headers in list whose name is name, set the value of
40 // the first such header to value and remove the others.
41 // 2. Otherwise, append a new header whose name is name and value is value,
42 // to list.
43 for (size_t i = 0; i < m_headerList.size(); ++i) {
44 if (m_headerList[i]->first == name) {
45 m_headerList[i]->second = value;
46 for (size_t j = i + 1; j < m_headerList.size(); ) {
47 if (m_headerList[j]->first == name)
48 m_headerList.remove(j);
49 else
50 ++j;
51 }
52 return;
53 }
54 }
55 m_headerList.append(adoptPtr(new Header(name, value)));
56 }
57
58 size_t FetchHeaderList::size() const
59 {
60 return m_headerList.size();
61 }
62
63 void FetchHeaderList::remove(const String& name)
64 {
65 for (size_t i = 0; i < m_headerList.size(); ) {
66 if (m_headerList[i]->first == name) {
67 m_headerList.remove(i);
68 } else {
69 ++i;
70 }
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 return isOnAccessControlSimpleRequestHeaderWhitelist(AtomicString(name), Ato micString(value));
130 }
131
132 bool FetchHeaderList::isForbiddenHeaderName(const String& name)
133 {
134 // A forbidden header name is a header names that is one of:
135 // Accept-Charset, Accept-Encoding, Access-Control-Request-Headers,
136 // Access-Control-Request-Method, Connection, Content-Length, Cookie,
137 // Cookie2, Date, DNT, Expect, Host, Keep-Alive, Origin, Referer, TE,
138 // Trailer, Transfer-Encoding, Upgrade, User-Agent, Via
139 // and a header name that starts with Proxy- or Sec- (including when it is
140 // just Proxy- or Sec-).
141 return !XMLHttpRequest::isAllowedHTTPHeader(name);
142 }
143
144 bool FetchHeaderList::isForbiddenResponseHeaderName(const String& name)
145 {
146 // A forbidden response header name is a header name that is one of:
147 // `Set-Cookie`, `Set-Cookie2`
148 return equalIgnoringCase(name, "set-cookie") || equalIgnoringCase(name, "set -cookie2");
149 }
150
151 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698