Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

http-headers

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-headers - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

36

index.js

@@ -1,19 +0,29 @@

'use strict';
'use strict'
var nl = /\r?\n/;
var nl = /\r\n|\n|\r/
module.exports = function (headers) {
if (typeof headers === 'object')
headers = headers._header;
module.exports = function (str) {
str = normalize(str)
var result = {};
// remove leading start-line (e.g. HTTP/1.1 200 OK)
var lines = str.split(nl)
if (!~lines[0].indexOf(':')) lines = lines.slice(1)
if (!headers) return result;
return parse(lines)
}
headers.trim().split(nl).slice(1).forEach(function (header) {
var index = header.indexOf(':');
result[header.substr(0, index).toLowerCase()] = header.substr(index+1).trim();
});
function normalize (str) {
if (str && str._header) str = str._header // extra headers from http.ServerResponse object
if (!str || typeof str.toString !== 'function') return ''
return str.toString().trim()
}
return result;
};
function parse (headers) {
var result = {}
headers.some(function (header) {
if (!header) return true
var index = header.indexOf(':')
result[header.substr(0, index).toLowerCase()] = header.substr(index + 1).trim()
})
return result
}
{
"name": "http-headers",
"version": "0.1.0",
"version": "1.0.0",
"description": "Parse http headers",
"main": "index.js",
"scripts": {
"test": "node test.js"
"test": "standard && tape test.js"
},

@@ -13,2 +13,6 @@ "repository": {

},
"devDependencies": {
"standard": "^5.1.1",
"tape": "^4.2.0"
},
"keywords": [

@@ -24,3 +28,3 @@ "http",

],
"author": "Thomas Watson Steen <w@tson.dk>",
"author": "Thomas Watson Steen <w@tson.dk> (https://twitter.com/wa7son)",
"license": "MIT",

@@ -30,3 +34,7 @@ "bugs": {

},
"homepage": "https://github.com/watson/http-headers"
"homepage": "https://github.com/watson/http-headers",
"coordinates": [
55.687573956701705,
12.595934558563272
]
}
# http-headers
[![Build Status](https://travis-ci.org/watson/http-headers.png)](https://travis-ci.org/watson/http-headers)
[![Build status](https://travis-ci.org/watson/http-headers.svg?branch=master)](https://travis-ci.org/watson/http-headers)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
HTTP header string parser.
Extract and parse headers from an HTTP request or reponse.

@@ -15,2 +16,3 @@ Converts:

Hello World
```

@@ -20,3 +22,3 @@

```javascript
```js
{ date: 'Tue, 10 Jun 2014 07:19:27 GMT',

@@ -27,4 +29,44 @@ connection: 'keep-alive',

## Why?
## Installation
```
npm install http-headers
```
## Usage
```js
var net = require('net')
var httpHeaders = require('http-headers')
// create TCP server
net.createServer(function (c) {
var buffers = []
c.on('data', buffers.push.bind(buffers))
c.on('end', function () {
var data = Buffer.concat(buffers)
// parse incoming data as an HTTP request and extra HTTP headers
console.log('Request headers:', httpHeaders(data))
})
}).listen(8080)
```
### `http.ServerReponse` support
If given an instance of `http.ServerResponse`, the reponse headers is
automatically extracted, parsed and returned:
```js
var http = require('http')
var httpHeaders = require('http-headers')
http.createServer(function (req, res) {
res.end('Hello World')
console.log('Response headers:', httpHeaders(res))
}).listen(8080)
```
#### Why?
If you've ever needed to log or in another way access the headers sent

@@ -42,21 +84,15 @@ to the client on a `http.ServerResponse` in Node.js, you know it's not

## Installation
## API
The http-headers module exposes a single parser function:
```
npm install http-headers
httpHeaders([ string | buffer | http.ServerReponse ])
```
## Usage
The module returns a JavaScript object with each element representing a
parsed header. All header names are lowercased.
```javascript
var httpHeaders = require('http-headers');
http.createServer(function (req, res) {
res.end('Hello World');
console.log('The headers sent to the client was:', httpHeaders(res));
});
```
## License
MIT

@@ -1,21 +0,73 @@

'use strict';
'use strict'
var assert = require('assert');
var http = require('http');
var httpHeaders = require('./index');
var test = require('tape')
var http = require('http')
var httpHeaders = require('./')
var hStr = 'HTTP/1.1 200 OK\r\nDate: Tue, 10 Jun 2014 07:29:20 GMT\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n';
var hObj = {
var startLineStr = 'HTTP/1.1 200 OK\r\n'
var headersStr = 'Date: Tue, 10 Jun 2014 07:29:20 GMT\r\nConnection: keep-alive\r\nTransfer-Encoding: chunked\r\n\r\n'
var fullResponseStr = startLineStr + headersStr + 'Hello: World'
var result = {
date: 'Tue, 10 Jun 2014 07:29:20 GMT',
connection: 'keep-alive',
'transfer-encoding': 'chunked'
};
}
assert.deepEqual(httpHeaders(), {});
assert.deepEqual(httpHeaders(''), {});
assert.deepEqual(httpHeaders({}), {});
assert.deepEqual(httpHeaders(new http.ServerResponse({})), {});
assert.deepEqual(httpHeaders({ _header: undefined }), {});
assert.deepEqual(httpHeaders({ _header: '' }), {});
assert.deepEqual(httpHeaders({ _header: hStr }), hObj);
assert.deepEqual(httpHeaders(hStr), hObj);
test('no argument', function (t) {
t.deepEqual(httpHeaders(), {})
t.end()
})
test('empty string', function (t) {
t.deepEqual(httpHeaders(''), {})
t.end()
})
test('empty object', function (t) {
t.deepEqual(httpHeaders({}), {})
t.end()
})
test('buffer', function (t) {
t.deepEqual(httpHeaders(new Buffer(headersStr)), result)
t.end()
})
test('start-line + header', function (t) {
t.deepEqual(httpHeaders(startLineStr + headersStr), result)
t.end()
})
test('headers only', function (t) {
t.deepEqual(httpHeaders(headersStr), result)
t.end()
})
test('full http response', function (t) {
t.deepEqual(httpHeaders(fullResponseStr), result)
t.end()
})
test('http.ServerResponse', function (t) {
t.test('real http.ServerResponse object', function (t) {
var res = new http.ServerResponse({})
t.deepEqual(httpHeaders(res), {})
t.end()
})
t.test('no _header property', function (t) {
t.deepEqual(httpHeaders({ _header: undefined }), {})
t.end()
})
t.test('empty string as _header', function (t) {
t.deepEqual(httpHeaders({ _header: '' }), {})
t.end()
})
t.test('normal _header property', function (t) {
t.deepEqual(httpHeaders({ _header: startLineStr + headersStr }), result)
t.end()
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc