From 3827864829a48f8e987b905af11e3625fe0adcb3 Mon Sep 17 00:00:00 2001 From: yyfrankyy Date: Tue, 18 Sep 2012 23:41:20 +0800 Subject: [PATCH 1/3] fix #85 and fix #86 --- lib/browser.js | 4 ++++ lib/cookie/index.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/browser.js b/lib/browser.js index 15c3a34..51f6520 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -232,6 +232,10 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){ // Redirect if (status >= 300 && status < 400) { + if (0 == res.headers.location.indexOf('//')) { + var isHttps = req.connection.server instanceof require('tls').Server || (req.headers && req.headers['x-forwarded-proto'] == 'https') + res.headers.location = (isHttps ? 'https:' : 'http:') + res.headers.location + } var location = res.headers.location , uri = url.parse(location) , path = uri.pathname + (uri.search || ''); diff --git a/lib/cookie/index.js b/lib/cookie/index.js index 2fe9907..3cda489 100644 --- a/lib/cookie/index.js +++ b/lib/cookie/index.js @@ -28,7 +28,7 @@ var Cookie = exports = module.exports = function Cookie(str, req) { // Map the key/val pairs str.split(/ *; */).reduce(function(obj, pair){ pair = pair.split(/ *= */); - obj[pair[0]] = pair[1] || true; + obj[pair[0].toLowerCase()] = pair[1] || true; return obj; }, this); From 8449e280cdea42fd6376db1327ad991e8f0acbb8 Mon Sep 17 00:00:00 2001 From: yyfrankyy Date: Wed, 19 Sep 2012 00:47:19 +0800 Subject: [PATCH 2/3] prevent cookie w/o Path=/ cause exception and fix #88 --- lib/browser.js | 20 ++++++++++++++++++++ lib/cookie/index.js | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/browser.js b/lib/browser.js index 51f6520..bdab7af 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -317,6 +317,10 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){ Browser.prototype.get = Browser.prototype.visit = Browser.prototype.open = function(path, options, fn, saveHistory){ + // relative path + if (0 == path.indexOf('?')) { + path = url.parse(this.history[this.history.length - 1]).pathname + path; + } if ('function' == typeof options) { saveHistory = fn; fn = options; @@ -336,6 +340,10 @@ Browser.prototype.open = function(path, options, fn, saveHistory){ */ Browser.prototype.head = function(path, options, fn, saveHistory){ + // relative path + if (0 == path.indexOf('?')) { + path = url.parse(this.history[this.history.length - 1]).pathname + path; + } if ('function' == typeof options) { saveHistory = fn; fn = options; @@ -355,6 +363,10 @@ Browser.prototype.head = function(path, options, fn, saveHistory){ */ Browser.prototype.post = function(path, options, fn, saveHistory){ + // relative path + if (0 == path.indexOf('?')) { + path = url.parse(this.history[this.history.length - 1]).pathname + path; + } if ('function' == typeof options) { saveHistory = fn; fn = options; @@ -374,6 +386,10 @@ Browser.prototype.post = function(path, options, fn, saveHistory){ */ Browser.prototype.put = function(path, options, fn, saveHistory){ + // relative path + if (0 == path.indexOf('?')) { + path = url.parse(this.history[this.history.length - 1]).pathname + path; + } if ('function' == typeof options) { saveHistory = fn; fn = options; @@ -393,6 +409,10 @@ Browser.prototype.put = function(path, options, fn, saveHistory){ */ Browser.prototype.delete = function(path, options, fn, saveHistory){ + // relative path + if (0 == path.indexOf('?')) { + path = url.parse(this.history[this.history.length - 1]).pathname + path; + } if ('function' == typeof options) { saveHistory = fn; fn = options; diff --git a/lib/cookie/index.js b/lib/cookie/index.js index 3cda489..17a3ef0 100644 --- a/lib/cookie/index.js +++ b/lib/cookie/index.js @@ -43,7 +43,7 @@ var Cookie = exports = module.exports = function Cookie(str, req) { // Default or trim path this.path = this.path ? this.path.trim() - : url.parse(req.url).pathname; + : (req ? url.parse(req.url).pathname : '/'); }; /** From d8ba9550c5535eaaf03174e02bdd5945a0b31919 Mon Sep 17 00:00:00 2001 From: yyfrankyy Date: Wed, 19 Sep 2012 00:53:37 +0800 Subject: [PATCH 3/3] use this.connection.encrypted for detecting https connection --- lib/browser.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/browser.js b/lib/browser.js index bdab7af..4107888 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -233,7 +233,8 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){ // Redirect if (status >= 300 && status < 400) { if (0 == res.headers.location.indexOf('//')) { - var isHttps = req.connection.server instanceof require('tls').Server || (req.headers && req.headers['x-forwarded-proto'] == 'https') + var fwd = this.headers && this.headers['x-forwarded-proto'] == 'https'; + var isHttps = this.connection.encrypted || fwd; res.headers.location = (isHttps ? 'https:' : 'http:') + res.headers.location } var location = res.headers.location