OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "Request.h" | 6 #include "Request.h" |
7 | 7 |
8 #include "bindings/core/v8/Dictionary.h" | 8 #include "bindings/core/v8/Dictionary.h" |
9 #include "core/dom/ExecutionContext.h" | 9 #include "core/dom/ExecutionContext.h" |
10 #include "core/fetch/FetchUtils.h" | 10 #include "core/fetch/FetchUtils.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 virtual bool handleItem(const String& value, const String& key, Headers*) | 37 virtual bool handleItem(const String& value, const String& key, Headers*) |
38 { | 38 { |
39 m_webRequest->setHeader(key, value); | 39 m_webRequest->setHeader(key, value); |
40 return true; | 40 return true; |
41 } | 41 } |
42 | 42 |
43 private: | 43 private: |
44 WebServiceWorkerRequest* m_webRequest; | 44 WebServiceWorkerRequest* m_webRequest; |
45 }; | 45 }; |
46 | 46 |
47 PassRefPtrWillBeRawPtr<Request> createRequestWithRequestData(PassRefPtrWillBeRaw
Ptr<FetchRequestData> request, const RequestInit& init, FetchRequestData::Mode m
ode, FetchRequestData::Credentials credentials, ExceptionState& exceptionState) | 47 Request* createRequestWithRequestData(FetchRequestData* request, const RequestIn
it& init, FetchRequestData::Mode mode, FetchRequestData::Credentials credentials
, ExceptionState& exceptionState) |
48 { | 48 { |
49 // "6. Let |mode| be |init|'s mode member if it is present, and | 49 // "6. Let |mode| be |init|'s mode member if it is present, and |
50 // |fallbackMode| otherwise." | 50 // |fallbackMode| otherwise." |
51 // "7. If |mode| is non-null, set |request|'s mode to |mode|." | 51 // "7. If |mode| is non-null, set |request|'s mode to |mode|." |
52 if (init.mode == "same-origin") { | 52 if (init.mode == "same-origin") { |
53 request->setMode(FetchRequestData::SameOriginMode); | 53 request->setMode(FetchRequestData::SameOriginMode); |
54 } else if (init.mode == "no-cors") { | 54 } else if (init.mode == "no-cors") { |
55 request->setMode(mode = FetchRequestData::NoCORSMode); | 55 request->setMode(mode = FetchRequestData::NoCORSMode); |
56 } else if (init.mode == "cors") { | 56 } else if (init.mode == "cors") { |
57 request->setMode(FetchRequestData::CORSMode); | 57 request->setMode(FetchRequestData::CORSMode); |
(...skipping 18 matching lines...) Expand all Loading... |
76 // current credentials in Request::create(). So we just set here. | 76 // current credentials in Request::create(). So we just set here. |
77 request->setCredentials(credentials); | 77 request->setCredentials(credentials); |
78 } | 78 } |
79 | 79 |
80 // "10. If |init|'s method member is present, let |method| be it and run | 80 // "10. If |init|'s method member is present, let |method| be it and run |
81 // these substeps:" | 81 // these substeps:" |
82 if (!init.method.isEmpty()) { | 82 if (!init.method.isEmpty()) { |
83 // "1. If |method| is not a useful method, throw a TypeError." | 83 // "1. If |method| is not a useful method, throw a TypeError." |
84 if (!FetchUtils::isUsefulMethod(init.method)) { | 84 if (!FetchUtils::isUsefulMethod(init.method)) { |
85 exceptionState.throwTypeError("'" + init.method + "' HTTP method is
unsupported."); | 85 exceptionState.throwTypeError("'" + init.method + "' HTTP method is
unsupported."); |
86 return nullptr; | 86 return 0; |
87 } | 87 } |
88 if (!isValidHTTPToken(init.method)) { | 88 if (!isValidHTTPToken(init.method)) { |
89 exceptionState.throwTypeError("'" + init.method + "' is not a valid
HTTP method."); | 89 exceptionState.throwTypeError("'" + init.method + "' is not a valid
HTTP method."); |
90 return nullptr; | 90 return 0; |
91 } | 91 } |
92 // FIXME: "2. Add case correction as in XMLHttpRequest?" | 92 // FIXME: "2. Add case correction as in XMLHttpRequest?" |
93 // "3. Set |request|'s method to |method|." | 93 // "3. Set |request|'s method to |method|." |
94 request->setMethod(XMLHttpRequest::uppercaseKnownHTTPMethod(AtomicString
(init.method))); | 94 request->setMethod(XMLHttpRequest::uppercaseKnownHTTPMethod(AtomicString
(init.method))); |
95 } | 95 } |
96 // "11. Let |r| be a new Request object associated with |request|, Headers | 96 // "11. Let |r| be a new Request object associated with |request|, Headers |
97 // object, and FetchBodyStream object." | 97 // object, and FetchBodyStream object." |
98 RefPtrWillBeRawPtr<Request> r = Request::create(request); | 98 Request* r = Request::create(request); |
99 | 99 |
100 // "12. Let |headers| be a copy of |r|'s Headers object." | 100 // "12. Let |headers| be a copy of |r|'s Headers object." |
101 // "13. If |init|'s headers member is present, set |headers| to |init|'s | 101 // "13. If |init|'s headers member is present, set |headers| to |init|'s |
102 // headers member." | 102 // headers member." |
103 // We don't create a copy of r's Headers object when init's headers member | 103 // We don't create a copy of r's Headers object when init's headers member |
104 // is present. | 104 // is present. |
105 RefPtrWillBeRawPtr<Headers> headers = nullptr; | 105 Headers* headers = 0; |
106 if (!init.headers && init.headersDictionary.isUndefinedOrNull()) { | 106 if (!init.headers && init.headersDictionary.isUndefinedOrNull()) { |
107 headers = r->headers()->createCopy(); | 107 headers = r->headers()->createCopy(); |
108 } | 108 } |
109 // "14. Empty |r|'s request's header list." | 109 // "14. Empty |r|'s request's header list." |
110 r->request()->headerList()->clearList(); | 110 r->request()->headerList()->clearList(); |
111 | 111 |
112 // "15. If |r|'s request's mode is no CORS, run these substeps: | 112 // "15. If |r|'s request's mode is no CORS, run these substeps: |
113 if (r->request()->mode() == FetchRequestData::NoCORSMode) { | 113 if (r->request()->mode() == FetchRequestData::NoCORSMode) { |
114 // "1. If |r|'s request's method is not a simple method, throw a | 114 // "1. If |r|'s request's method is not a simple method, throw a |
115 // TypeError." | 115 // TypeError." |
116 if (!FetchUtils::isSimpleMethod(r->request()->method())) { | 116 if (!FetchUtils::isSimpleMethod(r->request()->method())) { |
117 exceptionState.throwTypeError("'" + r->request()->method() + "' is u
nsupported in no-cors mode."); | 117 exceptionState.throwTypeError("'" + r->request()->method() + "' is u
nsupported in no-cors mode."); |
118 return nullptr; | 118 return 0; |
119 } | 119 } |
120 // "Set |r|'s Headers object's guard to |request-no-CORS|. | 120 // "Set |r|'s Headers object's guard to |request-no-CORS|. |
121 r->headers()->setGuard(Headers::RequestNoCORSGuard); | 121 r->headers()->setGuard(Headers::RequestNoCORSGuard); |
122 } | 122 } |
123 | 123 |
124 // "16. Fill |r|'s Headers object with |headers|. Rethrow any exceptions." | 124 // "16. Fill |r|'s Headers object with |headers|. Rethrow any exceptions." |
125 if (init.headers) { | 125 if (init.headers) { |
126 ASSERT(init.headersDictionary.isUndefinedOrNull()); | 126 ASSERT(init.headersDictionary.isUndefinedOrNull()); |
127 r->headers()->fillWith(init.headers.get(), exceptionState); | 127 r->headers()->fillWith(init.headers.get(), exceptionState); |
128 } else if (!init.headersDictionary.isUndefinedOrNull()) { | 128 } else if (!init.headersDictionary.isUndefinedOrNull()) { |
129 r->headers()->fillWith(init.headersDictionary, exceptionState); | 129 r->headers()->fillWith(init.headersDictionary, exceptionState); |
130 } else { | 130 } else { |
131 ASSERT(headers); | 131 ASSERT(headers); |
132 r->headers()->fillWith(headers.get(), exceptionState); | 132 r->headers()->fillWith(headers, exceptionState); |
133 } | 133 } |
134 if (exceptionState.hadException()) | 134 if (exceptionState.hadException()) |
135 return nullptr; | 135 return 0; |
136 // "17. If |init|'s body member is present, run these substeps:" | 136 // "17. If |init|'s body member is present, run these substeps:" |
137 if (init.bodyBlobHandle) { | 137 if (init.bodyBlobHandle) { |
138 // "1. Let |stream| and |Content-Type| be the result of extracting | 138 // "1. Let |stream| and |Content-Type| be the result of extracting |
139 // |init|'s body member." | 139 // |init|'s body member." |
140 // "2. Set |r|'s request's body to |stream|." | 140 // "2. Set |r|'s request's body to |stream|." |
141 // "3.If |Content-Type| is non-null and |r|'s request's header list | 141 // "3.If |Content-Type| is non-null and |r|'s request's header list |
142 // contains no header named `Content-Type`, append | 142 // contains no header named `Content-Type`, append |
143 // `Content-Type`/|Content-Type| to |r|'s Headers object. Rethrow any | 143 // `Content-Type`/|Content-Type| to |r|'s Headers object. Rethrow any |
144 // exception." | 144 // exception." |
145 r->setBodyBlobHandle(init.bodyBlobHandle); | 145 r->setBodyBlobHandle(init.bodyBlobHandle); |
146 if (!init.bodyBlobHandle->type().isEmpty() && !r->headers()->has("Conten
t-Type", exceptionState)) { | 146 if (!init.bodyBlobHandle->type().isEmpty() && !r->headers()->has("Conten
t-Type", exceptionState)) { |
147 r->headers()->append("Content-Type", init.bodyBlobHandle->type(), ex
ceptionState); | 147 r->headers()->append("Content-Type", init.bodyBlobHandle->type(), ex
ceptionState); |
148 } | 148 } |
149 if (exceptionState.hadException()) | 149 if (exceptionState.hadException()) |
150 return nullptr; | 150 return 0; |
151 } | 151 } |
152 // "18. Set |r|'s FetchBodyStream object's MIME type to the result of | 152 // "18. Set |r|'s FetchBodyStream object's MIME type to the result of |
153 // extracting a MIME type from |r|'s request's header list." | 153 // extracting a MIME type from |r|'s request's header list." |
154 // FIXME: We don't have MIME type in FetchBodyStream object yet. | 154 // FIXME: We don't have MIME type in FetchBodyStream object yet. |
155 | 155 |
156 // "19. Return |r|." | 156 // "19. Return |r|." |
157 return r.release(); | 157 return r; |
158 } | 158 } |
159 | 159 |
160 | |
161 } // namespace | 160 } // namespace |
162 | 161 |
163 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(Request); | 162 Request* Request::create(ExecutionContext* context, const String& input, Excepti
onState& exceptionState) |
164 | |
165 PassRefPtrWillBeRawPtr<Request> Request::create(ExecutionContext* context, const
String& input, ExceptionState& exceptionState) | |
166 { | 163 { |
167 return create(context, input, Dictionary(), exceptionState); | 164 return create(context, input, Dictionary(), exceptionState); |
168 } | 165 } |
169 | 166 |
170 PassRefPtrWillBeRawPtr<Request> Request::create(ExecutionContext* context, const
String& input, const Dictionary& init, ExceptionState& exceptionState) | 167 Request* Request::create(ExecutionContext* context, const String& input, const D
ictionary& init, ExceptionState& exceptionState) |
171 { | 168 { |
172 // "1. Let |request| be |input|'s associated request, if |input| is a | 169 // "1. Let |request| be |input|'s associated request, if |input| is a |
173 // Request object, and a new request otherwise." | 170 // Request object, and a new request otherwise." |
174 RefPtrWillBeRawPtr<FetchRequestData> request(FetchRequestData::create(contex
t)); | 171 FetchRequestData* request(FetchRequestData::create(context)); |
175 // "2. Set |request| to a restricted copy of itself." | 172 // "2. Set |request| to a restricted copy of itself." |
176 request = request->createRestrictedCopy(context, SecurityOrigin::create(cont
ext->url())); | 173 request = request->createRestrictedCopy(context, SecurityOrigin::create(cont
ext->url())); |
177 // "5. If |input| is a string, run these substeps:" | 174 // "5. If |input| is a string, run these substeps:" |
178 // "1. Let |parsedURL| be the result of parsing |input| with entry settings | 175 // "1. Let |parsedURL| be the result of parsing |input| with entry settings |
179 // object's API base URL." | 176 // object's API base URL." |
180 KURL parsedURL = context->completeURL(input); | 177 KURL parsedURL = context->completeURL(input); |
181 // "2. If |parsedURL| is failure, throw a TypeError." | 178 // "2. If |parsedURL| is failure, throw a TypeError." |
182 if (!parsedURL.isValid()) { | 179 if (!parsedURL.isValid()) { |
183 exceptionState.throwTypeError("Invalid URL"); | 180 exceptionState.throwTypeError("Invalid URL"); |
184 return nullptr; | 181 return 0; |
185 } | 182 } |
186 // "3. Set |request|'s url to |parsedURL|." | 183 // "3. Set |request|'s url to |parsedURL|." |
187 request->setURL(parsedURL); | 184 request->setURL(parsedURL); |
188 // "4. Set |fallbackMode| to CORS." | 185 // "4. Set |fallbackMode| to CORS." |
189 // "5. Set |fallbackCredentials| to omit." | 186 // "5. Set |fallbackCredentials| to omit." |
190 return createRequestWithRequestData(request.release(), RequestInit(context,
init, exceptionState), FetchRequestData::CORSMode, FetchRequestData::OmitCredent
ials, exceptionState); | 187 return createRequestWithRequestData(request, RequestInit(context, init, exce
ptionState), FetchRequestData::CORSMode, FetchRequestData::OmitCredentials, exce
ptionState); |
191 } | 188 } |
192 | 189 |
193 PassRefPtrWillBeRawPtr<Request> Request::create(ExecutionContext* context, Reque
st* input, ExceptionState& exceptionState) | 190 Request* Request::create(ExecutionContext* context, Request* input, ExceptionSta
te& exceptionState) |
194 { | 191 { |
195 return create(context, input, Dictionary(), exceptionState); | 192 return create(context, input, Dictionary(), exceptionState); |
196 } | 193 } |
197 | 194 |
198 PassRefPtrWillBeRawPtr<Request> Request::create(ExecutionContext* context, Reque
st* input, const Dictionary& init, ExceptionState& exceptionState) | 195 Request* Request::create(ExecutionContext* context, Request* input, const Dictio
nary& init, ExceptionState& exceptionState) |
199 { | 196 { |
200 // "1. Let |request| be |input|'s associated request, if |input| is a | 197 // "1. Let |request| be |input|'s associated request, if |input| is a |
201 // Request object, and a new request otherwise." | 198 // Request object, and a new request otherwise." |
202 // "2. Set |request| to a restricted copy of itself." | 199 // "2. Set |request| to a restricted copy of itself." |
203 RefPtrWillBeRawPtr<FetchRequestData> request(input->request()->createRestric
tedCopy(context, SecurityOrigin::create(context->url()))); | 200 FetchRequestData* request(input->request()->createRestrictedCopy(context, Se
curityOrigin::create(context->url()))); |
204 // "3. Let |fallbackMode| be null." | 201 // "3. Let |fallbackMode| be null." |
205 // "4. Let |fallbackCredentials| be null." | 202 // "4. Let |fallbackCredentials| be null." |
206 // Instead of using null as a special fallback value, just pass the current | 203 // Instead of using null as a special fallback value, just pass the current |
207 // mode and credentials; it has the same effect. | 204 // mode and credentials; it has the same effect. |
208 const FetchRequestData::Mode currentMode = request->mode(); | 205 const FetchRequestData::Mode currentMode = request->mode(); |
209 const FetchRequestData::Credentials currentCredentials = request->credential
s(); | 206 const FetchRequestData::Credentials currentCredentials = request->credential
s(); |
210 return createRequestWithRequestData(request.release(), RequestInit(context,
init, exceptionState), currentMode, currentCredentials, exceptionState); | 207 return createRequestWithRequestData(request, RequestInit(context, init, exce
ptionState), currentMode, currentCredentials, exceptionState); |
211 } | 208 } |
212 | 209 |
213 PassRefPtrWillBeRawPtr<Request> Request::create(PassRefPtrWillBeRawPtr<FetchRequ
estData> request) | 210 Request* Request::create(FetchRequestData* request) |
214 { | 211 { |
215 return adoptRefWillBeNoop(new Request(request)); | 212 return new Request(request); |
216 } | 213 } |
217 | 214 |
218 Request::Request(PassRefPtrWillBeRawPtr<FetchRequestData> request) | 215 Request::Request(FetchRequestData* request) |
219 : m_request(request) | 216 : m_request(request) |
220 , m_headers(Headers::create(m_request->headerList())) | 217 , m_headers(Headers::create(m_request->headerList())) |
221 { | 218 { |
222 m_headers->setGuard(Headers::RequestGuard); | 219 m_headers->setGuard(Headers::RequestGuard); |
223 ScriptWrappable::init(this); | 220 ScriptWrappable::init(this); |
224 } | 221 } |
225 | 222 |
226 PassRefPtrWillBeRawPtr<Request> Request::create(const WebServiceWorkerRequest& w
ebRequest) | 223 Request* Request::create(const WebServiceWorkerRequest& webRequest) |
227 { | 224 { |
228 return adoptRefWillBeNoop(new Request(webRequest)); | 225 return new Request(webRequest); |
229 } | 226 } |
230 | 227 |
231 Request::Request(const WebServiceWorkerRequest& webRequest) | 228 Request::Request(const WebServiceWorkerRequest& webRequest) |
232 : m_request(FetchRequestData::create(webRequest)) | 229 : m_request(FetchRequestData::create(webRequest)) |
233 , m_headers(Headers::create(m_request->headerList())) | 230 , m_headers(Headers::create(m_request->headerList())) |
234 { | 231 { |
235 m_headers->setGuard(Headers::RequestGuard); | 232 m_headers->setGuard(Headers::RequestGuard); |
236 ScriptWrappable::init(this); | 233 ScriptWrappable::init(this); |
237 } | 234 } |
238 | 235 |
239 String Request::method() const | 236 String Request::method() const |
240 { | 237 { |
241 // "The method attribute's getter must return request's method." | 238 // "The method attribute's getter must return request's method." |
242 return m_request->method(); | 239 return m_request->method(); |
243 } | 240 } |
244 | 241 |
245 String Request::url() const | 242 String Request::url() const |
246 { | 243 { |
247 // The url attribute's getter must return request's url, serialized with the
exclude fragment flag set. | 244 // The url attribute's getter must return request's url, serialized with the
exclude fragment flag set. |
248 if (!m_request->url().hasFragmentIdentifier()) | 245 if (!m_request->url().hasFragmentIdentifier()) |
249 return m_request->url(); | 246 return m_request->url(); |
250 KURL url(m_request->url()); | 247 KURL url(m_request->url()); |
251 url.removeFragmentIdentifier(); | 248 url.removeFragmentIdentifier(); |
252 return url; | 249 return url; |
253 } | 250 } |
254 | 251 |
255 PassRefPtrWillBeRawPtr<FetchBodyStream> Request::body(ExecutionContext* context) | 252 FetchBodyStream* Request::body(ExecutionContext* context) |
256 { | 253 { |
257 if (!m_request->blobDataHandle()) | 254 if (!m_request->blobDataHandle()) |
258 return nullptr; | 255 return 0; |
259 if (!m_fetchBodyStream) | 256 if (!m_fetchBodyStream) |
260 m_fetchBodyStream = FetchBodyStream::create(context, m_request->blobData
Handle()); | 257 m_fetchBodyStream = FetchBodyStream::create(context, m_request->blobData
Handle()); |
261 return m_fetchBodyStream; | 258 return m_fetchBodyStream; |
262 } | 259 } |
263 | 260 |
264 | 261 |
265 String Request::referrer() const | 262 String Request::referrer() const |
266 { | 263 { |
267 // "The referrer attribute's getter must return the empty string if | 264 // "The referrer attribute's getter must return the empty string if |
268 // request's referrer is none, and request's referrer, serialized, | 265 // request's referrer is none, and request's referrer, serialized, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 } | 317 } |
321 | 318 |
322 void Request::trace(Visitor* visitor) | 319 void Request::trace(Visitor* visitor) |
323 { | 320 { |
324 visitor->trace(m_request); | 321 visitor->trace(m_request); |
325 visitor->trace(m_headers); | 322 visitor->trace(m_headers); |
326 visitor->trace(m_fetchBodyStream); | 323 visitor->trace(m_fetchBodyStream); |
327 } | 324 } |
328 | 325 |
329 } // namespace blink | 326 } // namespace blink |
OLD | NEW |