Bug 782342 - blob: protocol no Content-Length header. r=sicking

This commit is contained in:
Andrea Marchesini 2012-08-27 19:34:29 -04:00
parent 1a2865e046
commit 611da6048d
5 changed files with 129 additions and 18 deletions

View File

@ -56,7 +56,7 @@ nsBlobProtocolHandler::GetFileDataEntryPrincipal(nsACString& aUri)
if (!gFileDataTable) {
return nullptr;
}
FileDataInfo* res;
gFileDataTable->Get(aUri, &res);
if (!res) {
@ -72,11 +72,11 @@ GetFileDataInfo(const nsACString& aUri)
NS_ASSERTION(StringBeginsWith(aUri,
NS_LITERAL_CSTRING(BLOBURI_SCHEME ":")),
"Bad URI");
if (!gFileDataTable) {
return nullptr;
}
FileDataInfo* res;
gFileDataTable->Get(aUri, &res);
return res;
@ -173,19 +173,25 @@ nsBlobProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
rv = info->mFile->GetType(type);
NS_ENSURE_SUCCESS(rv, rv);
PRUint64 size;
rv = info->mFile->GetSize(&size);
NS_ENSURE_SUCCESS(rv, rv);
channel->SetOwner(owner);
channel->SetOriginalURI(uri);
channel->SetContentType(NS_ConvertUTF16toUTF8(type));
channel->SetContentLength(size);
channel.forget(result);
return NS_OK;
}
NS_IMETHODIMP
NS_IMETHODIMP
nsBlobProtocolHandler::AllowPort(int32_t port, const char *scheme,
bool *_retval)
{
// don't override anything.
// don't override anything.
*_retval = false;
return NS_OK;
}

View File

@ -1468,6 +1468,13 @@ nsXMLHttpRequest::GetAllResponseHeaders(nsString& aResponseHeaders)
}
aResponseHeaders.AppendLiteral("\r\n");
}
PRInt32 length;
if (NS_SUCCEEDED(mChannel->GetContentLength(&length))) {
aResponseHeaders.AppendLiteral("Content-Length: ");
aResponseHeaders.AppendInt(length);
aResponseHeaders.AppendLiteral("\r\n");
}
}
NS_IMETHODIMP
@ -1495,28 +1502,38 @@ nsXMLHttpRequest::GetResponseHeader(const nsACString& header,
return;
}
// Even non-http channels supply content type.
// Even non-http channels supply content type and content length.
// Remember we don't leak header information from denied cross-site
// requests.
nsresult status;
if (!mChannel ||
NS_FAILED(mChannel->GetStatus(&status)) ||
NS_FAILED(status) ||
!header.LowerCaseEqualsASCII("content-type")) {
NS_FAILED(status)) {
return;
}
if (NS_FAILED(mChannel->GetContentType(_retval))) {
// Means no content type
_retval.SetIsVoid(true);
return;
// Content Type:
if (header.LowerCaseEqualsASCII("content-type")) {
if (NS_FAILED(mChannel->GetContentType(_retval))) {
// Means no content type
_retval.SetIsVoid(true);
return;
}
nsCString value;
if (NS_SUCCEEDED(mChannel->GetContentCharset(value)) &&
!value.IsEmpty()) {
_retval.Append(";charset=");
_retval.Append(value);
}
}
nsCString value;
if (NS_SUCCEEDED(mChannel->GetContentCharset(value)) &&
!value.IsEmpty()) {
_retval.Append(";charset=");
_retval.Append(value);
// Content Length:
else if (header.LowerCaseEqualsASCII("content-length")) {
PRInt32 length;
if (NS_SUCCEEDED(mChannel->GetContentLength(&length))) {
_retval.AppendInt(length);
}
}
return;

View File

@ -253,6 +253,8 @@ MOCHITEST_FILES_A = \
test_mutationobservers.html \
mutationobserver_dialog.html \
test_bug744830.html \
file_bug782342.txt \
test_bug782342.html \
$(NULL)
MOCHITEST_FILES_B = \

View File

@ -0,0 +1 @@
hello world

View File

@ -0,0 +1,85 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=782342
-->
<head>
<title>Test for bug 782342 - blob: protocol no Content-Length header</title>
<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=782342">Mozilla Bug 782342 - blob: protocol no Content-Length header</a>
<p id="display">
<input id="fileList" type="file"></input>
</p>
<script type="text/javascript;version=1.7">
SimpleTest.waitForExplicitFinish();
var url = "file_bug782342.txt";
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "blob";
function checkHeaders(xhr) {
var headers = xhr.getAllResponseHeaders();
dump(headers + "\n");
var p = headers.split("\n");
var contentType = false;
var contentLength = false;
for (var i = 0; i < p.length; ++i) {
var header = p[i].split(':')[0];
if (header.toLowerCase() == 'content-type')
contentType = true;
else if (header.toLowerCase() == 'content-length')
contentLength = true;
}
ok(contentLength == true, "Content-length is part of the headers!");
ok(contentType == true, "Content-type is part of the headers!");
var ct = xhr.getResponseHeader('content-type');
ok(ct.length > 0, 'Get Response Header - content type: ' + ct);
var cl = xhr.getResponseHeader('content-length');
ok(cl.length > 0, 'Get Response Header - content length: ' + cl);
}
xhr.addEventListener("load", function () {
ok(xhr.status === 200, "Status 200!");
if (xhr.status === 200) {
var blob = xhr.response;
ok(blob, "Blob is: " + blob);
var blobUrl = window.URL.createObjectURL(blob);
ok(blobUrl, "Blob URL is: " + blobUrl);
checkHeaders(xhr);
var xhrBlob = new XMLHttpRequest();
xhrBlob.open("GET", blobUrl, true);
xhrBlob.responseType = "blob";
xhrBlob.addEventListener("load", function () {
var blob2 = xhr.response;
ok(blob2, "Blob is: " + blob2);
var blob2Url = window.URL.createObjectURL(blob);
ok(blob2Url, "Blob URL is: " + blob2Url);
checkHeaders(xhrBlob);
ok(blob2.size == blob.size, "Blob sizes are: " + blob2.size + " - " + blob.size);
ok(blob2.type == blob.type, "Blob types are: " + blob2.type + " - " + blob.type);
SimpleTest.finish();
}, false);
xhrBlob.send();
}
}, false);
xhr.send();
</script>
</body>
</html>