Mailing List Archive

[PATCH] absolute URLs for email notifications
On Tue, May 11, 2004 at 01:04:12AM -0400, Daniel Lundin wrote:
> Good point. Maybe we should even have a config variable with a formatting
> string for the Subject-header.

That would be good.

> It's a bug and my bad. It's supposed to be project.url (as specified when
> running 'trac-admin foo initdb').

Thanks for fixing it.

> This is actually a [potential] issue with the Href class. The Href stuff
> should probably generate absolute paths for links instead of relative ones,
> making it more robust/portable. It would be nice to always rely on
> 'env.href.ticket(42)' always being a valid and useful URL no matter what
> context.

I added an env.abs_href in the attached patch, to avoid disturbing
much of everything. The method I use for constructing it is approved
for CGI environments at least:

<http://httpd.apache.org/docs-2.0/cgi_path.html>

I do not have a tracd installation so I don't know if it breaks
anything there.

--
=Nicholas Riley <njriley@uiuc.edu> | <http://www.uiuc.edu/ph/www/njriley>
-------------- next part --------------
Index: trac/core.py
===================================================================
--- trac/core.py (revision 490)
+++ trac/core.py (working copy)
@@ -284,6 +284,15 @@
def init_request(self):
Request.init_request(self)
self.cgi_location = os.getenv('SCRIPT_NAME')
+ host = os.getenv('SERVER_NAME')
+ port = int(os.getenv('SERVER_PORT'))
+ if port == 80:
+ self.cgi_url = 'http://%s%s' % (host, self.cgi_location)
+ elif port == 443:
+ self.cgi_url = 'https://%s%s' % (host, self.cgi_location)
+ else:
+ self.cgi_url = 'http://%s:%d%s' % (host, port,
+ self.cgi_location)
self.remote_addr = os.getenv('REMOTE_ADDR')
self.remote_user = os.getenv('REMOTE_USER')
self.command = os.getenv('REQUEST_METHOD')
@@ -384,6 +393,7 @@

if not hasattr(env, 'href') or not env.href:
env.href = Href.Href(req.cgi_location)
+ env.abs_href = Href.Href(req.cgi_url)

authenticator = auth.Authenticator(database, req)
if path_info == '/logout':
Index: trac/Notify.py
===================================================================
--- trac/Notify.py (revision 490)
+++ trac/Notify.py (working copy)
@@ -163,7 +163,7 @@
self.newticket = newticket
self.ticket['description'] = wrap(self.ticket['description'],
self.COLS)
- self.ticket['link'] = self.env.href.ticket(tktid)
+ self.ticket['link'] = self.env.abs_href.ticket(tktid)
add_dict_to_hdf(self.ticket, self.hdf, 'ticket')
self.hdf.setValue('email.ticket_props', self.format_props())
self.hdf.setValue('email.ticket_body_hdr', self.format_hdr())
Re: [PATCH] absolute URLs for email notifications [ In reply to ]
On Wed, May 12, 2004 at 01:26:17PM -0500, Nicholas Riley wrote:
> I added an env.abs_href in the attached patch, to avoid disturbing
> much of everything. The method I use for constructing it is approved
> for CGI environments at least:
>
> <http://httpd.apache.org/docs-2.0/cgi_path.html>
>
> I do not have a tracd installation so I don't know if it breaks
> anything there.

The attached patch works for tracd as well. The URLs generated are
incorrect without it in every circumstance I've found, so it or
something equivalent really should go into 0.7...

--
=Nicholas Riley <njriley@uiuc.edu> | <http://www.uiuc.edu/ph/www/njriley>
-------------- next part --------------
Index: scripts/tracd
===================================================================
--- scripts/tracd (revision 516)
+++ scripts/tracd (working copy)
@@ -315,6 +315,14 @@

