diff --git a/content/base/src/EventSource.cpp b/content/base/src/EventSource.cpp index ffa39ec50e80..303c52d17646 100644 --- a/content/base/src/EventSource.cpp +++ b/content/base/src/EventSource.cpp @@ -61,6 +61,7 @@ EventSource::EventSource() : mGoingToDispatchAllMessages(false), mWithCredentials(false), mWaitingForOnStopRequest(false), + mInterrupted(false), mLastConvertionResult(NS_OK), mReadyState(CONNECTING), mScriptLine(0), @@ -341,6 +342,16 @@ EventSource::OnStartRequest(nsIRequest *aRequest, rv = httpChannel->GetRequestSucceeded(&requestSucceeded); NS_ENSURE_SUCCESS(rv, rv); + uint32_t status; + rv = httpChannel->GetResponseStatus(&status); + NS_ENSURE_SUCCESS(rv, rv); + + if (status == 204) { + mInterrupted = true; + DispatchFailConnection(); + return NS_ERROR_ABORT; + } + nsAutoCString contentType; rv = httpChannel->GetContentType(contentType); NS_ENSURE_SUCCESS(rv, rv); @@ -970,7 +981,7 @@ EventSource::ConsoleError() NS_ConvertUTF8toUTF16 specUTF16(targetSpec); const PRUnichar *formatStrings[] = { specUTF16.get() }; - if (mReadyState == CONNECTING) { + if (mReadyState == CONNECTING && !mInterrupted) { rv = PrintErrorOnConsole("chrome://global/locale/appstrings.properties", NS_LITERAL_STRING("connectionFailure").get(), formatStrings, ArrayLength(formatStrings)); diff --git a/content/base/src/EventSource.h b/content/base/src/EventSource.h index 57bfcc5bc8ad..c9231f41a75f 100644 --- a/content/base/src/EventSource.h +++ b/content/base/src/EventSource.h @@ -227,6 +227,7 @@ protected: bool mGoingToDispatchAllMessages; bool mWithCredentials; bool mWaitingForOnStopRequest; + bool mInterrupted; // used while reading the input streams nsCOMPtr mUnicodeDecoder; diff --git a/content/base/test/Makefile.in b/content/base/test/Makefile.in index db4fd3a2c1e7..46ddc4dfed67 100644 --- a/content/base/test/Makefile.in +++ b/content/base/test/Makefile.in @@ -493,6 +493,8 @@ MOCHITEST_FILES_B = \ accesscontrol.resource^headers^ \ invalid_accesscontrol.resource \ invalid_accesscontrol.resource^headers^ \ + file_bug869432.eventsource \ + file_bug869432.eventsource^headers^ \ $(NULL) MOCHITEST_FILES_C= \ diff --git a/content/base/test/file_bug869432.eventsource b/content/base/test/file_bug869432.eventsource new file mode 100644 index 000000000000..de92d4dd8628 --- /dev/null +++ b/content/base/test/file_bug869432.eventsource @@ -0,0 +1,4 @@ +retry:500 +data: data + + diff --git a/content/base/test/file_bug869432.eventsource^headers^ b/content/base/test/file_bug869432.eventsource^headers^ new file mode 100644 index 000000000000..b8db77c582bc --- /dev/null +++ b/content/base/test/file_bug869432.eventsource^headers^ @@ -0,0 +1,3 @@ +HTTP 304 NO CONTENT (CLOSE) +Content-Type: text/event-stream +Cache-Control: no-cache, must-revalidate \ No newline at end of file diff --git a/content/base/test/test_bug338583.html b/content/base/test/test_bug338583.html index d90c29e9691b..6a69ace086b2 100644 --- a/content/base/test/test_bug338583.html +++ b/content/base/test/test_bug338583.html @@ -53,7 +53,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=338583 function runAllTests() { // these tests run asynchronously, and they will take 8000 ms var all_tests = [ - doTest1, doTest1_e, doTest2, doTest3, doTest3_b, doTest3_c, doTest3_d, + doTest1, doTest1_e, doTest1_f, doTest2, doTest3, doTest3_b, doTest3_c, doTest3_d, doTest3_e, doTest3_f, doTest3_g, doTest3_h, doTest4, doTest4_b, doTest5, doTest5_b, doTest5_c, doTest5_e, doTest6, doTest7 ]; @@ -92,7 +92,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=338583 var domBranch; var oldPrefVal; - var gEventSourceObj1 = null, gEventSourceObj1_e; + var gEventSourceObj1 = null, gEventSourceObj1_e, gEventSourceObj1_f; var gEventSourceObj2 = null; var gEventSourceObj3_a = null, gEventSourceObj3_b = null, gEventSourceObj3_c = null, gEventSourceObj3_d = null, @@ -121,6 +121,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=338583 // c) the close method (we expect readyState == CLOSED) // d) the close method (we expect no message events anymore) // e) use the default for withCredentials when passing dictionary arguments that don't explicitly set it +// f) if a 204 HTTP response closes (interrupts) connections. See bug 869432. function doTest1(test_id) { gEventSourceObj1 = new EventSource("eventsource.resource"); @@ -181,6 +182,30 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=338583 setTestHasFinished(test_id); } + function doTest1_f(test_id) { + var called_on_error = false; + + gEventSourceObj1_f = new EventSource("file_bug869432.eventsource"); + gEventSourceObj1_f.onopen = function(e) { + ok(false, "Test 1.f failed: onopen was called"); + }; + gEventSourceObj1_f.onmessage = function(e) { + ok(false, "Test 1.f failed: onmessage was called"); + }; + gEventSourceObj1_f.onerror = function(e) { + if (called_on_error) { + ok(false, "Test 1.f failed: onerror was called twice"); + } + called_on_error = true; + ok(gEventSourceObj1_f.readyState == 2, "Test 1.f failed: onerror was called with readyState = " + gEventSourceObj1_f.readyState); + }; + + setTimeout(function() { // just to clean... + ok(called_on_error, "Test 1.f failed: onerror was not called"); + setTestHasFinished(test_id); + }, parseInt(5000*stress_factor)); + } + // in order to test (2) // a) set a eventsource that give the dom events messages // b) expect trusted events