• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (C) 2003, 2006, 2008 Apple Inc. All rights reserved.
3  *  Copyright (C) 2005, 2006 Alexey Proskuryakov <ap@nypop.com>
4  *  Copyright (C) 2011 Google Inc. All rights reserved.
5  *  Copyright (C) 2012 Intel Corporation
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifndef XMLHttpRequest_h
23 #define XMLHttpRequest_h
24 
25 #include "bindings/v8/ScriptString.h"
26 #include "bindings/v8/ScriptWrappable.h"
27 #include "core/dom/ActiveDOMObject.h"
28 #include "core/events/EventListener.h"
29 #include "core/loader/ThreadableLoaderClient.h"
30 #include "core/xml/XMLHttpRequestEventTarget.h"
31 #include "core/xml/XMLHttpRequestProgressEventThrottle.h"
32 #include "platform/AsyncMethodRunner.h"
33 #include "platform/heap/Handle.h"
34 #include "platform/network/FormData.h"
35 #include "platform/network/ResourceResponse.h"
36 #include "platform/weborigin/SecurityOrigin.h"
37 #include "wtf/OwnPtr.h"
38 #include "wtf/text/AtomicStringHash.h"
39 #include "wtf/text/StringBuilder.h"
40 
41 namespace WebCore {
42 
43 class Blob;
44 class DOMFormData;
45 class Document;
46 class ExceptionState;
47 class ResourceRequest;
48 class SecurityOrigin;
49 class SharedBuffer;
50 class Stream;
51 class TextResourceDecoder;
52 class ThreadableLoader;
53 
54 typedef int ExceptionCode;
55 
56 class XMLHttpRequest FINAL
57     : public RefCountedWillBeRefCountedGarbageCollected<XMLHttpRequest>
58     , public ScriptWrappable
59     , public XMLHttpRequestEventTarget
60     , private ThreadableLoaderClient
61     , public ActiveDOMObject {
62     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
63     REFCOUNTED_EVENT_TARGET(XMLHttpRequest);
64     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(XMLHttpRequest);
65 public:
66     static PassRefPtrWillBeRawPtr<XMLHttpRequest> create(ExecutionContext*, PassRefPtr<SecurityOrigin> = nullptr);
67     virtual ~XMLHttpRequest();
68 
69     // These exact numeric values are important because JS expects them.
70     enum State {
71         UNSENT = 0,
72         OPENED = 1,
73         HEADERS_RECEIVED = 2,
74         LOADING = 3,
75         DONE = 4
76     };
77 
78     enum ResponseTypeCode {
79         ResponseTypeDefault,
80         ResponseTypeText,
81         ResponseTypeJSON,
82         ResponseTypeDocument,
83         ResponseTypeBlob,
84         ResponseTypeArrayBuffer,
85         ResponseTypeStream
86     };
87 
88     enum DropProtection {
89         DropProtectionSync,
90         DropProtectionAsync,
91     };
92 
93     virtual void contextDestroyed() OVERRIDE;
94     virtual void suspend() OVERRIDE;
95     virtual void resume() OVERRIDE;
96     virtual void stop() OVERRIDE;
97 
98     virtual const AtomicString& interfaceName() const OVERRIDE;
99     virtual ExecutionContext* executionContext() const OVERRIDE;
100 
url()101     const KURL& url() const { return m_url; }
102     String statusText() const;
103     int status() const;
104     State readyState() const;
withCredentials()105     bool withCredentials() const { return m_includeCredentials; }
106     void setWithCredentials(bool, ExceptionState&);
107     void open(const AtomicString& method, const KURL&, ExceptionState&);
108     void open(const AtomicString& method, const KURL&, bool async, ExceptionState&);
109     void open(const AtomicString& method, const KURL&, bool async, const String& user, ExceptionState&);
110     void open(const AtomicString& method, const KURL&, bool async, const String& user, const String& password, ExceptionState&);
111     void send(ExceptionState&);
112     void send(Document*, ExceptionState&);
113     void send(const String&, ExceptionState&);
114     void send(Blob*, ExceptionState&);
115     void send(DOMFormData*, ExceptionState&);
116     void send(ArrayBuffer*, ExceptionState&);
117     void send(ArrayBufferView*, ExceptionState&);
118     void abort();
119     void setRequestHeader(const AtomicString& name, const AtomicString& value, ExceptionState&);
120     void overrideMimeType(const AtomicString& override);
121     String getAllResponseHeaders() const;
122     const AtomicString& getResponseHeader(const AtomicString&) const;
123     ScriptString responseText(ExceptionState&);
124     ScriptString responseJSONSource();
125     Document* responseXML(ExceptionState&);
126     Blob* responseBlob();
127     Stream* responseStream();
timeout()128     unsigned long timeout() const { return m_timeoutMilliseconds; }
129     void setTimeout(unsigned long timeout, ExceptionState&);
130 
131     void sendForInspectorXHRReplay(PassRefPtr<FormData>, ExceptionState&);
132 
133     // Expose HTTP validation methods for other untrusted requests.
134     static bool isAllowedHTTPMethod(const String&);
135     static AtomicString uppercaseKnownHTTPMethod(const AtomicString&);
136     static bool isAllowedHTTPHeader(const String&);
137 
138     void setResponseType(const String&, ExceptionState&);
139     String responseType();
responseTypeCode()140     ResponseTypeCode responseTypeCode() const { return m_responseTypeCode; }
141 
142     String responseURL();
143 
144     // response attribute has custom getter.
145     ArrayBuffer* responseArrayBuffer();
146 
setLastSendLineNumber(unsigned lineNumber)147     void setLastSendLineNumber(unsigned lineNumber) { m_lastSendLineNumber = lineNumber; }
setLastSendURL(const String & url)148     void setLastSendURL(const String& url) { m_lastSendURL = url; }
149 
150     XMLHttpRequestUpload* upload();
151 
152     DEFINE_ATTRIBUTE_EVENT_LISTENER(readystatechange);
153 
154     virtual void trace(Visitor*) OVERRIDE;
155 
156 private:
157     XMLHttpRequest(ExecutionContext*, PassRefPtr<SecurityOrigin>);
158 
159     Document* document() const;
160     SecurityOrigin* securityOrigin() const;
161 
162     virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) OVERRIDE;
163     virtual void didReceiveResponse(unsigned long identifier, const ResourceResponse&) OVERRIDE;
164     virtual void didReceiveData(const char* data, int dataLength) OVERRIDE;
165     // When responseType is set to "blob", didDownloadData() is called instead
166     // of didReceiveData().
167     virtual void didDownloadData(int dataLength) OVERRIDE;
168     virtual void didFinishLoading(unsigned long identifier, double finishTime) OVERRIDE;
169     virtual void didFail(const ResourceError&) OVERRIDE;
170     virtual void didFailRedirectCheck() OVERRIDE;
171 
172     AtomicString responseMIMEType() const;
173     bool responseIsXML() const;
174 
175     bool areMethodAndURLValidForSend();
176 
177     bool initSend(ExceptionState&);
178     void sendBytesData(const void*, size_t, ExceptionState&);
179 
180     const AtomicString& getRequestHeader(const AtomicString& name) const;
181     void setRequestHeaderInternal(const AtomicString& name, const AtomicString& value);
182 
183     void trackProgress(int dataLength);
184     // Changes m_state and dispatches a readyStateChange event if new m_state
185     // value is different from last one.
186     void changeState(State newState);
187     void dispatchReadyStateChangeEvent();
188 
189     void dropProtectionSoon();
190     void dropProtection();
191     // Clears variables used only while the resource is being loaded.
192     void clearVariablesForLoading();
193     // Returns false iff reentry happened and a new load is started.
194     bool internalAbort(DropProtection = DropProtectionSync);
195     // Clears variables holding response header and body data.
196     void clearResponse();
197     void clearRequest();
198 
199     void createRequest(PassRefPtr<FormData>, ExceptionState&);
200 
201     // Dispatches a response ProgressEvent.
202     void dispatchProgressEvent(const AtomicString&, long long, long long);
203     // Dispatches a response ProgressEvent using values sampled from
204     // m_receivedLength and m_response.
205     void dispatchProgressEventFromSnapshot(const AtomicString&);
206 
207     // Does clean up common for all kind of didFail() call.
208     void handleDidFailGeneric();
209     // Handles didFail() call not caused by cancellation or timeout.
210     void handleNetworkError();
211     // Handles didFail() call triggered by m_loader->cancel().
212     void handleDidCancel();
213     // Handles didFail() call for timeout.
214     void handleDidTimeout();
215 
216     void handleRequestError(ExceptionCode, const AtomicString&, long long, long long);
217 
218     OwnPtrWillBeMember<XMLHttpRequestUpload> m_upload;
219 
220     KURL m_url;
221     AtomicString m_method;
222     HTTPHeaderMap m_requestHeaders;
223     AtomicString m_mimeTypeOverride;
224     bool m_async;
225     bool m_includeCredentials;
226     unsigned long m_timeoutMilliseconds;
227     RefPtrWillBeMember<Blob> m_responseBlob;
228     RefPtrWillBeMember<Stream> m_responseStream;
229 
230     RefPtr<ThreadableLoader> m_loader;
231     State m_state;
232 
233     ResourceResponse m_response;
234     String m_responseEncoding;
235 
236     OwnPtr<TextResourceDecoder> m_decoder;
237 
238     ScriptString m_responseText;
239     // Used to skip m_responseDocument creation if it's done previously. We need
240     // this separate flag since m_responseDocument can be 0 for some cases.
241     bool m_createdDocument;
242     RefPtrWillBeMember<Document> m_responseDocument;
243 
244     RefPtr<SharedBuffer> m_binaryResponseBuilder;
245     long long m_downloadedBlobLength;
246 
247     RefPtr<ArrayBuffer> m_responseArrayBuffer;
248 
249     bool m_error;
250 
251     bool m_uploadEventsAllowed;
252     bool m_uploadComplete;
253 
254     bool m_sameOriginRequest;
255 
256     // Used for onprogress tracking
257     long long m_receivedLength;
258 
259     unsigned m_lastSendLineNumber;
260     String m_lastSendURL;
261     // An exception to throw in synchronous mode. It's set when failure
262     // notification is received from m_loader and thrown at the end of send() if
263     // any.
264     ExceptionCode m_exceptionCode;
265 
266     XMLHttpRequestProgressEventThrottle m_progressEventThrottle;
267 
268     // An enum corresponding to the allowed string values for the responseType attribute.
269     ResponseTypeCode m_responseTypeCode;
270     AsyncMethodRunner<XMLHttpRequest> m_dropProtectionRunner;
271     RefPtr<SecurityOrigin> m_securityOrigin;
272 };
273 
274 } // namespace WebCore
275 
276 #endif // XMLHttpRequest_h
277