From 8b74570d44d991a483c2b8706702b2fb5faa9c25 Mon Sep 17 00:00:00 2001 From: "heikki%netscape.com" Date: Tue, 27 Nov 2001 23:44:12 +0000 Subject: [PATCH] Bug 98928, send() can take any type of parameter, i.e. it can now take JavaScript string. r=jst, sr=jband. --- .../base/public/nsIXMLHttpRequest.idl | 3 +- .../xmlextras/base/src/nsXMLHttpRequest.cpp | 82 ++++++++++++------- extensions/xmlextras/tests/posttext.html | 75 +++++++++++++++++ 3 files changed, 128 insertions(+), 32 deletions(-) create mode 100644 extensions/xmlextras/tests/posttext.html diff --git a/extensions/xmlextras/base/public/nsIXMLHttpRequest.idl b/extensions/xmlextras/base/public/nsIXMLHttpRequest.idl index 0286806084a..92468271e1d 100644 --- a/extensions/xmlextras/base/public/nsIXMLHttpRequest.idl +++ b/extensions/xmlextras/base/public/nsIXMLHttpRequest.idl @@ -41,6 +41,7 @@ interface nsIDOMDocument; interface nsIDOMEventListener; interface nsIChannel; +interface nsIVariant; /** * Mozilla's XMLHttpRequest is modelled after Microsoft's IXMLHttpRequest @@ -201,7 +202,7 @@ interface nsIXMLHttpRequest : nsISupports { * to contain the ContentType: and ContentLength: headers * and trailing carriage-return/line-feed pairs. */ - void send(in nsISupports body); + void send(in nsIVariant body); /** * Sets a HTTP request header for HTTP requests. You must call open diff --git a/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp b/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp index b54c86a75ab..bc74a1d099f 100644 --- a/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp +++ b/extensions/xmlextras/base/src/nsXMLHttpRequest.cpp @@ -77,6 +77,7 @@ #endif #include "nsIDOMClassInfo.h" #include "nsIDOMElement.h" +#include "nsIVariant.h" #include "nsIParser.h" #include "nsLoadListenerProxy.h" @@ -945,9 +946,9 @@ nsXMLHttpRequest::RequestCompleted() return rv; } -/* void send (in nsISupports body); */ +/* void send (in nsIVariant aBody); */ NS_IMETHODIMP -nsXMLHttpRequest::Send(nsISupports *body) +nsXMLHttpRequest::Send(nsIVariant *aBody) { nsresult rv; @@ -969,42 +970,61 @@ nsXMLHttpRequest::Send(nsISupports *body) httpChannel->GetRequestMethod(getter_Copies(method)); // If GET, method name will be uppercase } - if (body && httpChannel && nsCRT::strcmp("GET",method.get()) != 0) { + if (aBody && httpChannel && nsCRT::strcmp("GET", method.get()) != 0) { + nsXPIDLString serial; nsCOMPtr postDataStream; - nsCOMPtr doc(do_QueryInterface(body)); - if (doc) { - // Get an XML serializer - nsCOMPtr serializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID, &rv)); - if (NS_FAILED(rv)) return NS_ERROR_FAILURE; - - // Serialize the current document to string - nsXPIDLString serial; - rv = serializer->SerializeToString(doc, getter_Copies(serial)); - if (NS_FAILED(rv)) return NS_ERROR_FAILURE; + PRUint16 dataType; + rv = aBody->GetDataType(&dataType); + if (NS_FAILED(rv)) + return NS_ERROR_FAILURE; + + if (dataType == nsIDataType::VTYPE_INTERFACE || + dataType == nsIDataType::VTYPE_INTERFACE_IS) { + nsCOMPtr supports; + nsID *iid; + rv = aBody->GetAsInterface(&iid, getter_AddRefs(supports)); + if (NS_FAILED(rv)) + return NS_ERROR_FAILURE; + if (iid) + nsMemory::Free(iid); + + // document? + nsCOMPtr doc(do_QueryInterface(supports)); + if (doc) { + nsCOMPtr serializer(do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID, &rv)); + if (NS_FAILED(rv)) return NS_ERROR_FAILURE; + rv = serializer->SerializeToString(doc, getter_Copies(serial)); + if (NS_FAILED(rv)) + return NS_ERROR_FAILURE; + } else { + // nsISupportsWString? + nsCOMPtr wstr(do_QueryInterface(supports)); + if (wstr) { + wstr->GetData(getter_Copies(serial)); + } else { + // stream? + nsCOMPtr stream(do_QueryInterface(supports)); + if (stream) { + postDataStream = stream; + } + } + } + } else { + // try variant string + rv = aBody->GetAsWString(getter_Copies(serial)); + if (NS_FAILED(rv)) + return rv; + } + + if (serial) { // Convert to a byte stream rv = GetStreamForWString(serial.get(), nsCRT::strlen(serial.get()), getter_AddRefs(postDataStream)); - if (NS_FAILED(rv)) return rv; - } - else { - nsCOMPtr stream(do_QueryInterface(body)); - if (stream) { - postDataStream = stream; - } - else { - nsCOMPtr wstr(do_QueryInterface(body)); - if (wstr) { - nsXPIDLString holder; - wstr->GetData(getter_Copies(holder)); - rv = GetStreamForWString(holder.get(), - nsCRT::strlen(holder.get()), - getter_AddRefs(postDataStream)); - if (NS_FAILED(rv)) return rv; - } - } + if (NS_FAILED(rv)) + return rv; } if (postDataStream) { diff --git a/extensions/xmlextras/tests/posttext.html b/extensions/xmlextras/tests/posttext.html new file mode 100644 index 00000000000..7580b8f08c8 --- /dev/null +++ b/extensions/xmlextras/tests/posttext.html @@ -0,0 +1,75 @@ + +POST test + + + + +

POST test

+ +
responseText +
@@No result@@
+
+
responseXML serialized +
@@No result@@
+
+
getAllResponseHeaders() +
@@No result@@
+
+
status +
@@No result@@
+
+
statusText +
@@No result@@
+
+
readyState +
@@No result@@
+
+
Event information +
@@No result@@
+
+ + +