<commit_before>import http = require("http");
import types = require("utils/types");

module XMLHttpRequestResponseType {
    export var empty = "";
    export var text = "text";
    export var json = "json";
}

export class XMLHttpRequest {
    public UNSENT = 0;
    public OPENED = 1;
    public HEADERS_RECEIVED = 2;
    public LOADING = 3;
    public DONE = 4;

    public onload: () => void;
    public onerror: (any) => void;

    private _options: http.HttpRequestOptions;
    private _readyState: number;
    private _status: number;
    private _response: any;
    private _responseTextReader: Function;
    private _headers: any;
    private _errorFlag: boolean;
    private _responseType: string = "";

    public onreadystatechange: Function;

    constructor() {
        this._readyState = this.UNSENT;
    }

    public open(method: string, url: string, async?: boolean, user?: string, password?: string) {
        if (types.isString(method) && types.isString(url)) {
            this._options = { url: url, method: method };
            this._options.headers = {};

            if (types.isString(user)) {
                this._options.headers["user"] = user;
            }

            if (types.isString(password)) {
                this._options.headers["password"] = password;
            }

            this._setReadyState(this.OPENED);
        }
    }

    public abort() {
        this._errorFlag = true;

        this._response = null;
        this._responseTextReader = null;
        this._headers = null;
        this._status = null;

        if (this._readyState === this.UNSENT || this._readyState === this.OPENED || this._readyState === this.DONE) {
            this._readyState = this.UNSENT;
        } else {
            this._setReadyState(this.DONE);
        }
    }

    public send(data?: any) {
        this._errorFlag = false;
        this._response = null;
        this._responseTextReader = null;
        this._headers = null;
        this._status = null;

        if (types.isDefined(this._options)) {
            if (types.isString(data) && this._options.method !== 'GET') {
                //The Android Java HTTP lib throws an exception if we provide a
                //a request body for GET requests, so we avoid doing that.
                //Browser implementations silently ignore it as well.
                this._options.content = data;
            } else if (data instanceof FormData) {
                this._options.content = (<FormData>data).toString();
            }

            http.request(this._options).then(r=> {
                if (!this._errorFlag) {
                    this._loadResponse(r);
                }

            }).catch(e => {
                this._errorFlag = true;
                this._setReadyState(this.DONE, e);
            });
        }
    }

    private _loadResponse(r) {
        this._status = r.statusCode;
        this._response = r.content.raw;

        this._headers = r.headers;
        this._setReadyState(this.HEADERS_RECEIVED);

        this._setReadyState(this.LOADING);

        this._setResponseType();

        if (this.responseType === XMLHttpRequestResponseType.json) {
            this._responseTextReader = () => r.content.toString();
            this._response = JSON.parse(this.responseText);
        } else if (this.responseType === XMLHttpRequestResponseType.empty ||
            this.responseType === XMLHttpRequestResponseType.text) {
            this._responseTextReader = () => r.content.toString();
        }

        this._setReadyState(this.DONE);
    }

    private _setResponseType() {
        const header = this.getResponseHeader('Content-Type');
        const contentType = header && header.toLowerCase();

        if (contentType) {<commit_msg>Attach toString() method to xhr response<commit_after>import http = require("http");
import types = require("utils/types");

module XMLHttpRequestResponseType {
    export var empty = "";
    export var text = "text";
    export var json = "json";
}

export class XMLHttpRequest {
    public UNSENT = 0;
    public OPENED = 1;
    public HEADERS_RECEIVED = 2;
    public LOADING = 3;
    public DONE = 4;

    public onload: () => void;
    public onerror: (any) => void;

    private _options: http.HttpRequestOptions;
    private _readyState: number;
    private _status: number;
    private _response: any;
    private _responseTextReader: Function;
    private _headers: any;
    private _errorFlag: boolean;
    private _responseType: string = "";

    public onreadystatechange: Function;

    constructor() {
        this._readyState = this.UNSENT;
    }

    public open(method: string, url: string, async?: boolean, user?: string, password?: string) {
        if (types.isString(method) && types.isString(url)) {
            this._options = { url: url, method: method };
            this._options.headers = {};

            if (types.isString(user)) {
                this._options.headers["user"] = user;
            }

            if (types.isString(password)) {
                this._options.headers["password"] = password;
            }

            this._setReadyState(this.OPENED);
        }
    }

    public abort() {
        this._errorFlag = true;

        this._response = null;
        this._responseTextReader = null;
        this._headers = null;
        this._status = null;

        if (this._readyState === this.UNSENT || this._readyState === this.OPENED || this._readyState === this.DONE) {
            this._readyState = this.UNSENT;
        } else {
            this._setReadyState(this.DONE);
        }
    }

    public send(data?: any) {
        this._errorFlag = false;
        this._response = null;
        this._responseTextReader = null;
        this._headers = null;
        this._status = null;

        if (types.isDefined(this._options)) {
            if (types.isString(data) && this._options.method !== 'GET') {
                //The Android Java HTTP lib throws an exception if we provide a
                //a request body for GET requests, so we avoid doing that.
                //Browser implementations silently ignore it as well.
                this._options.content = data;
            } else if (data instanceof FormData) {
                this._options.content = (<FormData>data).toString();
            }

            http.request(this._options).then(r=> {
                if (!this._errorFlag) {
                    this._loadResponse(r);
                }

            }).catch(e => {
                this._errorFlag = true;
                this._setReadyState(this.DONE, e);
            });
        }
    }

    private _loadResponse(r) {
        this._status = r.statusCode;
        this._response = r.content.raw;

        this._headers = r.headers;
        this._setReadyState(this.HEADERS_RECEIVED);

        this._setReadyState(this.LOADING);

        this._setResponseType();

        if (this.responseType === XMLHttpRequestResponseType.json) {
            this._prepareJsonResponse(r);

        } else if (this.responseType === XMLHttpRequestResponseType.empty ||
            this.responseType === XMLHttpRequestResponseType.text) {
            this._responseTextReader = () => r.content.toString();
        }

        this._setReadyState(this.DONE);
    }

    private _prepareJsonResponse(r) {
        this._responseTextReader = () => r.content.toString();
        this._response = JSON.parse(this.responseText);

        // Add toString() method to ease debugging and 
        // make Angular2 response.text() method work properly.
        Object.defineProperty(this._response, "toString", {
            configurable: true,
            enumerable: false,
            writable: true,
            value: () => this.responseText
        });
    }

    private _setResponseType() {
        const header = this.getResponseHeader('Content-Type');
        const contentType = header && header.toLowerCase();

        if (contentType) {
{'fraction_non_alphanumeric': 0.11468408491395553, 'fraction_numerical': 0.0013817359628187414, 'mean_word_length': 2.015909090909091, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6555265', 'n_tokens_mistral': 2215, 'n_tokens_neox': 2124, 'n_words': 493}