Changeset 2156

Show
Ignore:
Timestamp:
04/03/07 14:49:05 (2 years ago)
Author:
martinpaljak
Message:

TracWsgiPlugin:

Small addition to usage - paste entry point added.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tracwsgiplugin/0.11/setup.cfg

    r2149 r2156  
    11[egg_info] 
    22tag_build = dev 
    3 tag_date = 0 
    4 tag_svn_revision = 0 
     3tag_svn_revision = true 
    54 
  • tracwsgiplugin/0.11/setup.py

    r2149 r2156  
    99    description='Trac WSGI plugin', 
    1010    author="Martin Paljak", author_email="martin@paljak.pri.ee", 
    11     license='GPL', url='http://ideelabor.ee/opensource/', 
     11    license='GPL', url='http://www.trac-hacks.org/wiki/TracWsgiPlugin', 
    1212    packages=find_packages(exclude=['ez_setup', '*.tests*']), 
     13    zip_safe=False, 
     14    install_requires = [ 
     15            'setuptools>=0.6c5', 
     16            'Trac>=0.11dev', 
     17            'PasteDeploy', 
     18        ], 
    1319    entry_points = { 
    1420        'trac.plugins': [ 
    1521            'wsgiplugin.wsgiplugin = wsgiplugin.wsgiplugin' 
     22        ], 
     23        'paste.app_factory': [ 
     24            'main = wsgiplugin.wsgiplugin:wsgi_trac', 
     25            'permanent_redirect = wsgiplugin.wsgiplugin:permanent_redirect', 
     26            'temporary_redirect = wsgiplugin.wsgiplugin:temporary_redirect' 
    1627        ] 
    1728    } 
  • tracwsgiplugin/0.11/wsgiplugin/wsgiplugin.py

    r2149 r2156  
    11from paste.deploy import loadapp 
     2from paste.deploy.converters import asbool 
    23 
    34import re 
     5import urlparse 
    46 
    57from trac.config import * 
     
    79from trac.web.api import IRequestHandler, RequestDone 
    810 
     11from trac.web.main import dispatch_request 
     12__all__ = ["WSGIPluginModule", "WSGITrac"] 
    913 
    1014class WSGIPluginModule(Component): 
     
    4650          req.write(chunk) 
    4751        raise RequestDone 
     52 
     53 
     54class WSGITrac: 
     55    """Callable class. Initi with path=/path/to/trac/env""" 
     56    def __init__(self, path, secure=False): 
     57      self.path = path 
     58      self.secure = secure 
     59       
     60    def __call__(self, environ, start_response): 
     61      environ['trac.env_path'] = self.path 
     62       
     63      https = environ.get("HTTPS", "off") 
     64      if self.secure and https != 'on': 
     65        return redirect_https(environ, start_response) 
     66 
     67      return dispatch_request(environ, start_response) 
     68 
     69 
     70def redirect_https(environ, start_response): 
     71        url = reconstruct_url(environ) 
     72        s=list(urlparse.urlsplit(url)) 
     73        s[0]="https" 
     74        u2 = urlparse.urlunsplit(s) 
     75        start_response("302 Temporary Redirect", [('location', u2), ('content-type', 'text/plain')]) 
     76        return [u2] 
     77 
     78def reconstruct_url(environ): 
     79        from urllib import quote 
     80        url = environ['wsgi.url_scheme']+'://' 
     81         
     82        if environ.get('HTTP_HOST'): 
     83                url += environ['HTTP_HOST'] 
     84        else: 
     85                url += environ['SERVER_NAME'] 
     86 
     87                if environ['wsgi.url_scheme'] == 'https': 
     88                        if environ['SERVER_PORT'] != '443': 
     89                                url += ':' + environ['SERVER_PORT'] 
     90                else: 
     91                        if environ['SERVER_PORT'] != '80': 
     92                                url += ':' + environ['SERVER_PORT'] 
     93        url += quote(environ.get('SCRIPT_NAME','')) 
     94        url += quote(environ.get('PATH_INFO','')) 
     95        if environ.get('QUERY_STRING'): 
     96                url += '?' + environ['QUERY_STRING'] 
     97        return url 
     98 
     99                         
     100def wsgi_trac(global_conf, path = None, secure = False, **local_conf): 
     101    return WSGITrac(path, secure=asbool(secure)) 
     102 
     103 
     104class Redirect(): 
     105  def __init__(self, url, code=302): 
     106    self.url = url 
     107    self.status = "%d Redirect" %(code) 
     108  def __call__(self, environ, start_response): 
     109    start_response(self.status, [('Location', self.url)]) 
     110    return [] 
     111 
     112def permanent_redirect(global_conf, url): 
     113  return Redirect(url, 301) 
     114 
     115def temporary_redirect(global_conf, url):        
     116  return Redirect(url, 302) 
     117