Example #1
0
    xhr.send = function (data) {
      var cookieJar = window.document._cookieJar;
      var cookieStr = cookieJar.getCookieStringSync(lastUrl, {http: true});

      if (cookieStr) {
        xhr.setDisableHeaderCheck(true);
        xhr.setRequestHeader("cookie", cookieStr);
        xhr.setDisableHeaderCheck(false);
      }

      function setReceivedCookies() {
        if (xhr.readyState === xhr.HEADERS_RECEIVED) {
          var receivedCookies = xhr.getResponseHeader("set-cookie");

          if (receivedCookies) {
            receivedCookies = Array.isArray(receivedCookies) ? receivedCookies : [receivedCookies];

            receivedCookies.forEach(function (cookieStr) {
              cookieJar.setCookieSync(cookieStr, lastUrl, {
                http: true,
                ignoreError: true
              });
            });
          }

          xhr.removeEventListener("readystatechange", setReceivedCookies);
        }
      }

      xhr.addEventListener("readystatechange", setReceivedCookies);
      return xhr._send(data);
    };
Example #2
0
 xhr.send = function (data) {
   if (window.document.cookie) {
     var cookieDomain = window.document._cookieDomain;
     var url = URL.parse(lastUrl);
     var host = url.host.split(":")[0];
     if (host.indexOf(cookieDomain, host.length - cookieDomain.length) !== -1) {
       xhr.setDisableHeaderCheck(true);
       xhr.setRequestHeader("cookie", window.document.cookie);
       xhr.setDisableHeaderCheck(false);
     }
   }
   return xhr._send(data);
 };
Example #3
0
function get (url, callback) {
  var httpRequest = new XMLHttpRequest();

  function requestHandler () {
    if (this.readyState === this.DONE) {
      if (this.status === 200) {
        try {
          var response = JSON.parse(this.responseText);
          callback(null, response);
        } catch (err) {
          callback("Invalid JSON on response: " + this.responseText);
        }
      }
    }
  }

  httpRequest.onreadystatechange = requestHandler;

  httpRequest.open("GET", url);
  if (httpRequest.setDisableHeaderCheck !== undefined) {
    httpRequest.setDisableHeaderCheck(true);
    httpRequest.setRequestHeader("Referer", "geoservices-js");
  }
  httpRequest.send(null);
}
Example #4
0
function post (url, data, callback) {
  var httpRequest = new XMLHttpRequest();

  function requestHandler () {
    if (this.readyState === this.DONE) {
      if (this.status === 200) {
        try {
          var response = JSON.parse(this.responseText);
          callback(null, response);
        } catch (err) {
          callback("Invalid JSON on response: " + this.responseText);
        }
      }
    }
  }

  httpRequest.onreadystatechange = requestHandler;

  httpRequest.open("POST", url);
  if (httpRequest.setDisableHeaderCheck !== undefined) {
    httpRequest.setDisableHeaderCheck(true);
    httpRequest.setRequestHeader("Referer", "geoservices-js");
  }
  
  httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  httpRequest.send(stringify(data));
}
module.exports = function getXMLHttpRequest() {
  var request = new xhr.XMLHttpRequest();
  request.setDisableHeaderCheck(true);
  return request;
};
Example #6
0
module.exports = function(options){
    
    options.headers = options.headers || {};
    if (options.body && _.isObject(options.body)) {
      var contentType = findContentType(options.headers);
      var isBufferBody = Buffer.isBuffer(options.body);
      if (!contentType) {
        if (isBufferBody) {
          contentType = 'application/octet-stream';
        } else {
          contentType = 'application/x-www-form-urlencoded';
        }
        options.headers["Content-Type"] = contentType;
      }
      if (contentType.indexOf('application/x-www-form-urlencoded') === 0) {
        options.body = formEncode(options.body);
      } else if (contentType.indexOf('application/json') === 0) {
        options.body = JSON.stringify(options.body);
      } else if (!isBufferBody && (typeof options.body !== 'string')) {
        throw "Error: Don't know how to convert httpRequest body Object to " +
            contentType + ". To send raw bytes, please assign httpRequest " +
            "body to a Buffer object containing your data.";
      }
    }

    if (options.params) {
      if (options.url.indexOf('?') !== -1) {
        throw "Error: Can't set params if there is already a query parameter " +
          "in the url";
      }
      options.url += "?" + formEncode(options.params);
    }

    options.method = options.method || 'GET';
    options.method = options.method.toUpperCase();

    options.uuid = options.uuid || createUUID();

    var promise;
    if (Parse.Promise) {
      promise = new Parse.Promise();
    } else {
      // for backward compatibility to older JS SDKs
      promise = { resolve: function() {}, reject: function() {} };
    }
    options.success = options.success || function(){};
    options.error = options.error || function(){};

    var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    var xhr = new XMLHttpRequest();
    // disable header check if runtime is not in a browser window (running in Node.js)
    if (typeof window === 'undefined') {
        xhr.setDisableHeaderCheck(true);
    }
    xhr.open(options.method, options.url, true);
    if (options.headers) {
        _.each(options.headers, function(value, key){
            xhr.setRequestHeader(key, value);
        })
    }
    xhr.send(options.body);
    var handled;
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4) {
        if (handled) {
          return;
        }
        handled = true;
        var httpResponse = {};
        httpResponse.status = xhr.status;
        httpResponse.headers = xhr.getAllResponseHeaders();
        httpResponse.buffer = new Buffer(xhr.responseText);
        httpResponse.cookies = xhr.getResponseHeader("set-cookie");
        httpResponse.text = xhr.responseText;
        try {
          httpResponse.data = JSON.parse(xhr.responseText);
        } catch (e) {
         // promise.reject(e);
        }
        if (xhr.status >= 200 && xhr.status < 300) {
          //var response;

          httpResponse.status = xhr.status;
          httpResponse.headers = xhr.getAllResponseHeaders();
          httpResponse.buffer = new Buffer(xhr.responseText);
          httpResponse.cookies = xhr.getResponseHeader("set-cookie");
          httpResponse.text = xhr.responseText;
          try {
            httpResponse.data = JSON.parse(xhr.responseText);
          } catch (e) {
           // promise.reject(e);
          }
          if (httpResponse) {
            promise.resolve(httpResponse);
            options.success(httpResponse);
          }
        } else {

          promise.reject(httpResponse);
          options.error(httpResponse);
        }
      }
    };
    return promise;
}