lib/XMLHttpRequest.js
function responseHandler(resp) {
        // Set response var to the response we got back
        // This is so it remains accessable outside this scope
        response = resp;
        // Check for redirect
        // @TODO Prevent looped redirects
        if (response.statusCode === 301 || response.statusCode === 302 || response.statusCode === 303 || response.statusCode === 307) {
          // Change URL to the redirect location
          settings.url = response.headers.location;
          var url = Url.parse(settings.url);
          // Set host var in case it's used later
          host = url.hostname;
          // Options for the new request
          var newOptions = {
            hostname: url.hostname,
            port: url.port,
            path: url.path,
            method: response.statusCode === 303 ? "GET" : settings.method,
            headers: headers,
            withCredentials: self.withCredentials
          };

          // Issue the new request
          request = doRequest(newOptions, responseHandler).on("error", errorHandler);
          request.end();
          // @TODO Check if an XHR event needs to be fired here
          return;
        }

        response.setEncoding("utf8");

        setState(self.HEADERS_RECEIVED);
        self.status = response.statusCode;

        response.on("data", function(chunk) {
          // Make sure there's some data
          if (chunk) {
            self.responseText += chunk;
          }
          // Don't emit state changes if the connection has been aborted.
          if (sendFlag) {
            setState(self.LOADING);
          }
        });

        response.on("end", function() {
          if (sendFlag) {
            // Discard the end event if the connection has been aborted
            setState(self.DONE);
            sendFlag = false;
          }
        });

        response.on("error", function(error) {
          self.handleError(error);
        });
      }
function responseHandler(resp) {
        // Set response var to the response we got back
        // This is so it remains accessable outside this scope
        response = resp;
        // Check for redirect
        // @TODO Prevent looped redirects
        if (response.statusCode === 301 || response.statusCode === 302 || response.statusCode === 303 || response.statusCode === 307) {
          // Change URL to the redirect location
          settings.url = response.headers.location;
          var url = Url.parse(settings.url);
          // Set host var in case it's used later
          host = url.hostname;
          // Options for the new request
          var newOptions = {
            hostname: url.hostname,
            port: url.port,
            path: url.path,
            method: response.statusCode === 303 ? "GET" : settings.method,
            headers: headers,
            withCredentials: self.withCredentials
          };

          // Issue the new request
          request = doRequest(newOptions, responseHandler).on("error", errorHandler);
          request.end();
          // @TODO Check if an XHR event needs to be fired here
          return;
        }

        response.setEncoding("utf8");

        setState(self.HEADERS_RECEIVED);
        self.status = response.statusCode;

        response.on("data", function(chunk) {
          // Make sure there's some data
          if (chunk) {
            self.responseText += chunk;
          }
          // Don't emit state changes if the connection has been aborted.
          if (sendFlag) {
            setState(self.LOADING);
          }
        });

        response.on("end", function() {
          if (sendFlag) {
            // Discard the end event if the connection has been aborted
            setState(self.DONE);
            sendFlag = false;
          }
        });

        response.on("error", function(error) {
          self.handleError(error);
        });
      }
function responseHandler ( resp ) { // Set response var to the response we got back // This is so it remains accessable outside this scope response = resp ; // Check for redirect // @TODO Prevent looped redirects if ( response . statusCode === 301 || response . statusCode === 302 || response . statusCode === 303 || response . statusCode === 307 ) { // Change URL to the redirect location settings . url = response . headers . location ; var url = Url . parse ( settings . url ) ; // Set host var in case it's used later host = url . hostname ; // Options for the new request var newOptions = { hostname : url . hostname , port : url . port , path : url . path , method : response . statusCode === 303 ? "GET" : settings . method , headers : headers , withCredentials : self . withCredentials } ; // Issue the new request request = doRequest ( newOptions , responseHandler ) . on ( "error" , errorHandler ) ; request . end ( ) ; // @TODO Check if an XHR event needs to be fired here return ; } response . setEncoding ( "utf8" ) ; setState ( self . HEADERS_RECEIVED ) ; self . status = response . statusCode ; response . on ( "data" , function ( chunk ) { // Make sure there's some data if ( chunk ) { self . responseText += chunk ; } // Don't emit state changes if the connection has been aborted. if ( sendFlag ) { setState ( self . LOADING ) ; } } ) ; response . on ( "end" , function ( ) { if ( sendFlag ) { // Discard the end event if the connection has been aborted setState ( self . DONE ) ; sendFlag = false ; } } ) ; response . on ( "error" , function ( error ) { self . handleError ( error ) ; } ) ; }
Handler for the response
Handler for the response