//     telegram-mt-node
//     Copyright 2014 Enrico Stara 'enrico.stara@gmail.com'
//     Released under the MIT License
//     https://github.com/enricostara/telegram-mt-node

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

// Import dependencies
var httpPath = '/apiw1';
var http = require('http');
var util = require('util');
var logger = require('get-log')('net.HttpConnection');

// The constructor accepts optionally an object to specify the connection address as following:
//
//     new HttpConnection({host: "173.240.5.253", port: "443"});
//
// `localhost:80` address is used as default otherwise
function HttpConnection(options) {
    options = options ?
        ({
            host: (options.proxyHost || options.host || 'localhost'),
            port: (options.proxyPort || options.port || '80'),
            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\n' +
                    'status: %s\n' +
                    'headers: %s',
                this._config, res.statusCode, 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');
    this._request.removeHeader('Content-Type');
    this._request.removeHeader('Accept');
    if (logger.isDebugEnabled()) logger.debug('content-length = %s\ncontent-type = %s\naccept = %s\nhost = %s\nconnection = %s\n',
        this._request.getHeader('Content-Length'), this._request.getHeader('Content-Type'), this._request.getHeader('Accept'),
        this._request.getHeader('Host'), this._request.getHeader('Connection'));
    this._request.on('error', onError);
    var request = Buffer.concat(this._writeBuffers);
    this._writeBuffers = [];
    this._writeOffset = 0;
    if (logger.isDebugEnabled())  logger.debug('writing request(%s) to %s', request.length, this._config);
    this._request.end(request);
};

// Call back, nothing else..
HttpConnection.prototype.close = function (callback) {
    if (callback) callback();
};

// Export the class
module.exports = exports = HttpConnection;
@@ -743,32 +743,85 @@
 ns ?%0A        (%7B%0A
+            protocol: (options.protocol %7C%7C 'http:'),%0A
             host

Add protocol parameter to HttpConnection