TooTallNate / node-http-proxy-agent

An HTTP(s) proxy `http.Agent` implementation for HTTP endpoints
130 stars 18 forks source link

setting auth: property on target #2

Closed codeuniquely closed 9 years ago

codeuniquely commented 9 years ago

For some reason the contents did not save:

I needed to send a request where the target.auth was set to a value. I was wondering if it would to pass in an options { target: url, auth: austhString }

and have the target.auth property set to the options.auth passed in.

I need to POST data to number of different remote severs (which for legacy reasons) don't have CORS enabled and a single proxy.on('proxyReq', ..) handler doesn't really cut it.

Also options.auth would differ by API request

app.post('/csc/broker/storage/:tenant/:id', function(req, res){
app.post('/Hi5Cloud/ContentManagement/uploadFile/:token/:tenant/:id', function(req, res){
etc

function storageRequest(url, auth, req, res) {
    var opts =  {
        target: url,
        auth: auth
    };
    proxy.web(req, res, opts);

}

right now the options block that is used for proxy is something like teh block below with the auth:null (which gets passed along to the target and the connect fails)

{ target:
   { protocol: 'https:',
     slashes: true,
     auth: null,
     host: 'host',
     port: null,
     hostname: 'hostname',
     hash: null,
     search: null,
     query: null,
     pathname: '/ ... path ...'
     path: '/... path .../',
     href: ' https:// ... href ... ' 
},
prependPath: true }

I also not that even though I set https as the protocol to use, port:null

TooTallNate commented 9 years ago

?

codeuniquely commented 9 years ago

For some reason the contents did not save:

TooTallNate commented 9 years ago

Can you show me some failing code?

codeuniquely commented 9 years ago

app.js

var express = require('express'),
    httpProxy = require('http-proxy');

var proxy = httpProxy.createProxyServer({}); 
var app = express();

app.use(express.static(__dirname + '/public'));

function send404(req, res) {
    var url = self.protocol + req.headers.host + req.url;
    res.statusCode = 404;
    res.write('Error 404: Document not found');
    res.end();
}

function proxyRequest(url, auth, req, res) {
    var opts =  {
        target: url,
        auth: auth         /* nice if this went into target.auth */
    };
    proxy.web(req, res, opts);
}

proxy.on('error', function (err, req, res) {
    if (err.code === 'ECONNREFUSED') {
        var remote = req.opts.target || req.opts.forward;
        var method = (req.opts.target) ? 'target' : 'forward';
        console.error('[error] unreachable', method + ':', remote);
    } else {
        console.error(err);
    }
    return send404(req, res);
});

// original site is nomally be somthing like https://www.host.com/storage/:tenant/:id
// local site request is is localhost:3000/storage/TAC/6000000012
app.post('/storage/:tenant/:id', function(req, res){

    var url = req.get('old-target');      // URL i want to sent the request to 
    var auth = 'test.usr:test@user';  // hard coded for now - will be logged in user auth - pass in

    proxyRequest(url,auth, req,res);
}

console.log('Server now listening on port 3000');
app.listen(3000);

<script>
var doneHandler = function(e) {
    console.log(e.response);
}

var xhr = new XMLHttpRequest();

xhr.addEventListener('load', doneHandler, false);
xhr.addEventListener('error', doneHandler, false);    

var target = http://localhost:3000/storage/TAC/6000000012'  
xhr.open('POST', target);
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8');
xhr.setRequestHeader('old-target', https://www.site.com/storage/TAC/6000000012');

var data = 'filename=text.txt&fileContentType=text&fileSize=1024&fileDesc=Test&tenant=TST&tenantKey=A62E12FB'

xhr.send(data);
</script>
TooTallNate commented 9 years ago

I don't see you using http-proxy-agent here. Are you sure you're not trying to report this to: https://github.com/nodejitsu/node-http-proxy ?

codeuniquely commented 9 years ago

sorry - yes - had a number of git windows open - my mistake ..