1 | 'use strict'
|
2 |
|
3 |
|
4 | exports.writeHead = function writeHead (statusCode, reason, obj) {
|
5 | var headers
|
6 |
|
7 | if (typeof reason === 'string') {
|
8 |
|
9 | this.statusMessage = reason
|
10 | } else {
|
11 |
|
12 | this.statusMessage =
|
13 | this.statusMessage || 'unknown'
|
14 | obj = reason
|
15 | }
|
16 | this.statusCode = statusCode
|
17 |
|
18 | if (this._headers) {
|
19 |
|
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 |
|
28 | headers = this._renderHeaders()
|
29 | } else {
|
30 |
|
31 | headers = obj
|
32 | }
|
33 |
|
34 | if (statusCode === 204 || statusCode === 304 ||
|
35 | (statusCode >= 100 && statusCode <= 199)) {
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | this._hasBody = false
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 | if (this._expect_continue && !this._sent100) {
|
52 | this.shouldKeepAlive = false
|
53 | }
|
54 |
|
55 |
|
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 |
|
62 | exports.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 |
|
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 |
|
82 | exports.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 |
|
96 | exports.writeContinue = function writeContinue (callback) {
|
97 | if (this.socket._handle) {
|
98 | this.socket._handle._spdyState.stream.respond(100, {}, callback)
|
99 | }
|
100 | }
|