Skip to content
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

Add X-Forwarded-Prefix header to reverse proxies #13

Closed
wants to merge 4 commits into from

Conversation

blaix
Copy link
Contributor

@blaix blaix commented Jan 29, 2015

So the proxied app can know what path it's proxied at. This will allow it to do things like fix URLs in REST API responses.

@thomasw I thought this would work, and the test does pass, but when I install this to my local application that's using generate_routes, it's not sending the header. Any ideas?

So the proxied app can know what path it's proxied at. This will allow
it to do things like fix URLs in REST API responses.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 99.44% when pulling f192bd5 on yola:x_forwarded_prefix into 2f137f6 on thomasw:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 99.44% when pulling f192bd5 on yola:x_forwarded_prefix into 2f137f6 on thomasw:master.

@blaix
Copy link
Contributor Author

blaix commented Jan 29, 2015

I'm pretty proud of that coverage increase.

@@ -135,6 +135,10 @@ def proxy(self):
headers['X-Forwarded-Host'] = self.request.get_host()
headers['X-Forwarded-For'] = self.xff

for prefix, url in self.reverse_urls:
if url == self.proxy_url and self.request.path.startswith(prefix):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe this check is too strict

Copy link
Owner

Choose a reason for hiding this comment

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

I think it should be self.proxy_url.startswith(url), not url == self.proxy_url:

Consider the proxy generated by the following call to generate_routes:

generate_routes({
    'service_one': {
        'base_url': 'https://yahoo.com/',
        'prefix': '/yahoo/'
    }
})

If I go to /yahoo/foo/bar, then proxy_url will be https://yahoo.com/foo/bar/, and won't match https://yahoo.com/.

The definition for proxy_url: https://github.com/yola/djproxy/blob/x_forwarded_prefix/djproxy/views.py#L30-L32

That attribute is used primarily to calculate what URL to hit when the request is sent upstream.

Copy link
Owner

Choose a reason for hiding this comment

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

So, the behavior you'd see with this implemented as is:

Going to /yahoo/ would be good. The prefix would be there. Going to /yahoo/yay/ would be broken, the prefix wouldn't be there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And that explains why my test was passing but my real use locally failed. Thanks!

On Mon, Feb 2, 2015 at 4:13 AM, Thomas Welfley notifications@github.com
wrote:

In djproxy/views.py
#13 (comment):

@@ -135,6 +135,10 @@ def proxy(self):
headers['X-Forwarded-Host'] = self.request.get_host()
headers['X-Forwarded-For'] = self.xff

  •    for prefix, url in self.reverse_urls:
    
  •        if url == self.proxy_url and self.request.path.startswith(prefix):
    

So, the behavior you'd see with this implemented as is:

Going to /yahoo/ would be good. The prefix would be there. Going to
/yahoo/yay/ would be broken, the prefix wouldn't be there.


Reply to this email directly or view it on GitHub
https://github.com/thomasw/djproxy/pull/13/files#r23913254.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nevermind, proxy_url doesn't seem to include the path. That's why I'm checking with ==. So I think this is working. I need to take another look at my look at my local application. I think the problem may be there and not in djproxy.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This still passes: 6972e84

Copy link
Owner

Choose a reason for hiding this comment

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

proxy_url requires information from the route to work correctly. It can't just take the path from the request and mash it onto the upstream base url. It needs to know what subset of the request path to mash onto the url (which is typically achieved with a named pattern).

In your test, proxy_url will always equal the base url unless you specific a url kwarg to the view. The url kwarg is appended to the base_url

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I'm dumb. What you described was the exact problem.

@thomasw
Copy link
Owner

thomasw commented Feb 2, 2015

Since this is a non-standard header, this shouldn't be added to djproxy itself. I currently have an implementation that adds support for proxy middleware which enable arbitrary additions/deletions to headers and much more: https://github.com/thomasw/djproxy/compare/middleware?expand=1#diff-5707531655fb014a093e0475c9472d64R32

My branch also pulls a bunch of stuff that shouldn't have lived in the HttpProxy view into its own classes.

Good news: The existing test suite passes after the updates I've made. Bad news: I don't want to ship that just yet because the complexity of the test suite can be significantly reduced.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 99.44% when pulling 6972e84 on yola:x_forwarded_prefix into 2f137f6 on thomasw:master.

If foo.com is proxied at bar.com/foo, before this change, requests to
bar.com/foo/baz would not send the header.
@blaix
Copy link
Contributor Author

blaix commented Feb 2, 2015

d79369a fixes the issues I was having.

Good news: The existing test suite passes after the updates I've made. Bad news: I don't want to ship that just yet because the complexity of the test suite can be significantly reduced.

I can resubmit this after it ships (if you could comment on this issue when that point comes it would be great). In the meantime, we can use our fork. I'll close this pull.

@blaix blaix closed this Feb 2, 2015
@coveralls
Copy link

Coverage Status

Coverage increased (+0.01%) to 99.44% when pulling d79369a on yola:x_forwarded_prefix into 2f137f6 on thomasw:master.

@thomasw
Copy link
Owner

thomasw commented Feb 2, 2015

I'll let you know. Hopefully I will have something out today.

@thomasw
Copy link
Owner

thomasw commented Feb 3, 2015

2.0.0 has shipped.

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.

3 participants