Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

fix #268

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 126 additions & 129 deletions lib/Request.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
"use strict";

var needle = require('needle'),
URL = require('url'),
libxml = require('libxmljs-dom');
var needle = require("needle"),
URL = require("url"),
libxml = require("libxmljs-dom");

/**
* Make an HTTP request.
Expand All @@ -11,170 +11,167 @@ var needle = require('needle'),
*/

function Request(method, url, params, opts, tries, callback) {
var location = url;
return needle.request(method,
url.href,
params,
opts,
function (err, res, data) {

if (!(url.params instanceof Object) || url.params === null) {
url.params = url.query;
}

if (err !== null) {
callback(err.message);
return;
}
var location = url;
return needle
.request(method, url.href, params, opts, function (err, res, data) {
if (!(url.params instanceof Object) || url.params === null) {
url.params = url.query;
}

if (err !== null) {
callback(err.message);
return;
}

if (
opts.ignore_http_errors !== true &&
res !== undefined &&
res.statusCode >= 400 &&
res.statusCode <= 500
) {
// HTTP error
callback(res.statusCode + " " + res.statusMessage);
return;
}

if (opts.ignore_http_errors !== true &&
res !== undefined &&
res.statusCode >= 400 &&
res.statusCode <= 500
) {
// HTTP error
callback(res.statusCode + ' ' + res.statusMessage);
return;
}
if (method !== "head" && (!data || data.length === 0)) {
callback("Data is empty");
return;
}

if (method !== 'head' && (!data || data.length === 0)) {
callback('Data is empty');
return;
function next(document) {
if (opts.parse === false) {
callback(null, res, document);
return;
}

function next(document) {
if (opts.parse === false) {
callback(null, res, document);
return;
}

document = libxml.parseHtml(document,
{ baseUrl: location.href, huge: true });
document = libxml.parseHtml(document, {
baseUrl: location.href,
huge: true,
});

if (document === null) {
callback('Couldn\'t parse response');
return;
}

if (document.errors[0] !== undefined &&
document.errors[0].code === 4) {
callback('Document is empty');
return;
}
if (document === null) {
callback("Couldn't parse response");
return;
}

if (document.root() === null) {
callback('Document has no root');
return;
}
if (document.errors[0] !== undefined && document.errors[0].code === 4) {
callback("Document is empty");
return;
}

location.headers = res.req._headers;
location.proxy = opts.proxy;
location.user_agent = opts.user_agent;
if (document.root() === null) {
callback("Document has no root");
return;
}

document.location = location;
document.request = location;
location.headers = res.req.getHeaders();
location.proxy = opts.proxy;
location.user_agent = opts.user_agent;

setResponseMeta(document, res, data.length);
setCookies(document, res.cookies);
setCookies(document, opts.cookies);
document.location = location;
document.request = location;

if (opts.keep_data === true) {
document.response.data = data;
}
setResponseMeta(document, res, data.length);
setCookies(document, res.cookies);
setCookies(document, opts.cookies);

callback(null, res, document);
if (opts.keep_data === true) {
document.response.data = data;
}

if (
opts.process_response !== undefined &&
typeof opts.process_response === 'function'
) {
if (opts.process_response.length > 2) {
opts.process_response(data, res, next, callback);
return;
}

next(opts.process_response(data, res));
} else {
next(data);
callback(null, res, document);
}

if (
opts.process_response !== undefined &&
typeof opts.process_response === "function"
) {
if (opts.process_response.length > 2) {
opts.process_response(data, res, next, callback);
return;
}

next(opts.process_response(data, res));
} else {
next(data);
}
})
.on('redirect', function (href) {
extend(location, URL.parse(URL.resolve(location.href, href)));
.on("redirect", function (href) {
extend(location, URL.parse(URL.resolve(location.href, href)));
});
}

function setResponseMeta(document, res, size) {
var response = {
type: getResponseType(res.headers['content-type']),
statusCode: res.statusCode,
statusMessage: res.statusMessage,
headers: res.headers,
size: {
body: size
}
};


if (res.socket !== undefined) {
response.size.total = res.socket.bytesRead;
response.size.headers = res.socket.bytesRead - size;
}

document.response = response;
var response = {
type: getResponseType(res.headers["content-type"]),
statusCode: res.statusCode,
statusMessage: res.statusMessage,
headers: res.headers,
size: {
body: size,
},
};

if (res.socket !== undefined) {
response.size.total = res.socket.bytesRead;
response.size.headers = res.socket.bytesRead - size;
}

document.response = response;
}

function getResponseType(contentType) {
if (contentType === undefined) {
return null;
}
if (contentType === undefined) {
return null;
}

if (contentType.indexOf('xml') !== -1) {
return 'xml';
}
if (contentType.indexOf("xml") !== -1) {
return "xml";
}

if (contentType.indexOf('html') !== -1) {
return 'html';
}
if (contentType.indexOf("html") !== -1) {
return "html";
}

return contentType;
return contentType;
}


function setCookies(document, cookies) {
var key, keys, length;
var key, keys, length;

if (cookies === undefined) {
return;
}
if (cookies === undefined) {
return;
}

keys = Object.keys(cookies);
length = keys.length;
keys = Object.keys(cookies);
length = keys.length;

if (length === 0) {
return;
}
if (length === 0) {
return;
}

if (document.cookies === undefined) {
document.cookies = {};
}
if (document.cookies === undefined) {
document.cookies = {};
}

while (length--) {
key = keys[length];
document.cookies[key] = cookies[key];
}
while (length--) {
key = keys[length];
document.cookies[key] = cookies[key];
}
}

function extend(object, donor) {
var key, keys = Object.keys(donor), i = keys.length;
var key,
keys = Object.keys(donor),
i = keys.length;

while (i--) {
key = keys[i];
object[key] = donor[key];
}
while (i--) {
key = keys[i];
object[key] = donor[key];
}

return object;
return object;
}

module.exports = Request;