Ticket #316 (assigned defect)

Opened 3 years ago

Last modified 2 weeks ago

[PATCH] Modify turbogears.url so that it can return an absolute path

Reported by: michele Assigned to: Chris Arndt (accepted)
Priority: low Milestone: 1.1 beta 2
Component: TurboGears Version: 1.0.4.4
Severity: minor Keywords: needs tests, needs review
Cc:

Description

Link to the discussion where Elvelind declared he had a possible patch. :P

Attachments

full_url_patch.diff (3.0 kB) - added by singletoned on 03/07/07 12:37:47.
A patch to add full url capabilities into the url() function

Change History

01/16/06 16:10:39 changed by anonymous

  • milestone set to 0.9.

02/23/06 12:57:57 changed by kevin

  • status changed from new to closed.
  • resolution set to wontfix.

The problem with trying to build out a full URL (rather than just an absolute path on the same server) is that there could be problems if you're running behind a proxy (like Apache) and the server name shows up as localhost. There's really no need for a full URL to come from this function anyhow. An absolute URL or relative URL will get you where you need to be within your app.

03/06/07 08:56:29 changed by singletoned

  • status changed from closed to reopened.
  • resolution deleted.
  • milestone changed from 0.9 to 1.0.2.

Can this ticket be reopened? There are lots of valid reasons for wanting a full url, such as when sending out emails, creating RSS feeds, etc.

03/07/07 12:37:47 changed by singletoned

  • attachment full_url_patch.diff added.

A patch to add full url capabilities into the url() function

03/07/07 12:39:51 changed by singletoned

  • summary changed from Modify turbogears.url so that it can return an absolute path to [patch] Modify turbogears.url so that it can return an absolute path.

04/06/07 08:02:11 changed by alberto

Hmmm, patch breaks 6 tests. Can you please submit a modified version that works?

Thanks,

Alberto

04/06/07 08:03:08 changed by alberto

Where did I get that "6 tests" figure?... anyaway...

Ran 247 tests in 13.683s

FAILED (failures=1, errors=8)

04/06/07 08:04:23 changed by alberto

  • summary changed from [patch] Modify turbogears.url so that it can return an absolute path to Modify turbogears.url so that it can return an absolute path.

05/01/07 12:56:57 changed by alberto

  • milestone changed from 1.0.2 to 1.0.3.

07/01/07 18:22:34 changed by faide

  • summary changed from Modify turbogears.url so that it can return an absolute path to [PATCH] Modify turbogears.url so that it can return an absolute path.

07/03/07 09:45:30 changed by faide

  • milestone changed from 1.0.3 to 1.1.

06/06/08 06:25:29 changed by faide

  • version set to 1.0.4.4.

08/22/08 09:22:56 changed by Chris Arndt

Wow, this ticket is waaay old! It don't like the proposed patch because it changes the function signature by adding an extra keyword argument, which might (though unlikely) break existing code and also, it doesn't really work - explanation follows:

Unless you use HTTPS (more below), an absolute URL can easily be constructed with the following function:

from turbogears import config, url

def absolute_url(tgpath='/', params=None, **kw):
    """Returns absolute URL (including schema and host to this server)."""

    h = cherrypy.request.headers
    use xfh = config.get('base_url_filter.use_x_forwarded_host', False)
    base_url = 'http://%s' % h.get('X-Forwarded-Host', h['Host'])
    if config.get('base_url_filter.on', False) and not use_xfh:
        base_url = config.get('base_url_filter.base_url').rstrip('/')
    return 'http://%s%s' % (base_url, url(tgpath, params, **kw))

The problem with HTTPS is that there is no way to detect that the client is talking HTTPS when your app is running behind a reverse proxy (see also this wiki document).

Two possible solutions:

  1. You can configure your web server to add a special header if the connection between the client and the webserver is over HTTPS and then use this information in absolute_url function:
def absolute_url(tgpath='/', params=None, **kw):
    """Returns absolute URL (including schema and host to this server)."""

    h = cherrypy.request.headers
    use xfh = config.get('base_url_filter.use_x_forwarded_host', False)
    if cherrypy.request.headers.get('X-Use-SSL'):
        scheme = 'https'
    else:
        scheme = 'http'
    base_url = '%s://%s' % (scheme, h.get('X-Forwarded-Host', h['Host']))
    if config.get('base_url_filter.on', False) and not use_xfh:
        base_url = config.get('base_url_filter.base_url').rstrip('/')
    return 'http://%s%s' % (base_url, url(tgpath, params, **kw))
  1. You can just set base_url_filter.on = True, base_url_filter.use_x_forwarded_host = False and base_url_filter.base_url to the base URL of your site including the HTTPS scheme, e.g. https://example.com.

To sum it up, there is no automatic way for TG to construct an absolute URL that works in all cases, without the help from either web server or application configuration. I suggest that we add the above function as a new feature to the 1.1 branch. Maybe it should then also determine the URL scheme from a new configuration setting, e.g. tg.url_scheme = 'https' or sth. similar.

08/22/08 09:37:51 changed by singletoned

  • priority changed from normal to low.
  • status changed from reopened to closed.
  • resolution set to wontfix.
  • severity changed from normal to minor.

To be honest, unless there's demand for this from other people, I'd just drop it. I stopped using TG quite a while ago, and therefore don't have any need for the feature anymore.

08/22/08 09:52:35 changed by faide

  • status changed from closed to reopened.
  • resolution deleted.

+1 for Chris solution. But this helper function is not a _MUST_ have and could be postponed a little bit more, or applied in 1.5...

08/22/08 09:54:28 changed by faide

Please if you don't maintain TG, don't close tickets when developers are working on them. It may become confusing to us :)

08/24/08 10:27:00 changed by faide

  • milestone changed from 1.5 to 1.1.

09/10/08 16:51:38 changed by Chris Arndt

  • keywords set to needs tests, needs review.
  • milestone changed from 1.1 to 1.1 beta 2.

09/24/08 06:13:01 changed by Chris Arndt

  • owner changed from anonymous to Chris Arndt.
  • status changed from reopened to new.

09/24/08 06:13:12 changed by Chris Arndt

  • status changed from new to assigned.