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 "ActiveDOMObject.h"
26#include "EventListener.h"
27#include "EventNames.h"
28#include "EventTarget.h"
29#include "FormData.h"
30#include "ResourceResponse.h"
31#include "ScriptWrappable.h"
32#include "ThreadableLoaderClient.h"
33#include "XMLHttpRequestProgressEventThrottle.h"
34#include <wtf/OwnPtr.h>
35#include <wtf/text/AtomicStringHash.h>
36#include <wtf/text/StringBuilder.h>
37
38namespace WebCore {
39
40class Blob;
41class Document;
42class DOMFormData;
43class ResourceRequest;
44class SecurityOrigin;
45class SharedBuffer;
46class TextResourceDecoder;
47class ThreadableLoader;
48
49class XMLHttpRequest : public ScriptWrappable, public RefCounted<XMLHttpRequest>, public EventTarget, private ThreadableLoaderClient, public ActiveDOMObject {
50    WTF_MAKE_FAST_ALLOCATED;
51public:
52    static PassRefPtr<XMLHttpRequest> create(ScriptExecutionContext*);
53    ~XMLHttpRequest();
54
55    // These exact numeric values are important because JS expects them.
56    enum State {
57        UNSENT = 0,
58        OPENED = 1,
59        HEADERS_RECEIVED = 2,
60        LOADING = 3,
61        DONE = 4
62    };
63
64    enum ResponseTypeCode {
65        ResponseTypeDefault,
66        ResponseTypeText,
67        ResponseTypeDocument,
68        ResponseTypeBlob,
69        ResponseTypeArrayBuffer
70    };
71
72    virtual void contextDestroyed();
73#if ENABLE(XHR_TIMEOUT)
74    virtual void didTimeout();
75#endif
76    virtual bool canSuspend() const;
77    virtual void suspend(ReasonForSuspension);
78    virtual void resume();
79    virtual void stop();
80
81    virtual const AtomicString& interfaceName() const;
82    virtual ScriptExecutionContext* scriptExecutionContext() const;
83
84    const KURL& url() const { return m_url; }
85    String statusText(ExceptionCode&) const;
86    int status(ExceptionCode&) const;
87    State readyState() const;
88    bool withCredentials() const { return m_includeCredentials; }
89    void setWithCredentials(bool, ExceptionCode&);
90    void open(const String& method, const KURL&, ExceptionCode&);
91    void open(const String& method, const KURL&, bool async, ExceptionCode&);
92    void open(const String& method, const KURL&, bool async, const String& user, ExceptionCode&);
93    void open(const String& method, const KURL&, bool async, const String& user, const String& password, ExceptionCode&);
94    void send(ExceptionCode&);
95    void send(Document*, ExceptionCode&);
96    void send(const String&, ExceptionCode&);
97    void send(Blob*, ExceptionCode&);
98    void send(DOMFormData*, ExceptionCode&);
99    void send(ArrayBuffer*, ExceptionCode&);
100    void send(ArrayBufferView*, ExceptionCode&);
101    void abort();
102    void setRequestHeader(const AtomicString& name, const String& value, ExceptionCode&);
103    void overrideMimeType(const String& override);
104    String getAllResponseHeaders(ExceptionCode&) const;
105    String getResponseHeader(const AtomicString& name, ExceptionCode&) const;
106    String responseText(ExceptionCode&);
107    String responseMIMEType() const;
108    Document* responseXML(ExceptionCode&);
109    Document* optionalResponseXML() const { return m_responseDocument.get(); }
110    Blob* responseBlob(ExceptionCode&);
111    Blob* optionalResponseBlob() const { return m_responseBlob.get(); }
112#if ENABLE(XHR_TIMEOUT)
113    unsigned long timeout() const { return m_timeoutMilliseconds; }
114    void setTimeout(unsigned long timeout, ExceptionCode&);
115#endif
116
117    void sendForInspectorXHRReplay(PassRefPtr<FormData>, ExceptionCode&);
118
119    // Expose HTTP validation methods for other untrusted requests.
120    static bool isAllowedHTTPMethod(const String&);
121    static String uppercaseKnownHTTPMethod(const String&);
122    static bool isAllowedHTTPHeader(const String&);
123
124    void setResponseType(const String&, ExceptionCode&);
125    String responseType();
126    ResponseTypeCode responseTypeCode() const { return m_responseTypeCode; }
127
128    // response attribute has custom getter.
129    ArrayBuffer* responseArrayBuffer(ExceptionCode&);
130    ArrayBuffer* optionalResponseArrayBuffer() const { return m_responseArrayBuffer.get(); }
131
132    void setLastSendLineNumber(unsigned lineNumber) { m_lastSendLineNumber = lineNumber; }
133    void setLastSendURL(const String& url) { m_lastSendURL = url; }
134
135    XMLHttpRequestUpload* upload();
136    XMLHttpRequestUpload* optionalUpload() const { return m_upload.get(); }
137
138    DEFINE_ATTRIBUTE_EVENT_LISTENER(readystatechange);
139    DEFINE_ATTRIBUTE_EVENT_LISTENER(abort);
140    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
141    DEFINE_ATTRIBUTE_EVENT_LISTENER(load);
142    DEFINE_ATTRIBUTE_EVENT_LISTENER(loadend);
143    DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart);
144    DEFINE_ATTRIBUTE_EVENT_LISTENER(progress);
145#if ENABLE(XHR_TIMEOUT)
146    DEFINE_ATTRIBUTE_EVENT_LISTENER(timeout);
147#endif
148
149    using RefCounted<XMLHttpRequest>::ref;
150    using RefCounted<XMLHttpRequest>::deref;
151
152private:
153    XMLHttpRequest(ScriptExecutionContext*);
154
155    virtual void refEventTarget() { ref(); }
156    virtual void derefEventTarget() { deref(); }
157    virtual EventTargetData* eventTargetData();
158    virtual EventTargetData* ensureEventTargetData();
159
160    Document* document() const;
161    SecurityOrigin* securityOrigin() const;
162
163#if ENABLE(DASHBOARD_SUPPORT)
164    bool usesDashboardBackwardCompatibilityMode() const;
165#endif
166
167    virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
168    virtual void didReceiveResponse(unsigned long identifier, const ResourceResponse&);
169    virtual void didReceiveData(const char* data, int dataLength);
170    virtual void didFinishLoading(unsigned long identifier, double finishTime);
171    virtual void didFail(const ResourceError&);
172    virtual void didFailRedirectCheck();
173
174    bool responseIsXML() const;
175
176    bool initSend(ExceptionCode&);
177    void sendBytesData(const void*, size_t, ExceptionCode&);
178
179    String getRequestHeader(const AtomicString& name) const;
180    void setRequestHeaderInternal(const AtomicString& name, const String& value);
181
182    void changeState(State newState);
183    void callReadyStateChangeListener();
184    void dropProtection();
185    void internalAbort();
186    void clearResponse();
187    void clearResponseBuffers();
188    void clearRequest();
189
190    void createRequest(ExceptionCode&);
191
192    void genericError();
193    void networkError();
194    void abortError();
195
196    OwnPtr<XMLHttpRequestUpload> m_upload;
197
198    KURL m_url;
199    String m_method;
200    HTTPHeaderMap m_requestHeaders;
201    RefPtr<FormData> m_requestEntityBody;
202    String m_mimeTypeOverride;
203    bool m_async;
204    bool m_includeCredentials;
205#if ENABLE(XHR_TIMEOUT)
206    unsigned long m_timeoutMilliseconds;
207#endif
208    RefPtr<Blob> m_responseBlob;
209
210    RefPtr<ThreadableLoader> m_loader;
211    State m_state;
212
213    ResourceResponse m_response;
214    String m_responseEncoding;
215
216    RefPtr<TextResourceDecoder> m_decoder;
217
218    StringBuilder m_responseBuilder;
219    mutable bool m_createdDocument;
220    mutable RefPtr<Document> m_responseDocument;
221
222    RefPtr<SharedBuffer> m_binaryResponseBuilder;
223    mutable RefPtr<ArrayBuffer> m_responseArrayBuffer;
224
225    bool m_error;
226
227    bool m_uploadEventsAllowed;
228    bool m_uploadComplete;
229
230    bool m_sameOriginRequest;
231
232    // Used for onprogress tracking
233    long long m_receivedLength;
234
235    unsigned m_lastSendLineNumber;
236    String m_lastSendURL;
237    ExceptionCode m_exceptionCode;
238
239    EventTargetData m_eventTargetData;
240
241    XMLHttpRequestProgressEventThrottle m_progressEventThrottle;
242
243    // An enum corresponding to the allowed string values for the responseType attribute.
244    ResponseTypeCode m_responseTypeCode;
245};
246
247} // namespace WebCore
248
249#endif // XMLHttpRequest_h
250