Edgewall Software
Modify

Ticket #2299 (reopened defect)

Opened 6 years ago

Last modified 2 months ago

Ability to override SCRIPT_NAME from base_url

Reported by: oetiker@… Owned by: mgood
Priority: normal Milestone: unscheduled
Component: general Version: 0.11.6
Severity: normal Keywords: review fcgi
Cc: CarstenFuchs@…
Release Notes:
API Changes:

Description

I am using mod_rewrite to hide the fcgi trac script ... this works fine, except that
trac uses absolte urls in its generated content ... so the name of the fcgi script will be exposed all the same ... with this patch I can set the TRAC_BASE_URL environment variable to tell trac what base URL it should be using.

--- temp/trac/trac-0.9/trac/web/cgi_frontend.py 2005-10-31 19:37:51.000000000 +0100
+++ pack/trac-0.9/lib/python2.3/site-packages/trac/web/cgi_frontend.py  2005-11-02 23:30:52.393286000 +0100
@@ -49,6 +49,10 @@
         self.args = self._getFieldStorage()
 
         self.cgi_location = self.__environ.get('SCRIPT_NAME')
+
+        if 'TRAC_BASE_URL' in os.environ:
+            self.cgi_location = os.environ['TRAC_BASE_URL'];
+
         self.idx_location = self.cgi_location
 
         self.path_info = self.__environ.get('PATH_INFO', '')

Attachments

Change History

comment:1 Changed 6 years ago by mgood

  • Resolution set to worksforme
  • Status changed from new to closed

Instead of mod_rewrite, use a ScriptAlias command to point to the fcgi script and the CGI SCRIPT_NAME variable will be set appropriately.

To run Trac at the root URL:

ScriptAlias / /usr/share/trac/cgi-bin/trac.fcgi

comment:2 Changed 6 years ago by jo@…

  • Resolution worksforme deleted
  • Status changed from closed to reopened

But if one uses RewriteEngine to hide user names in suexec configurations, ScriptAlias does not work. It breaks the suexec mechansim. In such a situation BASE_URL would be a good idea.

comment:3 Changed 5 years ago by sid

  • Keywords review added

Patch attached, so should be reviewed.

comment:4 Changed 5 years ago by cmlenz

  • Resolution set to wontfix
  • Status changed from reopened to closed
  1. the patch is outdated (would need to be updated for 0.10)
  2. this functionality is available by use of the base_url option in trac.ini

comment:5 Changed 5 years ago by tobi@…

  • Resolution wontfix deleted
  • Severity changed from minor to normal
  • Status changed from closed to reopened
  • Version changed from 0.9 to 0.10

base_url in trac.ini does not allow to override what SCRIPT_NAME returns ...
which is necessary when working without ScriptAlias? (see above). The following patch adds the necessary functionallity for trac 0.10:

--- trac/web/api.py.orig        2006-09-21 20:32:16.000000000 +0200
+++ trac/web/api.py     2006-10-22 14:00:07.000000000 +0200
@@ -223,7 +223,15 @@
                                'has not logged in using HTTP authentication')
     scheme = property(fget=lambda self: self.environ['wsgi.url_scheme'],
                       doc='The scheme of the request URL')
-    base_path = property(fget=lambda self: self.environ.get('SCRIPT_NAME', ''),
+
+    def _get_base_url(self):
+       from urlparse import urlparse
+       if self.base_url:
+               return urlparse(self.base_url.encode('latin-1'))[2]
+       else:
+               return self.environ.get('SCRIPT_NAME')
+
+    base_path = property(fget=_get_base_url,
                          doc='The root path of the application')
     server_name = property(fget=lambda self: self.environ['SERVER_NAME'],
                            doc='Name of the server')
                                                                                                                                                                                              

comment:6 Changed 5 years ago by sid

  • Keywords fcgi added

comment:7 Changed 5 years ago by dom@…

I would like this patch to make it in - it is as far as I can see the only way to make nice URLs with fcgi and mod_rewrite. I am currently manually patching to achieve this aim.

comment:8 follow-up: Changed 5 years ago by cboos

  • Milestone set to 0.10.4

Could someone familiar with fcgi review this?

comment:9 Changed 5 years ago by thatch

  • Summary changed from patch enhancing trac for nice urls to Ability to override SCRIPT_NAME from base_url

A slightly related ticket is #2418, which strangely shows up in searches while this one initially didn't.

I run Trac under mod_python and thus can't have this issue (modpython_frontend bails if TRAC_URI_ROOT and base_url don't match), however if this issue still needs fixing I came up with a patch with tests that doesn't reimport urlparse.

comment:10 in reply to: ↑ 8 Changed 5 years ago by mgood

  • Owner changed from jonas to mgood
  • Status changed from reopened to new

