<commit_before>var Utils = require('../utils');
var CreateGateway = require('../create-gateway');

var VanillaGateway = CreateGateway({

  get: function() {
    var request = new XMLHttpRequest();
    this._configureCallbacks(request);
    request.open('GET', this.url, true);
    this._setUserDefinedHeaders(request);
    request.send();
  },

  post: function() {
    this._performRequest('POST');
  },

  put: function() {
    this._performRequest('PUT');
  },

  patch: function() {
    this._performRequest('PATCH');
  },

  delete: function() {
    this._performRequest('DELETE');
  },

  _performRequest: function(method) {
    var emulateHTTP = this.shouldEmulateHTTP(method);
    var requestMethod = method;
    var request = new XMLHttpRequest();

    this._configureCallbacks(request);

    if (emulateHTTP) {
      this.body = this.body || {};
      if (typeof this.body === 'object') this.body._method = method;
      requestMethod = 'POST';
    }

    request.open(requestMethod, this.url, true);
    if (emulateHTTP) request.setRequestHeader('X-HTTP-Method-Override', method);

    this._setContentTypeHeader(request);
    this._setUserDefinedHeaders(request);

    var args = [];
    if (this.body !== undefined) {
      args.push(Utils.params(this.body));
    }

    request.send.apply(request, args);
  },

  _configureCallbacks: function(request) {
    request.onload = function() {
      var data = null;
      var status = request.status;

      // IE sends 1223 instead of 204
      if (status === 1223) status = 204;

      try {
        if (status >= 200 && status < 400) {
          if (this._isContentTypeJSON(request)) {
            data = JSON.parse(request.responseText);

          } else {
            data = request.responseText;
          }

          var responseHeaders = request.getAllResponseHeaders();
          var extra = {
            status: status,
            responseHeaders: Utils.parseResponseHeaders(responseHeaders)
          };

          this.successCallback(data, extra);

        } else {
          this.failCallback({status: status, args: [request]});
        }
      } catch(e) {
        this.failCallback({status: status, args: [request]});

      } finally {
        this.completeCallback(data, request);
      }

    }.bind(this);

    request.onerror = function() {
      this.failCallback({status: 400, args: [arguments]});
      this.completeCallback.apply(this, arguments);
    }.bind(this);

    if (this.opts.withCredentials) {
      request.withCredentials = true;
    }

    if (this.opts.configure) {
      this.opts.configure(request);
    }
  },

  _setUserDefinedHeaders: function(request) {
    var headers = this._getHeaders();
    Object.keys(headers).forEach(function(headerName) {
      request.setRequestHeader(headerName, headers[headerName]);
    });
  },

  _isContentTypeJSON: function(request) {
    return /application\/json/.test(request.getResponseHeader('Content-Type'));
  },

  _setContentTypeHeader: function(request) {
    var headers = this._getHeaders();
    var contentType = null;

    Object.keys(headers).forEach(function(headerName) {
      if (/^content-type$/i.test(headerName)) contentType = headers[headerName];
    });

    if (!contentType) contentType = 'application/x-www-form-urlencoded;charset=utf-8';
    request.setRequestHeader('Content-Type', contentType);
  },

  _getHeaders: function() {
    return Utils.extend({}, this.opts.headers);
  }

});
<commit_msg>quick fix for duplicated content-type on vanilla gateway (closes #32)<commit_after>var Utils = require('../utils');
var CreateGateway = require('../create-gateway');

var VanillaGateway = CreateGateway({

  get: function() {
    var request = new XMLHttpRequest();
    this._configureCallbacks(request);
    request.open('GET', this.url, true);
    this._setUserDefinedHeaders(request);
    request.send();
  },

  post: function() {
    this._performRequest('POST');
  },

  put: function() {
    this._performRequest('PUT');
  },

  patch: function() {
    this._performRequest('PATCH');
  },

  delete: function() {
    this._performRequest('DELETE');
  },

  _performRequest: function(method) {
    var emulateHTTP = this.shouldEmulateHTTP(method);
    var requestMethod = method;
    var request = new XMLHttpRequest();

    this._configureCallbacks(request);

    if (emulateHTTP) {
      this.body = this.body || {};
      if (typeof this.body === 'object') this.body._method = method;
      requestMethod = 'POST';
    }

    request.open(requestMethod, this.url, true);
    if (emulateHTTP) request.setRequestHeader('X-HTTP-Method-Override', method);

    this._setContentTypeHeader(request);
    this._setUserDefinedHeaders(request);

    var args = [];
    if (this.body !== undefined) {
      args.push(Utils.params(this.body));
    }

    request.send.apply(request, args);
  },

  _configureCallbacks: function(request) {
    request.onload = function() {
      var data = null;
      var status = request.status;

      // IE sends 1223 instead of 204
      if (status === 1223) status = 204;

      try {
        if (status >= 200 && status < 400) {
          if (this._isContentTypeJSON(request)) {
            data = JSON.parse(request.responseText);

          } else {
            data = request.responseText;
          }

          var responseHeaders = request.getAllResponseHeaders();
          var extra = {
            status: status,
            responseHeaders: Utils.parseResponseHeaders(responseHeaders)
          };

          this.successCallback(data, extra);

        } else {
          this.failCallback({status: status, args: [request]});
        }
      } catch(e) {
        this.failCallback({status: status, args: [request]});

      } finally {
        this.completeCallback(data, request);
      }

    }.bind(this);

    request.onerror = function() {
      this.failCallback({status: 400, args: [arguments]});
      this.completeCallback.apply(this, arguments);
    }.bind(this);

    if (this.opts.withCredentials) {
      request.withCredentials = true;
    }

    if (this.opts.configure) {
      this.opts.configure(request);
    }
  },

  _setUserDefinedHeaders: function(request) {
    var headers = this._getHeaders();
    Object.keys(headers).forEach(function(headerName) {
      if (!/^content-type$/i.test(headerName)) {
        request.setRequestHeader(headerName, headers[headerName]);
      }
    });
  },

  _isContentTypeJSON: function(request) {
    return /application\/json/.test(request.getResponseHeader('Content-Type'));
  },

  _setContentTypeHeader: function(request) {
    var headers = this._getHeaders();
    var contentType = null;

    Object.keys(headers).forEach(function(headerName) {
      if (/^content-type$/i.test(headerName)) contentType = headers[headerName];
    });

    if (!contentType) contentType = 'application/x-www-form-urlencoded;charset=utf-8';
    request.setRequestHeader('Content-Type', contentType);
  },

  _getHeaders: function() {
    return Utils.extend({}, this.opts.headers);
  }

});

{'fraction_non_alphanumeric': 0.14216867469879518, 'fraction_numerical': 0.007087172218284904, 'mean_word_length': 2.904814609850581, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1531295', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2035, 'n_words': 383}