Hi,

While experimenting with server.cgi, I found I wasn't too fond of requiring 
URLs to be in the form 'http://mysite.com/server.cgi/ModuleName'. 
Ugly to my eyes.

So I've come up with some tricks that allow more elegant URLs like:
'http://mysite.com/ModuleName'.
In effect, it creates an invisible framework that allows entire websites to be 
built as a set of modules in the Scripts subdirectory.

Basically, this is done with the following .htaccess commands for URL 
rewriting:
        ErrorDocument 404 /404.html
        RewriteEngine on
        RewriteCond %{REQUEST_URI} !(.*)404(.*)
        RewriteCond %{REQUEST_URI} !^images(.*)  # add similar for any other dirs
        RewriteCond %{REQUEST_URI} !(.*)server\.cgi(.*)$
        RewriteRule ^(.*) server.cgi/$1

Also, I've added a module in Scripts called 'Err':

        from CGIWrapper import wrapper
        import os

        def IllegalAccess():
            global wrapper
            wrapper._headers = {'Status':'404 Not Found', 'Content-Type':'text/html'}
            print file('../404.html', 'r').read()

        if os.environ['REQUEST_URI'] == '/Err':
            IllegalAccess()

The method 'IllegalAccess()' sends back the 404.html page in the site's root 
directory.

Also, I've created a patch for CGIWrapper.py which you can get from
http://www.freenet.org.nz/downloads/CGIWrapper.patch.

The patch adds the following:
* Assumes a module called 'Default' when no module is specified
* Sends back a 404 page (in /404.html) when module is not found
* Puts some statements into 'try...except' to cover extraneous failures
* Prevents access to *anything* except permitted modules, plus authorised
  directories such as 'images'.

And lastly, within any modules in Scripts dir that aren't meant to be used 
directly, I put:
        # prevent direct call to this script
        from Err import IllegalAccess
        if os.environ['REQUEST_URI'] == '/NameOfThisModule':
            IllegalAccess()

Results of all this?

* The presence of CGIWrapper is totally transparent
* URLs like 'http://mysite.com' run the 'Default' module
* Attempts to access a module which shouldn't be directly accessed
  results in a 404
* Attempts to access a nonexistent module return a 404
* URLs like 'http://mysite.com/SomeModule' are equivalent to
  'http://mysite.com/server.cgi/SomeModule'. More elegant.

Thanks for writing WebWare and CGIWrapper. I'm a total newbie to Python, 
battling to learn it in the fastest possible time. Your code is helping me 
heaps!

I'll be free of the pain of PHP and PHP-Nuke sooner than I thought. :)

Cheers
David



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to