From 89d68b8b892b2e920779533771315e1b7d768efb Mon Sep 17 00:00:00 2001 From: "cbiesinger%web.de" Date: Sat, 13 Aug 2005 11:46:11 +0000 Subject: [PATCH] bug 250375 allow necko consumers to append to the Cookie header r+sr=darin --- netwerk/protocol/http/src/nsHttpChannel.cpp | 9 +++ netwerk/protocol/http/src/nsHttpChannel.h | 1 + netwerk/test/Makefile.in | 1 + netwerk/test/unit/test_cookie_header.js | 78 +++++++++++++++++++++ netwerk/test/unit/test_http_headers.js | 20 ++++-- 5 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 netwerk/test/unit/test_cookie_header.js diff --git a/netwerk/protocol/http/src/nsHttpChannel.cpp b/netwerk/protocol/http/src/nsHttpChannel.cpp index 73e493623033..42755541fd32 100644 --- a/netwerk/protocol/http/src/nsHttpChannel.cpp +++ b/netwerk/protocol/http/src/nsHttpChannel.cpp @@ -663,6 +663,10 @@ nsHttpChannel::AddCookiesToRequest() mDocumentURI ? mDocumentURI : mOriginalURI, this, getter_Copies(cookie)); + if (cookie.IsEmpty()) + cookie = mUserSetCookieHeader; + else if (!mUserSetCookieHeader.IsEmpty()) + cookie.Append(NS_LITERAL_CSTRING("; ") + mUserSetCookieHeader); // overwrite any existing cookie headers. be sure to clear any // existing cookies if we have no cookies to set or if the cookie @@ -3316,6 +3320,11 @@ nsHttpChannel::AsyncOpen(nsIStreamListener *listener, nsISupports *context) if (NS_FAILED(rv)) return rv; + // Remember the cookie header that was set, if any + const char* cookieHeader = mRequestHead.PeekHeader(nsHttp::Cookie); + if (cookieHeader) + mUserSetCookieHeader = cookieHeader; + // fetch cookies, and add them to the request header AddCookiesToRequest(); diff --git a/netwerk/protocol/http/src/nsHttpChannel.h b/netwerk/protocol/http/src/nsHttpChannel.h index 150a335b7377..67647f02686a 100644 --- a/netwerk/protocol/http/src/nsHttpChannel.h +++ b/netwerk/protocol/http/src/nsHttpChannel.h @@ -243,6 +243,7 @@ private: nsCString mContentTypeHint; nsCString mContentCharsetHint; + nsCString mUserSetCookieHeader; // cache specific data nsCOMPtr mCacheEntry; diff --git a/netwerk/test/Makefile.in b/netwerk/test/Makefile.in index 5b8fd6bfeeb9..599bfd1354a4 100644 --- a/netwerk/test/Makefile.in +++ b/netwerk/test/Makefile.in @@ -98,6 +98,7 @@ _UNIT_FILES = unit/test_all.sh \ unit/tail.js \ unit/test_protocolproxyservice.js \ unit/test_http_headers.js \ + unit/test_cookie_header.js \ unit/test_parse_content_type.js \ $(NULL) libs:: $(_UNIT_FILES) diff --git a/netwerk/test/unit/test_cookie_header.js b/netwerk/test/unit/test_cookie_header.js new file mode 100644 index 000000000000..c074b556d45e --- /dev/null +++ b/netwerk/test/unit/test_cookie_header.js @@ -0,0 +1,78 @@ +function check_request_header(chan, name, value) { + var chanValue; + try { + chanValue = chan.getRequestHeader(name); + } catch (e) { + do_throw("Expected to find header '" + name + "' but didn't find it"); + } + dump("Value for header '" + name + "' is '" + chanValue + "'\n"); + do_check_eq(chanValue, value); +} + +var cookieVal = "C1=V1"; + +var listener = { + onStartRequest: function test_onStartR(request, ctx) { + try { + var chan = request.QueryInterface(Components.interfaces.nsIChannel); + check_request_header(chan, "Cookie", cookieVal); + } catch (e) { + do_throw("Unexpected exception: " + e); + } + + throw Components.results.NS_ERROR_ABORT; + }, + + onDataAvailable: function test_ODA() { + throw Components.results.NS_ERROR_UNEXPECTED; + }, + + onStopRequest: function test_onStopR(request, ctx, status) { + if (this._iteration == 1 && Components.isSuccessCode(status)) + run_test_continued(); + do_test_finished(); + }, + + _iteration: 1 +}; + +function makeChan() { + var ios = Components.classes["@mozilla.org/network/io-service;1"] + .getService(Components.interfaces.nsIIOService); + var chan = ios.newChannel("http://www.mozilla.org/", null, null) + .QueryInterface(Components.interfaces.nsIHttpChannel); + + return chan; +} + +function run_test() { + dump("Note: This test needs a network connection\n"); + + var chan = makeChan(); + + chan.setRequestHeader("Cookie", cookieVal, false); + + chan.asyncOpen(listener, null); + + do_test_pending(); +} + +function run_test_continued() { + var chan = makeChan(); + + var cookServ = Components.classes["@mozilla.org/cookieService;1"] + .getService(Components.interfaces.nsICookieService); + var cookie2 = "C2=V2"; + cookServ.setCookieString(chan.URI, null, cookie2, chan); + chan.setRequestHeader("Cookie", cookieVal, false); + + // We expect that the setRequestHeader overrides the + // automatically-added one, so insert cookie2 in front + cookieVal = cookie2 + "; " + cookieVal; + + listener._iteration++; + chan.asyncOpen(listener, null); + + do_test_pending(); +} + diff --git a/netwerk/test/unit/test_http_headers.js b/netwerk/test/unit/test_http_headers.js index 94ee51e9bece..cb5ed4cca834 100644 --- a/netwerk/test/unit/test_http_headers.js +++ b/netwerk/test/unit/test_http_headers.js @@ -1,5 +1,11 @@ -function check_header(chan, name, value) { - do_check_eq(chan.getRequestHeader(name), value); +function check_request_header(chan, name, value) { + var chanValue; + try { + chanValue = chan.getRequestHeader(name); + } catch (e) { + do_throw("Expected to find header '" + name + "' but didn't find it"); + } + do_check_eq(chanValue, value); } function run_test() { @@ -8,20 +14,20 @@ function run_test() { var chan = ios.newChannel("http://www.mozilla.org/", null, null) .QueryInterface(Components.interfaces.nsIHttpChannel); - check_header(chan, "host", "www.mozilla.org"); - check_header(chan, "Host", "www.mozilla.org"); + check_request_header(chan, "host", "www.mozilla.org"); + check_request_header(chan, "Host", "www.mozilla.org"); chan.setRequestHeader("foopy", "bar", false); - check_header(chan, "foopy", "bar"); + check_request_header(chan, "foopy", "bar"); chan.setRequestHeader("foopy", "baz", true); - check_header(chan, "foopy", "bar, baz"); + check_request_header(chan, "foopy", "bar, baz"); for (var i = 0; i < 100; ++i) chan.setRequestHeader("foopy" + i, i, false); for (var i = 0; i < 100; ++i) - check_header(chan, "foopy" + i, i); + check_request_header(chan, "foopy" + i, i); var x = false; try {