UNPKG

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