c7611cf1e80861b15c3cd418d2f2bb47d1091a7c2327f7a655a0af1fdeb8a0dcc43ecfece64e271b6f9a4306baa8808b697c5798b1cb5f2ae9782a05149e7e 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict'
  2. // NOTE: Mostly copy paste from node
  3. exports.writeHead = function writeHead (statusCode, reason, obj) {
  4. var headers
  5. if (typeof reason === 'string') {
  6. // writeHead(statusCode, reasonPhrase[, headers])
  7. this.statusMessage = reason
  8. } else {
  9. // writeHead(statusCode[, headers])
  10. this.statusMessage =
  11. this.statusMessage || 'unknown'
  12. obj = reason
  13. }
  14. this.statusCode = statusCode
  15. if (this._headers) {
  16. // Slow-case: when progressive API and header fields are passed.
  17. if (obj) {
  18. var keys = Object.keys(obj)
  19. for (var i = 0; i < keys.length; i++) {
  20. var k = keys[i]
  21. if (k) this.setHeader(k, obj[k])
  22. }
  23. }
  24. // only progressive api is used
  25. headers = this._renderHeaders()
  26. } else {
  27. // only writeHead() called
  28. headers = obj
  29. }
  30. if (statusCode === 204 || statusCode === 304 ||
  31. (statusCode >= 100 && statusCode <= 199)) {
  32. // RFC 2616, 10.2.5:
  33. // The 204 response MUST NOT include a message-body, and thus is always
  34. // terminated by the first empty line after the header fields.
  35. // RFC 2616, 10.3.5:
  36. // The 304 response MUST NOT contain a message-body, and thus is always
  37. // terminated by the first empty line after the header fields.
  38. // RFC 2616, 10.1 Informational 1xx:
  39. // This class of status code indicates a provisional response,
  40. // consisting only of the Status-Line and optional headers, and is
  41. // terminated by an empty line.
  42. this._hasBody = false
  43. }
  44. // don't keep alive connections where the client expects 100 Continue
  45. // but we sent a final status; they may put extra bytes on the wire.
  46. if (this._expect_continue && !this._sent100) {
  47. this.shouldKeepAlive = false
  48. }
  49. // Implicit headers sent!
  50. this._header = true
  51. this._headerSent = true
  52. if (this.socket._handle) { this.socket._handle._spdyState.stream.respond(this.statusCode, headers) }
  53. }
  54. exports.end = function end (data, encoding, callback) {
  55. if (!this._headerSent) {
  56. this.writeHead(this.statusCode)
  57. }
  58. if (!this.socket._handle) {
  59. return
  60. }
  61. // Compatibility with Node.js core
  62. this.finished = true
  63. var self = this
  64. var handle = this.socket._handle
  65. handle._spdyState.ending = true
  66. this.socket.end(data, encoding, function () {
  67. self.constructor.prototype.end.call(self, '', 'utf8', callback)
  68. })
  69. }
  70. exports.push = function push (path, headers, callback) {
  71. var frame = {
  72. path: path,
  73. method: headers.method ? headers.method.toString() : 'GET',
  74. status: headers.status ? parseInt(headers.status, 10) : 200,
  75. host: this._req.headers.host,
  76. headers: headers.request,
  77. response: headers.response
  78. }
  79. var stream = this.spdyStream
  80. return stream.pushPromise(frame, callback)
  81. }
  82. exports.writeContinue = function writeContinue (callback) {
  83. if (this.socket._handle) {
  84. this.socket._handle._spdyState.stream.respond(100, {}, callback)
  85. }
  86. }