Spaces:
Running
Running
File size: 4,687 Bytes
05a77ff |
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
/*
* response-stream.js: A Stream focused on writing any relevant information to
* a raw http.ServerResponse object.
*
* (C) 2011, Charlie Robbins & the Contributors
* MIT LICENSE
*
*/
var util = require('util'),
HttpStream = require('./http-stream');
var STATUS_CODES = require('http').STATUS_CODES;
//
// ### function ResponseStream (options)
//
//
var ResponseStream = module.exports = function (options) {
var self = this,
key;
options = options || {};
HttpStream.call(this, options);
this.writeable = true;
this.response = options.response;
if (options.headers) {
for (key in options.headers) {
this.response.setHeader(key, options.headers[key]);
}
}
//
// Proxy `statusCode` changes to the actual `response.statusCode`.
//
Object.defineProperty(this, 'statusCode', {
get: function () {
return self.response.statusCode;
},
set: function (value) {
self.response.statusCode = value;
},
enumerable: true,
configurable: true
});
if (this.response) {
this._headers = this.response._headers = this.response._headers || {};
// Patch to node core
this.response._headerNames = this.response._headerNames || {};
//
// Proxy to emit "header" event
//
this._renderHeaders = this.response._renderHeaders;
this.response._renderHeaders = function () {
if (!self._emittedHeader) {
self._emittedHeader = true;
self.headerSent = true;
self._header = true;
self.emit('header');
}
return self._renderHeaders.call(self.response);
};
}
};
util.inherits(ResponseStream, HttpStream);
ResponseStream.prototype.writeHead = function (statusCode, statusMessage, headers) {
if (typeof statusMessage === 'string') {
this.response.statusMessage = statusMessage;
} else {
this.response.statusMessage = this.response.statusMessage
|| STATUS_CODES[statusCode] || 'unknown';
headers = statusMessage;
}
this.response.statusCode = statusCode;
if (headers) {
var keys = Object.keys(headers);
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
if (k) this.response.setHeader(k, headers[k]);
}
}
};
//
// Create pass-thru for the necessary
// `http.ServerResponse` methods.
//
['setHeader', 'getHeader', 'removeHeader', '_implicitHeader', 'addTrailers'].forEach(function (method) {
ResponseStream.prototype[method] = function () {
return this.response[method].apply(this.response, arguments);
};
});
ResponseStream.prototype.json = function (obj) {
if (!this.response.writable) {
return;
}
if (typeof obj === 'number') {
this.response.statusCode = obj;
obj = arguments[1];
}
this.modified = true;
if (!this.response._header && this.response.getHeader('content-type') !== 'application/json') {
this.response.setHeader('content-type', 'application/json');
}
this.end(obj ? JSON.stringify(obj) : '');
};
ResponseStream.prototype.html = function (str) {
if (!this.response.writable) {
return;
}
if (typeof str === 'number') {
this.response.statusCode = str;
str = arguments[1];
}
this.modified = true;
if (!this.response._header && this.response.getHeader('content-type') !== 'text/html') {
this.response.setHeader('content-type', 'text/html');
}
this.end(str ? str: '');
};
ResponseStream.prototype.text = function (str) {
if (!this.response.writable) {
return;
}
if (typeof str === 'number') {
this.response.statusCode = str;
str = arguments[1];
}
this.modified = true;
if (!this.response._header && this.response.getHeader('content-type') !== 'text/plain') {
this.response.setHeader('content-type', 'text/plain');
}
this.end(str ? str: '');
};
ResponseStream.prototype.end = function (data) {
if (data && this.writable) {
this.emit('data', data);
}
this.modified = true;
this.emit('end');
};
ResponseStream.prototype.pipe = function () {
var self = this,
dest;
self.dest = dest = HttpStream.prototype.pipe.apply(self, arguments);
dest.on('drain', function() {
self.emit('drain')
})
return dest;
};
ResponseStream.prototype.write = function (data) {
this.modified = true;
if (this.writable) {
return this.dest.write(data);
}
};
ResponseStream.prototype.redirect = function (path, status) {
var url = '';
if (~path.indexOf('://')) {
url = path;
} else {
url += this.req.connection.encrypted ? 'https://' : 'http://';
url += this.req.headers.host;
url += (path[0] === '/') ? path : '/' + path;
}
this.res.writeHead(status || 302, { 'Location': url });
this.end();
};
|