bare-http1 4.0.3 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -24,6 +24,7 @@ export {
24
24
  HTTPError as errors
25
25
  }
26
26
 
27
+ export const METHODS: HTTPMethod[]
27
28
  export const STATUS_CODES: typeof constants.status
28
29
 
29
30
  export interface HTTPIncomingMessageEvents extends ReadableEvents {
package/index.js CHANGED
@@ -14,6 +14,7 @@ exports.ClientConnection = require('./lib/client-connection')
14
14
  exports.constants = require('./lib/constants')
15
15
  exports.errors = require('./lib/errors')
16
16
 
17
+ exports.METHODS = Object.values(exports.constants.method) // For Node.js compatibility
17
18
  exports.STATUS_CODES = exports.constants.status // For Node.js compatibility
18
19
 
19
20
  exports.createServer = function createServer(opts, onrequest) {
@@ -143,12 +143,13 @@ module.exports = class HTTPClientConnection {
143
143
  if (r.length === 0) return this.socket.destroy()
144
144
 
145
145
  const [, statusCode, ...statusMessage] = r[0].split(' ')
146
- if (!statusCode || !statusMessage) return this.socket.destroy()
146
+ if (!statusCode || !statusMessage.length) return this.socket.destroy()
147
147
 
148
148
  const headers = {}
149
149
 
150
150
  for (let i = 1; i < r.length; i++) {
151
- const [name, value] = r[i].split(': ')
151
+ const [name, value] = splitHeader(r[i])
152
+ if (name === null) return this.socket.destroy()
152
153
  headers[name.toLowerCase()] = value
153
154
  }
154
155
 
@@ -262,3 +263,9 @@ module.exports = class HTTPClientConnection {
262
263
  HTTPClientConnection._connections.delete(this.socket)
263
264
  }
264
265
  }
266
+
267
+ function splitHeader(s) {
268
+ const i = s.indexOf(': ')
269
+ if (i === -1) return [null, null]
270
+ return [s.slice(0, i), s.slice(i + 2)]
271
+ }
@@ -1,6 +1,9 @@
1
1
  const HTTPAgent = require('./agent')
2
2
  const HTTPOutgoingMessage = require('./outgoing-message')
3
3
 
4
+ const CHUNK_DELIMITER = Buffer.from('\r\n')
5
+ const CHUNK_TERMINATOR = Buffer.from('0\r\n\r\n')
6
+
4
7
  module.exports = class HTTPClientRequest extends HTTPOutgoingMessage {
5
8
  constructor(opts = {}, onresponse = null) {
6
9
  if (typeof opts === 'function') {
@@ -62,9 +65,10 @@ module.exports = class HTTPClientRequest extends HTTPOutgoingMessage {
62
65
 
63
66
  if (this._chunked) {
64
67
  data = Buffer.concat([
65
- Buffer.from('' + data.byteLength.toString(16) + '\r\n'),
68
+ Buffer.from(data.byteLength.toString(16)),
69
+ CHUNK_DELIMITER,
66
70
  data,
67
- Buffer.from('\r\n')
71
+ CHUNK_DELIMITER
68
72
  ])
69
73
  }
70
74
 
@@ -75,7 +79,7 @@ module.exports = class HTTPClientRequest extends HTTPOutgoingMessage {
75
79
  _final(cb) {
76
80
  if (this.headersSent === false) this.flushHeaders()
77
81
 
78
- if (this._chunked) this.socket.write(Buffer.from('0\r\n\r\n'))
82
+ if (this._chunked) this.socket.write(CHUNK_TERMINATOR)
79
83
 
80
84
  this._pendingFinal = cb
81
85
  }
@@ -145,7 +145,8 @@ module.exports = class HTTPServerConnection {
145
145
  const headers = {}
146
146
 
147
147
  for (let i = 1; i < r.length; i++) {
148
- const [name, value] = r[i].split(': ')
148
+ const [name, value] = splitHeader(r[i])
149
+ if (name === null) return this.socket.destroy()
149
150
  headers[name.toLowerCase()] = value
150
151
  }
151
152
 
@@ -270,3 +271,9 @@ module.exports = class HTTPServerConnection {
270
271
  }
271
272
 
272
273
  function noop() {}
274
+
275
+ function splitHeader(s) {
276
+ const i = s.indexOf(': ')
277
+ if (i === -1) return [null, null]
278
+ return [s.slice(0, i), s.slice(i + 2)]
279
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-http1",
3
- "version": "4.0.3",
3
+ "version": "4.1.0",
4
4
  "description": "Native HTTP/1 library for JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",