httpreq.js 14.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
/*
Copyright (c) 2013 Sam Decrock <sam.decrock@gmail.com>

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

var querystring = require ('querystring');
var https = require ('https');
var http = require ('http');
var url = require ('url');
var fs = require ('fs');


/**
 * Generate multipart boundary
 *
 * @returns {string}
 */

function generateBoundary () {
  var boundary = '---------------------------';
  var charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

  for (var i = 0; i < 29; i++) {
    boundary += charset.charAt (Math.floor (Math.random () * charset.length));
  }

  return boundary;
}


/**
 * Extract cookies from headers
 *
 * @param headers {object} - Response headers
 * @returns {array} - Extracted cookie strings
 */

function extractCookies (headers) {
  var rawcookies = headers['set-cookie'];

  if (!rawcookies) {
    return [];
  }

  if (rawcookies == []) {
    return [];
  }

  var cookies = [];
  for (var i = 0; i < rawcookies.length; i++) {
    var rawcookie = rawcookies[i].split (';');
    if (rawcookie[0]) {
      cookies.push (rawcookie[0]);
    }
  }
  return cookies;
}


/**
 * Custom HTTP request
 *
 * @callback callback
 * @param o {object} - Request options
 * @param callback [function] - Process response
 * @returns {void}
 */

function doRequest (o, callback) {
  if (!callback) {
    callback = function (err) {}; // dummy function
  }

  // prevent multiple callbacks
  var finalCallbackDone = false;
  function finalCallback (err, res) {
    if (!finalCallbackDone) {
      finalCallbackDone = true;
      callback (err, res);
    }
  }

  if (o.maxRedirects === undefined) {
    o.maxRedirects = 10;
  }

  if (o.encodePostParameters === undefined) {
    o.encodePostParameters = true;
  }

  var chunks = [];
  var body; // Buffer
  var contentType;

  var port;
  var host;
  var path;
  var isHttps = false;

  if (o.proxy) {
    port = o.proxy.port;
    host = o.proxy.host;
    path = o.url; // complete url

    if (o.proxy.protocol && o.proxy.protocol.match (/https/)) {
      isHttps = true;
    }
  } else {
    var reqUrl = url.parse (o.url);
    host = reqUrl.hostname;
    path = reqUrl.path;

    if (reqUrl.protocol === 'https:') {
      isHttps = true;
    }

    if (reqUrl.port) {
      port = reqUrl.port;
    } else if (isHttps) {
      port = 443;
    } else {
      port = 80;
    }
  }

  if (o.files && o.files.length > 0 && o.method === 'GET') {
    var err = new Error ('Can\'t send files using GET');
    err.code = 'CANT_SEND_FILES_USING_GET';
    return finalCallback (err);
  }

  if (o.parameters) {
    if (o.method === 'GET') {
      path += '?' + querystring.stringify (o.parameters);
    } else {
      body = new Buffer (querystring.stringify (o.parameters), 'utf8');
      contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
    }
  }

  if (o.json) {
    body = new Buffer (JSON.stringify (o.json), 'utf8');
    contentType = 'application/json';
  }

  if (o.files) {
    var crlf = '\r\n';
    var boundary = generateBoundary ();
    var separator = '--' + boundary;
    var bodyArray = new Array (); // temporary body array

    // if the user wants to POST/PUT files, other parameters need to be encoded using 'Content-Disposition'
    for (var key in o.parameters) {
      // According to RFC 2388 (https://www.ietf.org/rfc/rfc2388.txt)
      // "Field names originally in non-ASCII character sets MAY be encoded
        // within the value of the "name" parameter using the standard method
        // described in RFC 2047."
        // -- encodePostParameters -- true by default and MAY be changed by the user
      var headerKey = o.encodePostParameters ? encodeURIComponent (key) : key;
      var encodedParameter = separator + crlf
        + 'Content-Disposition: form-data; name="' + headerKey + '"' + crlf
        + crlf
        + o.parameters[key] + crlf;
      bodyArray.push (new Buffer (encodedParameter));
    }

    // now for the files:
    var haveAlreadyAddedAFile = false;

    for (var file in o.files) {
      var filepath = o.files[file];
      var filename = filepath.replace (/\\/g, '/').replace (/.*\//, '');

      var encodedFile = separator + crlf
        + 'Content-Disposition: form-data; name="' + file + '"; filename="' + filename + '"' + crlf
        + 'Content-Type: application/octet-stream' + crlf
        + crlf;

      // add crlf before separator if we have already added a file
      if (haveAlreadyAddedAFile) {
        encodedFile = crlf + encodedFile;
      }

      bodyArray.push (new Buffer (encodedFile));

      // add binary file:
      bodyArray.push (require ('fs').readFileSync (filepath));

      haveAlreadyAddedAFile = true;
    }

    var footer = crlf + separator + '--' + crlf;
    bodyArray.push (new Buffer (footer));

    // set body and contentType:
    body = Buffer.concat (bodyArray);
    contentType = 'multipart/form-data; boundary=' + boundary;
  }

  // overwrites the body if the user passes a body:
  // clears the content-type
  if (o.body) {
    body = new Buffer (o.body, 'utf8');
    contentType = null;
  }


  var requestoptions = {
    host: host,
    port: port,
    path: path,
    method: o.method,
    headers: {}
  };

  if (!o.redirectCount) {
    o.redirectCount = 0;
  }

  if (body) {
    requestoptions.headers['Content-Length'] = body.length;
  }

  if (contentType) {
    requestoptions.headers['Content-Type'] = contentType;
  }

  if (o.cookies) {
    requestoptions.headers.Cookie = o.cookies.join ('; ');
  }

  if (o.rejectUnauthorized !== undefined && isHttps) {
    requestoptions.rejectUnauthorized = o.rejectUnauthorized;
  }

  if (isHttps && o.key) {
    requestoptions.key = o.key;
  }

  if (isHttps && o.cert) {
    requestoptions.cert = o.cert;
  }

  if (isHttps && o.secureProtocol) {
    requestoptions.secureProtocol = o.secureProtocol;
  }

  if (isHttps && o.ciphers) {
    requestoptions.ciphers = o.ciphers;
  }

  if (isHttps && o.passphrase) {
    requestoptions.passphrase = o.passphrase;
  }

  if (isHttps && o.pfx) {
    requestoptions.pfx = o.pfx;
  }

  if (isHttps && o.ca) {
    requestoptions.ca = o.ca;
  }

  // add custom headers:
  if (o.headers) {
    for (var headerkey in o.headers) {
      requestoptions.headers[headerkey] = o.headers[headerkey];
    }
  }

  if (o.agent) {
    requestoptions.agent = o.agent;
  }

  if (o.auth) {
    requestoptions.auth = o.auth;
  }

  if (o.localAddress) {
    requestoptions.localAddress = o.localAddress;
  }

  if (o.secureOptions) {
    requestoptions.secureOptions = o.secureOptions;
  }


  /**
   * Process request response
   *
   * @param res {object} - Response details
   * @returns {void}
   */

  function requestResponse (res) {
    var ended = false;
    var currentsize = 0;

    var downloadstream = null;
    if (o.downloadlocation) {
      downloadstream = fs.createWriteStream (o.downloadlocation);
    }

    res.on ('data', function (chunk) {
      if (o.downloadlocation) {
        downloadstream.write (chunk); //write it to disk, not to memory
      } else {
        chunks.push (chunk);
      }

      if (o.progressCallback) {
        var totalsize = res.headers['content-length'];
        if (totalsize) {
          currentsize += chunk.length;

          o.progressCallback (null, {
            url: o.url,
            totalsize: totalsize,
            currentsize: currentsize,
            percentage: currentsize * 100 / totalsize
          });
        } else {
          o.progressCallback (new Error ('no content-length specified for file, so no progress monitoring possible'));
        }
      }
    });

    res.on ('end', function (err) {
      ended = true;

      // check for redirects
      if (res.headers.location && o.allowRedirects) {
        // Close any open file
        if (o.downloadlocation) {
          downloadstream.end ();
        }

        if (o.redirectCount < o.maxRedirects) {
          o.redirectCount++;
          o.url = res.headers.location;
          o.cookies = extractCookies (res.headers);
          return doRequest (o, finalCallback);
        } else {
          var err = new Error ('Too many redirects (> ' + o.maxRedirects + ')');
          err.code = 'TOOMANYREDIRECTS';
          err.redirects = o.maxRedirects;
          return finalCallback (err);
        }
      }

      if (!o.downloadlocation) {
        var responsebody = Buffer.concat (chunks);
        if (!o.binary) {
          responsebody = responsebody.toString ('utf8');
        }

        finalCallback (null, {
          headers: res.headers,
          statusCode: res.statusCode,
          body: responsebody,
          cookies: extractCookies (res.headers)
        });
      } else {
        downloadstream.end (null, null, function () {
          finalCallback (null, {
            headers: res.headers,
            statusCode: res.statusCode,
            downloadlocation: o.downloadlocation,
            cookies: extractCookies (res.headers)
          });
        });
      }
    });

    res.on ('close', function () {
      if (!ended) {
        finalCallback (new Error ('Request aborted'));
      }
    });
  }

  var request;

  // remove headers with undefined keys or values
  // else we get an error in Node 0.12.0 about "setHeader ()"
  for (var headerName in requestoptions.headers) {
    var headerValue = requestoptions.headers[headerName];
    if (!headerName || !headerValue) {
      delete requestoptions.headers[headerName];
    }
  }

  if (isHttps) {
    request = https.request (requestoptions, requestResponse);
  } else {
    request = http.request (requestoptions, requestResponse);
  }

  if (o.timeout) {
    request.setTimeout (parseInt (o.timeout, 10), function () {
      var err = new Error ('request timed out');
      err.code = 'TIMEOUT';
      finalCallback (err);
      request.abort ();
    });
  }

  request.on ('error', function (err) {
    finalCallback (err);
  });

  if (body) {
    request.write (body);
  }

  request.end ();
}

exports.doRequest = doRequest;


/**
 * HTTP GET method
 *
 * @callback callback
 * @param url {string} - Request URL
 * @param [options] {object} - Request options
 * @param callback [function] - Process response
 * @returns {void}
 */

exports.get = function (url, options, callback) {
  if (callback === undefined && options && typeof options === 'function') {
    callback = options;
  }

  var moreOptions = options;
  moreOptions.url = url;
  moreOptions.method = 'GET';

  if (moreOptions.allowRedirects === undefined) {
    moreOptions.allowRedirects = true;
  }

  doRequest (moreOptions, callback);
};


/**
 * HTTP OPTIONS method
 *
 * @callback callback
 * @param url {string} - Request URL
 * @param [options] {object} - Request options
 * @param callback [function] - Process response
 * @returns {void}
 */

exports.options = function (url, options, callback) {
  if (callback === undefined && options && typeof options === 'function') {
    callback = options;
  }

  var moreOptions = options;
  moreOptions.url = url;
  moreOptions.method = 'OPTIONS';
  doRequest (moreOptions, callback);
};


/**
 * HTTP POST method
 *
 * @callback callback
 * @param url {string} - Request URL
 * @param [options] {object} - Request options
 * @param callback [function] - Process response
 * @returns {void}
 */

exports.post = function (url, options, callback) {
  if (callback === undefined && options && typeof options === 'function') {
    callback = options;
  }

  var moreOptions = options;
  moreOptions.url = url;
  moreOptions.method = 'POST';
  doRequest (moreOptions, callback);
};


/**
 * HTTP PUT method
 *
 * @callback callback
 * @param url {string} - Request URL
 * @param [options] {object} - Request options
 * @param callback [function] - Process response
 * @returns {void}
 */

exports.put = function (url, options, callback) {
  if (callback === undefined && options && typeof options === 'function') {
    callback = options;
  }

  var moreOptions = options;
  moreOptions.url = url;
  moreOptions.method = 'PUT';
  doRequest (moreOptions, callback);
};


/**
 * HTTP PATCH method
 *
 * @callback callback
 * @param url {string} - Request URL
 * @param [options] {object} - Request options
 * @param callback [function] - Process response
 * @returns {void}
 */

exports.patch = function (url, options, callback) {
  if (callback === undefined && options && typeof options === 'function') {
    callback = options;
  }

  var moreOptions = options;
  moreOptions.url = url;
  moreOptions.method = 'PATCH';
  doRequest (moreOptions, callback);
};


/**
 * HTTP DELETE method
 *
 * @callback callback
 * @param url {string} - Request URL
 * @param [options] {object} - Request options
 * @param callback [function] - Process response
 * @returns {void}
 */

exports.delete = function (url, options, callback) {
  if (callback === undefined && options && typeof options === 'function') {
    callback = options;
  }

  var moreOptions = options;
  moreOptions.url = url;
  moreOptions.method = 'DELETE';
  doRequest (moreOptions, callback);
};


/**
 * Download a file
 *
 * @callback callback
 * @param url {string} - Request URL
 * @param downloadlocation {string} - Path where to store file
 * @param [progressCallback] {function} - Called multiple times during download
 * @param callback {function} - Called once when download ends
 * @returns {void}
 */

exports.download = function (url, downloadlocation, progressCallback, callback) {
  var options = {};
  options.url = url;
  options.method = 'GET';
  options.downloadlocation = downloadlocation;
  options.allowRedirects = true;

  // if only 3 args are provided, so no progressCallback
  if (callback === undefined && progressCallback && typeof progressCallback === 'function') {
    callback = progressCallback;
  } else {
    options.progressCallback = progressCallback;
  }

  doRequest (options, callback);
};


/**
 * Upload files
 * old function, can still be used
 *
 * @callback callback
 * @param options {object} - Request options
 * @param callback [function] - Process response
 * @returns {void}
 */

exports.uploadFiles = function (options, callback) {
  var moreOptions = options;
  moreOptions.method = 'POST';
  doRequest (moreOptions, callback);
};