bug 250375 allow necko consumers to append to the Cookie header

r+sr=darin
This commit is contained in:
cbiesinger%web.de 2005-08-13 11:46:11 +00:00
parent c5688dfef2
commit 89d68b8b89
5 changed files with 102 additions and 7 deletions

View File

@ -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();

View File

@ -243,6 +243,7 @@ private:
nsCString mContentTypeHint;
nsCString mContentCharsetHint;
nsCString mUserSetCookieHeader;
// cache specific data
nsCOMPtr<nsICacheEntryDescriptor> mCacheEntry;

View File

@ -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)

View File

@ -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();
}

View File

@ -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 {