Replying to cboos:

Could someone familiar with fcgi review this?

Yeah, I'll do it.

comment:11 Changed 5 years ago by mgood

  • Resolution set to fixed
  • Status changed from new to closed

This has been fixed with r4718:4719 and r4720.

This uses Apache's SCRIPT_URL variable which is specifically intended for reconstructing the logical URL when rewriting has been used, so I think this a cleaner solution.

comment:12 Changed 4 years ago by James Teh <jamie@…>

  • Component changed from general to admin/console
  • Milestone 0.10.4 deleted
  • Priority changed from normal to highest
  • Severity changed from normal to blocker
  • Type changed from enhancement to defect
  • Version 0.10 deleted

If you are using Apache mod_rewrite and the script path is still exposed, be aware that the SCRIPT_URL variable is not set by mod_rewrite if the RewriteEngine on directive is used in .htaccess or directory context. SCRIPT_URL is only set if RewriteEngine on is used in the server or VirtualHost context. This is an extremely frustrating issue in mod_rewrite. I thought this worth noting here to hopefully save others some agony in the future. :)

comment:13 Changed 4 years ago by James Teh <jamie@…>

  • Component changed from admin/console to general
  • Milestone set to 0.10.5
  • Priority changed from highest to normal
  • Severity changed from blocker to normal
  • Type changed from defect to enhancement
  • Version set to 0.10

Err... it'd be nice if I didn't change all the properties by mistake. I think I just found a Firefox bug. :) Sorry people. I can't change the milestone back to 0.10.4, which is what it should be.

comment:14 Changed 2 years ago by cboos

  • Milestone changed from 0.10.5 to 0.10.4

(fixing milestone)

comment:15 follow-ups: Changed 2 years ago by Carsten Fuchs <CarstenFuchs@…>

  • Cc CarstenFuchs@… added
  • Milestone changed from 0.10.4 to 0.12
  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Type changed from enhancement to defect
  • Version changed from 0.10 to 0.11.6

I have been experiencing the same situation as the ticket reporter, see my mails to the trac-users mailing list.

After long and daunting research I found that James Teh is right in his previous comment and that this ticket addresses the root of the problem and thus should be reopened:

When you use mod_rewrite for pretty Trac URLs as described at TracPrettyUrls, and SCRIPT_URL is not set as described by James Teh, then SCRIPT_NAME is always the rewritten name, which in turn causes "unprettied" or "absolute" URLs in the generated content page.

Work-around: Insert line

os.environ['SCRIPT_NAME'] = ""

into file trac.fcgi.

I'm unfortunately not safe enough in Python in order to know better or even to provide a patch, but wouldn't using REQUEST_URI instead of SCRIPT_NAME help here? Alternatively, please apply the above original patch.

comment:16 in reply to: ↑ 15 Changed 2 years ago by Carsten Fuchs <CarstenFuchs@…>

Work-around: Insert line
os.environSCRIPT_NAME? = ""
into file trac.fcgi.

The above work-around unfortunately works only for trac.cgi, not for trac.fcgi, sorry.

comment:17 in reply to: ↑ 15 Changed 2 years ago by Martin.vGagern@…

Replying to Carsten Fuchs <CarstenFuchs@…>:

wouldn't using REQUEST_URI instead of SCRIPT_NAME help here?

One problem is that REQUEST_URI is URL-encoded while SCRIPT_NAME and PATH_INFO are not. So in order to find the base of the Trac environment, one would have to strip the query from REQUEST_URI, URL-decode the rest, strip off PATH_INFO, and probably URL-encode the remainder again. So it's not easy, as I learned the hard way in comment:4:ticket:8328.

comment:18 follow-up: Changed 2 years ago by rblank

  • Milestone changed from 0.12 to next-major-0.1X

We currently don't have a complete solution, and we're a bit short on the testing side for frontends other than mod_python or mod_wsgi. Postponing...

comment:19 Changed 17 months ago by cboos

  • Milestone changed from next-major-0.1X to unscheduled

comment:20 in reply to: ↑ 18 Changed 2 months ago by Mike Kelly <pioto@…>

Replying to rblank:

We currently don't have a complete solution, and we're a bit short on the testing side for frontends other than mod_python or mod_wsgi. Postponing...

If you need volunteers to test patches on mod_fcgi, I can help with that. I'm not familiar enough with the trac codebase to develop a patch myself, but if you gave me pointers I could try to hack something up.

I think a solution to this would be helpful for use in shared hosting environments, where using ScriptAlias or adding anything to the apache config isn't an option, but .htaccess is.

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as reopened
as The resolution will be set. Next status will be 'closed'
to The owner will be changed from mgood. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.