Skip to content

Commit

Permalink
Typo, docs , remove obsolete vars in writer
Browse files Browse the repository at this point in the history
  • Loading branch information
akaspin committed Nov 10, 2010
1 parent 754e5ff commit 1713b1a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions chain/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ exports.Proc = Proc;
* @param {Boolean} encrypt Encrypt cookie value
*/
Proc.prototype.set = function(name, value, options, encrypt) {
if (this._response._headerSent) return;
options = options || {};
if (!this._outgoing) {
this._outgoing = {};
Expand Down
14 changes: 5 additions & 9 deletions chain/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ function Writer(request, response) {
this._request = request;
this._response = response;

this._headersWritten = false;
this._encoding = 'utf8';
this._statusCode = 200;
this._headers = {'Content-Type': "text/html; charset=UTF-8"};
Expand All @@ -35,7 +34,7 @@ exports.Writer = Writer;
* @param {Number} code HTTP Status code
*/
Writer.prototype.setStatus = function(code) {
if (!this._headersWritten && code in http.STATUS_CODES)
if (!this._response._headerSent && code in http.STATUS_CODES)
this._statusCode = code;
};

Expand All @@ -46,6 +45,7 @@ Writer.prototype.setStatus = function(code) {
* @throws {Error}
*/
Writer.prototype.setHeader = function(name, value) {
if (this._response._headerSent) return;
value = value ? value.toString() : '';
if (value != value.replace(/[\x00-\x1f]/, " ").substring(0, 4000))
throw new Error('Unsafe header value ' + value);
Expand All @@ -61,8 +61,7 @@ Writer.prototype.write = function(data, encoding) {
if (!data) return; // If no data - do nothing
if (!Buffer.isBuffer(data) && typeof data !== 'string')
data = data.toString();

if (!this._headersWritten) this._sendHeaders();
if (!this._response._headerSent) this._sendHeaders();
this._response.write(data, (encoding || this._encoding));
};

Expand All @@ -74,7 +73,7 @@ Writer.prototype.write = function(data, encoding) {
Writer.prototype.end = function(data, encoding) {
if (data && !Buffer.isBuffer(data) && typeof data !== 'string')
data = data.toString();
if (!this._headersWritten) {
if (!this._response._headerSent) {
// if any data present - add some info in headers:
if (!!data && this._statusCode == 200
&& this._request.method == 'GET') {
Expand All @@ -96,8 +95,6 @@ Writer.prototype.end = function(data, encoding) {
}

// Content-Length


if (!("Content-Length" in this._headers)) {
var l = Buffer.isBuffer(data) ? data.length
: Buffer.byteLength(data, encoding || this._encoding);
Expand All @@ -117,7 +114,7 @@ Writer.prototype.end = function(data, encoding) {
*/
Writer.prototype.redirect = function(redirectUrl, permanent) {
permanent = permanent || false;
if (this._headersWritten) {
if (this._response._headerSent) {
throw new Error('Cannot redirect after headers have been written');
}
this.setStatus(permanent ? 301 : 302);
Expand All @@ -130,6 +127,5 @@ Writer.prototype.redirect = function(redirectUrl, permanent) {
* Sends all headers to client.
*/
Writer.prototype._sendHeaders = function() {
this._headersWritten = true;
this._response.writeHead(this._statusCode, this._headers);
};
24 changes: 20 additions & 4 deletions chain/writer.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,25 @@ redirect URL and optional `permanent` flag.
With `permanent` flag, `redirect` send client status `301` instead of `302`.
You can't redirect after after headers have been written.

## Usage with Kaph chain
## Usage in Kaph chain

TODO

## Pitfalls
This module provides *operation* to use with *kaph*. Usage is similar to that
described above. With one difference - operation creates a new object `writer`
inside *kaph* handler.

var http = require('http');
var HttpHandler = require('kaph/http').Handler;
var kaphWriter = require('kaph/chain/writer');

Op = {
GET: function() {
this.writer.end('It\'s works');
}
};

var chain = [kaphWriter.Op, Op];

http.createServer(function(request, response) {
(new HttpHandler(request, response, chain)).next();
}).listen(9080);

2 changes: 1 addition & 1 deletion http.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Handler.prototype.error = function(code, reason) {
*/
Handler.prototype._handleError = function(e, errGen) {
var summary = this.request.method + " " + this.request.url +
'(level ' + this.level + '):';
'(level ' + this._level + '):';
var code = 0;
var message = '';

Expand Down

0 comments on commit 1713b1a

Please sign in to comment.