if not args:
usage()
+
+ server_address = (hostname, port)
+ httpd = TracHTTPServer(server_address, TracHTTPRequestHandler)
+ if httpd.server_port == 80:
+ httpd.http_host = httpd.server_name
+ else:
+ httpd.http_host = '%s:%d' % (httpd.server_name, httpd.server_port)
+
for path in args:
project = os.path.split(path)[1]
if not project:
@@ -326,17 +334,11 @@
# Upgrade the database schema if needed
env.upgrade(backup=1)
env.href = Href('/' + project)
+ env.abs_href = Href('http://%s/%s' % (httpd.http_host, project))
projects[project] = env
projects[project].set_config('trac', 'htdocs_location',
'/trac_common/')
projects[project].auth = auth
-
- server_address = (hostname, port)
- httpd = TracHTTPServer(server_address, TracHTTPRequestHandler)
- if httpd.server_port == 80:
- httpd.http_host = httpd.server_name
- else:
- httpd.http_host = '%s:%d' % (httpd.server_name, httpd.server_port)
httpd.projects = projects
httpd.serve_forever()

Index: trac/core.py
===================================================================
--- trac/core.py (revision 516)
+++ trac/core.py (working copy)
@@ -284,6 +284,18 @@
def init_request(self):
Request.init_request(self)
self.cgi_location = os.getenv('SCRIPT_NAME')
+ host = os.getenv('SERVER_NAME')
+ try:
+ port = int(os.getenv('SERVER_PORT'))
+ except TypeError:
+ port = 80
+ if port == 80:
+ self.cgi_url = 'http://%s%s' % (host, self.cgi_location)
+ elif port == 443:
+ self.cgi_url = 'https://%s%s' % (host, self.cgi_location)
+ else:
+ self.cgi_url = 'http://%s:%d%s' % (host, port,
+ self.cgi_location)
self.remote_addr = os.getenv('REMOTE_ADDR')
self.remote_user = os.getenv('REMOTE_USER')
self.command = os.getenv('REQUEST_METHOD')
@@ -387,6 +399,8 @@

if not hasattr(env, 'href') or not env.href:
env.href = Href.Href(req.cgi_location)
+ if not hasattr(env, 'abs_href') or not env.abs_href:
+ env.abs_href = Href.Href(req.cgi_url)

authenticator = auth.Authenticator(database, req)
if path_info == '/logout':
Index: trac/Notify.py
===================================================================
--- trac/Notify.py (revision 516)
+++ trac/Notify.py (working copy)
@@ -163,7 +163,7 @@
self.newticket = newticket
self.ticket['description'] = wrap(self.ticket['description'],
self.COLS)
- self.ticket['link'] = self.env.href.ticket(tktid)
+ self.ticket['link'] = self.env.abs_href.ticket(tktid)
add_dict_to_hdf(self.ticket, self.hdf, 'ticket')
self.hdf.setValue('email.ticket_props', self.format_props())
self.hdf.setValue('email.ticket_body_hdr', self.format_hdr())
Re: [PATCH] absolute URLs for email notifications [ In reply to ]
Nicholas Riley wrote:
> On Wed, May 12, 2004 at 01:26:17PM -0500, Nicholas Riley wrote:
>
>>I added an env.abs_href in the attached patch, to avoid disturbing
>>much of everything. The method I use for constructing it is approved
>>for CGI environments at least:
>>
>> <http://httpd.apache.org/docs-2.0/cgi_path.html>
>>
>>I do not have a tracd installation so I don't know if it breaks
>>anything there.
>
>
> The attached patch works for tracd as well. The URLs generated are
> incorrect without it in every circumstance I've found, so it or
> something equivalent really should go into 0.7...
>

Yes, this is definitely a bug and I like your solution by adding a new
Href instance that returns absolute urls. Your patch is committed in
[524]. Thanks!

/ Jonas
--
Jonas Borgstr?m | Edgewall Software
jonas@edgewall.com | Professional GNU/Linux & Open Source Consulting.
| http://www.edgewall.com/