From 46c665dd8fa2dd8761f0969e3aca942f2fd4f644 Mon Sep 17 00:00:00 2001 From: Mihai Sucan Date: Sat, 20 Nov 2010 15:53:14 -0400 Subject: [PATCH] Bug 600183 - Web Console: currentContext is always used in network logging code; f=ddahl r=sdwilsh a=blocking2.0 --- .../console/hudservice/HUDService.jsm | 43 ++++++++++----- .../hudservice/tests/browser/Makefile.in | 3 + .../browser_webconsole_bug_600183_charset.js | 55 +++++++++++++++++++ .../browser/test-bug-600183-charset.html | 9 +++ .../test-bug-600183-charset.html^headers^ | 1 + 5 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 toolkit/components/console/hudservice/tests/browser/browser_webconsole_bug_600183_charset.js create mode 100644 toolkit/components/console/hudservice/tests/browser/test-bug-600183-charset.html create mode 100644 toolkit/components/console/hudservice/tests/browser/test-bug-600183-charset.html^headers^ diff --git a/toolkit/components/console/hudservice/HUDService.jsm b/toolkit/components/console/hudservice/HUDService.jsm index d0757b49868..684d3035a25 100644 --- a/toolkit/components/console/hudservice/HUDService.jsm +++ b/toolkit/components/console/hudservice/HUDService.jsm @@ -243,7 +243,8 @@ ResponseListener.prototype = if (HUDService.saveRequestAndResponseBodies && this.receivedData.length < RESPONSE_BODY_LIMIT) { - this.receivedData += data; + this.receivedData += NetworkHelper. + convertToUnicode(data, aRequest.contentCharset); } binaryOutputStream.writeBytes(data, aCount); @@ -401,10 +402,22 @@ var NetworkHelper = */ convertToUnicode: function NH_convertToUnicode(aText, aCharset) { + if (!aCharset) { + return aText; + } + let conv = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. createInstance(Ci.nsIScriptableUnicodeConverter); - conv.charset = aCharset || "UTF-8"; - return conv.ConvertToUnicode(aText); + conv.charset = aCharset; + + try { + return conv.ConvertToUnicode(aText); + } + catch (ex) { + Cu.reportError("NH_convertToUnicode(aText, '" + + aCharset + "') exception: " + ex); + return aText; + } }, /** @@ -2471,7 +2484,10 @@ HUD_SERVICE.prototype = return; } - let msgObject, updatePanel = false; + hudId = httpActivity.hudId; + let msgObject = httpActivity.messageObject; + + let updatePanel = false; let data, textNode; // Store the time information for this activity subtype. httpActivity.timing[transCodes[aActivitySubtype]] = aTimestamp; @@ -2483,10 +2499,14 @@ HUD_SERVICE.prototype = break; } - let gBrowser = HUDService.currentContext().gBrowser; + let gBrowser = msgObject.messageNode.ownerDocument. + defaultView.gBrowser; + let HUD = HUDService.hudReferences[hudId]; + let browser = gBrowser. + getBrowserForDocument(HUD.contentDocument); - let sentBody = NetworkHelper.readPostTextFromRequest( - aChannel, gBrowser); + let sentBody = NetworkHelper. + readPostTextFromRequest(aChannel, browser); if (!sentBody) { // If the request URL is the same as the current page url, then // we can try to get the posted text from the page directly. @@ -2495,8 +2515,8 @@ HUD_SERVICE.prototype = // function is called for image requests as well but these // are not web pages and as such don't store the posted text // in the cache of the webpage. - if (httpActivity.url == gBrowser.contentWindow.location.href) { - sentBody = NetworkHelper.readPostTextFromPage(gBrowser); + if (httpActivity.url == browser.contentWindow.location.href) { + sentBody = NetworkHelper.readPostTextFromPage(browser); } if (!sentBody) { sentBody = ""; @@ -2506,8 +2526,6 @@ HUD_SERVICE.prototype = break; case activityDistributor.ACTIVITY_SUBTYPE_RESPONSE_HEADER: - msgObject = httpActivity.messageObject; - // aExtraStringData contains the response header. The first line // contains the response status (e.g. HTTP/1.1 200 OK). // @@ -2535,9 +2553,6 @@ HUD_SERVICE.prototype = break; case activityDistributor.ACTIVITY_SUBTYPE_TRANSACTION_CLOSE: - msgObject = httpActivity.messageObject; - - let timing = httpActivity.timing; let requestDuration = Math.round((timing.RESPONSE_COMPLETE - diff --git a/toolkit/components/console/hudservice/tests/browser/Makefile.in b/toolkit/components/console/hudservice/tests/browser/Makefile.in index c0f80946c7f..84d877c958f 100644 --- a/toolkit/components/console/hudservice/tests/browser/Makefile.in +++ b/toolkit/components/console/hudservice/tests/browser/Makefile.in @@ -104,6 +104,7 @@ _BROWSER_TEST_FILES = \ browser_webconsole_bug_601909_remember_height.js \ browser_webconsole_bug_613013_console_api_iframe.js \ browser_webconsole_bug_597756_reopen_closed_tab.js \ + browser_webconsole_bug_600183_charset.js \ head.js \ $(NULL) @@ -138,6 +139,8 @@ _BROWSER_TEST_PAGES = \ test-bug-597136-external-script-errors.js \ test-bug-613013-console-api-iframe.html \ test-bug-597756-reopen-closed-tab.html \ + test-bug-600183-charset.html \ + test-bug-600183-charset.html^headers^ \ $(NULL) libs:: $(_BROWSER_TEST_FILES) diff --git a/toolkit/components/console/hudservice/tests/browser/browser_webconsole_bug_600183_charset.js b/toolkit/components/console/hudservice/tests/browser/browser_webconsole_bug_600183_charset.js new file mode 100644 index 00000000000..bef7243631f --- /dev/null +++ b/toolkit/components/console/hudservice/tests/browser/browser_webconsole_bug_600183_charset.js @@ -0,0 +1,55 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + * + * Contributor(s): + * Mihai Șucan + * + * ***** END LICENSE BLOCK ***** */ + +const TEST_URI = "http://example.com/browser/toolkit/components/console/hudservice/tests/browser/test-bug-600183-charset.html"; + +let lastFinishedRequest = null; + +function requestDoneCallback(aHttpRequest) +{ + lastFinishedRequest = aHttpRequest; +} + +function performTest() +{ + ok(lastFinishedRequest, "charset test page was loaded and logged"); + + let body = lastFinishedRequest.response.body; + ok(body, "we have the response body"); + + let chars = "\u7684\u95ee\u5019!"; // 的问候! + isnot(body.indexOf("

" + chars + "

"), -1, + "found the chinese simplified string"); + + lastFinishedRequest = null; + HUDService.saveRequestAndResponseBodies = false; + HUDService.lastFinishedRequestCallback = null; + finishTest(); +} + +function test() +{ + addTab("data:text/html,Web Console - bug 600183 test"); + + browser.addEventListener("load", function(aEvent) { + browser.removeEventListener(aEvent.type, arguments.callee, true); + + waitForFocus(function() { + openConsole(); + + HUDService.saveRequestAndResponseBodies = true; + HUDService.lastFinishedRequestCallback = requestDoneCallback; + + browser.addEventListener("load", performTest, true); + content.location = TEST_URI; + }, content); + }, true); +} + diff --git a/toolkit/components/console/hudservice/tests/browser/test-bug-600183-charset.html b/toolkit/components/console/hudservice/tests/browser/test-bug-600183-charset.html new file mode 100644 index 00000000000..040490a6b0f --- /dev/null +++ b/toolkit/components/console/hudservice/tests/browser/test-bug-600183-charset.html @@ -0,0 +1,9 @@ + + + + Console HTTP test page (chinese) + + +

µÄÎʺò!

+ + diff --git a/toolkit/components/console/hudservice/tests/browser/test-bug-600183-charset.html^headers^ b/toolkit/components/console/hudservice/tests/browser/test-bug-600183-charset.html^headers^ new file mode 100644 index 00000000000..9f3e2302f5d --- /dev/null +++ b/toolkit/components/console/hudservice/tests/browser/test-bug-600183-charset.html^headers^ @@ -0,0 +1 @@ +Content-Type: text/html; charset=gb2312