Commit 93b29209 authored by weinig@apple.com's avatar weinig@apple.com

2008-06-23 Sam Weinig <sam@webkit.org>

        Reviewed by Alexey Proskuryakov.

        Some XMLHttpRequest re-organization to aid further enhancements coming soon.

        * bindings/js/JSXMLHttpRequestCustom.cpp:
        (WebCore::JSXMLHttpRequest::send): Explicitly call overloaded versions of send,
        instead of always calling though send(DOMString).
        * dom/Document.idl: Adds native converter.

        * xml/XMLHttpRequest.cpp:
        (WebCore::XMLHttpRequest::open):
        (WebCore::XMLHttpRequest::initSend):
        (WebCore::XMLHttpRequest::send):
        (WebCore::XMLHttpRequest::createRequest):
        (WebCore::XMLHttpRequest::sameOriginRequest):
        (WebCore::XMLHttpRequest::crossSiteAccessRequest):
        (WebCore::XMLHttpRequest::abort):
        (WebCore::XMLHttpRequest::clearResponse):
        (WebCore::XMLHttpRequest::clearRequest):
        (WebCore::XMLHttpRequest::genericError):
        (WebCore::XMLHttpRequest::dispatchProgressEvent):
        * xml/XMLHttpRequest.h:
        (WebCore::XMLHttpRequest::setOnReadyStateChangeListener): Inline.
        (WebCore::XMLHttpRequest::onReadyStateChangeListener): Ditto.
        (WebCore::XMLHttpRequest::setOnLoadListener): Ditto.
        (WebCore::XMLHttpRequest::onLoadListener): Ditto.
        (WebCore::XMLHttpRequest::setOnProgressListener): Ditto.
        (WebCore::XMLHttpRequest::onProgressListener): Ditto.
        Makes the request entity body a member variable so that the send method
        can be more easily broken up.



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@34741 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent ef76a280
2008-06-23 Sam Weinig <sam@webkit.org>
Reviewed by Alexey Proskuryakov.
Some XMLHttpRequest re-organization to aid further enhancements coming soon.
* bindings/js/JSXMLHttpRequestCustom.cpp:
(WebCore::JSXMLHttpRequest::send): Explicitly call overloaded versions of send,
instead of always calling though send(DOMString).
* dom/Document.idl: Adds native converter.
* xml/XMLHttpRequest.cpp:
(WebCore::XMLHttpRequest::open):
(WebCore::XMLHttpRequest::initSend):
(WebCore::XMLHttpRequest::send):
(WebCore::XMLHttpRequest::createRequest):
(WebCore::XMLHttpRequest::sameOriginRequest):
(WebCore::XMLHttpRequest::crossSiteAccessRequest):
(WebCore::XMLHttpRequest::abort):
(WebCore::XMLHttpRequest::clearResponse):
(WebCore::XMLHttpRequest::clearRequest):
(WebCore::XMLHttpRequest::genericError):
(WebCore::XMLHttpRequest::dispatchProgressEvent):
* xml/XMLHttpRequest.h:
(WebCore::XMLHttpRequest::setOnReadyStateChangeListener): Inline.
(WebCore::XMLHttpRequest::onReadyStateChangeListener): Ditto.
(WebCore::XMLHttpRequest::setOnLoadListener): Ditto.
(WebCore::XMLHttpRequest::onLoadListener): Ditto.
(WebCore::XMLHttpRequest::setOnProgressListener): Ditto.
(WebCore::XMLHttpRequest::onProgressListener): Ditto.
Makes the request entity body a member variable so that the send method
can be more easily broken up.
2008-06-23 Timothy Hatcher <timothy@apple.com>
Make profiles of the same name in the Inspector group in the
......@@ -29,9 +29,8 @@
#include "config.h"
#include "JSXMLHttpRequest.h"
#include "XMLHttpRequest.h"
#include "DOMWindow.h"
#include "Document.h"
#include "Event.h"
#include "Frame.h"
#include "FrameLoader.h"
......@@ -40,7 +39,7 @@
#include "JSDocument.h"
#include "JSEvent.h"
#include "JSEventListener.h"
#include "markup.h"
#include "XMLHttpRequest.h"
using namespace KJS;
......@@ -164,21 +163,19 @@ JSValue* JSXMLHttpRequest::setRequestHeader(ExecState* exec, const ArgList& args
JSValue* JSXMLHttpRequest::send(ExecState* exec, const ArgList& args)
{
String body;
if (args.size() >= 1) {
if (args[0]->toObject(exec)->inherits(&JSDocument::s_info))
body = createMarkup(static_cast<Document*>(static_cast<JSDocument*>(args[0]->toObject(exec))->impl()));
else {
// converting certain values (like null) to object can set an exception
if (exec->hadException())
exec->clearException();
else
body = args[0]->toString(exec);
}
ExceptionCode ec = 0;
if (args.isEmpty())
impl()->send(ec);
else {
JSValue* val = args[0];
if (val->isUndefinedOrNull())
impl()->send(ec);
else if (val->isObject(&JSDocument::s_info))
impl()->send(toDocument(val), ec);
else
impl()->send(val->toString(exec), ec);
}
ExceptionCode ec = 0;
impl()->send(body, ec);
setDOMException(exec, ec);
return jsUndefined();
}
......
......@@ -23,6 +23,7 @@ module core {
interface [
GenerateConstructor,
GenerateToJS,
GenerateNativeConverter,
CustomMarkFunction,
InlineGetOwnPropertySlot,
InterfaceUUID=48BB95FC-2D08-4c54-BE65-7558736A4CAE,
......
......@@ -33,13 +33,14 @@
#include "FrameLoader.h"
#include "HTTPParsers.h"
#include "InspectorController.h"
#include "JSDOMBinding.h"
#include "Page.h"
#include "Settings.h"
#include "SubresourceLoader.h"
#include "TextResourceDecoder.h"
#include "XMLHttpRequestException.h"
#include "XMLHttpRequestProgressEvent.h"
#include "JSDOMBinding.h"
#include "markup.h"
namespace WebCore {
......@@ -200,36 +201,6 @@ Document* XMLHttpRequest::responseXML() const
return m_responseXML.get();
}
EventListener* XMLHttpRequest::onReadyStateChangeListener() const
{
return m_onReadyStateChangeListener.get();
}
void XMLHttpRequest::setOnReadyStateChangeListener(PassRefPtr<EventListener> eventListener)
{
m_onReadyStateChangeListener = eventListener;
}
EventListener* XMLHttpRequest::onLoadListener() const
{
return m_onLoadListener.get();
}
EventListener* XMLHttpRequest::onProgressListener() const
{
return m_onProgressListener.get();
}
void XMLHttpRequest::setOnLoadListener(PassRefPtr<EventListener> eventListener)
{
m_onLoadListener = eventListener;
}
void XMLHttpRequest::setOnProgressListener(PassRefPtr<EventListener> eventListener)
{
m_onProgressListener = eventListener;
}
void XMLHttpRequest::addEventListener(const AtomicString& eventType, PassRefPtr<EventListener> eventListener, bool)
{
EventListenersMap::iterator iter = m_eventListeners.find(eventType.impl());
......@@ -325,8 +296,8 @@ void XMLHttpRequest::open(const String& method, const KURL& url, bool async, Exc
m_error = false;
// clear stuff from possible previous load
m_requestHeaders.clear();
clearResponseEntityBody();
clearResponse();
clearRequest();
ASSERT(m_state == UNSENT);
......@@ -382,43 +353,61 @@ void XMLHttpRequest::open(const String& method, const KURL& url, bool async, con
open(method, urlWithCredentials, async, ec);
}
void XMLHttpRequest::send(const String& body, ExceptionCode& ec)
bool XMLHttpRequest::initSend(ExceptionCode& ec)
{
if (!m_doc)
return;
return false;
if (m_state != OPENED || m_loader) {
ec = INVALID_STATE_ERR;
return;
return false;
}
m_error = false;
return true;
}
m_sameOriginRequest = m_doc->securityOrigin()->canRequest(m_url);
void XMLHttpRequest::send(ExceptionCode& ec)
{
send(String(), ec);
}
ResourceRequest request;
if (m_sameOriginRequest)
sameOriginRequest(body, request);
else {
crossSiteAccessRequest(body, request, ec);
if (ec)
return;
if (m_inMethodCheck)
return;
}
void XMLHttpRequest::send(Document* document, ExceptionCode& ec)
{
ASSERT(document);
if (m_async) {
loadRequestAsynchronously(request);
if (!initSend(ec))
return;
if (m_method != "GET" && m_method != "HEAD" && (m_url.protocolIs("http") || m_url.protocolIs("https"))) {
String contentType = getRequestHeader("Content-Type");
if (contentType.isEmpty()) {
#if ENABLE(DASHBOARD_SUPPORT)
Settings* settings = m_doc->settings();
if (settings && settings->usesDashboardBackwardCompatibilityMode())
setRequestHeaderInternal("Content-Type", "application/x-www-form-urlencoded");
else
#endif
// FIXME: this should include the charset used for encoding.
setRequestHeaderInternal("Content-Type", "application/xml");
}
// FIXME: According to XMLHttpRequest Level 2, this should use the Document.innerHTML algorithm
// from the HTML5 specification to serialize the document.
String body = createMarkup(document);
// FIXME: this should use value of document.inputEncoding to determine the encoding to use.
TextEncoding encoding = UTF8Encoding();
m_requestEntityBody = FormData::create(encoding.encode(body.characters(), body.length(), EntitiesForUnencodables));
}
loadRequestSynchronously(request, ec);
createRequest(ec);
}
void XMLHttpRequest::sameOriginRequest(const String& body, ResourceRequest& request)
void XMLHttpRequest::send(const String& body, ExceptionCode& ec)
{
request.setURL(m_url);
request.setHTTPMethod(m_method);
if (!initSend(ec))
return;
if (!body.isNull() && m_method != "GET" && m_method != "HEAD" && (m_url.protocolIs("http") || m_url.protocolIs("https"))) {
String contentType = getRequestHeader("Content-Type");
......@@ -432,15 +421,43 @@ void XMLHttpRequest::sameOriginRequest(const String& body, ResourceRequest& requ
setRequestHeaderInternal("Content-Type", "application/xml");
}
// FIXME: must use xmlEncoding for documents.
String charset = "UTF-8";
m_requestEntityBody = FormData::create(UTF8Encoding().encode(body.characters(), body.length(), EntitiesForUnencodables));
}
createRequest(ec);
}
void XMLHttpRequest::createRequest(ExceptionCode& ec)
{
m_sameOriginRequest = m_doc->securityOrigin()->canRequest(m_url);
ResourceRequest request;
if (m_sameOriginRequest)
sameOriginRequest(request);
else {
crossSiteAccessRequest(request, ec);
if (ec)
return;
if (m_inMethodCheck)
return;
}
TextEncoding encoding(charset);
if (!encoding.isValid()) // FIXME: report an error?
encoding = UTF8Encoding();
request.setHTTPBody(FormData::create(encoding.encode(body.characters(), body.length(), EntitiesForUnencodables)));
if (m_async) {
loadRequestAsynchronously(request);
return;
}
loadRequestSynchronously(request, ec);
}
void XMLHttpRequest::sameOriginRequest(ResourceRequest& request)
{
request.setURL(m_url);
request.setHTTPMethod(m_method);
if (m_requestEntityBody)
request.setHTTPBody(m_requestEntityBody.release());
if (m_requestHeaders.size() > 0)
request.addHTTPHeaderFields(m_requestHeaders);
}
......@@ -453,7 +470,7 @@ String XMLHttpRequest::accessControlOrigin() const
return accessControlOrigin;
}
void XMLHttpRequest::crossSiteAccessRequest(const String& body, ResourceRequest& request, ExceptionCode& ec)
void XMLHttpRequest::crossSiteAccessRequest(ResourceRequest& request, ExceptionCode& ec)
{
KURL url = m_url;
url.setUser(String());
......@@ -577,6 +594,8 @@ void XMLHttpRequest::abort()
changeState(DONE);
m_state = UNSENT;
}
}
void XMLHttpRequest::internalAbort()
......@@ -599,7 +618,7 @@ void XMLHttpRequest::internalAbort()
dropProtection();
}
void XMLHttpRequest::clearResponseEntityBody()
void XMLHttpRequest::clearResponse()
{
m_response = ResourceResponse();
{
......@@ -610,10 +629,16 @@ void XMLHttpRequest::clearResponseEntityBody()
m_responseXML = 0;
}
void XMLHttpRequest::genericError()
void XMLHttpRequest::clearRequest()
{
clearResponseEntityBody();
m_requestHeaders.clear();
m_requestEntityBody = 0;
}
void XMLHttpRequest::genericError()
{
clearResponse();
clearRequest();
m_error = true;
// The spec says we should "Synchronously switch the state to DONE." and then "Synchronously dispatch a readystatechange event on the object"
......@@ -983,7 +1008,7 @@ void XMLHttpRequest::dispatchProgressEvent(long long expectedLength)
}
ExceptionCode ec = 0;
dispatchEvent(evt, ec, false);
dispatchEvent(evt.release(), ec, false);
ASSERT(!ec);
}
......
......@@ -21,7 +21,9 @@
#define XMLHttpRequest_h
#include "AccessControlList.h"
#include "EventListener.h"
#include "EventTarget.h"
#include "FormData.h"
#include "ResourceResponse.h"
#include "SubresourceLoaderClient.h"
#include <wtf/OwnPtr.h>
......@@ -29,6 +31,7 @@
namespace WebCore {
class Document;
class File;
class TextResourceDecoder;
// these exact numeric values are important because JS expects them
......@@ -56,7 +59,9 @@ public:
void open(const String& method, const KURL&, bool async, ExceptionCode&);
void open(const String& method, const KURL&, bool async, const String& user, ExceptionCode&);
void open(const String& method, const KURL&, bool async, const String& user, const String& password, ExceptionCode&);
void send(const String& body, ExceptionCode&);
void send(ExceptionCode&);
void send(Document*, ExceptionCode&);
void send(const String&, ExceptionCode&);
void abort();
void setRequestHeader(const String& name, const String& value, ExceptionCode&);
void overrideMimeType(const String& override);
......@@ -65,12 +70,14 @@ public:
const KJS::UString& responseText() const;
Document* responseXML() const;
void setOnReadyStateChangeListener(PassRefPtr<EventListener>);
EventListener* onReadyStateChangeListener() const;
void setOnLoadListener(PassRefPtr<EventListener>);
EventListener* onLoadListener() const;
void setOnProgressListener(PassRefPtr<EventListener>);
EventListener* onProgressListener() const;
void setOnReadyStateChangeListener(PassRefPtr<EventListener> eventListener) { m_onReadyStateChangeListener = eventListener; }
EventListener* onReadyStateChangeListener() const { return m_onReadyStateChangeListener.get(); }
void setOnLoadListener(PassRefPtr<EventListener> eventListener) { m_onLoadListener = eventListener; }
EventListener* onLoadListener() const { return m_onLoadListener.get(); }
void setOnProgressListener(PassRefPtr<EventListener> eventListener) { m_onProgressListener = eventListener; }
EventListener* onProgressListener() const { return m_onProgressListener.get(); }
typedef Vector<RefPtr<EventListener> > ListenerVector;
typedef HashMap<AtomicStringImpl*, ListenerVector> EventListenersMap;
......@@ -111,6 +118,8 @@ private:
String responseMIMEType() const;
bool responseIsXML() const;
bool initSend(ExceptionCode&);
String getRequestHeader(const String& name) const;
void setRequestHeaderInternal(const String& name, const String& value);
......@@ -118,10 +127,13 @@ private:
void callReadyStateChangeListener();
void dropProtection();
void internalAbort();
void clearResponseEntityBody();
void clearResponse();
void clearRequest();
void createRequest(ExceptionCode&);
void sameOriginRequest(const String& body, ResourceRequest&);
void crossSiteAccessRequest(const String& body, ResourceRequest&, ExceptionCode&);
void sameOriginRequest(ResourceRequest&);
void crossSiteAccessRequest(ResourceRequest&, ExceptionCode&);
void loadRequestSynchronously(ResourceRequest&, ExceptionCode&);
void loadRequestAsynchronously(ResourceRequest&);
......@@ -144,6 +156,7 @@ private:
KURL m_url;
String m_method;
HTTPHeaderMap m_requestHeaders;
RefPtr<FormData> m_requestEntityBody;
String m_mimeTypeOverride;
bool m_async;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment