Bug 796850 - Change XMLHttpRequest interface to support ByteString r=bz

This commit is contained in:
James Kitchener 2013-06-13 01:20:10 -04:00
parent 6d46bd7a62
commit aaa284cd89
9 changed files with 96 additions and 72 deletions

View File

@ -21,7 +21,7 @@ interface nsIDOMBlob;
#include "jsapi.h"
%}
[scriptable, builtinclass, uuid(5bc978f2-41e5-4349-a12d-b018092271f7)]
[scriptable, builtinclass, uuid(ac97e161-9f1d-4163-adc9-e9a59e18682c)]
interface nsIXMLHttpRequestEventTarget : nsIDOMEventTarget {
// event handler attributes
[implicit_jscontext] attribute jsval onabort;
@ -133,7 +133,7 @@ interface nsIXMLHttpRequest : nsISupports
* The string representing the status of the response for
* HTTP requests.
*/
readonly attribute DOMString statusText;
readonly attribute ACString statusText;
/**
* If the request has been sent already, this method will
@ -148,7 +148,7 @@ interface nsIXMLHttpRequest : nsISupports
* @returns A string containing all of the response headers.
* The empty string if the response has not yet been received.
*/
DOMString getAllResponseHeaders();
ACString getAllResponseHeaders();
/**
* Returns the text of the header with the specified name for
@ -159,7 +159,7 @@ interface nsIXMLHttpRequest : nsISupports
* NULL if the response has not yet been received or the
* header does not exist in the response.
*/
ACString getResponseHeader(in AUTF8String header);
ACString getResponseHeader(in ACString header);
%{C++
// note this is NOT virtual so this won't muck with the vtable!
@ -189,7 +189,7 @@ interface nsIXMLHttpRequest : nsISupports
* @param password (optional) A password for authentication if necessary.
* The default value is the empty string
*/
[optional_argc] void open(in AUTF8String method, in AUTF8String url,
[optional_argc] void open(in ACString method, in AUTF8String url,
[optional] in boolean async,
[optional,Undefined(Empty)] in DOMString user,
[optional,Undefined(Empty)] in DOMString password);
@ -237,7 +237,7 @@ interface nsIXMLHttpRequest : nsISupports
* @param header The name of the header to set in the request.
* @param value The body of the header.
*/
void setRequestHeader(in AUTF8String header, in AUTF8String value);
void setRequestHeader(in ACString header, in ACString value);
/**
* The amount of milliseconds a request can take before being terminated.

View File

@ -139,11 +139,11 @@ using namespace mozilla::dom;
#define NS_PROGRESS_EVENT_INTERVAL 50
#define IMPL_STRING_GETTER(_name) \
#define IMPL_CSTRING_GETTER(_name) \
NS_IMETHODIMP \
nsXMLHttpRequest::_name(nsAString& aOut) \
nsXMLHttpRequest::_name(nsACString& aOut) \
{ \
nsString tmp; \
nsCString tmp; \
_name(tmp); \
aOut = tmp; \
return NS_OK; \
@ -1128,9 +1128,9 @@ nsXMLHttpRequest::Status()
return status;
}
IMPL_STRING_GETTER(GetStatusText)
IMPL_CSTRING_GETTER(GetStatusText)
void
nsXMLHttpRequest::GetStatusText(nsString& aStatusText)
nsXMLHttpRequest::GetStatusText(nsCString& aStatusText)
{
nsCOMPtr<nsIHttpChannel> httpChannel = GetCurrentHttpChannel();
@ -1152,17 +1152,8 @@ nsXMLHttpRequest::GetStatusText(nsString& aStatusText)
}
}
nsCString statusText;
httpChannel->GetResponseStatusText(statusText);
if (statusText.IsVoid()) {
aStatusText.SetIsVoid(true);
} else {
// We use UTF8ToNewUnicode here because it truncates after invalid UTF-8
// characters, CopyUTF8toUTF16 just doesn't copy in that case.
uint32_t length;
PRUnichar* chars = UTF8ToNewUnicode(statusText, &length);
aStatusText.Adopt(chars, length);
}
httpChannel->GetResponseStatusText(aStatusText);
}
void
@ -1286,10 +1277,10 @@ nsXMLHttpRequest::IsSafeHeader(const nsACString& header, nsIHttpChannel* httpCha
return isSafe;
}
/* DOMString getAllResponseHeaders(); */
IMPL_STRING_GETTER(GetAllResponseHeaders)
/* ByteString getAllResponseHeaders(); */
IMPL_CSTRING_GETTER(GetAllResponseHeaders)
void
nsXMLHttpRequest::GetAllResponseHeaders(nsString& aResponseHeaders)
nsXMLHttpRequest::GetAllResponseHeaders(nsCString& aResponseHeaders)
{
aResponseHeaders.Truncate();
@ -1303,7 +1294,7 @@ nsXMLHttpRequest::GetAllResponseHeaders(nsString& aResponseHeaders)
if (nsCOMPtr<nsIHttpChannel> httpChannel = GetCurrentHttpChannel()) {
nsRefPtr<nsHeaderVisitor> visitor = new nsHeaderVisitor(this, httpChannel);
if (NS_SUCCEEDED(httpChannel->VisitResponseHeaders(visitor))) {
CopyASCIItoUTF16(visitor->Headers(), aResponseHeaders);
aResponseHeaders = visitor->Headers();
}
return;
}
@ -1316,10 +1307,10 @@ nsXMLHttpRequest::GetAllResponseHeaders(nsString& aResponseHeaders)
nsAutoCString value;
if (NS_SUCCEEDED(mChannel->GetContentType(value))) {
aResponseHeaders.AppendLiteral("Content-Type: ");
AppendASCIItoUTF16(value, aResponseHeaders);
aResponseHeaders.Append(value);
if (NS_SUCCEEDED(mChannel->GetContentCharset(value)) && !value.IsEmpty()) {
aResponseHeaders.AppendLiteral(";charset=");
AppendASCIItoUTF16(value, aResponseHeaders);
aResponseHeaders.Append(value);
}
aResponseHeaders.AppendLiteral("\r\n");
}
@ -2950,7 +2941,7 @@ nsXMLHttpRequest::Send(nsIVariant* aVariant, const Nullable<RequestBody>& aBody)
return rv;
}
/* void setRequestHeader (in AUTF8String header, in AUTF8String value); */
/* void setRequestHeader (in ByteString header, in ByteString value); */
// http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#dom-xmlhttprequest-setrequestheader
NS_IMETHODIMP
nsXMLHttpRequest::SetRequestHeader(const nsACString& header,

View File

@ -242,19 +242,18 @@ public:
uint16_t ReadyState();
// request
void Open(const nsAString& aMethod, const nsAString& aUrl, bool aAsync,
void Open(const nsACString& aMethod, const nsAString& aUrl, bool aAsync,
const mozilla::dom::Optional<nsAString>& aUser,
const mozilla::dom::Optional<nsAString>& aPassword,
ErrorResult& aRv)
{
aRv = Open(NS_ConvertUTF16toUTF8(aMethod), NS_ConvertUTF16toUTF8(aUrl),
aRv = Open(aMethod, NS_ConvertUTF16toUTF8(aUrl),
aAsync, aUser, aPassword);
}
void SetRequestHeader(const nsAString& aHeader, const nsAString& aValue,
void SetRequestHeader(const nsACString& aHeader, const nsACString& aValue,
ErrorResult& aRv)
{
aRv = SetRequestHeader(NS_ConvertUTF16toUTF8(aHeader),
NS_ConvertUTF16toUTF8(aValue));
aRv = SetRequestHeader(aHeader, aValue);
}
uint32_t Timeout()
{
@ -400,7 +399,7 @@ public:
// response
uint32_t Status();
void GetStatusText(nsString& aStatusText);
void GetStatusText(nsCString& aStatusText);
void GetResponseHeader(const nsACString& aHeader, nsACString& aResult,
ErrorResult& aRv);
void GetResponseHeader(const nsAString& aHeader, nsString& aResult,
@ -416,7 +415,7 @@ public:
CopyASCIItoUTF16(result, aResult);
}
}
void GetAllResponseHeaders(nsString& aResponseHeaders);
void GetAllResponseHeaders(nsCString& aResponseHeaders);
bool IsSafeHeader(const nsACString& aHeaderName, nsIHttpChannel* aHttpChannel);
void OverrideMimeType(const nsAString& aMimeType)
{

View File

@ -2,6 +2,7 @@
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=638112
https://bugzilla.mozilla.org/show_bug.cgi?id=796850
-->
<head>
<title>Test for Bug 638112</title>
@ -10,15 +11,18 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=638112
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=638112">Mozilla Bug 638112</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=796850">Mozilla Bug 796850</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="text/javascript">
SimpleTest.expectAssertions(1);
/** Test for Bug 638112, revised for Bug 796850 **/
/** Test for Bug 638112 **/
/* Bug 796850 changed the type of statusText to ByteString. As a result it is
* now considered to be raw 8 bit encoded rather than UTF8.
*/
function run_test() {
var req = new XMLHttpRequest();
@ -26,7 +30,7 @@ function run_test() {
req.send(null);
var statusText = req.statusText;
is(statusText, "Information Sans-Autorit", "");
is(statusText, "Information Sans-Autorit\u00E9", "");
SimpleTest.finish();
}

View File

@ -74,6 +74,7 @@ MOCHITEST_FILES := \
test_bug862092.html \
test_bug560072.html \
test_lenientThis.html \
test_ByteString.html \
$(NULL)
MOCHITEST_CHROME_FILES = \

View File

@ -0,0 +1,32 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=796850
-->
<head>
<meta charset="utf-8">
<title>Test for ByteString support</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=796850">Mozilla Bug 796850</a>
<p id="display"></p>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 796850 **/
var xhr = new XMLHttpRequest();
caught = false;
try {
xhr.open("\u5427", "about:mozilla", true);
}
catch (TypeError) {
caught = true;
}
ok(caught, "Character values > 255 not rejected for ByteString");
</script>
</pre>
</body>
</html>

View File

@ -71,10 +71,10 @@ interface XMLHttpRequest : XMLHttpRequestEventTarget {
// request
[Throws]
void open(DOMString method, DOMString url, optional boolean async = true,
void open(ByteString method, DOMString url, optional boolean async = true,
optional DOMString? user, optional DOMString? password);
[Throws]
void setRequestHeader(DOMString header, DOMString value);
void setRequestHeader(ByteString header, ByteString value);
[SetterThrows]
attribute unsigned long timeout;
@ -109,12 +109,12 @@ interface XMLHttpRequest : XMLHttpRequestEventTarget {
[Throws=Workers]
readonly attribute unsigned short status;
readonly attribute DOMString statusText;
readonly attribute ByteString statusText;
[Throws]
DOMString? getResponseHeader(DOMString header);
ByteString? getResponseHeader(ByteString header);
[Throws=Workers]
DOMString getAllResponseHeaders();
ByteString getAllResponseHeaders();
[Throws=Workers]
void overrideMimeType(DOMString mime);

View File

@ -523,7 +523,7 @@ class EventRunnable : public MainThreadProxyRunnable
nsTArray<nsCOMPtr<nsISupports> > mClonedObjects;
jsval mResponse;
nsString mResponseText;
nsString mStatusText;
nsCString mStatusText;
uint64_t mLoaded;
uint64_t mTotal;
uint32_t mEventStreamId;
@ -970,11 +970,11 @@ public:
class GetAllResponseHeadersRunnable : public WorkerThreadProxySyncRunnable
{
nsString& mResponseHeaders;
nsCString& mResponseHeaders;
public:
GetAllResponseHeadersRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
nsString& aResponseHeaders)
nsCString& aResponseHeaders)
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy),
mResponseHeaders(aResponseHeaders)
{ }
@ -994,7 +994,7 @@ class GetResponseHeaderRunnable : public WorkerThreadProxySyncRunnable
public:
GetResponseHeaderRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
const nsCString& aHeader, nsCString& aValue)
const nsACString& aHeader, nsCString& aValue)
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mHeader(aHeader),
mValue(aValue)
{ }
@ -1008,7 +1008,7 @@ public:
class OpenRunnable : public WorkerThreadProxySyncRunnable
{
nsString mMethod;
nsCString mMethod;
nsString mURL;
Optional<nsAString> mUser;
nsString mUserStr;
@ -1020,7 +1020,7 @@ class OpenRunnable : public WorkerThreadProxySyncRunnable
public:
OpenRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
const nsAString& aMethod, const nsAString& aURL,
const nsACString& aMethod, const nsAString& aURL,
const Optional<nsAString>& aUser,
const Optional<nsAString>& aPassword,
bool aBackgroundRequest, bool aWithCredentials,
@ -1201,7 +1201,7 @@ class SetRequestHeaderRunnable : public WorkerThreadProxySyncRunnable
public:
SetRequestHeaderRunnable(WorkerPrivate* aWorkerPrivate, Proxy* aProxy,
const nsCString& aHeader, const nsCString& aValue)
const nsACString& aHeader, const nsACString& aValue)
: WorkerThreadProxySyncRunnable(aWorkerPrivate, aProxy), mHeader(aHeader),
mValue(aValue)
{ }
@ -1716,7 +1716,7 @@ XMLHttpRequest::Notify(JSContext* aCx, Status aStatus)
}
void
XMLHttpRequest::Open(const nsAString& aMethod, const nsAString& aUrl,
XMLHttpRequest::Open(const nsACString& aMethod, const nsAString& aUrl,
bool aAsync, const Optional<nsAString>& aUser,
const Optional<nsAString>& aPassword, ErrorResult& aRv)
{
@ -1754,8 +1754,8 @@ XMLHttpRequest::Open(const nsAString& aMethod, const nsAString& aUrl,
}
void
XMLHttpRequest::SetRequestHeader(const nsAString& aHeader,
const nsAString& aValue, ErrorResult& aRv)
XMLHttpRequest::SetRequestHeader(const nsACString& aHeader,
const nsACString& aValue, ErrorResult& aRv)
{
mWorkerPrivate->AssertIsOnWorkerThread();
@ -1770,9 +1770,7 @@ XMLHttpRequest::SetRequestHeader(const nsAString& aHeader,
}
nsRefPtr<SetRequestHeaderRunnable> runnable =
new SetRequestHeaderRunnable(mWorkerPrivate, mProxy,
NS_ConvertUTF16toUTF8(aHeader),
NS_ConvertUTF16toUTF8(aValue));
new SetRequestHeaderRunnable(mWorkerPrivate, mProxy, aHeader, aValue);
if (!runnable->Dispatch(GetJSContext())) {
aRv.Throw(NS_ERROR_FAILURE);
return;
@ -2012,8 +2010,8 @@ XMLHttpRequest::Abort(ErrorResult& aRv)
}
void
XMLHttpRequest::GetResponseHeader(const nsAString& aHeader,
nsAString& aResponseHeader, ErrorResult& aRv)
XMLHttpRequest::GetResponseHeader(const nsACString& aHeader,
nsACString& aResponseHeader, ErrorResult& aRv)
{
mWorkerPrivate->AssertIsOnWorkerThread();
@ -2027,20 +2025,19 @@ XMLHttpRequest::GetResponseHeader(const nsAString& aHeader,
return;
}
nsCString value;
nsCString responseHeader;
nsRefPtr<GetResponseHeaderRunnable> runnable =
new GetResponseHeaderRunnable(mWorkerPrivate, mProxy,
NS_ConvertUTF16toUTF8(aHeader), value);
new GetResponseHeaderRunnable(mWorkerPrivate, mProxy, aHeader,
responseHeader);
if (!runnable->Dispatch(GetJSContext())) {
aRv.Throw(NS_ERROR_FAILURE);
return;
}
aResponseHeader = NS_ConvertUTF8toUTF16(value);
aResponseHeader = responseHeader;
}
void
XMLHttpRequest::GetAllResponseHeaders(nsAString& aResponseHeaders,
XMLHttpRequest::GetAllResponseHeaders(nsACString& aResponseHeaders,
ErrorResult& aRv)
{
mWorkerPrivate->AssertIsOnWorkerThread();
@ -2055,7 +2052,7 @@ XMLHttpRequest::GetAllResponseHeaders(nsAString& aResponseHeaders,
return;
}
nsString responseHeaders;
nsCString responseHeaders;
nsRefPtr<GetAllResponseHeadersRunnable> runnable =
new GetAllResponseHeadersRunnable(mWorkerPrivate, mProxy, responseHeaders);
if (!runnable->Dispatch(GetJSContext())) {

View File

@ -29,7 +29,7 @@ public:
{
nsString mResponseText;
uint32_t mStatus;
nsString mStatusText;
nsCString mStatusText;
uint16_t mReadyState;
jsval mResponse;
nsresult mResponseTextResult;
@ -122,12 +122,12 @@ public:
}
void
Open(const nsAString& aMethod, const nsAString& aUrl, bool aAsync,
Open(const nsACString& aMethod, const nsAString& aUrl, bool aAsync,
const Optional<nsAString>& aUser, const Optional<nsAString>& aPassword,
ErrorResult& aRv);
void
SetRequestHeader(const nsAString& aHeader, const nsAString& aValue,
SetRequestHeader(const nsACString& aHeader, const nsACString& aValue,
ErrorResult& aRv);
uint32_t
@ -199,17 +199,17 @@ public:
}
void
GetStatusText(nsAString& aStatusText) const
GetStatusText(nsACString& aStatusText) const
{
aStatusText = mStateData.mStatusText;
}
void
GetResponseHeader(const nsAString& aHeader, nsAString& aResponseHeader,
GetResponseHeader(const nsACString& aHeader, nsACString& aResponseHeader,
ErrorResult& aRv);
void
GetAllResponseHeaders(nsAString& aResponseHeaders, ErrorResult& aRv);
GetAllResponseHeaders(nsACString& aResponseHeaders, ErrorResult& aRv);
void
OverrideMimeType(const nsAString& aMimeType, ErrorResult& aRv);