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

url_for() doesn't return a working URI for a blueprint route with host #1788

Closed
don-makaron opened this issue Feb 19, 2020 · 1 comment
Closed

Comments

@don-makaron
Copy link

Describe the bug
When i use blueprints with host argument and try to get url with url_for i get an incorrect url - blueprint's host is used as a path, and not as a hostname

Code snippet

from sanic import Sanic, Blueprint
from sanic.response import text

bp = Blueprint('bp_app', host='bp.example.com')


@bp.route('/', name='index')
async def bp_index(request):
    url = request.app.url_for('bp_app.index')
    return text(url)


@bp.route('/internal', name='internal')
async def bp_index(request):
    url = request.app.url_for('bp_app.internal')
    return text(url)


@bp.route('/external', name='external')
async def bp_index(request):
    url = request.app.url_for('bp_app.external', _external=True)
    return text(url)


app = Sanic('app_name')
app.blueprint(bp)


if __name__ == '__main__':
    app.run(port=8000)

Output

# 1
$ curl -H "Host:bp.example.com" http://127.0.0.1:8000/
bp.example.com

# 2
$ curl -H "Host:bp.example.com" http://127.0.0.1:8000/internal
bp.example.com/internal

# 3
$ curl -H "Host:bp.example.com" http://127.0.0.1:8000/external
http:///bp.example.com/external

1 example returns hostname by like a path
2 example returns hostname and path but it's like a path
3 example return full url, but with no hostname (3 slashes)

Expected behavior
I'm expecting correct urls.
For internal urls - only path returned as stated in route.
For external urls - fully qualified domain name with full path

1 example - /
2 example - /internal
3 example - http://bp.example.com/external

Environment (please complete the following information):

  • OS: macOS
  • Version: 19.12.2

Additional context
Can't get correct place, but found some places, where it can be
app.py#L829-L832,
app.py#L848 (perhaps, blueprint's host should be used, and config's server name as default)

@Tronic
Copy link
Member

Tronic commented Feb 20, 2020

Thanks for reporting and triaging this!

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

No branches or pull requests

2 participants