Browse files

Only add socket error handler callback once

  • Loading branch information...
1 parent 9cadd61 commit 6a00fea09eed99257c0aec2bb66fbf109b0f573a @mscdex mscdex committed Jul 27, 2012
Showing with 21 additions and 9 deletions.
  1. +21 −9 main.js
View
30 main.js
@@ -222,6 +222,18 @@ Request.prototype.init = function (options) {
self.emit('error', error)
}
+ self._parserErrorHandler = function (error) {
+ if (this.res) {
+ if (this.res.request) {
+ this.res.request.emit('error', error);
+ } else {
+ this.res.emit('error', error);
+ }
+ } else {
+ this._httpMessage.emit('error', error);
+ }
+ }
+
if (options.form) {
self.form(options.form)
}
@@ -480,28 +492,28 @@ Request.prototype.getAgent = function () {
Request.prototype.start = function () {
var self = this
-
+
if (self._aborted) return
-
+
self._started = true
self.method = self.method || 'GET'
self.href = self.uri.href
if (log) log('%method %href', self)
-
+
if (self.src && self.src.stat && self.src.stat.size) {
self.headers['content-length'] = self.src.stat.size
}
if (self._aws) {
self.aws(self._aws, true)
}
-
+
self.req = self.httpModule.request(self, function (response) {
- response.connection.on('error', function(err) {
- self.emit('error', err);
- });
+ if (response.connection.listeners('error').indexOf(self._parserErrorHandler) === -1) {
+ response.connection.on('error', self._parserErrorHandler);
+ }
if (self._aborted) return
if (self._paused) response.pause()
-
+
self.response = response
response.request = self
response.toJSON = toJSON
@@ -519,7 +531,7 @@ Request.prototype.start = function () {
clearTimeout(self.timeoutTimer)
self.timeoutTimer = null
}
-
+
var addCookie = function (cookie) {
if (self._jar) self._jar.add(new Cookie(cookie))
else cookieJar.add(new Cookie(cookie))

0 comments on commit 6a00fea

Please sign in to comment.