<commit_before>//       telegram.link
//
//       Copyright 2014 <NAME> '<EMAIL>'
//       Released under the Simplified BSD License
//       http://telegram.link

//      HttpConnection class
//
// This class provides a HTTP transport to communicate with `Telegram` using `MTProto` protocol

// Import dependencies
var http = require('http');
var util = require('util');
var AbstractObject = require("lib/type-language").AbstractObject;
var logger = require('get-log')('net.HttpConnection');

// The constructor accepts optionally an object to specify the connection address as following:
//
//      new HttpConnection({host: "172.16.17.32", port: "443"});
//
// `localhost:80` address is used as default otherwise
function HttpConnection(options) {
    var httpPath = require('../static').telegram.httpPath;
    options = options ?
        ({
            host: (options.proxyHost || options.host || 'localhost'),
            port: (options.proxyPort || options.port || '80'),
//            path: 'http://' + (options.host || 'localhost') + ':' + (options.port || '80') + httpPath
            path: httpPath
        }) :
        ({
            path: 'http://localhost:80' + httpPath
        });
    this.options = util._extend({
        localAddress: process.env.LOCAL_ADDRESS
    }, options);
    this._config = JSON.stringify(this.options);
    if (logger.isDebugEnabled()) logger.debug('created with %s', this._config);
}

HttpConnection.prototype.connect = function (callback) {
    if (logger.isDebugEnabled()) logger.debug('connected to %s', this._config);
    this._writeBuffers = [];
    this._writeOffset = 0;
    if (callback) callback();
};

HttpConnection.prototype.write = function (data, callback) {
    this._writeBuffers.push(data);
    this._writeOffset += data.length;
    if (logger.isDebugEnabled()) logger.debug('add buffer(%s) to the write buffer queue, total length %s',
        data.length, this._writeOffset);
    if (callback) callback();
};

HttpConnection.prototype.read = function (callback) {
    var self = this;
    var options = util._extend(this.options, {
        method: (this._writeOffset === 0 ? 'GET' : 'POST')
    });
    var onError = function (e) {
        self._request.removeListener('error', arguments.callee);
        logger.error('Error %s reading from %s', e.code, self._config);
        if (callback) {
            callback(e);
        }
    };
    this._request = http.request(options, function (res) {
        if (logger.isDebugEnabled()) {
            logger.debug('reading from %s', this._config);
            logger.debug('status: ' + res.statusCode);
            logger.debug('headers: ' + JSON.stringify(res.headers));
        }
        var onData = function (data) {
            res.removeListener('data', arguments.callee);
            self._request.removeListener('error', onError);
            if (res.statusCode === 200) {
                if (callback) {
                    callback(null, data);
                }
            } else {
                if (callback) {
                    callback(data);
                }
            }
        };
        res.on('data', onData);
    }.bind(this));
    this._request.setHeader('Content-Length', this._writeOffset);
    this._request.setHeader('Connection', 'keep-alive');<commit_msg>Add `withCredentials` and `responseType` headers<commit_after>//       telegram.link
//
//       Copyright 2014 <NAME> '<EMAIL>'
//       Released under the Simplified BSD License
//       http://telegram.link

//      HttpConnection class
//
// This class provides a HTTP transport to communicate with `Telegram` using `MTProto` protocol

// Import dependencies
var http = require('http');
var util = require('util');
var AbstractObject = require("lib/type-language").AbstractObject;
var logger = require('get-log')('net.HttpConnection');

// The constructor accepts optionally an object to specify the connection address as following:
//
//      new HttpConnection({host: "172.16.17.32", port: "443"});
//
// `localhost:80` address is used as default otherwise
function HttpConnection(options) {
    var httpPath = require('../static').telegram.httpPath;
    options = options ?
        ({
            host: (options.proxyHost || options.host || 'localhost'),
            port: (options.proxyPort || options.port || '80'),
//            path: 'http://' + (options.host || 'localhost') + ':' + (options.port || '80') + httpPath
            path: httpPath,
            withCredentials: false
        }) :
        ({
            path: 'http://localhost:80' + httpPath
        });
    this.options = util._extend({
        localAddress: process.env.LOCAL_ADDRESS
    }, options);
    this._config = JSON.stringify(this.options);
    if (logger.isDebugEnabled()) logger.debug('created with %s', this._config);
}

HttpConnection.prototype.connect = function (callback) {
    if (logger.isDebugEnabled()) logger.debug('connected to %s', this._config);
    this._writeBuffers = [];
    this._writeOffset = 0;
    if (callback) callback();
};

HttpConnection.prototype.write = function (data, callback) {
    this._writeBuffers.push(data);
    this._writeOffset += data.length;
    if (logger.isDebugEnabled()) logger.debug('add buffer(%s) to the write buffer queue, total length %s',
        data.length, this._writeOffset);
    if (callback) callback();
};

HttpConnection.prototype.read = function (callback) {
    var self = this;
    var options = util._extend(this.options, {
        method: (this._writeOffset === 0 ? 'GET' : 'POST'),
        responseType: 'arraybuffer'
    });
    var onError = function (e) {
        self._request.removeListener('error', arguments.callee);
        logger.error('Error %s reading from %s', e.code, self._config);
        if (callback) {
            callback(e);
        }
    };
    this._request = http.request(options, function (res) {
        if (logger.isDebugEnabled()) {
            logger.debug('reading from %s', this._config);
            logger.debug('status: ' + res.statusCode);
            logger.debug('headers: ' + JSON.stringify(res.headers));
        }
        var onData = function (data) {
            res.removeListener('data', arguments.callee);
            self._request.removeListener('error', onError);
            if (res.statusCode === 200) {
                if (callback) {
                    callback(null, data);
                }
            } else {
                if (callback) {
                    callback(data);
                }
            }
        };
        res.on('data', onData);
    }.bind(this));
    this._request.setHeader('Content-Length', this._writeOffset);
    this._request.setHeader('Connection', 'keep-alive');
{'fraction_non_alphanumeric': 0.14755077658303464, 'fraction_numerical': 0.008661887694145758, 'mean_word_length': 2.4645628556647696, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2543352', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1816, 'n_words': 439}