lib/browser.js
function getRequest(options,callback){
    var destroyed = false;
    var req = request.get(options.url)
        .set(options.headers)
        .timeout(options.timeout)
        .redirects(options.redirects)
        .buffer(false)
        .end(function(err,res){
            if(err && !err.status) return onError(err);

            // Check HTTP status code
            var isHTTPError = isRedirect(res.status) || isClientError(res.status) || isServerError(res.status);
            if(isHTTPError) {
                var reqFormated = req.req;
                var resFormated = err.response.res;
                return onError(new HTTPError(reqFormated, resFormated));
            }

            // Attach event handlers and build the body
            var body = '';
            res.on('data',function(chunk){
                body += chunk;
            });
            res.on('end',function(){
                if(destroyed) return;
                // Check if a HTTP refresh/redirection is present into the HTML page, if yes refreshes/redirects.
                var matches = body.match(/<meta[ ]*http-equiv="REFRESH"[ ]*content="[0-9]{1,};[ ]*URL=(.*?)"[ ]*\/?>/i);
                if(matches && matches[1]){
                    options.url = matches[1];
                    return getRequest(options,callback);
                }
                callback(null,body);
            });
            res.on('error',onError);

            // Check if content-type is an image, if yes destroy the response and build a HTML page with the image in it
            if(isImage(res.headers)){
                res.destroy();
                destroyed = true;
                body = '<!DOCTYPE html><html><head></head><body><img src="' + options.url + '" /></body></html>';
                return callback(null,body);
            }
        });

    // Error event handler
    function onError(err){
        if(options.retries--) return getRequest(options,callback);
        callback(err);
    }

    return req;
}
function getRequest(options,callback){
    var destroyed = false;
    var req = request.get(options.url)
        .set(options.headers)
        .timeout(options.timeout)
        .redirects(options.redirects)
        .buffer(false)
        .end(function(err,res){
            if(err && !err.status) return onError(err);

            // Check HTTP status code
            var isHTTPError = isRedirect(res.status) || isClientError(res.status) || isServerError(res.status);
            if(isHTTPError) {
                var reqFormated = req.req;
                var resFormated = err.response.res;
                return onError(new HTTPError(reqFormated, resFormated));
            }

            // Attach event handlers and build the body
            var body = '';
            res.on('data',function(chunk){
                body += chunk;
            });
            res.on('end',function(){
                if(destroyed) return;
                // Check if a HTTP refresh/redirection is present into the HTML page, if yes refreshes/redirects.
                var matches = body.match(/<meta[ ]*http-equiv="REFRESH"[ ]*content="[0-9]{1,};[ ]*URL=(.*?)"[ ]*\/?>/i);
                if(matches && matches[1]){
                    options.url = matches[1];
                    return getRequest(options,callback);
                }
                callback(null,body);
            });
            res.on('error',onError);

            // Check if content-type is an image, if yes destroy the response and build a HTML page with the image in it
            if(isImage(res.headers)){
                res.destroy();
                destroyed = true;
                body = '<!DOCTYPE html><html><head></head><body><img src="' + options.url + '" /></body></html>';
                return callback(null,body);
            }
        });

    // Error event handler
    function onError(err){
        if(options.retries--) return getRequest(options,callback);
        callback(err);
    }

    return req;
}
function getRequest ( options , callback ) { var destroyed = false ; var req = request . get ( options . url ) . set ( options . headers ) . timeout ( options . timeout ) . redirects ( options . redirects ) . buffer ( false ) . end ( function ( err , res ) { if ( err && ! err . status ) return onError ( err ) ; // Check HTTP status code var isHTTPError = isRedirect ( res . status ) || isClientError ( res . status ) || isServerError ( res . status ) ; if ( isHTTPError ) { var reqFormated = req . req ; var resFormated = err . response . res ; return onError ( new HTTPError ( reqFormated , resFormated ) ) ; } // Attach event handlers and build the body var body = '' ; res . on ( 'data' , function ( chunk ) { body += chunk ; } ) ; res . on ( 'end' , function ( ) { if ( destroyed ) return ; // Check if a HTTP refresh/redirection is present into the HTML page, if yes refreshes/redirects. var matches = body . match ( / <meta[ ]*http-equiv="REFRESH"[ ]*content="[0-9]{1,};[ ]*URL=(.*?)"[ ]*\/?> / i ) ; if ( matches && matches [ 1 ] ) { options . url = matches [ 1 ] ; return getRequest ( options , callback ) ; } callback ( null , body ) ; } ) ; res . on ( 'error' , onError ) ; // Check if content-type is an image, if yes destroy the response and build a HTML page with the image in it if ( isImage ( res . headers ) ) { res . destroy ( ) ; destroyed = true ; body = '<!DOCTYPE html><html><head></head><body><img src="' + options . url + '" /></body></html>' ; return callback ( null , body ) ; } } ) ; // Error event handler function onError ( err ) { if ( options . retries -- ) return getRequest ( options , callback ) ; callback ( err ) ; } return req ; }
Send an HTTP GET request to `options.url`

@param {Object} options - configuration of the HTTP GET request
@param {String} options.headers - set of headers
@param {Number} options.timeout - timeout
@param {Number} options.redirects - number of times the request will follow redirection instructions
@param {Number} options.retries - number of times the request will be resend if the request failed
@param {Function} callback -

@return {http.ClientRequest} req
@api private
Send an HTTP GET request to options . url