index.js 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*!
  2. * send
  3. * Copyright(c) 2012 TJ Holowaychuk
  4. * Copyright(c) 2014-2016 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', createError(status, err, {
  221. expose: false
  222. }))
  223. }
  224. var res = this.res
  225. var msg = statuses[status] || String(status)
  226. var doc = createHtmlDocument('Error', escapeHtml(msg))
  227. // clear existing headers
  228. clearHeaders(res)
  229. // add error headers
  230. if (err && err.headers) {
  231. setHeaders(res, err.headers)
  232. }
  233. // send basic response
  234. res.statusCode = status
  235. res.setHeader('Content-Type', 'text/html; charset=UTF-8')
  236. res.setHeader('Content-Length', Buffer.byteLength(doc))
  237. res.setHeader('Content-Security-Policy', "default-src 'self'")
  238. res.setHeader('X-Content-Type-Options', 'nosniff')
  239. res.end(doc)
  240. }
  241. /**
  242. * Check if the pathname ends with "/".
  243. *
  244. * @return {boolean}
  245. * @private
  246. */
  247. SendStream.prototype.hasTrailingSlash = function hasTrailingSlash () {
  248. return this.path[this.path.length - 1] === '/'
  249. }
  250. /**
  251. * Check if this is a conditional GET request.
  252. *
  253. * @return {Boolean}
  254. * @api private
  255. */
  256. SendStream.prototype.isConditionalGET = function isConditionalGET () {
  257. return this.req.headers['if-match'] ||
  258. this.req.headers['if-unmodified-since'] ||
  259. this.req.headers['if-none-match'] ||
  260. this.req.headers['if-modified-since']
  261. }
  262. /**
  263. * Check if the request preconditions failed.
  264. *
  265. * @return {boolean}
  266. * @private
  267. */
  268. SendStream.prototype.isPreconditionFailure = function isPreconditionFailure () {
  269. var req = this.req
  270. var res = this.res
  271. // if-match
  272. var match = req.headers['if-match']
  273. if (match) {
  274. var etag = res.getHeader('ETag')
  275. return !etag || (match !== '*' && parseTokenList(match).every(function (match) {
  276. return match !== etag && match !== 'W/' + etag && 'W/' + match !== etag
  277. }))
  278. }
  279. // if-unmodified-since
  280. var unmodifiedSince = parseHttpDate(req.headers['if-unmodified-since'])
  281. if (!isNaN(unmodifiedSince)) {
  282. var lastModified = parseHttpDate(res.getHeader('Last-Modified'))
  283. return isNaN(lastModified) || lastModified > unmodifiedSince
  284. }
  285. return false
  286. }
  287. /**
  288. * Strip content-* header fields.
  289. *
  290. * @private
  291. */
  292. SendStream.prototype.removeContentHeaderFields = function removeContentHeaderFields () {
  293. var res = this.res
  294. var headers = getHeaderNames(res)
  295. for (var i = 0; i < headers.length; i++) {
  296. var header = headers[i]
  297. if (header.substr(0, 8) === 'content-' && header !== 'content-location') {
  298. res.removeHeader(header)
  299. }
  300. }
  301. }
  302. /**
  303. * Respond with 304 not modified.
  304. *
  305. * @api private
  306. */
  307. SendStream.prototype.notModified = function notModified () {
  308. var res = this.res
  309. debug('not modified')
  310. this.removeContentHeaderFields()
  311. res.statusCode = 304
  312. res.end()
  313. }
  314. /**
  315. * Raise error that headers already sent.
  316. *
  317. * @api private
  318. */
  319. SendStream.prototype.headersAlreadySent = function headersAlreadySent () {
  320. var err = new Error('Can\'t set headers after they are sent.')
  321. debug('headers already sent')
  322. this.error(500, err)
  323. }
  324. /**
  325. * Check if the request is cacheable, aka
  326. * responded with 2xx or 304 (see RFC 2616 section 14.2{5,6}).
  327. *
  328. * @return {Boolean}
  329. * @api private
  330. */
  331. SendStream.prototype.isCachable = function isCachable () {
  332. var statusCode = this.res.statusCode
  333. return (statusCode >= 200 && statusCode < 300) ||
  334. statusCode === 304
  335. }
  336. /**
  337. * Handle stat() error.
  338. *
  339. * @param {Error} error
  340. * @private
  341. */
  342. SendStream.prototype.onStatError = function onStatError (error) {
  343. switch (error.code) {
  344. case 'ENAMETOOLONG':
  345. case 'ENOENT':
  346. case 'ENOTDIR':
  347. this.error(404, error)
  348. break
  349. default:
  350. this.error(500, error)
  351. break
  352. }
  353. }
  354. /**
  355. * Check if the cache is fresh.
  356. *
  357. * @return {Boolean}
  358. * @api private
  359. */
  360. SendStream.prototype.isFresh = function isFresh () {
  361. return fresh(this.req.headers, {
  362. 'etag': this.res.getHeader('ETag'),
  363. 'last-modified': this.res.getHeader('Last-Modified')
  364. })
  365. }
  366. /**
  367. * Check if the range is fresh.
  368. *
  369. * @return {Boolean}
  370. * @api private
  371. */
  372. SendStream.prototype.isRangeFresh = function isRangeFresh () {
  373. var ifRange = this.req.headers['if-range']
  374. if (!ifRange) {
  375. return true
  376. }
  377. // if-range as etag
  378. if (ifRange.indexOf('"') !== -1) {
  379. var etag = this.res.getHeader('ETag')
  380. return Boolean(etag && ifRange.indexOf(etag) !== -1)
  381. }
  382. // if-range as modified date
  383. var lastModified = this.res.getHeader('Last-Modified')
  384. return parseHttpDate(lastModified) <= parseHttpDate(ifRange)
  385. }
  386. /**
  387. * Redirect to path.
  388. *
  389. * @param {string} path
  390. * @private
  391. */
  392. SendStream.prototype.redirect = function redirect (path) {
  393. var res = this.res
  394. if (hasListeners(this, 'directory')) {
  395. this.emit('directory', res, path)
  396. return
  397. }
  398. if (this.hasTrailingSlash()) {
  399. this.error(403)
  400. return
  401. }
  402. var loc = encodeUrl(collapseLeadingSlashes(this.path + '/'))
  403. var doc = createHtmlDocument('Redirecting', 'Redirecting to <a href="' + escapeHtml(loc) + '">' +
  404. escapeHtml(loc) + '</a>')
  405. // redirect
  406. res.statusCode = 301
  407. res.setHeader('Content-Type', 'text/html; charset=UTF-8')
  408. res.setHeader('Content-Length', Buffer.byteLength(doc))
  409. res.setHeader('Content-Security-Policy', "default-src 'self'")
  410. res.setHeader('X-Content-Type-Options', 'nosniff')
  411. res.setHeader('Location', loc)
  412. res.end(doc)
  413. }
  414. /**
  415. * Pipe to `res.
  416. *
  417. * @param {Stream} res
  418. * @return {Stream} res
  419. * @api public
  420. */
  421. SendStream.prototype.pipe = function pipe (res) {
  422. // root path
  423. var root = this._root
  424. // references
  425. this.res = res
  426. // decode the path
  427. var path = decode(this.path)
  428. if (path === -1) {
  429. this.error(400)
  430. return res
  431. }
  432. // null byte(s)
  433. if (~path.indexOf('\0')) {
  434. this.error(400)
  435. return res
  436. }
  437. var parts
  438. if (root !== null) {
  439. // normalize
  440. if (path) {
  441. path = normalize('.' + sep + path)
  442. }
  443. // malicious path
  444. if (UP_PATH_REGEXP.test(path)) {
  445. debug('malicious path "%s"', path)
  446. this.error(403)
  447. return res
  448. }
  449. // explode path parts
  450. parts = path.split(sep)
  451. // join / normalize from optional root dir
  452. path = normalize(join(root, path))
  453. root = normalize(root + sep)
  454. } else {
  455. // ".." is malicious without "root"
  456. if (UP_PATH_REGEXP.test(path)) {
  457. debug('malicious path "%s"', path)
  458. this.error(403)
  459. return res
  460. }
  461. // explode path parts
  462. parts = normalize(path).split(sep)
  463. // resolve the path
  464. path = resolve(path)
  465. }
  466. // dotfile handling
  467. if (containsDotFile(parts)) {
  468. var access = this._dotfiles
  469. // legacy support
  470. if (access === undefined) {
  471. access = parts[parts.length - 1][0] === '.'
  472. ? (this._hidden ? 'allow' : 'ignore')
  473. : 'allow'
  474. }
  475. debug('%s dotfile "%s"', access, path)
  476. switch (access) {
  477. case 'allow':
  478. break
  479. case 'deny':
  480. this.error(403)
  481. return res
  482. case 'ignore':
  483. default:
  484. this.error(404)
  485. return res
  486. }
  487. }
  488. // index file support
  489. if (this._index.length && this.hasTrailingSlash()) {
  490. this.sendIndex(path)
  491. return res
  492. }
  493. this.sendFile(path)
  494. return res
  495. }
  496. /**
  497. * Transfer `path`.
  498. *
  499. * @param {String} path
  500. * @api public
  501. */
  502. SendStream.prototype.send = function send (path, stat) {
  503. var len = stat.size
  504. var options = this.options
  505. var opts = {}
  506. var res = this.res
  507. var req = this.req
  508. var ranges = req.headers.range
  509. var offset = options.start || 0
  510. if (headersSent(res)) {
  511. // impossible to send now
  512. this.headersAlreadySent()
  513. return
  514. }
  515. debug('pipe "%s"', path)
  516. // set header fields
  517. this.setHeader(path, stat)
  518. // set content-type
  519. this.type(path)
  520. // conditional GET support
  521. if (this.isConditionalGET()) {
  522. if (this.isPreconditionFailure()) {
  523. this.error(412)
  524. return
  525. }
  526. if (this.isCachable() && this.isFresh()) {
  527. this.notModified()
  528. return
  529. }
  530. }
  531. // adjust len to start/end options
  532. len = Math.max(0, len - offset)
  533. if (options.end !== undefined) {
  534. var bytes = options.end - offset + 1
  535. if (len > bytes) len = bytes
  536. }
  537. // Range support
  538. if (this._acceptRanges && BYTES_RANGE_REGEXP.test(ranges)) {
  539. // parse
  540. ranges = parseRange(len, ranges, {
  541. combine: true
  542. })
  543. // If-Range support
  544. if (!this.isRangeFresh()) {
  545. debug('range stale')
  546. ranges = -2
  547. }
  548. // unsatisfiable
  549. if (ranges === -1) {
  550. debug('range unsatisfiable')
  551. // Content-Range
  552. res.setHeader('Content-Range', contentRange('bytes', len))
  553. // 416 Requested Range Not Satisfiable
  554. return this.error(416, {
  555. headers: {'Content-Range': res.getHeader('Content-Range')}
  556. })
  557. }
  558. // valid (syntactically invalid/multiple ranges are treated as a regular response)
  559. if (ranges !== -2 && ranges.length === 1) {
  560. debug('range %j', ranges)
  561. // Content-Range
  562. res.statusCode = 206
  563. res.setHeader('Content-Range', contentRange('bytes', len, ranges[0]))
  564. // adjust for requested range
  565. offset += ranges[0].start
  566. len = ranges[0].end - ranges[0].start + 1
  567. }
  568. }
  569. // clone options
  570. for (var prop in options) {
  571. opts[prop] = options[prop]
  572. }
  573. // set read options
  574. opts.start = offset
  575. opts.end = Math.max(offset, offset + len - 1)
  576. // content-length
  577. res.setHeader('Content-Length', len)
  578. // HEAD support
  579. if (req.method === 'HEAD') {
  580. res.end()
  581. return
  582. }
  583. this.stream(path, opts)
  584. }
  585. /**
  586. * Transfer file for `path`.
  587. *
  588. * @param {String} path
  589. * @api private
  590. */
  591. SendStream.prototype.sendFile = function sendFile (path) {
  592. var i = 0
  593. var self = this
  594. debug('stat "%s"', path)
  595. fs.stat(path, function onstat (err, stat) {
  596. if (err && err.code === 'ENOENT' && !extname(path) && path[path.length - 1] !== sep) {
  597. // not found, check extensions
  598. return next(err)
  599. }
  600. if (err) return self.onStatError(err)
  601. if (stat.isDirectory()) return self.redirect(path)
  602. self.emit('file', path, stat)
  603. self.send(path, stat)
  604. })
  605. function next (err) {
  606. if (self._extensions.length <= i) {
  607. return err
  608. ? self.onStatError(err)
  609. : self.error(404)
  610. }
  611. var p = path + '.' + self._extensions[i++]
  612. debug('stat "%s"', p)
  613. fs.stat(p, function (err, stat) {
  614. if (err) return next(err)
  615. if (stat.isDirectory()) return next()
  616. self.emit('file', p, stat)
  617. self.send(p, stat)
  618. })
  619. }
  620. }
  621. /**
  622. * Transfer index for `path`.
  623. *
  624. * @param {String} path
  625. * @api private
  626. */
  627. SendStream.prototype.sendIndex = function sendIndex (path) {
  628. var i = -1
  629. var self = this
  630. function next (err) {
  631. if (++i >= self._index.length) {
  632. if (err) return self.onStatError(err)
  633. return self.error(404)
  634. }
  635. var p = join(path, self._index[i])
  636. debug('stat "%s"', p)
  637. fs.stat(p, function (err, stat) {
  638. if (err) return next(err)
  639. if (stat.isDirectory()) return next()
  640. self.emit('file', p, stat)
  641. self.send(p, stat)
  642. })
  643. }
  644. next()
  645. }
  646. /**
  647. * Stream `path` to the response.
  648. *
  649. * @param {String} path
  650. * @param {Object} options
  651. * @api private
  652. */
  653. SendStream.prototype.stream = function stream (path, options) {
  654. // TODO: this is all lame, refactor meeee
  655. var finished = false
  656. var self = this
  657. var res = this.res
  658. // pipe
  659. var stream = fs.createReadStream(path, options)
  660. this.emit('stream', stream)
  661. stream.pipe(res)
  662. // response finished, done with the fd
  663. onFinished(res, function onfinished () {
  664. finished = true
  665. destroy(stream)
  666. })
  667. // error handling code-smell
  668. stream.on('error', function onerror (err) {
  669. // request already finished
  670. if (finished) return
  671. // clean up stream
  672. finished = true
  673. destroy(stream)
  674. // error
  675. self.onStatError(err)
  676. })
  677. // end
  678. stream.on('end', function onend () {
  679. self.emit('end')
  680. })
  681. }
  682. /**
  683. * Set content-type based on `path`
  684. * if it hasn't been explicitly set.
  685. *
  686. * @param {String} path
  687. * @api private
  688. */
  689. SendStream.prototype.type = function type (path) {
  690. var res = this.res
  691. if (res.getHeader('Content-Type')) return
  692. var type = mime.lookup(path)
  693. if (!type) {
  694. debug('no content-type')
  695. return
  696. }
  697. var charset = mime.charsets.lookup(type)
  698. debug('content-type %s', type)
  699. res.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''))
  700. }
  701. /**
  702. * Set response header fields, most
  703. * fields may be pre-defined.
  704. *
  705. * @param {String} path
  706. * @param {Object} stat
  707. * @api private
  708. */
  709. SendStream.prototype.setHeader = function setHeader (path, stat) {
  710. var res = this.res
  711. this.emit('headers', res, path, stat)
  712. if (this._acceptRanges && !res.getHeader('Accept-Ranges')) {
  713. debug('accept ranges')
  714. res.setHeader('Accept-Ranges', 'bytes')
  715. }
  716. if (this._cacheControl && !res.getHeader('Cache-Control')) {
  717. var cacheControl = 'public, max-age=' + Math.floor(this._maxage / 1000)
  718. if (this._immutable) {
  719. cacheControl += ', immutable'
  720. }
  721. debug('cache-control %s', cacheControl)
  722. res.setHeader('Cache-Control', cacheControl)
  723. }
  724. if (this._lastModified && !res.getHeader('Last-Modified')) {
  725. var modified = stat.mtime.toUTCString()
  726. debug('modified %s', modified)
  727. res.setHeader('Last-Modified', modified)
  728. }
  729. if (this._etag && !res.getHeader('ETag')) {
  730. var val = etag(stat)
  731. debug('etag %s', val)
  732. res.setHeader('ETag', val)
  733. }
  734. }
  735. /**
  736. * Clear all headers from a response.
  737. *
  738. * @param {object} res
  739. * @private
  740. */
  741. function clearHeaders (res) {
  742. var headers = getHeaderNames(res)
  743. for (var i = 0; i < headers.length; i++) {
  744. res.removeHeader(headers[i])
  745. }
  746. }
  747. /**
  748. * Collapse all leading slashes into a single slash
  749. *
  750. * @param {string} str
  751. * @private
  752. */
  753. function collapseLeadingSlashes (str) {
  754. for (var i = 0; i < str.length; i++) {
  755. if (str[i] !== '/') {
  756. break
  757. }
  758. }
  759. return i > 1
  760. ? '/' + str.substr(i)
  761. : str
  762. }
  763. /**
  764. * Determine if path parts contain a dotfile.
  765. *
  766. * @api private
  767. */
  768. function containsDotFile (parts) {
  769. for (var i = 0; i < parts.length; i++) {
  770. var part = parts[i]
  771. if (part.length > 1 && part[0] === '.') {
  772. return true
  773. }
  774. }
  775. return false
  776. }
  777. /**
  778. * Create a Content-Range header.
  779. *
  780. * @param {string} type
  781. * @param {number} size
  782. * @param {array} [range]
  783. */
  784. function contentRange (type, size, range) {
  785. return type + ' ' + (range ? range.start + '-' + range.end : '*') + '/' + size
  786. }
  787. /**
  788. * Create a minimal HTML document.
  789. *
  790. * @param {string} title
  791. * @param {string} body
  792. * @private
  793. */
  794. function createHtmlDocument (title, body) {
  795. return '<!DOCTYPE html>\n' +
  796. '<html lang="en">\n' +
  797. '<head>\n' +
  798. '<meta charset="utf-8">\n' +
  799. '<title>' + title + '</title>\n' +
  800. '</head>\n' +
  801. '<body>\n' +
  802. '<pre>' + body + '</pre>\n' +
  803. '</body>\n' +
  804. '</html>\n'
  805. }
  806. /**
  807. * decodeURIComponent.
  808. *
  809. * Allows V8 to only deoptimize this fn instead of all
  810. * of send().
  811. *
  812. * @param {String} path
  813. * @api private
  814. */
  815. function decode (path) {
  816. try {
  817. return decodeURIComponent(path)
  818. } catch (err) {
  819. return -1
  820. }
  821. }
  822. /**
  823. * Get the header names on a respnse.
  824. *
  825. * @param {object} res
  826. * @returns {array[string]}
  827. * @private
  828. */
  829. function getHeaderNames (res) {
  830. return typeof res.getHeaderNames !== 'function'
  831. ? Object.keys(res._headers || {})
  832. : res.getHeaderNames()
  833. }
  834. /**
  835. * Determine if emitter has listeners of a given type.
  836. *
  837. * The way to do this check is done three different ways in Node.js >= 0.8
  838. * so this consolidates them into a minimal set using instance methods.
  839. *
  840. * @param {EventEmitter} emitter
  841. * @param {string} type
  842. * @returns {boolean}
  843. * @private
  844. */
  845. function hasListeners (emitter, type) {
  846. var count = typeof emitter.listenerCount !== 'function'
  847. ? emitter.listeners(type).length
  848. : emitter.listenerCount(type)
  849. return count > 0
  850. }
  851. /**
  852. * Determine if the response headers have been sent.
  853. *
  854. * @param {object} res
  855. * @returns {boolean}
  856. * @private
  857. */
  858. function headersSent (res) {
  859. return typeof res.headersSent !== 'boolean'
  860. ? Boolean(res._header)
  861. : res.headersSent
  862. }
  863. /**
  864. * Normalize the index option into an array.
  865. *
  866. * @param {boolean|string|array} val
  867. * @param {string} name
  868. * @private
  869. */
  870. function normalizeList (val, name) {
  871. var list = [].concat(val || [])
  872. for (var i = 0; i < list.length; i++) {
  873. if (typeof list[i] !== 'string') {
  874. throw new TypeError(name + ' must be array of strings or false')
  875. }
  876. }
  877. return list
  878. }
  879. /**
  880. * Parse an HTTP Date into a number.
  881. *
  882. * @param {string} date
  883. * @private
  884. */
  885. function parseHttpDate (date) {
  886. var timestamp = date && Date.parse(date)
  887. return typeof timestamp === 'number'
  888. ? timestamp
  889. : NaN
  890. }
  891. /**
  892. * Parse a HTTP token list.
  893. *
  894. * @param {string} str
  895. * @private
  896. */
  897. function parseTokenList (str) {
  898. var end = 0
  899. var list = []
  900. var start = 0
  901. // gather tokens
  902. for (var i = 0, len = str.length; i < len; i++) {
  903. switch (str.charCodeAt(i)) {
  904. case 0x20: /* */
  905. if (start === end) {
  906. start = end = i + 1
  907. }
  908. break
  909. case 0x2c: /* , */
  910. list.push(str.substring(start, end))
  911. start = end = i + 1
  912. break
  913. default:
  914. end = i + 1
  915. break
  916. }
  917. }
  918. // final token
  919. list.push(str.substring(start, end))
  920. return list
  921. }
  922. /**
  923. * Set an object of headers on a response.
  924. *
  925. * @param {object} res
  926. * @param {object} headers
  927. * @private
  928. */
  929. function setHeaders (res, headers) {
  930. var keys = Object.keys(headers)
  931. for (var i = 0; i < keys.length; i++) {
  932. var key = keys[i]
  933. res.setHeader(key, headers[key])
  934. }
  935. }