Problem using a proxy #56

Closed
automatthew opened this Issue Aug 28, 2013 · 1 comment

2 participants

@automatthew
Panda Strike member

Submitted by Fuli via email

Hi,

I used your nodeJS module "shred" for a while, it is pretty good.
Recently, I want to support proxy in my project, but I found it does not
work when I set "proxy" attribute in options, the request is sent to
proxy server, but it can not get response back. In network monitor
application, I found the http request sent to proxy is unreadable.

the proxy server I used is CCProxy.


I output the request header in shred/lib/request.js before it calls
http.request().
it is as below:

reqParams = {
    host: '10.10.7.252',
    port: '808',
    method: 'get',
    path: 'http://10.10.7.64:2000/lib/defaultsite/publish/50dd19a9f11bc57013000012/playlist_20130828_20130828.publ',
    headers: {
        'User-Agent': 'Shred',
        Connection: 'keep-alive',
        Accept: '*/*',
        'If-Modified-Since': '',
        Cookie: 'connect.sid=s%3A5ufL%2BBYOw41Gtnz71YalDECz.MC%2FXf8wWZe12bzF1Pm7q4RmvMrgaGk6HymceW3AQvgU'
    },
    scheme: '',
    agent: undefined
}

With my test code which just call http.request with above request data,
it can get correct data in response event. the source code is:

this.sendRequestToServer = function (urlString, cookie, proxy, callback) {
    var req = null;
    var urlObj = {};
    var postString = '';
    var that = this;
    var authBuffer = null;
    var bufferArray = [];
    var bufferIndex = 0;
    var finalBuffer = null;
    var status = false;
    var returncookie = '';
    var bufferSize = 0;

    if (!urlString || (urlString.indexOf('http') !== 0)) {
        return callback(4,
            '');
    }
    if (proxy && proxy.host && proxy.port) {
        urlObj.host = proxy.host;
        urlObj.port = proxy.port;
        urlObj.headers = {};
        urlObj.path = urlString;
    } else {
        urlObj = url.parse(urlString);
        urlObj.headers = {};
    }

    urlObj.method = 'get';
    urlObj.headers.connection = 'keep-alive';
    urlObj.headers.accept = 'application/json, text/javascript, */*; q=0.01';
    urlObj.headers['Content-Type'] = 'application/x-www-form-urlencoded';
    urlObj.headers.Cookie = cookie;

    req = http.request(urlObj, function (res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            if (res.statusCode === 200) {
                bufferArray[bufferIndex] = chunk;
                bufferIndex++;
                bufferSize += chunk.length;

                if (res.headers['set-cookie']) {
                    returncookie = res.headers['set-cookie'];
                }
                status = true;
            } else {
                status = false;
            }
        });

        res.on('end', function () {
            if (status) {
                if (bufferIndex === 0) { //never download at all
                    return callback(11, '', returncookie);
                } else {
                    if (bufferIndex === 1) {
                        finalBuffer = bufferArray[0];
                    } else {
                        finalBuffer = Buffer.concat(bufferArray, bufferSize);
                    }
                    return callback(0, finalBuffer, returncookie);
                }
            } else {
                return callback(11, res.statusCode, '');
            }
        });
        res.on('close', function (err) {
            console.log('receive close event.' + err);
        });
    });

    req.on('error', function (e) {
        console.log('receive error event.');
        console.log(e);
        return callback(11, '', '');
    });

    req.end();
}

I do not know where has problem in shred, can you help me?

@dyoder
Panda Strike member

Shred NG is a complete rewrite. We have yet to add proxy support: see #69.

@dyoder dyoder closed this May 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment