Skip to content

Fix proxy path #645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Fix proxy path #645

wants to merge 1 commit into from

Conversation

Domiii
Copy link
Contributor

@Domiii Domiii commented May 31, 2014

This fix considers the actual target path again (which has been ignored).

This fix considers the actual target path again (which has been ignored).
@jcrugzz
Copy link
Contributor

jcrugzz commented May 31, 2014

@Domiii I can see this being valid but you need to handle the case where path is not defined. See the failing tests under 0.10.x.

@Domiii
Copy link
Contributor Author

Domiii commented Jun 3, 2014

Yeah. I suppose, we should also use url.resolve instead of +. I'll take a shot at it later today.

@jcrugzz
Copy link
Contributor

jcrugzz commented Jun 3, 2014

@Domiii well it needs to be ignored in the case where the path is undefined

@Rush
Copy link
Contributor

Rush commented Jun 8, 2014

I would instead like to see a hook so that is could be fixed manually.

@greaterweb
Copy link

I ran into a similar issue when using this in my project. Having the full path honored seems to be the expected functionality so hopefully this will be patch in a future release.

For the time being, here is an example work around that doesn't require any updates to http-proxy, it expands on the ProxyTable API example.

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

// Additional dependencies needed
var path = require('path');
var url = require('url');

var proxy = httpProxy.createProxy();

var routes = {  
    'foo.com': 'http://website.com:8001',
    'bar.com': 'http://website2.com:8002/foo',
    'baz.com': 'http://website.com:8001/foo/bar',
    'qux.com': 'http://website2.com:8002/foo/bar/baz'
};

require('http').createServer(function(req, res) {  
    // Before sending to the proxy, I actually test to see
    // there is a key present in the proxy table (aka, routes)
    if (routes[req.headers.host]) {
        // parse out the hostname
        var route = url.parse(routes[req.headers.host]);
        // use path to neatly join the route path to the
        // requested url. this keeps the output valid when
        // the route.path == '/'
        req.url = path.join(route.path, req.url);
        // send off to the proxy as you normally would
        return proxy.web(req, res, {
            target: routes[req.headers.host]
        });
    }
    // If we have no proxy target just send a 404
    res.writeHead(404);
    res.end();
}).listen(8000);

@jcrugzz
Copy link
Contributor

jcrugzz commented Aug 5, 2014

@greaterweb thanks for the example! I just modified your comment to have the correct api :).

@@ -57,10 +57,14 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
) { outgoing.headers.connection = 'close'; }
}


// the final path is target path + relative path requested by user:
outgoing.path = options.target.path;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this better fit as written like below? This matches the convention used throughout the setupOutgoing method.

outgoing.path =  options[forward || 'target'].path;

This could also be expanded to be written as below. This would likely satisfy the integration testing.

outgoing.path =  options[forward || 'target'].path || '';

If we don't like the idea of setting outgoing.path as an empty string this could easily be rewritten to be wrapped in some conditionals.

@greaterweb
Copy link

Thanks for the fix, @jcrugzz

Copy and paste fail on my end :-) I coupled things from the ProxyTable example and my project (which uses express and does a few other things).

@manast manast mentioned this pull request Aug 17, 2014
@EndangeredMassa
Copy link
Contributor

What's the next step for this? Just fix the tests?

@jcrugzz
Copy link
Contributor

jcrugzz commented Sep 8, 2014

@EndangeredMassa yes it needs to be able to pass the current tests and there should be tests for the use case that this is a fix for.

@EndangeredMassa
Copy link
Contributor

@Domiii do you plan on fixing up the tests for this? If not, I could take a look.

@indexzero
Copy link
Member

@EndangeredMassa that would be huge.

@EndangeredMassa
Copy link
Contributor

Submitted a new PR (that includes this original commit) to address the tests. Required some code changes.

#693

@jcrugzz
Copy link
Contributor

jcrugzz commented Sep 8, 2014

Fixed in #693

@jcrugzz jcrugzz closed this Sep 8, 2014
@Domiii
Copy link
Contributor Author

Domiii commented Sep 13, 2014

Thank you, EndangeredMassa! And sorry for the late response!

@EndangeredMassa
Copy link
Contributor

No worries!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants