1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | var EventEmitter = require('events').EventEmitter;
|
6 | var util = require('util');
|
7 | var assert = require('assert-plus');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function InvalidUpgradeStateError(msg) {
|
17 | if (Error.captureStackTrace) {
|
18 | Error.captureStackTrace(this, InvalidUpgradeStateError);
|
19 | }
|
20 |
|
21 | this.message = msg;
|
22 | this.name = 'InvalidUpgradeStateError';
|
23 | }
|
24 | util.inherits(InvalidUpgradeStateError, Error);
|
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 | function createServerUpgradeResponse(req, socket, head) {
|
62 | return new ServerUpgradeResponse(socket, head);
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function ServerUpgradeResponse(socket, head) {
|
76 | assert.object(socket, 'socket');
|
77 | assert.buffer(head, 'head');
|
78 |
|
79 | EventEmitter.call(this);
|
80 |
|
81 | this.sendDate = true;
|
82 | this.statusCode = 400;
|
83 |
|
84 | this._upgrade = {
|
85 | socket: socket,
|
86 | head: head
|
87 | };
|
88 |
|
89 | this._headWritten = false;
|
90 | this._upgradeClaimed = false;
|
91 | }
|
92 | util.inherits(ServerUpgradeResponse, EventEmitter);
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | function notImplemented(method) {
|
104 | if (!method.throws) {
|
105 | return function returns() {
|
106 | return method.returns;
|
107 | };
|
108 | } else {
|
109 | return function throws() {
|
110 | throw new Error('Method ' + method.name + ' is not implemented!');
|
111 | };
|
112 | }
|
113 | }
|
114 |
|
115 | var NOT_IMPLEMENTED = [
|
116 | { name: 'writeContinue', throws: true },
|
117 | { name: 'setHeader', throws: false, returns: null },
|
118 | { name: 'getHeader', throws: false, returns: null },
|
119 | { name: 'getHeaders', throws: false, returns: {} },
|
120 | { name: 'removeHeader', throws: false, returns: null },
|
121 | { name: 'addTrailer', throws: false, returns: null },
|
122 | { name: 'cache', throws: false, returns: 'public' },
|
123 | { name: 'format', throws: true },
|
124 | { name: 'set', throws: false, returns: null },
|
125 | { name: 'get', throws: false, returns: null },
|
126 | { name: 'headers', throws: false, returns: {} },
|
127 | { name: 'header', throws: false, returns: null },
|
128 | { name: 'json', throws: false, returns: null },
|
129 | { name: 'link', throws: false, returns: null }
|
130 | ];
|
131 |
|
132 |
|
133 | NOT_IMPLEMENTED.forEach(function forEach(method) {
|
134 | ServerUpgradeResponse.prototype[method.name] = notImplemented(method);
|
135 | });
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | ServerUpgradeResponse.prototype._writeHeadImpl = function _writeHeadImpl(
|
147 | statusCode,
|
148 | reason
|
149 | ) {
|
150 | if (this._headWritten) {
|
151 | return;
|
152 | }
|
153 | this._headWritten = true;
|
154 |
|
155 | if (this._upgradeClaimed) {
|
156 | throw new InvalidUpgradeStateError('Upgrade already claimed!');
|
157 | }
|
158 |
|
159 | var head = ['HTTP/1.1 ' + statusCode + ' ' + reason, 'Connection: close'];
|
160 |
|
161 | if (this.sendDate) {
|
162 | head.push('Date: ' + new Date().toUTCString());
|
163 | }
|
164 |
|
165 | this._upgrade.socket.write(head.join('\r\n') + '\r\n');
|
166 | };
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | ServerUpgradeResponse.prototype.status = function status(code) {
|
177 | assert.number(code, 'code');
|
178 | this.statusCode = code;
|
179 | return code;
|
180 | };
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | ServerUpgradeResponse.prototype.send = function send(code, body) {
|
192 | if (typeof code === 'number') {
|
193 | this.statusCode = code;
|
194 | } else {
|
195 | body = code;
|
196 | }
|
197 |
|
198 | if (typeof body === 'object') {
|
199 | if (typeof body.statusCode === 'number') {
|
200 | this.statusCode = body.statusCode;
|
201 | }
|
202 |
|
203 | if (typeof body.message === 'string') {
|
204 | this.statusReason = body.message;
|
205 | }
|
206 | }
|
207 |
|
208 | return this.end();
|
209 | };
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | ServerUpgradeResponse.prototype.end = function end() {
|
219 | this._writeHeadImpl(this.statusCode, 'Connection Not Upgraded');
|
220 | this._upgrade.socket.end('\r\n');
|
221 | return true;
|
222 | };
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | ServerUpgradeResponse.prototype.write = function write() {
|
232 | this._writeHeadImpl(this.statusCode, 'Connection Not Upgraded');
|
233 | return true;
|
234 | };
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | ServerUpgradeResponse.prototype.writeHead = function writeHead(
|
246 | statusCode,
|
247 | reason
|
248 | ) {
|
249 | assert.number(statusCode, 'statusCode');
|
250 | assert.optionalString(reason, 'reason');
|
251 |
|
252 | this.statusCode = statusCode;
|
253 |
|
254 | if (!reason) {
|
255 | reason = 'Connection Not Upgraded';
|
256 | }
|
257 |
|
258 | if (this._headWritten) {
|
259 | throw new Error('Head already written!');
|
260 | }
|
261 |
|
262 | return this._writeHeadImpl(statusCode, reason);
|
263 | };
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | ServerUpgradeResponse.prototype.claimUpgrade = function claimUpgrade() {
|
273 | if (this._upgradeClaimed) {
|
274 | throw new InvalidUpgradeStateError('Upgrade already claimed!');
|
275 | }
|
276 |
|
277 | if (this._headWritten) {
|
278 | throw new InvalidUpgradeStateError('Upgrade already aborted!');
|
279 | }
|
280 |
|
281 | this._upgradeClaimed = true;
|
282 |
|
283 | return this._upgrade;
|
284 | };
|
285 |
|
286 | module.exports = {
|
287 | createResponse: createServerUpgradeResponse,
|
288 | InvalidUpgradeStateError: InvalidUpgradeStateError
|
289 | };
|