diff --git a/netwerk/protocol/http/HttpBaseChannel.cpp b/netwerk/protocol/http/HttpBaseChannel.cpp index de344b10ec1..d5bcfd603e9 100644 --- a/netwerk/protocol/http/HttpBaseChannel.cpp +++ b/netwerk/protocol/http/HttpBaseChannel.cpp @@ -589,14 +589,43 @@ HttpBaseChannel::ApplyContentConversions() return NS_OK; } - const char *val = mResponseHead->PeekHeader(nsHttp::Content_Encoding); - if (gHttpHandler->IsAcceptableEncoding(val)) { - nsCOMPtr serv; - nsresult rv = gHttpHandler-> - GetStreamConverterService(getter_AddRefs(serv)); - // we won't fail to load the page just because we couldn't load the - // stream converter service.. carry on.. - if (NS_SUCCEEDED(rv)) { + nsCAutoString contentEncoding; + char *cePtr, *val; + nsresult rv; + + rv = mResponseHead->GetHeader(nsHttp::Content_Encoding, contentEncoding); + if (NS_FAILED(rv) || contentEncoding.IsEmpty()) + return NS_OK; + + // The encodings are listed in the order they were applied + // (see rfc 2616 section 14.11), so they need to removed in reverse + // order. This is accomplished because the converter chain ends up + // being a stack with the last converter created being the first one + // to accept the raw network data. + + cePtr = contentEncoding.BeginWriting(); + PRUint32 count = 0; + while ((val = nsCRT::strtok(cePtr, HTTP_LWS ",", &cePtr))) { + if (++count > 16) { + // That's ridiculous. We only understand 2 different ones :) + // but for compatibility with old code, we will just carry on without + // removing the encodings + LOG(("Too many Content-Encodings. Ignoring remainder.\n")); + break; + } + + if (gHttpHandler->IsAcceptableEncoding(val)) { + nsCOMPtr serv; + rv = gHttpHandler->GetStreamConverterService(getter_AddRefs(serv)); + + // we won't fail to load the page just because we couldn't load the + // stream converter service.. carry on.. + if (NS_FAILED(rv)) { + if (val) + LOG(("Unknown content encoding '%s', ignoring\n", val)); + continue; + } + nsCOMPtr converter; nsCAutoString from(val); ToLowerCase(from); @@ -605,13 +634,18 @@ HttpBaseChannel::ApplyContentConversions() mListener, mListenerContext, getter_AddRefs(converter)); - if (NS_SUCCEEDED(rv)) { - LOG(("converter installed from \'%s\' to \'uncompressed\'\n", val)); - mListener = converter; + if (NS_FAILED(rv)) { + LOG(("Unexpected failure of AsyncConvertData %s\n", val)); + return rv; } + + LOG(("converter removed '%s' content-encoding\n", val)); + mListener = converter; + } + else { + if (val) + LOG(("Unknown content encoding '%s', ignoring\n", val)); } - } else if (val != nsnull) { - LOG(("Unknown content encoding '%s', ignoring\n", val)); } return NS_OK; diff --git a/netwerk/test/unit/test_content_encoding_gzip.js b/netwerk/test/unit/test_content_encoding_gzip.js new file mode 100644 index 00000000000..f37ab3cc234 --- /dev/null +++ b/netwerk/test/unit/test_content_encoding_gzip.js @@ -0,0 +1,72 @@ +do_load_httpd_js(); + +var httpserver = new nsHttpServer(); +var index = 0; +var tests = [ + {url: "/test/cegzip1", + flags: CL_EXPECT_GZIP, + ce: "gzip", + body: [ + 0x1f, 0x8b, 0x08, 0x08, 0x5a, 0xa0, 0x31, 0x4f, 0x00, 0x03, 0x74, 0x78, 0x74, 0x00, 0x2b, 0xc9, + 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0x92, 0xd4, 0xe2, 0x12, 0x43, 0x2e, 0x00, 0xb9, 0x23, 0xd7, 0x3b, + 0x0e, 0x00, 0x00, 0x00], + datalen: 14 // the data length of the uncompressed document + }, + + {url: "/test/cegzip2", + flags: CL_EXPECT_GZIP, + ce: "gzip, gzip", + body: [ + 0x1f, 0x8b, 0x08, 0x00, 0x72, 0xa1, 0x31, 0x4f, 0x00, 0x03, 0x93, 0xef, 0xe6, 0xe0, 0x88, 0x5a, + 0x60, 0xe8, 0xcf, 0xc0, 0x5c, 0x52, 0x51, 0xc2, 0xa0, 0x7d, 0xf2, 0x84, 0x4e, 0x18, 0xc3, 0xa2, + 0x49, 0x57, 0x1e, 0x09, 0x39, 0xeb, 0x31, 0xec, 0x54, 0xbe, 0x6e, 0xcd, 0xc7, 0xc0, 0xc0, 0x00, + 0x00, 0x6e, 0x90, 0x7a, 0x85, 0x24, 0x00, 0x00, 0x00], + datalen: 14 // the data length of the uncompressed document + }, +]; + +function setupChannel(url) { + var ios = Components.classes["@mozilla.org/network/io-service;1"]. + getService(Ci.nsIIOService); + var chan = ios.newChannel("http://localhost:4444" + url, "", null); + return chan; +} + +function startIter() { + var channel = setupChannel(tests[index].url); + channel.asyncOpen(new ChannelListener(completeIter, channel, tests[index].flags), null); +} + +function completeIter(request, data, ctx) { + do_check_true(data.length == tests[index].datalen); + if (++index < tests.length) { + startIter(); + } else { + httpserver.stop(do_test_finished); + } +} + +function run_test() { + httpserver.registerPathHandler("/test/cegzip1", handler); + httpserver.registerPathHandler("/test/cegzip2", handler); + httpserver.start(4444); + + startIter(); + do_test_pending(); +} + +function handler(metadata, response) { + response.setStatusLine(metadata.httpVersion, 200, "OK"); + response.setHeader("Content-Type", "text/plain", false); + response.setHeader("Content-Encoding", tests[index].ce, false); + response.setHeader("Content-Length", "" + tests[index].body.length, false); + + var bos = Components.classes["@mozilla.org/binaryoutputstream;1"] + .createInstance(Components.interfaces.nsIBinaryOutputStream); + bos.setOutputStream(response.bodyOutputStream); + + response.processAsync(); + bos.writeByteArray(tests[index].body, tests[index].body.length); + response.finish(); +} + diff --git a/netwerk/test/unit/xpcshell.ini b/netwerk/test/unit/xpcshell.ini index 837b94cc401..795cd3d7614 100644 --- a/netwerk/test/unit/xpcshell.ini +++ b/netwerk/test/unit/xpcshell.ini @@ -87,6 +87,7 @@ fail-if = os == "android" [test_channel_close.js] [test_compareURIs.js] [test_compressappend.js] +[test_content_encoding_gzip.js] [test_content_sniffer.js] [test_cookie_header.js] [test_data_protocol.js]