<commit_before>/*
 Source is originated from https://github.com/morungos/java-xmlhttprequest

 Articles about Nashorn:
 - https://blog.codecentric.de/en/2014/06/project-nashorn-javascript-jvm-polyglott/
 */
(function nashornEventLoopMain(context) {
  'use strict';

  var System = Java.type('java.lang.System');
  var RequestBuilder = Packages.org.apache.http.client.methods.RequestBuilder;
  var FutureCallback = Packages.org.apache.http.concurrent.FutureCallback;
  var HttpAsyncClientBuilder = Packages.org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
  var BasicHeader = Packages.org.apache.http.message.BasicHeader;
  var ArrayList = Java.type('java.util.ArrayList');
  var ByteArrayEntity = Packages.org.apache.http.entity.ByteArrayEntity;
  var StringEntity = Packages.org.apache.http.entity.StringEntity;
  var EntityBuilder = Packages.org.apache.http.client.entity.EntityBuilder;
  var ContentType = Packages.org.apache.http.entity.ContentType;

  var XMLHttpRequest = function () {
    var method, url, async, user, password, headers = {};

    var that = this;

    this.onreadystatechange = function () {
    };

    this.onload = function () {
    };

    this.readyState = 0;
    this.response = null;
    this.responseText = null;
    this.responseType = '';
    this.status = null;
    this.statusText = null;
    this.timeout = 0; // no timeout by default
    this.ontimeout = function () {
    };
    this.withCredentials = false;
    var requestBuilder = null;

    this.abort = function () {

    };

    this.getAllResponseHeaders = function () {

    };

    this.getResponseHeader = function (key) {

    };

    this.setRequestHeader = function (key, value) {
      headers[key] = value;
    };

    this.open = function (_method, _url, _async, _user, _password) {
      this.readyState = 1;

      method = _method;
      url = _url;

      async = _async === false ? false : true;

      user = _user || '';
      password = _password || '';

      requestBuilder = RequestBuilder.create(_method);
      requestBuilder.setUri(_url);

      // for (var prop in headers) {
      //   requestBuilder.addHeader(prop, headers[prop])
      // }

      this.onreadystatechange();
    };

    this.send = function (data) {
      var that = this;

      var clientBuilder = HttpAsyncClientBuilder.create();
      var httpHeaders = new ArrayList(headers.length);

      for (var prop in headers) {
        httpHeaders.add(new BasicHeader(prop, headers[prop]));
      }
      clientBuilder.setDefaultHeaders(httpHeaders);

      if (data === undefined || data === null) {
        requestBuilder.setEntity(null);
      } else if (typeof data === 'string') {
        requestBuilder.setEntity(new StringEntity(data));
      } else {
        throw new Error('unsupported body data type');
      }

      var httpclient = clientBuilder.build();
      httpclient.start();

      var callback = new FutureCallback({
        completed: function (response) {
          that.readyState = 4;

          var body = org.apache.http.util.EntityUtils.toString(response.getEntity(), 'UTF-8');
          that.responseText = that.response = body;

          var finalException = null;
          if (that.responseType === 'json') {
            try {
              that.response = JSON.parse(that.response);
            } catch (e) {

              // Store the error
              finalException = e;

            }<commit_msg>Nashorn passes __HTTP_SERVLET_REQUEST__ which is the HttpServletReqeust from server, then copy cookie header to request so that api accessed by javascript can recognize the request just like it fired from user browser<commit_after>/*
 Source is originated from https://github.com/morungos/java-xmlhttprequest

 Articles about Nashorn:
 - https://blog.codecentric.de/en/2014/06/project-nashorn-javascript-jvm-polyglott/
 */
(function nashornEventLoopMain(context) {
  'use strict';

  var System = Java.type('java.lang.System');
  var RequestBuilder = Packages.org.apache.http.client.methods.RequestBuilder;
  var FutureCallback = Packages.org.apache.http.concurrent.FutureCallback;
  var HttpAsyncClientBuilder = Packages.org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
  var BasicHeader = Packages.org.apache.http.message.BasicHeader;
  var ArrayList = Java.type('java.util.ArrayList');
  var ByteArrayEntity = Packages.org.apache.http.entity.ByteArrayEntity;
  var StringEntity = Packages.org.apache.http.entity.StringEntity;
  var EntityBuilder = Packages.org.apache.http.client.entity.EntityBuilder;
  var ContentType = Packages.org.apache.http.entity.ContentType;

  var XMLHttpRequest = function () {
    var method, url, async, user, password, headers = {};

    var that = this;

    this.onreadystatechange = function () {
    };

    this.onload = function () {
    };

    this.readyState = 0;
    this.response = null;
    this.responseText = null;
    this.responseType = '';
    this.status = null;
    this.statusText = null;
    this.timeout = 0; // no timeout by default
    this.ontimeout = function () {
    };
    this.withCredentials = false;
    var requestBuilder = null;

    this.abort = function () {

    };

    this.getAllResponseHeaders = function () {

    };

    this.getResponseHeader = function (key) {

    };

    this.setRequestHeader = function (key, value) {
      headers[key] = value;
    };

    this.open = function (_method, _url, _async, _user, _password) {
      this.readyState = 1;

      method = _method;
      url = _url;

      async = _async === false ? false : true;

      user = _user || '';
      password = _password || '';

      requestBuilder = RequestBuilder.create(_method);
      requestBuilder.setUri(_url);

      // for (var prop in headers) {
      //   requestBuilder.addHeader(prop, headers[prop])
      // }

      this.onreadystatechange();
    };

    this.send = function (data) {
      var that = this;

      var clientBuilder = HttpAsyncClientBuilder.create();
      var httpHeaders = new ArrayList(headers.length);

      for (var prop in headers) {
        httpHeaders.add(new BasicHeader(prop, headers[prop]));
      }
      if (__HTTP_SERVLET_REQUEST__ && __HTTP_SERVLET_REQUEST__.getHeader("Cookie")) {
        httpHeaders.add(new BasicHeader("Cookie", __HTTP_SERVLET_REQUEST__.getHeader("Cookie")))
      }
      clientBuilder.setDefaultHeaders(httpHeaders);

      if (data === undefined || data === null) {
        requestBuilder.setEntity(null);
      } else if (typeof data === 'string') {
        requestBuilder.setEntity(new StringEntity(data));
      } else {
        throw new Error('unsupported body data type');
      }

      var httpclient = clientBuilder.build();
      httpclient.start();

      var callback = new FutureCallback({
        completed: function (response) {
          that.readyState = 4;

          var body = org.apache.http.util.EntityUtils.toString(response.getEntity(), 'UTF-8');
          that.responseText = that.response = body;

          var finalException = null;
          if (that.responseType === 'json') {
            try {
              that.response = JSON.parse(that.response);
            } catch (e) {

              // Store the error
              finalException = e;

            }
{'fraction_non_alphanumeric': 0.11922811853893867, 'fraction_numerical': 0.003032391454169538, 'mean_word_length': 2.7172131147540983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '906079', 'n_tokens_mistral': 2122, 'n_tokens_neox': 2030, 'n_words': 455}