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