Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

某些变态的WEB服务器不认全小写的请求头, 特别是Cookie这个字段. #669

Closed
wants to merge 2 commits into from

7 participants

@youxiachai

Please use english .....

@xqin

@youxiachai 楼上你看不懂?
即便不认识汉字, 看图片和diff,也知道是改的什么和为什么要改的.

PS: 本人英文不行, 如果你行的话,麻烦你翻译一下,造福大众.

@leukhin

@xqin ingenious troll.

@leukhin

@xqin @youxiachai BTW: code says it all.

@meteormatt

这bug报的:-1:

@sxyizhiren

request的cookie总觉得不够强大

@xqin

@sxyizhiren request的cookie使用的 cookie-jar 这个模块,而这个模块对Cookie的处理在get cookie时根本不区分 域名. 而有人给作者pull request了, 但作者好像很长时间没登陆github了,一直没合并请求.
Example:

var jar = new Jar;
var a = new Cookie('sid=1111; path=/foo/bar; domain=bar.com');
jar.add(a);

var cookies = jar.get({ url: 'http://foo.com/foo/bar' });

console.log(cookie.length);
console.dir(cookie[0]);

@youxiachai

cookie-jar author is @mikeal ..

@sxyizhiren

@xqin
它的cookie我也提了个issue,#670 .
另外我fork了分支,替换掉了cookie模块,已经能够实现domain的区分。https://github.com/sxyizhiren/request-5291 .

@xqin

@sxyizhiren 麻烦你帮忙把我中文翻译一下, request 的作者好像不管中文提交的合并请求.

这个人提的(他只提了Authorization )比我晚, 都合并了,我半年前都提了,不给我合并.
#804

而且我看目前最新的request.js 里面在一些setHeader中还是全小写的key.

@sxyizhiren

@xqin cookie模块已经改了,作者已经合并了新的cookie模块。区分域名的问题解决了,不过大小写的问题我不知道怎么样。
你的大小写问题的翻译:some web server don't accept cookie field if the word "cookie:" was all lowercase.

@xqin

@sxyizhiren 谢谢, 我刚刚从原来的分支上反合了一次代码,并更新为最新的,然后修改了 setHeader方法(Line:1035), 自动将HTTP请求中的header的name值转换为 Pascal-Case 风格. :)
就看作者合不合了.

@LoicMahieu
Collaborator

Speak english on Github please.

@LoicMahieu LoicMahieu closed this
@andyhu

The author says no :) 作者确定不会改这个问题,可以自己在本地patch,一样的
#830

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 27, 2013
  1. @xqin
Commits on Apr 1, 2014
  1. @xqin

    Merge remote-tracking branch 'upstream/master'

    xqin authored
    Conflicts:
    	request.js
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 21 deletions.
  1. +24 −21 request.js
