Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Typo, docs , remove obsolete vars in writer

  • Loading branch information...
commit 1713b1a277f2c5f324741a56330489aad58fc34b 1 parent 754e5ff
@akaspin authored
Showing with 27 additions and 14 deletions.
  1. +1 −0  chain/cookie.js
  2. +5 −9 chain/writer.js
  3. +20 −4 chain/writer.md
  4. +1 −1  http.js
View
1  chain/cookie.js
@@ -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 = {};
View
14 chain/writer.js
@@ -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"};
@@ -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;
};
@@ -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);
@@ -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));
};
@@ -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') {
@@ -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);
@@ -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);
@@ -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);
};
View
24 chain/writer.md
@@ -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);
+
View
2  http.js
@@ -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 = '';
Please sign in to comment.
Something went wrong with that request. Please try again.