add a sendAsBinary() method to XMLHttpRequest (for sending data from an <input type=file> element, among other things). b=371432, r+sr=jonas

This commit is contained in:
dcamp@mozilla.com 2007-07-24 21:53:21 -07:00
Родитель d554b1cee2
Коммит 319fc3fbe1
2 изменённых файлов: 48 добавлений и 1 удалений

Просмотреть файл

@ -87,7 +87,7 @@ interface nsIVariant;
* you're aware of all the security implications. And then think twice about
* it.
*/
[scriptable, uuid(7b3b3c62-9d53-4abc-bc89-c33ce78f439f)]
[scriptable, uuid(7ceba511-b7a1-4b38-9643-d09c60c08146)]
interface nsIXMLHttpRequest : nsISupports
{
/**
@ -242,6 +242,15 @@ interface nsIXMLHttpRequest : nsISupports
*/
void send(in nsIVariant body);
/**
* A variant of the send() method used to send binary data.
*
* @param body The request body as a DOM string. The string data will be
* converted to a single-byte string by truncation (i.e., the
* high-order byte of each character will be discarded).
*/
void sendAsBinary(in DOMString body);
/**
* Sets a HTTP request header for HTTP requests. You must call open
* before setting the request headers.

Просмотреть файл

@ -64,6 +64,7 @@
#include "nsIDOMElement.h"
#include "nsIDOMWindow.h"
#include "nsIVariant.h"
#include "nsVariant.h"
#include "nsIParser.h"
#include "nsLoadListenerProxy.h"
#include "nsIWindowWatcher.h"
@ -82,6 +83,7 @@
#include "nsContentPolicyUtils.h"
#include "nsContentErrors.h"
#include "nsLayoutStatics.h"
#include "nsDOMError.h"
#define LOAD_STR "load"
#define ERROR_STR "error"
@ -1493,6 +1495,42 @@ nsXMLHttpRequest::RequestCompleted()
return rv;
}
NS_IMETHODIMP
nsXMLHttpRequest::SendAsBinary(const nsAString &aBody)
{
char *data = static_cast<char*>(NS_Alloc(aBody.Length() + 1));
if (!data)
return NS_ERROR_OUT_OF_MEMORY;
nsAString::const_iterator iter, end;
aBody.BeginReading(iter);
aBody.EndReading(end);
char *p = data;
while (iter != end) {
if (*iter & 0xFF00) {
NS_Free(data);
return NS_ERROR_DOM_INVALID_CHARACTER_ERR;
}
*p++ = static_cast<char>(*iter++);
}
*p = '\0';
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), data,
aBody.Length(), NS_ASSIGNMENT_ADOPT);
if (NS_FAILED(rv))
NS_Free(data);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIWritableVariant> variant = new nsVariant();
if (!variant) return NS_ERROR_OUT_OF_MEMORY;
rv = variant->SetAsISupports(stream);
NS_ENSURE_SUCCESS(rv, rv);
return Send(variant);
}
/* void send (in nsIVariant aBody); */
NS_IMETHODIMP
nsXMLHttpRequest::Send(nsIVariant *aBody)