Bug 445225 - Make progress events to work properly with multipart XHR, r+sr=jst

This commit is contained in:
Olli Pettay 2008-09-11 13:44:15 +03:00
parent 6ff7e46a0c
commit 384c28fdaa
6 changed files with 103 additions and 0 deletions

View File

@ -121,6 +121,9 @@
#define XML_HTTP_REQUEST_MULTIPART (1 << 12) // Internal
#define XML_HTTP_REQUEST_GOT_FINAL_STOP (1 << 13) // Internal
#define XML_HTTP_REQUEST_BACKGROUND (1 << 14) // Internal
// This is set when we've got the headers for a multipart XMLHttpRequest,
// but haven't yet started to process the first part.
#define XML_HTTP_REQUEST_MPART_HEADERS (1 << 15) // Internal
#define XML_HTTP_REQUEST_LOADSTATES \
(XML_HTTP_REQUEST_UNINITIALIZED | \
@ -227,6 +230,8 @@ nsMultipartProxyListener::OnStartRequest(nsIRequest *aRequest,
// decoder in the pipeline to handle the content and pass it along
// to our original listener.
nsCOMPtr<nsIXMLHttpRequest> xhr = do_QueryInterface(mDestListener);
nsCOMPtr<nsIStreamConverterService> convServ =
do_GetService("@mozilla.org/streamConverters;1", &rv);
if (NS_SUCCEEDED(rv)) {
@ -243,6 +248,11 @@ nsMultipartProxyListener::OnStartRequest(nsIRequest *aRequest,
mDestListener = fromListener;
}
if (xhr) {
static_cast<nsXMLHttpRequest*>(xhr.get())->mState |=
XML_HTTP_REQUEST_MPART_HEADERS;
}
return mDestListener->OnStartRequest(aRequest, ctxt);
}
@ -1315,6 +1325,8 @@ nsXMLHttpRequest::OpenRequest(const nsACString& method,
mState &= ~XML_HTTP_REQUEST_ASYNC;
}
mState &= ~XML_HTTP_REQUEST_MPART_HEADERS;
rv = NS_NewURI(getter_AddRefs(uri), url, nsnull, GetBaseURI());
if (NS_FAILED(rv)) return rv;
@ -1586,6 +1598,7 @@ nsXMLHttpRequest::OnStartRequest(nsIRequest *request, nsISupports *ctxt)
mReadRequest = request;
mContext = ctxt;
mState |= XML_HTTP_REQUEST_PARSEBODY;
mState &= ~XML_HTTP_REQUEST_MPART_HEADERS;
ChangeState(XML_HTTP_REQUEST_LOADED);
nsIURI* uri = GetBaseURI();
@ -2441,6 +2454,13 @@ nsXMLHttpRequest::OnChannelRedirect(nsIChannel *aOldChannel,
NS_IMETHODIMP
nsXMLHttpRequest::OnProgress(nsIRequest *aRequest, nsISupports *aContext, PRUint64 aProgress, PRUint64 aProgressMax)
{
// We're in middle of processing multipart headers and we don't want to report
// any progress because upload's 'load' is dispatched when we start to load
// the first response.
if (XML_HTTP_REQUEST_MPART_HEADERS & mState) {
return NS_OK;
}
// We're uploading if our state is XML_HTTP_REQUEST_OPENED or
// XML_HTTP_REQUEST_SENT
PRBool upload = !!((XML_HTTP_REQUEST_OPENED | XML_HTTP_REQUEST_SENT) & mState);

View File

@ -254,6 +254,8 @@ public:
nsXHREventTarget)
protected:
friend class nsMultipartProxyListener;
nsresult DetectCharset(nsACString& aCharset);
nsresult ConvertBodyToText(nsAString& aOutBuffer);
static NS_METHOD StreamReaderFunc(nsIInputStream* in,

View File

@ -201,6 +201,9 @@ _TEST_FILES = test_bug5141.html \
test_NodeIterator_mutations_2.html \
test_bug28293.html \
file_bug28293.sjs \
test_bug445225.html \
file_bug445225_multipart.txt \
file_bug445225_multipart.txt^headers^ \
test_title.html \
test_bug453521.html \
test_bug391728.html \

View File

@ -0,0 +1,7 @@
--boundary
Content-Type: text/xml
<?xml version="1.0"?>
<data>data1</data>
--boundary--

View File

@ -0,0 +1,2 @@
HTTP 200 OK
Content-Type: multipart/x-mixed-replace;boundary="--boundary"

View File

@ -0,0 +1,69 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=445225
-->
<head>
<title>Test for Bug 445225</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/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=445225">Mozilla Bug 445225</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for Bug 445225 **/
var xhr = null;
function runTest() {
xhr = new XMLHttpRequest();
xhr.multipart = true;
xhr.upload.onprogress =
function(evt) {
ok(false, ".upload shouldn't get any progress events!");
}
xhr.onload = function(evt) { setTimeout(runTest2, 0); }
xhr.open("GET", "file_bug445225_multipart.txt");
xhr.send();
}
var uploadGotLoad = false;
function runTest2() {
xhr = new XMLHttpRequest();
xhr.multipart = true;
xhr.upload.onload =
function(evt) {
uploadGotLoad = true;
}
xhr.onprogress =
function(evt) {
ok(uploadGotLoad,
"XHR shouldn't get any progress events before load event is dispatched to .upload");
}
xhr.onload =
function(evt) {
ok(uploadGotLoad, ".upload should have got load event.");
SimpleTest.finish();
}
xhr.open("POST", "file_bug445225_multipart.txt");
// Send some huge string.
xhr.send(new Array(100).join('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'));
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(runTest);
</script>
</pre>
</body>
</html>