[d24f17c] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | const EventEmitter = require('node:events').EventEmitter
|
---|
| 4 | const inherits = require('node:util').inherits
|
---|
| 5 | const getLimit = require('../../../lib/utils/getLimit')
|
---|
| 6 |
|
---|
| 7 | const StreamSearch = require('../../streamsearch/sbmh')
|
---|
| 8 |
|
---|
| 9 | const B_DCRLF = Buffer.from('\r\n\r\n')
|
---|
| 10 | const RE_CRLF = /\r\n/g
|
---|
| 11 | const RE_HDR = /^([^:]+):[ \t]?([\x00-\xFF]+)?$/ // eslint-disable-line no-control-regex
|
---|
| 12 |
|
---|
| 13 | function HeaderParser (cfg) {
|
---|
| 14 | EventEmitter.call(this)
|
---|
| 15 |
|
---|
| 16 | cfg = cfg || {}
|
---|
| 17 | const self = this
|
---|
| 18 | this.nread = 0
|
---|
| 19 | this.maxed = false
|
---|
| 20 | this.npairs = 0
|
---|
| 21 | this.maxHeaderPairs = getLimit(cfg, 'maxHeaderPairs', 2000)
|
---|
| 22 | this.maxHeaderSize = getLimit(cfg, 'maxHeaderSize', 80 * 1024)
|
---|
| 23 | this.buffer = ''
|
---|
| 24 | this.header = {}
|
---|
| 25 | this.finished = false
|
---|
| 26 | this.ss = new StreamSearch(B_DCRLF)
|
---|
| 27 | this.ss.on('info', function (isMatch, data, start, end) {
|
---|
| 28 | if (data && !self.maxed) {
|
---|
| 29 | if (self.nread + end - start >= self.maxHeaderSize) {
|
---|
| 30 | end = self.maxHeaderSize - self.nread + start
|
---|
| 31 | self.nread = self.maxHeaderSize
|
---|
| 32 | self.maxed = true
|
---|
| 33 | } else { self.nread += (end - start) }
|
---|
| 34 |
|
---|
| 35 | self.buffer += data.toString('binary', start, end)
|
---|
| 36 | }
|
---|
| 37 | if (isMatch) { self._finish() }
|
---|
| 38 | })
|
---|
| 39 | }
|
---|
| 40 | inherits(HeaderParser, EventEmitter)
|
---|
| 41 |
|
---|
| 42 | HeaderParser.prototype.push = function (data) {
|
---|
| 43 | const r = this.ss.push(data)
|
---|
| 44 | if (this.finished) { return r }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | HeaderParser.prototype.reset = function () {
|
---|
| 48 | this.finished = false
|
---|
| 49 | this.buffer = ''
|
---|
| 50 | this.header = {}
|
---|
| 51 | this.ss.reset()
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | HeaderParser.prototype._finish = function () {
|
---|
| 55 | if (this.buffer) { this._parseHeader() }
|
---|
| 56 | this.ss.matches = this.ss.maxMatches
|
---|
| 57 | const header = this.header
|
---|
| 58 | this.header = {}
|
---|
| 59 | this.buffer = ''
|
---|
| 60 | this.finished = true
|
---|
| 61 | this.nread = this.npairs = 0
|
---|
| 62 | this.maxed = false
|
---|
| 63 | this.emit('header', header)
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | HeaderParser.prototype._parseHeader = function () {
|
---|
| 67 | if (this.npairs === this.maxHeaderPairs) { return }
|
---|
| 68 |
|
---|
| 69 | const lines = this.buffer.split(RE_CRLF)
|
---|
| 70 | const len = lines.length
|
---|
| 71 | let m, h
|
---|
| 72 |
|
---|
| 73 | for (var i = 0; i < len; ++i) { // eslint-disable-line no-var
|
---|
| 74 | if (lines[i].length === 0) { continue }
|
---|
| 75 | if (lines[i][0] === '\t' || lines[i][0] === ' ') {
|
---|
| 76 | // folded header content
|
---|
| 77 | // RFC2822 says to just remove the CRLF and not the whitespace following
|
---|
| 78 | // it, so we follow the RFC and include the leading whitespace ...
|
---|
| 79 | if (h) {
|
---|
| 80 | this.header[h][this.header[h].length - 1] += lines[i]
|
---|
| 81 | continue
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | const posColon = lines[i].indexOf(':')
|
---|
| 86 | if (
|
---|
| 87 | posColon === -1 ||
|
---|
| 88 | posColon === 0
|
---|
| 89 | ) {
|
---|
| 90 | return
|
---|
| 91 | }
|
---|
| 92 | m = RE_HDR.exec(lines[i])
|
---|
| 93 | h = m[1].toLowerCase()
|
---|
| 94 | this.header[h] = this.header[h] || []
|
---|
| 95 | this.header[h].push((m[2] || ''))
|
---|
| 96 | if (++this.npairs === this.maxHeaderPairs) { break }
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | module.exports = HeaderParser
|
---|