Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var nextLine = require('next-line')
// RFC-2068 Start-Line definitions:
// Request-Line: Method SP Request-URI SP HTTP-Version CRLF
// Status-Line: HTTP-Version SP Status-Code SP Reason-Phrase CRLF
var startLine = /^[A-Z_]+(\/\d\.\d)? /
var requestLine = /^([A-Z_]+) (.+) [A-Z]+\/(\d)\.(\d)$/
var statusLine = /^[A-Z]+\/(\d)\.(\d) (\d{3}) (.*)$/
var startLine = /^[A-Z_]+[^:]*$/
var requestLine = /^([A-Z_]+) (.+) HTTP\/([123](\.(\d))?)$/
var statusLine = /^HTTP\/([123](\.(\d))?) (\d{3})\s?(.*)?$/

module.exports = function (data, onlyHeaders) {
return parse(normalize(data), onlyHeaders)
Expand All @@ -23,14 +23,14 @@ function parse (str, onlyHeaders) {
return {
method: match[1],
url: match[2],
version: { major: parseInt(match[3], 10), minor: parseInt(match[4], 10) },
version: match[3],
headers: parseHeaders(str)
}
} else if ((match = line.match(statusLine)) !== null) {
return {
version: { major: parseInt(match[1], 10), minor: parseInt(match[2], 10) },
statusCode: parseInt(match[3], 10),
statusMessage: match[4],
version: match[1],
statusCode: parseInt(match[4], 10),
statusMessage: match[5] ?? '',
headers: parseHeaders(str)
}
} else {
Expand Down
39 changes: 35 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var httpHeaders = require('./')

var requestLine = 'GET /foo HTTP/1.1\r\n'
var statusLine = 'HTTP/1.1 200 OK\r\n'
var statusLineV2 = 'HTTP/2 200 \r\n'
var msgHeaders = 'Date: Tue, 10 Jun 2014 07:29:20 GMT\r\n' +
'Connection: keep-alive\r\n' +
'Transfer-Encoding: chunked\r\n' +
Expand All @@ -20,6 +21,7 @@ var msgHeaders = 'Date: Tue, 10 Jun 2014 07:29:20 GMT\r\n' +
'\r\n'
var requestMsg = requestLine + msgHeaders + 'Hello: World'
var responseMsg = statusLine + msgHeaders + 'Hello: World'
var responseMsgV2 = statusLine + msgHeaders + 'Hello: World'

var headerResult = {
date: 'Tue, 10 Jun 2014 07:29:20 GMT',
Expand All @@ -31,15 +33,22 @@ var headerResult = {
'x-multi-line-header': 'Foo Bar'
}
var responseResult = {
version: { major: 1, minor: 1 },
version: '1.1',
statusCode: 200,
statusMessage: 'OK',
headers: headerResult
}
var responseResultV2 = {
version: '2',
statusCode: 200,
statusMessage: '',
headers: headerResult
}

var requestResult = {
method: 'GET',
url: '/foo',
version: { major: 1, minor: 1 },
version: '1.1',
headers: headerResult
}

Expand Down Expand Up @@ -70,11 +79,13 @@ test('empty buffer', function (t) {
test('start-line + header', function (t) {
t.deepEqual(httpHeaders(requestLine + msgHeaders), requestResult)
t.deepEqual(httpHeaders(statusLine + msgHeaders), responseResult)
t.deepEqual(httpHeaders(statusLineV2 + msgHeaders), responseResultV2)
t.deepEqual(httpHeaders(new Buffer(requestLine + msgHeaders)), requestResult)
t.deepEqual(httpHeaders(new Buffer(statusLine + msgHeaders)), responseResult)
t.deepEqual(httpHeaders(requestLine + msgHeaders, true), headerResult)
t.deepEqual(httpHeaders(statusLine + msgHeaders, true), headerResult)
t.deepEqual(httpHeaders(new Buffer(requestLine + msgHeaders), true), headerResult)

t.deepEqual(httpHeaders(new Buffer(statusLine + msgHeaders), true), headerResult)
t.end()
})
Expand All @@ -83,7 +94,7 @@ test('request-line only', function (t) {
var requestResult = {
method: 'GET',
url: '/foo',
version: { major: 1, minor: 1 },
version: '1.1',
headers: {}
}

Expand All @@ -96,7 +107,7 @@ test('request-line only', function (t) {

test('status-line only', function (t) {
var responseResult = {
version: { major: 1, minor: 1 },
version: '1.1',
statusCode: 200,
statusMessage: 'OK',
headers: {}
Expand All @@ -109,14 +120,32 @@ test('status-line only', function (t) {
t.end()
})

test('status-line only (HTTP/2)', function (t) {
var responseResult = {
version: '2',
statusCode: 200,
statusMessage: '',
headers: {}
}

t.deepEqual(httpHeaders(statusLineV2 + '\r\n'), responseResult)
t.deepEqual(httpHeaders(new Buffer(statusLineV2 + '\r\n')), responseResult)
t.deepEqual(httpHeaders(statusLineV2 + '\r\n', true), {})
t.deepEqual(httpHeaders(new Buffer(statusLineV2 + '\r\n'), true), {})
t.end()
})


test('headers only', function (t) {
console.log('checking', msgHeaders)
t.deepEqual(httpHeaders(msgHeaders), headerResult)
t.deepEqual(httpHeaders(new Buffer(msgHeaders)), headerResult)
t.deepEqual(httpHeaders(msgHeaders, true), headerResult)
t.deepEqual(httpHeaders(new Buffer(msgHeaders), true), headerResult)
t.end()
})


test('full http response', function (t) {
t.deepEqual(httpHeaders(requestMsg), requestResult)
t.deepEqual(httpHeaders(responseMsg), responseResult)
Expand All @@ -129,6 +158,7 @@ test('full http response', function (t) {
t.end()
})


test('http.ServerResponse', function (t) {
t.test('real http.ServerResponse object', function (t) {
var res = new http.ServerResponse({})
Expand Down Expand Up @@ -156,6 +186,7 @@ test('http.ServerResponse', function (t) {
})
})


test('set-cookie', function (t) {
t.deepEqual(httpHeaders('Set-Cookie: foo'), { 'set-cookie': ['foo'] })
t.deepEqual(httpHeaders('Set-Cookie: foo\r\nSet-Cookie: bar'), { 'set-cookie': ['foo', 'bar'] })
Expand Down