lib: turn on strict mode
[platform/upstream/nodejs.git] / lib / _http_outgoing.js
index 25786ed..cef135c 100644 (file)
@@ -19,6 +19,8 @@
 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 // USE OR OTHER DEALINGS IN THE SOFTWARE.
 
+'use strict';
+
 var assert = require('assert').ok;
 var Stream = require('stream');
 var timers = require('timers');
@@ -82,9 +84,13 @@ function OutgoingMessage() {
 
   this.finished = false;
   this._hangupClose = false;
+  this._headerSent = false;
 
   this.socket = null;
   this.connection = null;
+  this._header = null;
+  this._headers = null;
+  this._headerNames = {};
 }
 util.inherits(OutgoingMessage, Stream);
 
@@ -228,7 +234,7 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
   }
 
   // Date header
-  if (this.sendDate == true && state.sentDateHeader == false) {
+  if (this.sendDate === true && state.sentDateHeader === false) {
     state.messageHeader += 'Date: ' + utcDate() + CRLF;
   }
 
@@ -244,7 +250,7 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
   // of creating security liabilities, so suppress the zero chunk and force
   // the connection to close.
   var statusCode = this.statusCode;
-  if ((statusCode == 204 || statusCode === 304) &&
+  if ((statusCode === 204 || statusCode === 304) &&
       this.chunkedEncoding === true) {
     debug(statusCode + ' response should not use chunked encoding,' +
           ' closing connection.');
@@ -269,8 +275,8 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) {
     }
   }
 
-  if (state.sentContentLengthHeader == false &&
-      state.sentTransferEncodingHeader == false) {
+  if (state.sentContentLengthHeader === false &&
+      state.sentTransferEncodingHeader === false) {
     if (this._hasBody && !this._removedHeader['transfer-encoding']) {
       if (this.useChunkedEncodingByDefault) {
         state.messageHeader += 'Transfer-Encoding: chunked\r\n';
@@ -323,23 +329,22 @@ function storeHeader(self, state, field, value) {
 
 
 OutgoingMessage.prototype.setHeader = function(name, value) {
-  if (arguments.length < 2) {
-    throw new Error('`name` and `value` are required for setHeader().');
-  }
-
-  if (this._header) {
+  if (typeof name !== 'string')
+    throw new TypeError('"name" should be a string');
+  if (value === undefined)
+    throw new Error('"name" and "value" are required for setHeader().');
+  if (this._header)
     throw new Error('Can\'t set headers after they are sent.');
-  }
+
+  if (this._headers === null)
+    this._headers = {};
 
   var key = name.toLowerCase();
-  this._headers = this._headers || {};
-  this._headerNames = this._headerNames || {};
   this._headers[key] = value;
   this._headerNames[key] = name;
 
-  if (automaticHeaders[key]) {
+  if (automaticHeaders[key])
     this._removedHeader[key] = false;
-  }
 };
 
 
@@ -387,6 +392,7 @@ OutgoingMessage.prototype._renderHeaders = function() {
 
   var headers = {};
   var keys = Object.keys(this._headers);
+
   for (var i = 0, l = keys.length; i < l; i++) {
     var key = keys[i];
     headers[this._headerNames[key]] = this._headers[key];
@@ -403,6 +409,18 @@ Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
 
 
 OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
+  var self = this;
+
+  if (this.finished) {
+    var err = new Error('write after end');
+    process.nextTick(function() {
+      self.emit('error', err);
+      if (callback) callback(err);
+    });
+
+    return true;
+  }
+
   if (!this._header) {
     this._implicitHeader();
   }
@@ -528,7 +546,7 @@ OutgoingMessage.prototype.end = function(data, encoding, callback) {
     ret = this.write(data, encoding);
   }
 
-  if (this.chunkedEncoding) {
+  if (this._hasBody && this.chunkedEncoding) {
     ret = this._send('0\r\n' + this._trailer + '\r\n', 'binary', finish);
   } else {
     // Force a flush, HACK.
@@ -595,3 +613,12 @@ OutgoingMessage.prototype._flush = function() {
     }
   }
 };
+
+
+OutgoingMessage.prototype.flush = function() {
+  if (!this._header) {
+    // Force-flush the headers.
+    this._implicitHeader();
+    this._send('');
+  }
+};