Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix cookie parsing problem and location redirection with '//' at the beginning of location header #87

Open
wants to merge 3 commits into from

2 participants

@yyfrankyy

fix #85 and #86

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')
@tj
tj added a note

this line is mega-long, this should be fine this.connection.encrypted || forwardedstuffhere, break it into two vars so it's not so crazy.
var fwd = req.headers && req.... this.connection.encrypted || fwd == 'https'

Thanks for mention that, I committed a fix here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 2 deletions.
  1. +25 −0 lib/browser.js
  2. +2 −2 lib/cookie/index.js
View
25 lib/browser.js
@@ -232,6 +232,11 @@ Browser.prototype.request = function(method, path, options, fn, saveHistory){
// Redirect
if (status >= 300 && status < 400) {
+ if (0 == res.headers.location.indexOf('//')) {
+ 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
, uri = url.parse(location)
, path = uri.pathname + (uri.search || '');
@@ -313,6 +318,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;
@@ -332,6 +341,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;
@@ -351,6 +364,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;
@@ -370,6 +387,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;
@@ -389,6 +410,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;
View
4 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);
@@ -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 : '/');
};
/**
Something went wrong with that request. Please try again.