index.js 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /*!
  2. * send
  3. * Copyright(c) 2012 TJ Holowaychuk
  4. * Copyright(c) 2014-2022 Douglas Christopher Wilson
  5. * MIT Licensed
  6. */
  7. 'use strict'
  8. /**
  9. * Module dependencies.
  10. * @private
  11. */
  12. var createError = require('http-errors')
  13. var debug = require('debug')('send')
  14. var deprecate = require('depd')('send')
  15. var destroy = require('destroy')
  16. var encodeUrl = require('encodeurl')
  17. var escapeHtml = require('escape-html')
  18. var etag = require('etag')
  19. var fresh = require('fresh')
  20. var fs = require('fs')
  21. var mime = require('mime')
  22. var ms = require('ms')
  23. var onFinished = require('on-finished')
  24. var parseRange = require('range-parser')
  25. var path = require('path')
  26. var statuses = require('statuses')
  27. var Stream = require('stream')
  28. var util = require('util')
  29. /**
  30. * Path function references.
  31. * @private
  32. */
  33. var extname = path.extname
  34. var join = path.join
  35. var normalize = path.normalize
  36. var resolve = path.resolve
  37. var sep = path.sep
  38. /**
  39. * Regular expression for identifying a bytes Range header.
  40. * @private
  41. */
  42. var BYTES_RANGE_REGEXP = /^ *bytes=/
  43. /**
  44. * Maximum value allowed for the max age.
  45. * @private
  46. */
  47. var MAX_MAXAGE = 60 * 60 * 24 * 365 * 1000 // 1 year
  48. /**
  49. * Regular expression to match a path with a directory up component.
  50. * @private
  51. */
  52. var UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/
  53. /**
  54. * Module exports.
  55. * @public
  56. */
  57. module.exports = send
  58. module.exports.mime = mime
  59. /**
  60. * Return a `SendStream` for `req` and `path`.
  61. *
  62. * @param {object} req
  63. * @param {string} path
  64. * @param {object} [options]
  65. * @return {SendStream}
  66. * @public
  67. */
  68. function send (req, path, options) {
  69. return new SendStream(req, path, options)
  70. }
  71. /**
  72. * Initialize a `SendStream` with the given `path`.
  73. *
  74. * @param {Request} req
  75. * @param {String} path
  76. * @param {object} [options]
  77. * @private
  78. */
  79. function SendStream (req, path, options) {
  80. Stream.call(this)
  81. var opts = options || {}
  82. this.options = opts
  83. this.path = path
  84. this.req = req
  85. this._acceptRanges = opts.acceptRanges !== undefined
  86. ? Boolean(opts.acceptRanges)
  87. : true
  88. this._cacheControl = opts.cacheControl !== undefined
  89. ? Boolean(opts.cacheControl)
  90. : true
  91. this._etag = opts.etag !== undefined
  92. ? Boolean(opts.etag)
  93. : true
  94. this._dotfiles = opts.dotfiles !== undefined
  95. ? opts.dotfiles
  96. : 'ignore'
  97. if (this._dotfiles !== 'ignore' && this._dotfiles !== 'allow' && this._dotfiles !== 'deny') {
  98. throw new TypeError('dotfiles option must be "allow", "deny", or "ignore"')
  99. }
  100. this._hidden = Boolean(opts.hidden)
  101. if (opts.hidden !== undefined) {
  102. deprecate('hidden: use dotfiles: \'' + (this._hidden ? 'allow' : 'ignore') + '\' instead')
  103. }
  104. // legacy support
  105. if (opts.dotfiles === undefined) {
  106. this._dotfiles = undefined
  107. }
  108. this._extensions = opts.extensions !== undefined
  109. ? normalizeList(opts.extensions, 'extensions option')
  110. : []
  111. this._immutable = opts.immutable !== undefined
  112. ? Boolean(opts.immutable)
  113. : false
  114. this._index = opts.index !== undefined
  115. ? normalizeList(opts.index, 'index option')
  116. : ['index.html']
  117. this._lastModified = opts.lastModified !== undefined
  118. ? Boolean(opts.lastModified)
  119. : true
  120. this._maxage = opts.maxAge || opts.maxage
  121. this._maxage = typeof this._maxage === 'string'
  122. ? ms(this._maxage)
  123. : Number(this._maxage)
  124. this._maxage = !isNaN(this._maxage)
  125. ? Math.min(Math.max(0, this._maxage), MAX_MAXAGE)
  126. : 0
  127. this._root = opts.root
  128. ? resolve(opts.root)
  129. : null
  130. if (!this._root && opts.from) {
  131. this.from(opts.from)
  132. }
  133. }
  134. /**
  135. * Inherits from `Stream`.
  136. */
  137. util.inherits(SendStream, Stream)
  138. /**
  139. * Enable or disable etag generation.
  140. *
  141. * @param {Boolean} val
  142. * @return {SendStream}
  143. * @api public
  144. */
  145. SendStream.prototype.etag = deprecate.function(function etag (val) {
  146. this._etag = Boolean(val)
  147. debug('etag %s', this._etag)
  148. return this
  149. }, 'send.etag: pass etag as option')
  150. /**
  151. * Enable or disable "hidden" (dot) files.
  152. *
  153. * @param {Boolean} path
  154. * @return {SendStream}
  155. * @api public
  156. */
  157. SendStream.prototype.hidden = deprecate.function(function hidden (val) {
  158. this._hidden = Boolean(val)
  159. this._dotfiles = undefined
  160. debug('hidden %s', this._hidden)
  161. return this
  162. }, 'send.hidden: use dotfiles option')
  163. /**
  164. * Set index `paths`, set to a falsy
  165. * value to disable index support.
  166. *
  167. * @param {String|Boolean|Array} paths
  168. * @return {SendStream}
  169. * @api public
  170. */
  171. SendStream.prototype.index = deprecate.function(function index (paths) {
  172. var index = !paths ? [] : normalizeList(paths, 'paths argument')
  173. debug('index %o', paths)
  174. this._index = index
  175. return this
  176. }, 'send.index: pass index as option')
  177. /**
  178. * Set root `path`.
  179. *
  180. * @param {String} path
  181. * @return {SendStream}
  182. * @api public
  183. */
  184. SendStream.prototype.root = function root (path) {
  185. this._root = resolve(String(path))
  186. debug('root %s', this._root)
  187. return this
  188. }
  189. SendStream.prototype.from = deprecate.function(SendStream.prototype.root,
  190. 'send.from: pass root as option')
  191. SendStream.prototype.root = deprecate.function(SendStream.prototype.root,
  192. 'send.root: pass root as option')
  193. /**
  194. * Set max-age to `maxAge`.
  195. *
  196. * @param {Number} maxAge
  197. * @return {SendStream}
  198. * @api public
  199. */
  200. SendStream.prototype.maxage = deprecate.function(function maxage (maxAge) {
  201. this._maxage = typeof maxAge === 'string'
  202. ? ms(maxAge)
  203. : Number(maxAge)
  204. this._maxage = !isNaN(this._maxage)
  205. ? Math.min(Math.max(0, this._maxage), MAX_MAXAGE)
  206. : 0
  207. debug('max-age %d', this._maxage)
  208. return this
  209. }, 'send.maxage: pass maxAge as option')
  210. /**
  211. * Emit error with `status`.
  212. *
  213. * @param {number} status
  214. * @param {Error} [err]
  215. * @private
  216. */
  217. SendStream.prototype.error = function error (status, err) {
  218. // emit if listeners instead of responding
  219. if (hasListeners(this, 'error')) {
  220. return this.emit('error', createHttpError(status, err))
  221. }
  222. var res = this.res
  223. var msg = statuses.message[status] || String(status)
  224. var doc = createHtmlDocument('Error', escapeHtml(msg))
  225. // clear existing headers
  226. clearHeaders(res)
  227. // add error headers
  228. if (err && err.headers) {
  229. setHeaders(res, err.headers)
  230. }
  231. // send basic response
  232. res.statusCode = status
  233. res.setHeader('Content-Type', 'text/html; charset=UTF-8')
  234. res.setHeader('Content-Length', Buffer.byteLength(doc))
  235. res.setHeader('Content-Security-Policy', "default-src 'none'")
  236. res.setHeader('X-Content-Type-Options', 'nosniff')
  237. res.end(doc)
  238. }
  239. /**
  240. * Check if the pathname ends with "/".
  241. *
  242. * @return {boolean}
  243. * @private
  244. */
  245. SendStream.prototype.hasTrailingSlash = function hasTrailingSlash () {
  246. return this.path[this.path.length - 1] === '/'
  247. }
  248. /**
  249. * Check if this is a conditional GET request.
  250. *
  251. * @return {Boolean}
  252. * @api private
  253. */
  254. SendStream.prototype.isConditionalGET = function isConditionalGET () {
  255. return this.req.headers['if-match'] ||
  256. this.req.headers['if-unmodified-since'] ||
  257. this.req.headers['if-none-match'] ||
  258. this.req.headers['if-modified-since']
  259. }
  260. /**
  261. * Check if the request preconditions failed.
  262. *
  263. * @return {boolean}
  264. * @private
  265. */
  266. SendStream.prototype.isPreconditionFailure = function isPreconditionFailure () {
  267. var req = this.req
  268. var res = this.res
  269. // if-match
  270. var match = req.headers['if-match']
  271. if (match) {
  272. var etag = res.getHeader('ETag')
  273. return !etag || (match !== '*' && parseTokenList(match).every(function (match) {
  274. return match !== etag && match !== 'W/' + etag && 'W/' + match !== etag
  275. }))
  276. }
  277. // if-unmodified-since
  278. var unmodifiedSince = parseHttpDate(req.headers['if-unmodified-since'])
  279. if (!isNaN(unmodifiedSince)) {
  280. var lastModified = parseHttpDate(res.getHeader('Last-Modified'))
  281. return isNaN(lastModified) || lastModified > unmodifiedSince
  282. }
  283. return false
  284. }
  285. /**
  286. * Strip various content header fields for a change in entity.
  287. *
  288. * @private
  289. */
  290. SendStream.prototype.removeContentHeaderFields = function removeContentHeaderFields () {
  291. var res = this.res
  292. res.removeHeader('Content-Encoding')
  293. res.removeHeader('Content-Language')
  294. res.removeHeader('Content-Length')
  295. res.removeHeader('Content-Range')
  296. res.removeHeader('Content-Type')
  297. }
  298. /**
  299. * Respond with 304 not modified.
  300. *
  301. * @api private
  302. */
  303. SendStream.prototype.notModified = function notModified () {
  304. var res = this.res
  305. debug('not modified')
  306. this.removeContentHeaderFields()
  307. res.statusCode = 304
  308. res.end()
  309. }
  310. /**
  311. * Raise error that headers already sent.
  312. *
  313. * @api private
  314. */
  315. SendStream.prototype.headersAlreadySent = function headersAlreadySent () {
  316. var err = new Error('Can\'t set headers after they are sent.')
  317. debug('headers already sent')
  318. this.error(500, err)
  319. }
  320. /**
  321. * Check if the request is cacheable, aka
  322. * responded with 2xx or 304 (see RFC 2616 section 14.2{5,6}).
  323. *
  324. * @return {Boolean}
  325. * @api private
  326. */
  327. SendStream.prototype.isCachable = function isCachable () {
  328. var statusCode = this.res.statusCode
  329. return (statusCode >= 200 && statusCode < 300) ||
  330. statusCode === 304
  331. }
  332. /**
  333. * Handle stat() error.
  334. *
  335. * @param {Error} error
  336. * @private
  337. */
  338. SendStream.prototype.onStatError = function onStatError (error) {
  339. switch (error.code) {
  340. case 'ENAMETOOLONG':
  341. case 'ENOENT':
  342. case 'ENOTDIR':
  343. this.error(404, error)
  344. break
  345. default:
  346. this.error(500, error)
  347. break
  348. }
  349. }
  350. /**
  351. * Check if the cache is fresh.
  352. *
  353. * @return {Boolean}
  354. * @api private
  355. */
  356. SendStream.prototype.isFresh = function isFresh () {
  357. return fresh(this.req.headers, {
  358. etag: this.res.getHeader('ETag'),
  359. 'last-modified': this.res.getHeader('Last-Modified')
  360. })
  361. }
  362. /**
  363. * Check if the range is fresh.
  364. *
  365. * @return {Boolean}
  366. * @api private
  367. */
  368. SendStream.prototype.isRangeFresh = function isRangeFresh () {
  369. var ifRange = this.req.headers['if-range']
  370. if (!ifRange) {
  371. return true
  372. }
  373. // if-range as etag
  374. if (ifRange.indexOf('"') !== -1) {
  375. var etag = this.res.getHeader('ETag')
  376. return Boolean(etag && ifRange.indexOf(etag) !== -1)
  377. }
  378. // if-range as modified date
  379. var lastModified = this.res.getHeader('Last-Modified')
  380. return parseHttpDate(lastModified) <= parseHttpDate(ifRange)
  381. }
  382. /**
  383. * Redirect to path.
  384. *
  385. * @param {string} path
  386. * @private
  387. */
  388. SendStream.prototype.redirect = function redirect (path) {
  389. var res = this.res
  390. if (hasListeners(this, 'directory')) {
  391. this.emit('directory', res, path)
  392. return
  393. }
  394. if (this.hasTrailingSlash()) {
  395. this.error(403)
  396. return
  397. }
  398. var loc = encodeUrl(collapseLeadingSlashes(this.path + '/'))
  399. var doc = createHtmlDocument('Redirecting', 'Redirecting to ' + escapeHtml(loc))
  400. // redirect
  401. res.statusCode = 301
  402. res.setHeader('Content-Type', 'text/html; charset=UTF-8')
  403. res.setHeader('Content-Length', Buffer.byteLength(doc))
  404. res.setHeader('Content-Security-Policy', "default-src 'none'")
  405. res.setHeader('X-Content-Type-Options', 'nosniff')
  406. res.setHeader('Location', loc)
  407. res.end(doc)
  408. }
  409. /**
  410. * Pipe to `res.
  411. *
  412. * @param {Stream} res
  413. * @return {Stream} res
  414. * @api public
  415. */
  416. SendStream.prototype.pipe = function pipe (res) {
  417. // root path
  418. var root = this._root
  419. // references
  420. this.res = res
  421. // decode the path
  422. var path = decode(this.path)
  423. if (path === -1) {
  424. this.error(400)
  425. return res
  426. }
  427. // null byte(s)
  428. if (~path.indexOf('\0')) {
  429. this.error(400)
  430. return res
  431. }
  432. var parts
  433. if (root !== null) {
  434. // normalize
  435. if (path) {
  436. path = normalize('.' + sep + path)
  437. }
  438. // malicious path
  439. if (UP_PATH_REGEXP.test(path)) {
  440. debug('malicious path "%s"', path)
  441. this.error(403)
  442. return res
  443. }
  444. // explode path parts
  445. parts = path.split(sep)
  446. // join / normalize from optional root dir
  447. path = normalize(join(root, path))
  448. } else {
  449. // ".." is malicious without "root"
  450. if (UP_PATH_REGEXP.test(path)) {
  451. debug('malicious path "%s"', path)
  452. this.error(403)
  453. return res
  454. }
  455. // explode path parts
  456. parts = normalize(path).split(sep)
  457. // resolve the path
  458. path = resolve(path)
  459. }
  460. // dotfile handling
  461. if (containsDotFile(parts)) {
  462. var access = this._dotfiles
  463. // legacy support
  464. if (access === undefined) {
  465. access = parts[parts.length - 1][0] === '.'
  466. ? (this._hidden ? 'allow' : 'ignore')
  467. : 'allow'
  468. }
  469. debug('%s dotfile "%s"', access, path)
  470. switch (access) {
  471. case 'allow':
  472. break
  473. case 'deny':
  474. this.error(403)
  475. return res
  476. case 'ignore':
  477. default:
  478. this.error(404)
  479. return res
  480. }
  481. }
  482. // index file support
  483. if (this._index.length && this.hasTrailingSlash()) {
  484. this.sendIndex(path)
  485. return res
  486. }
  487. this.sendFile(path)
  488. return res
  489. }
  490. /**
  491. * Transfer `path`.
  492. *
  493. * @param {String} path
  494. * @api public
  495. */
  496. SendStream.prototype.send = function send (path, stat) {
  497. var len = stat.size
  498. var options = this.options
  499. var opts = {}
  500. var res = this.res
  501. var req = this.req
  502. var ranges = req.headers.range
  503. var offset = options.start || 0
  504. if (headersSent(res)) {
  505. // impossible to send now
  506. this.headersAlreadySent()
  507. return
  508. }
  509. debug('pipe "%s"', path)
  510. // set header fields
  511. this.setHeader(path, stat)
  512. // set content-type
  513. this.type(path)
  514. // conditional GET support
  515. if (this.isConditionalGET()) {
  516. if (this.isPreconditionFailure()) {
  517. this.error(412)
  518. return
  519. }
  520. if (this.isCachable() && this.isFresh()) {
  521. this.notModified()
  522. return
  523. }
  524. }
  525. // adjust len to start/end options
  526. len = Math.max(0, len - offset)
  527. if (options.end !== undefined) {
  528. var bytes = options.end - offset + 1
  529. if (len > bytes) len = bytes
  530. }
  531. // Range support
  532. if (this._acceptRanges && BYTES_RANGE_REGEXP.test(ranges)) {
  533. // parse
  534. ranges = parseRange(len, ranges, {
  535. combine: true
  536. })
  537. // If-Range support
  538. if (!this.isRangeFresh()) {
  539. debug('range stale')
  540. ranges = -2
  541. }
  542. // unsatisfiable
  543. if (ranges === -1) {
  544. debug('range unsatisfiable')
  545. // Content-Range
  546. res.setHeader('Content-Range', contentRange('bytes', len))
  547. // 416 Requested Range Not Satisfiable
  548. return this.error(416, {
  549. headers: { 'Content-Range': res.getHeader('Content-Range') }
  550. })
  551. }
  552. // valid (syntactically invalid/multiple ranges are treated as a regular response)
  553. if (ranges !== -2 && ranges.length === 1) {
  554. debug('range %j', ranges)
  555. // Content-Range
  556. res.statusCode = 206
  557. res.setHeader('Content-Range', contentRange('bytes', len, ranges[0]))
  558. // adjust for requested range
  559. offset += ranges[0].start
  560. len = ranges[0].end - ranges[0].start + 1
  561. }
  562. }
  563. // clone options
  564. for (var prop in options) {
  565. opts[prop] = options[prop]
  566. }
  567. // set read options
  568. opts.start = offset
  569. opts.end = Math.max(offset, offset + len - 1)
  570. // content-length
  571. res.setHeader('Content-Length', len)
  572. // HEAD support
  573. if (req.method === 'HEAD') {
  574. res.end()
  575. return
  576. }
  577. this.stream(path, opts)
  578. }
  579. /**
  580. * Transfer file for `path`.
  581. *
  582. * @param {String} path
  583. * @api private
  584. */
  585. SendStream.prototype.sendFile = function sendFile (path) {
  586. var i = 0
  587. var self = this
  588. debug('stat "%s"', path)
  589. fs.stat(path, function onstat (err, stat) {
  590. if (err && err.code === 'ENOENT' && !extname(path) && path[path.length - 1] !== sep) {
  591. // not found, check extensions
  592. return next(err)
  593. }
  594. if (err) return self.onStatError(err)
  595. if (stat.isDirectory()) return self.redirect(path)
  596. self.emit('file', path, stat)
  597. self.send(path, stat)
  598. })
  599. function next (err) {
  600. if (self._extensions.length <= i) {
  601. return err
  602. ? self.onStatError(err)
  603. : self.error(404)
  604. }
  605. var p = path + '.' + self._extensions[i++]
  606. debug('stat "%s"', p)
  607. fs.stat(p, function (err, stat) {
  608. if (err) return next(err)
  609. if (stat.isDirectory()) return next()
  610. self.emit('file', p, stat)
  611. self.send(p, stat)
  612. })
  613. }
  614. }
  615. /**
  616. * Transfer index for `path`.
  617. *
  618. * @param {String} path
  619. * @api private
  620. */
  621. SendStream.prototype.sendIndex = function sendIndex (path) {
  622. var i = -1
  623. var self = this
  624. function next (err) {
  625. if (++i >= self._index.length) {
  626. if (err) return self.onStatError(err)
  627. return self.error(404)
  628. }
  629. var p = join(path, self._index[i])
  630. debug('stat "%s"', p)
  631. fs.stat(p, function (err, stat) {
  632. if (err) return next(err)
  633. if (stat.isDirectory()) return next()
  634. self.emit('file', p, stat)
  635. self.send(p, stat)
  636. })
  637. }
  638. next()
  639. }
  640. /**
  641. * Stream `path` to the response.
  642. *
  643. * @param {String} path
  644. * @param {Object} options
  645. * @api private
  646. */
  647. SendStream.prototype.stream = function stream (path, options) {
  648. var self = this
  649. var res = this.res
  650. // pipe
  651. var stream = fs.createReadStream(path, options)
  652. this.emit('stream', stream)
  653. stream.pipe(res)
  654. // cleanup
  655. function cleanup () {
  656. destroy(stream, true)
  657. }
  658. // response finished, cleanup
  659. onFinished(res, cleanup)
  660. // error handling
  661. stream.on('error', function onerror (err) {
  662. // clean up stream early
  663. cleanup()
  664. // error
  665. self.onStatError(err)
  666. })
  667. // end
  668. stream.on('end', function onend () {
  669. self.emit('end')
  670. })
  671. }
  672. /**
  673. * Set content-type based on `path`
  674. * if it hasn't been explicitly set.
  675. *
  676. * @param {String} path
  677. * @api private
  678. */
  679. SendStream.prototype.type = function type (path) {
  680. var res = this.res
  681. if (res.getHeader('Content-Type')) return
  682. var type = mime.lookup(path)
  683. if (!type) {
  684. debug('no content-type')
  685. return
  686. }
  687. var charset = mime.charsets.lookup(type)
  688. debug('content-type %s', type)
  689. res.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''))
  690. }
  691. /**
  692. * Set response header fields, most
  693. * fields may be pre-defined.
  694. *
  695. * @param {String} path
  696. * @param {Object} stat
  697. * @api private
  698. */
  699. SendStream.prototype.setHeader = function setHeader (path, stat) {
  700. var res = this.res
  701. this.emit('headers', res, path, stat)
  702. if (this._acceptRanges && !res.getHeader('Accept-Ranges')) {
  703. debug('accept ranges')
  704. res.setHeader('Accept-Ranges', 'bytes')
  705. }
  706. if (this._cacheControl && !res.getHeader('Cache-Control')) {
  707. var cacheControl = 'public, max-age=' + Math.floor(this._maxage / 1000)
  708. if (this._immutable) {
  709. cacheControl += ', immutable'
  710. }
  711. debug('cache-control %s', cacheControl)
  712. res.setHeader('Cache-Control', cacheControl)
  713. }
  714. if (this._lastModified && !res.getHeader('Last-Modified')) {
  715. var modified = stat.mtime.toUTCString()
  716. debug('modified %s', modified)
  717. res.setHeader('Last-Modified', modified)
  718. }
  719. if (this._etag && !res.getHeader('ETag')) {
  720. var val = etag(stat)
  721. debug('etag %s', val)
  722. res.setHeader('ETag', val)
  723. }
  724. }
  725. /**
  726. * Clear all headers from a response.
  727. *
  728. * @param {object} res
  729. * @private
  730. */
  731. function clearHeaders (res) {
  732. var headers = getHeaderNames(res)
  733. for (var i = 0; i < headers.length; i++) {
  734. res.removeHeader(headers[i])
  735. }
  736. }
  737. /**
  738. * Collapse all leading slashes into a single slash
  739. *
  740. * @param {string} str
  741. * @private
  742. */
  743. function collapseLeadingSlashes (str) {
  744. for (var i = 0; i < str.length; i++) {
  745. if (str[i] !== '/') {
  746. break
  747. }
  748. }
  749. return i > 1
  750. ? '/' + str.substr(i)
  751. : str
  752. }
  753. /**
  754. * Determine if path parts contain a dotfile.
  755. *
  756. * @api private
  757. */
  758. function containsDotFile (parts) {
  759. for (var i = 0; i < parts.length; i++) {
  760. var part = parts[i]
  761. if (part.length > 1 && part[0] === '.') {
  762. return true
  763. }
  764. }
  765. return false
  766. }
  767. /**
  768. * Create a Content-Range header.
  769. *
  770. * @param {string} type
  771. * @param {number} size
  772. * @param {array} [range]
  773. */
  774. function contentRange (type, size, range) {
  775. return type + ' ' + (range ? range.start + '-' + range.end : '*') + '/' + size
  776. }
  777. /**
  778. * Create a minimal HTML document.
  779. *
  780. * @param {string} title
  781. * @param {string} body
  782. * @private
  783. */
  784. function createHtmlDocument (title, body) {
  785. return '<!DOCTYPE html>\n' +
  786. '<html lang="en">\n' +
  787. '<head>\n' +
  788. '<meta charset="utf-8">\n' +
  789. '<title>' + title + '</title>\n' +
  790. '</head>\n' +
  791. '<body>\n' +
  792. '<pre>' + body + '</pre>\n' +
  793. '</body>\n' +
  794. '</html>\n'
  795. }
  796. /**
  797. * Create a HttpError object from simple arguments.
  798. *
  799. * @param {number} status
  800. * @param {Error|object} err
  801. * @private
  802. */
  803. function createHttpError (status, err) {
  804. if (!err) {
  805. return createError(status)
  806. }
  807. return err instanceof Error
  808. ? createError(status, err, { expose: false })
  809. : createError(status, err)
  810. }
  811. /**
  812. * decodeURIComponent.
  813. *
  814. * Allows V8 to only deoptimize this fn instead of all
  815. * of send().
  816. *
  817. * @param {String} path
  818. * @api private
  819. */
  820. function decode (path) {
  821. try {
  822. return decodeURIComponent(path)
  823. } catch (err) {
  824. return -1
  825. }
  826. }
  827. /**
  828. * Get the header names on a respnse.
  829. *
  830. * @param {object} res
  831. * @returns {array[string]}
  832. * @private
  833. */
  834. function getHeaderNames (res) {
  835. return typeof res.getHeaderNames !== 'function'
  836. ? Object.keys(res._headers || {})
  837. : res.getHeaderNames()
  838. }
  839. /**
  840. * Determine if emitter has listeners of a given type.
  841. *
  842. * The way to do this check is done three different ways in Node.js >= 0.8
  843. * so this consolidates them into a minimal set using instance methods.
  844. *
  845. * @param {EventEmitter} emitter
  846. * @param {string} type
  847. * @returns {boolean}
  848. * @private
  849. */
  850. function hasListeners (emitter, type) {
  851. var count = typeof emitter.listenerCount !== 'function'
  852. ? emitter.listeners(type).length
  853. : emitter.listenerCount(type)
  854. return count > 0
  855. }
  856. /**
  857. * Determine if the response headers have been sent.
  858. *
  859. * @param {object} res
  860. * @returns {boolean}
  861. * @private
  862. */
  863. function headersSent (res) {
  864. return typeof res.headersSent !== 'boolean'
  865. ? Boolean(res._header)
  866. : res.headersSent
  867. }
  868. /**
  869. * Normalize the index option into an array.
  870. *
  871. * @param {boolean|string|array} val
  872. * @param {string} name
  873. * @private
  874. */
  875. function normalizeList (val, name) {
  876. var list = [].concat(val || [])
  877. for (var i = 0; i < list.length; i++) {
  878. if (typeof list[i] !== 'string') {
  879. throw new TypeError(name + ' must be array of strings or false')
  880. }
  881. }
  882. return list
  883. }
  884. /**
  885. * Parse an HTTP Date into a number.
  886. *
  887. * @param {string} date
  888. * @private
  889. */
  890. function parseHttpDate (date) {
  891. var timestamp = date && Date.parse(date)
  892. return typeof timestamp === 'number'
  893. ? timestamp
  894. : NaN
  895. }
  896. /**
  897. * Parse a HTTP token list.
  898. *
  899. * @param {string} str
  900. * @private
  901. */
  902. function parseTokenList (str) {
  903. var end = 0
  904. var list = []
  905. var start = 0
  906. // gather tokens
  907. for (var i = 0, len = str.length; i < len; i++) {
  908. switch (str.charCodeAt(i)) {
  909. case 0x20: /* */
  910. if (start === end) {
  911. start = end = i + 1
  912. }
  913. break
  914. case 0x2c: /* , */
  915. if (start !== end) {
  916. list.push(str.substring(start, end))
  917. }
  918. start = end = i + 1
  919. break
  920. default:
  921. end = i + 1
  922. break
  923. }
  924. }
  925. // final token
  926. if (start !== end) {
  927. list.push(str.substring(start, end))
  928. }
  929. return list
  930. }
  931. /**
  932. * Set an object of headers on a response.
  933. *
  934. * @param {object} res
  935. * @param {object} headers
  936. * @private
  937. */
  938. function setHeaders (res, headers) {
  939. var keys = Object.keys(headers)
  940. for (var i = 0; i < keys.length; i++) {
  941. var key = keys[i]
  942. res.setHeader(key, headers[key])
  943. }
  944. }