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