View
45 request.js
@@ -190,11 +190,11 @@ Request.prototype.init = function (options) {
self.setHost = false
if (!self.hasHeader('host')) {
- self.setHeader('host', self.uri.hostname)
+ self.setHeader('Host', self.uri.hostname)
if (self.uri.port) {
if ( !(self.uri.port === 80 && self.uri.protocol === 'http:') &&
!(self.uri.port === 443 && self.uri.protocol === 'https:') )
- self.setHeader('host', self.getHeader('host') + (':'+self.uri.port) )
+ self.setHeader('Host', self.getHeader('host') + (':'+self.uri.port) )
}
self.setHost = true
}
@@ -293,7 +293,7 @@ Request.prototype.init = function (options) {
self.auth(authPieces[0], authPieces.slice(1).join(':'), true)
}
if (self.proxy && self.proxy.auth && !self.hasHeader('proxy-authorization') && !self.tunnel) {
- self.setHeader('proxy-authorization', "Basic " + toBase64(self.proxy.auth.split(':').map(function(item){ return querystring.unescape(item)}).join(':')))
+ self.setHeader('Proxy-Authorization', "Basic " + toBase64(self.proxy.auth.split(':').map(function(item){ return querystring.unescape(item)}).join(':')))
}
@@ -321,7 +321,7 @@ Request.prototype.init = function (options) {
length = self.body.length
}
if (length) {
- if (!self.hasHeader('content-length')) self.setHeader('content-length', length)
+ if (!self.hasHeader('content-length')) self.setHeader('Content-Length', length)
} else {
throw new Error('Argument error, options.body.')
}
@@ -367,7 +367,7 @@ Request.prototype.init = function (options) {
if (self.ntick && self._started) throw new Error("You cannot pipe to this stream after the outbound request has started.")
self.src = src
if (isReadStream(src)) {
- if (!self.hasHeader('content-type')) self.setHeader('content-type', mime.lookup(src.path))
+ if (!self.hasHeader('content-type')) self.setHeader('Content-Type', mime.lookup(src.path))
} else {
if (src.headers) {
for (var i in src.headers) {
@@ -377,7 +377,7 @@ Request.prototype.init = function (options) {
}
}
if (self._json && !self.hasHeader('content-type'))
- self.setHeader('content-type', 'application/json')
+ self.setHeader('Content-Type', 'application/json')
if (src.method && !self.explicitMethod) {
self.method = src.method
}
@@ -395,7 +395,7 @@ Request.prototype.init = function (options) {
self.setHeaders(self._form.getHeaders())
try {
var length = self._form.getLengthSync()
- self.setHeader('content-length', length)
+ self.setHeader('Content-Length', length)
} catch(e){}
self._form.pipe(self)
}
@@ -413,7 +413,7 @@ Request.prototype.init = function (options) {
self.requestBodyStream.pipe(self)
} else if (!self.src) {
if (self.method !== 'GET' && typeof self.method !== 'undefined') {
- self.setHeader('content-length', 0)
+ self.setHeader('Content-Length', 0)
}
self.end()
}
@@ -658,7 +658,7 @@ Request.prototype.start = function () {
self.href = self.uri.href
if (self.src && self.src.stat && self.src.stat.size && !self.hasHeader('content-length')) {
- self.setHeader('content-length', self.src.stat.size)
+ self.setHeader('Content-Length', self.src.stat.size)
}
if (self._aws) {
self.aws(self._aws, true)
@@ -839,7 +839,7 @@ Request.prototype.onResponse = function (response) {
}
}
authHeader = 'Digest ' + authHeader.join(', ')
- self.setHeader('authorization', authHeader)
+ self.setHeader('Authorization', authHeader)
self._sentAuth = true
redirectTo = self.uri
@@ -1033,6 +1033,9 @@ Request.prototype.pipeDest = function (dest) {
// Composable API
Request.prototype.setHeader = function (name, value, clobber) {
+ name = name.replace(/(^.|-.)/g, function(a, b){//outputs headers in Pascal-Case.
+ return b.toUpperCase();
+ });
if (clobber === undefined) clobber = true
if (clobber || !this.hasHeader(name)) this.headers[name] = value
else this.headers[this.hasHeader(name)] += ',' + value
@@ -1076,7 +1079,7 @@ Request.prototype.qs = function (q, clobber) {
}
Request.prototype.form = function (form) {
if (form) {
- this.setHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8')
+ this.setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8')
this.body = qs.stringify(form).toString('utf8')
return this
}
@@ -1089,10 +1092,10 @@ Request.prototype.multipart = function (multipart) {
self.body = []
if (!self.hasHeader('content-type')) {
- self.setHeader('content-type', 'multipart/related; boundary=' + self.boundary)
+ self.setHeader('Content-Type', 'multipart/related; boundary=' + self.boundary)
} else {
var headerName = self.hasHeader('content-type');
- self.setHeader(headerName, self.headers[headerName].split(';')[0] + '; boundary=' + self.boundary)
+ self.setHeader('Content-Type', self.headers[headerName].split(';')[0] + '; boundary=' + self.boundary)
}
if (!multipart.forEach) throw new Error('Argument error, options.multipart.')
@@ -1120,19 +1123,19 @@ Request.prototype.multipart = function (multipart) {
Request.prototype.json = function (val) {
var self = this
- if (!self.hasHeader('accept')) self.setHeader('accept', 'application/json')
+ if (!self.hasHeader('accept')) self.setHeader('Accept', 'application/json')
this._json = true
if (typeof val === 'boolean') {
if (typeof this.body === 'object') {
this.body = safeStringify(this.body)
if (!self.hasHeader('content-type'))
- self.setHeader('content-type', 'application/json')
+ self.setHeader('Content-Type', 'application/json')
}
} else {
this.body = safeStringify(val)
if (!self.hasHeader('content-type'))
- self.setHeader('content-type', 'application/json')
+ self.setHeader('Content-Type', 'application/json')
}
return this
@@ -1159,7 +1162,7 @@ Request.prototype.auth = function (user, pass, sendImmediately) {
this._hasAuth = true
var header = typeof pass !== 'undefined' ? user + ':' + pass : user
if (sendImmediately || typeof sendImmediately == 'undefined') {
- this.setHeader('authorization', 'Basic ' + toBase64(header))
+ this.setHeader('Authorization', 'Basic ' + toBase64(header))
this._sentAuth = true
}
return this
@@ -1170,7 +1173,7 @@ Request.prototype.aws = function (opts, now) {
return this
}
var date = new Date()
- this.setHeader('date', date.toUTCString())
+ this.setHeader('Date', date.toUTCString())
var auth =
{ key: opts.key
, secret: opts.secret
@@ -1190,7 +1193,7 @@ Request.prototype.aws = function (opts, now) {
auth.resource = '/'
}
auth.resource = aws.canonicalizeResource(auth.resource)
- this.setHeader('authorization', aws.authorization(auth))
+ this.setHeader('Authorization', aws.authorization(auth))
return this
}
@@ -1284,9 +1287,9 @@ Request.prototype.jar = function (jar) {
if (cookies && cookies.length) {
if (this.originalCookieHeader) {
// Don't overwrite existing Cookie header
- this.setHeader('cookie', this.originalCookieHeader + '; ' + cookies)
+ this.setHeader('Cookie', this.originalCookieHeader + '; ' + cookies)
} else {
- this.setHeader('cookie', cookies)
+ this.setHeader('Cookie', cookies)
}
}
this._jar = jar
Something went wrong with that request. Please try again.