Current Folder
This commit is contained in:
1 parent
8b3382289b
commit
cfcada880d
4379 files changed
No files matched your search
-11
@@ -1,11 +0,0 @@
|
||||
var parse = require('./parse-headers')
|
||||
|
||||
, headers = [
|
||||
'Date: Sun, 17 Aug 2014 16:24:52 GMT'
|
||||
, 'Content-Type: text/html; charset=utf-8'
|
||||
, 'Transfer-Encoding: chunked'
|
||||
, 'X-Custom-Header: beep'
|
||||
, 'X-Custom-Header: boop'
|
||||
].join('\n')
|
||||
|
||||
console.log(parse(headers))
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
{
|
||||
"name": "parse-headers",
|
||||
"version": "2.0.3",
|
||||
"description": "Parse http headers, works with browserify/xhr",
|
||||
"main": "parse-headers.js",
|
||||
"scripts": {
|
||||
"test": "node test.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/kesla/parse-headers.git"
|
||||
},
|
||||
"keywords": [
|
||||
"http",
|
||||
"headers"
|
||||
],
|
||||
"author": "David Björklund <david.bjorklund@gmail.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/kesla/parse-headers/issues"
|
||||
},
|
||||
"homepage": "https://github.com/kesla/parse-headers",
|
||||
"devDependencies": {
|
||||
"tape": "^4.10.1"
|
||||
},
|
||||
"dependencies": {}
|
||||
|
||||
,"_resolved": "https://registry.npmjs.org/parse-headers/-/parse-headers-2.0.3.tgz"
|
||||
,"_integrity": "sha512-QhhZ+DCCit2Coi2vmAKbq5RGTRcQUOE2+REgv8vdyu7MnYx2eZztegqtTx99TZ86GTIwqiy3+4nQTWZ2tgmdCA=="
|
||||
,"_from": "parse-headers@^2.0.0"
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
var trim = function(string) {
|
||||
return string.replace(/^\s+|\s+$/g, '');
|
||||
}
|
||||
, isArray = function(arg) {
|
||||
return Object.prototype.toString.call(arg) === '[object Array]';
|
||||
}
|
||||
|
||||
module.exports = function (headers) {
|
||||
if (!headers)
|
||||
return {}
|
||||
|
||||
var result = {}
|
||||
|
||||
var headersArr = trim(headers).split('\n')
|
||||
|
||||
for (var i = 0; i < headersArr.length; i++) {
|
||||
var row = headersArr[i]
|
||||
var index = row.indexOf(':')
|
||||
, key = trim(row.slice(0, index)).toLowerCase()
|
||||
, value = trim(row.slice(index + 1))
|
||||
|
||||
if (typeof(result[key]) === 'undefined') {
|
||||
result[key] = value
|
||||
} else if (isArray(result[key])) {
|
||||
result[key].push(value)
|
||||
} else {
|
||||
result[key] = [ result[key], value ]
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
-70
@@ -1,70 +0,0 @@
|
||||
var test = require('tape')
|
||||
, parse = require('./parse-headers')
|
||||
|
||||
, headers1 = [
|
||||
''
|
||||
, 'Date: Sun, 17 Aug 2014 16:24:52 GMT'
|
||||
, 'Content-Type: text/html; charset=utf-8'
|
||||
, 'Transfer-Encoding: chunked'
|
||||
, ''
|
||||
]
|
||||
, headers2 = [
|
||||
''
|
||||
, 'Date: Sun, 17 Aug 2014 16:24:52 GMT'
|
||||
, 'Content-Type: text/html; charset=utf-8'
|
||||
, 'Transfer-Encoding: chunked'
|
||||
, 'Set-Cookie: Foo'
|
||||
, 'set-Cookie: bar'
|
||||
, 'set-cookie: bong'
|
||||
]
|
||||
|
||||
test('sanity check', function (t) {
|
||||
|
||||
t.deepEqual(parse(), {})
|
||||
t.deepEqual(parse(''), {})
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('simple', function (t) {
|
||||
t.deepEqual(
|
||||
parse(headers1.join('\r\n'))
|
||||
, {
|
||||
date: 'Sun, 17 Aug 2014 16:24:52 GMT'
|
||||
, 'content-type': 'text/html; charset=utf-8'
|
||||
, 'transfer-encoding': 'chunked'
|
||||
}
|
||||
)
|
||||
t.deepEqual(
|
||||
parse(headers1.join('\n'))
|
||||
, {
|
||||
date: 'Sun, 17 Aug 2014 16:24:52 GMT'
|
||||
, 'content-type': 'text/html; charset=utf-8'
|
||||
, 'transfer-encoding': 'chunked'
|
||||
}
|
||||
)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('duplicate keys', function (t) {
|
||||
t.deepEqual(
|
||||
parse(headers2.join('\r\n'))
|
||||
, {
|
||||
date: 'Sun, 17 Aug 2014 16:24:52 GMT'
|
||||
, 'content-type': 'text/html; charset=utf-8'
|
||||
, 'transfer-encoding': 'chunked'
|
||||
, 'set-cookie': [ 'Foo', 'bar', 'bong' ]
|
||||
}
|
||||
)
|
||||
t.deepEqual(
|
||||
parse(headers2.join('\n'))
|
||||
, {
|
||||
date: 'Sun, 17 Aug 2014 16:24:52 GMT'
|
||||
, 'content-type': 'text/html; charset=utf-8'
|
||||
, 'transfer-encoding': 'chunked'
|
||||
, 'set-cookie': [ 'Foo', 'bar', 'bong' ]
|
||||
}
|
||||
)
|
||||
|
||||
t.end()
|
||||
})
|
||||
Reference in new issue
Block a user