1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/*
* Copyright (C) 2003, 2006, 2008 Apple Inc. All rights reserved.
* Copyright (C) 2005, 2006 Alexey Proskuryakov <ap@nypop.com>
* Copyright (C) 2011 Google Inc. All rights reserved.
* Copyright (C) 2012 Intel Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef XMLHttpRequest_h
#define XMLHttpRequest_h
#include "ActiveDOMObject.h"
#include "EventListener.h"
#include "FormData.h"
#include "ResourceResponse.h"
#include "ScriptWrappable.h"
#include "ThreadableLoaderClient.h"
#include "XMLHttpRequestEventTarget.h"
#include "XMLHttpRequestProgressEventThrottle.h"
#include <wtf/text/AtomicStringHash.h>
#include <wtf/text/StringBuilder.h>
namespace JSC {
class ArrayBuffer;
class ArrayBufferView;
}
namespace WebCore {
class Blob;
class Document;
class DOMFormData;
class ResourceRequest;
class SecurityOrigin;
class SharedBuffer;
class TextResourceDecoder;
class ThreadableLoader;
class XMLHttpRequest final : public RefCounted<XMLHttpRequest>, public XMLHttpRequestEventTarget, private ThreadableLoaderClient, public ActiveDOMObject {
WTF_MAKE_FAST_ALLOCATED;
public:
static Ref<XMLHttpRequest> create(ScriptExecutionContext&);
WEBCORE_EXPORT ~XMLHttpRequest();
// These exact numeric values are important because JS expects them.
enum State {
UNSENT = 0,
OPENED = 1,
HEADERS_RECEIVED = 2,
LOADING = 3,
DONE = 4
};
enum ResponseTypeCode {
ResponseTypeDefault,
ResponseTypeText,
ResponseTypeJSON,
ResponseTypeDocument,
// Binary format
ResponseTypeBlob,
ResponseTypeArrayBuffer
};
static const ResponseTypeCode FirstBinaryResponseType = ResponseTypeBlob;
virtual void didReachTimeout();
virtual EventTargetInterface eventTargetInterface() const override { return XMLHttpRequestEventTargetInterfaceType; }
virtual ScriptExecutionContext* scriptExecutionContext() const override { return ActiveDOMObject::scriptExecutionContext(); }
const URL& url() const { return m_url; }
String statusText() const;
int status() const;
State readyState() const;
bool withCredentials() const { return m_includeCredentials; }
void setWithCredentials(bool, ExceptionCode&);
void open(const String& method, const URL&, ExceptionCode&);
void open(const String& method, const URL&, bool async, ExceptionCode&);
void open(const String& method, const URL&, bool async, const String& user, ExceptionCode&);
void open(const String& method, const URL&, bool async, const String& user, const String& password, ExceptionCode&);
void send(ExceptionCode&);
void send(Document*, ExceptionCode&);
void send(const String&, ExceptionCode&);
void send(Blob*, ExceptionCode&);
void send(DOMFormData*, ExceptionCode&);
void send(JSC::ArrayBuffer*, ExceptionCode&);
void send(JSC::ArrayBufferView*, ExceptionCode&);
void abort();
void setRequestHeader(const String& name, const String& value, ExceptionCode&);
void overrideMimeType(const String& override, ExceptionCode&);
bool doneWithoutErrors() const { return !m_error && m_state == DONE; }
String getAllResponseHeaders() const;
String getResponseHeader(const String& name) const;
String responseText(ExceptionCode&);
String responseTextIgnoringResponseType() const { return m_responseBuilder.toStringPreserveCapacity(); }
String responseMIMEType() const;
Document* responseXML(ExceptionCode&);
Document* optionalResponseXML() const { return m_responseDocument.get(); }
Blob* responseBlob();
Blob* optionalResponseBlob() const { return m_responseBlob.get(); }
unsigned timeout() const { return m_timeoutMilliseconds; }
void setTimeout(unsigned timeout, ExceptionCode&);
bool responseCacheIsValid() const { return m_responseCacheIsValid; }
void didCacheResponseJSON();
// Expose HTTP validation methods for other untrusted requests.
static bool isAllowedHTTPMethod(const String&);
static String uppercaseKnownHTTPMethod(const String&);
static bool isAllowedHTTPHeader(const String&);
void setResponseType(const String&, ExceptionCode&);
String responseType();
ResponseTypeCode responseTypeCode() const { return m_responseTypeCode; }
String responseURL() const;
// response attribute has custom getter.
JSC::ArrayBuffer* responseArrayBuffer();
JSC::ArrayBuffer* optionalResponseArrayBuffer() const { return m_responseArrayBuffer.get(); }
void setLastSendLineAndColumnNumber(unsigned lineNumber, unsigned columnNumber);
void setLastSendURL(const String& url) { m_lastSendURL = url; }
XMLHttpRequestUpload* upload();
XMLHttpRequestUpload* optionalUpload() const { return m_upload.get(); }
const ResourceResponse& resourceResponse() const { return m_response; }
using RefCounted<XMLHttpRequest>::ref;
using RefCounted<XMLHttpRequest>::deref;
private:
explicit XMLHttpRequest(ScriptExecutionContext&);
// ActiveDOMObject
void contextDestroyed() override;
bool canSuspendForDocumentSuspension() const override;
void suspend(ReasonForSuspension) override;
void resume() override;
void stop() override;
const char* activeDOMObjectName() const override;
virtual void refEventTarget() override { ref(); }
virtual void derefEventTarget() override { deref(); }
Document* document() const;
SecurityOrigin* securityOrigin() const;
#if ENABLE(DASHBOARD_SUPPORT)
bool usesDashboardBackwardCompatibilityMode() const;
#endif
virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) override;
virtual void didReceiveResponse(unsigned long identifier, const ResourceResponse&) override;
virtual void didReceiveData(const char* data, int dataLength) override;
virtual void didFinishLoading(unsigned long identifier, double finishTime) override;
virtual void didFail(const ResourceError&) override;
virtual void didFailRedirectCheck() override;
bool responseIsXML() const;
bool initSend(ExceptionCode&);
void sendBytesData(const void*, size_t, ExceptionCode&);
void changeState(State newState);
void callReadyStateChangeListener();
void dropProtection();
// Returns false when cancelling the loader within internalAbort() triggers an event whose callback creates a new loader.
// In that case, the function calling internalAbort should exit.
bool internalAbort();
void clearResponse();
void clearResponseBuffers();
void clearRequest();
void createRequest(ExceptionCode&);
void genericError();
void networkError();
void abortError();
bool shouldDecodeResponse() const { return m_responseTypeCode < FirstBinaryResponseType; }
void dispatchErrorEvents(const AtomicString&);
void resumeTimerFired();
std::unique_ptr<XMLHttpRequestUpload> m_upload;
URL m_url;
String m_method;
HTTPHeaderMap m_requestHeaders;
RefPtr<FormData> m_requestEntityBody;
String m_mimeTypeOverride;
bool m_async;
bool m_includeCredentials;
RefPtr<Blob> m_responseBlob;
RefPtr<ThreadableLoader> m_loader;
State m_state;
bool m_sendFlag { false };
ResourceResponse m_response;
String m_responseEncoding;
RefPtr<TextResourceDecoder> m_decoder;
StringBuilder m_responseBuilder;
bool m_createdDocument;
RefPtr<Document> m_responseDocument;
RefPtr<SharedBuffer> m_binaryResponseBuilder;
RefPtr<JSC::ArrayBuffer> m_responseArrayBuffer;
bool m_error;
bool m_uploadEventsAllowed;
bool m_uploadComplete;
bool m_sameOriginRequest;
// Used for onprogress tracking
long long m_receivedLength;
unsigned m_lastSendLineNumber;
unsigned m_lastSendColumnNumber;
String m_lastSendURL;
ExceptionCode m_exceptionCode;
XMLHttpRequestProgressEventThrottle m_progressEventThrottle;
// An enum corresponding to the allowed string values for the responseType attribute.
ResponseTypeCode m_responseTypeCode;
bool m_responseCacheIsValid;
Timer m_resumeTimer;
bool m_dispatchErrorOnResuming;
Timer m_networkErrorTimer;
void networkErrorTimerFired();
unsigned m_timeoutMilliseconds { 0 };
std::chrono::steady_clock::time_point m_sendingTime;
Timer m_timeoutTimer;
};
} // namespace WebCore
#endif // XMLHttpRequest_h
|