"Fossies" - the Fresh Open Source Software Archive

Member "PURELIB/trac/web/modpython_frontend.py" (27 Aug 2019, 5366 Bytes) of package /windows/misc/Trac-1.4.win-amd64.exe:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "modpython_frontend.py": 1.3.5_vs_1.3.6.

    1 # -*- coding: utf-8 -*-
    2 #
    3 # Copyright (C) 2004-2019 Edgewall Software
    4 # Copyright (C) 2004-2007 Christopher Lenz <cmlenz@gmx.de>
    5 # Copyright (C) 2005 Matthew Good <trac@matt-good.net>
    6 # All rights reserved.
    7 #
    8 # This software is licensed as described in the file COPYING, which
    9 # you should have received as part of this distribution. The terms
   10 # are also available at https://trac.edgewall.org/wiki/TracLicense.
   11 #
   12 # This software consists of voluntary contributions made by many
   13 # individuals. For the exact contribution history, see the revision
   14 # history and logs, available at https://trac.edgewall.org/log/.
   15 #
   16 # Author: Christopher Lenz <cmlenz@gmx.de>
   17 #         Matthew Good <trac@matt-good.net>
   18 
   19 import os
   20 import pkg_resources
   21 import sys
   22 import urllib
   23 try:
   24     import threading
   25 except ImportError:
   26     import dummy_threading as threading
   27 
   28 from mod_python import apache
   29 try:
   30     try:
   31         from mod_python import mp_version as version
   32     except ImportError:
   33         from mod_python import version
   34 except ImportError:
   35     version = "< 3.2"
   36 
   37 from trac import __version__ as VERSION
   38 from trac.web.wsgi import WSGIGateway, _ErrorsWrapper
   39 
   40 
   41 class InputWrapper(object):
   42 
   43     def __init__(self, req):
   44         self.req = req
   45 
   46     def close(self):
   47         pass
   48 
   49     def read(self, size=-1):
   50         return self.req.read(size)
   51 
   52     def readline(self, size=-1):
   53         return self.req.readline(size)
   54 
   55     def readlines(self, hint=-1):
   56         return self.req.readlines(hint)
   57 
   58 
   59 class ModPythonGateway(WSGIGateway):
   60 
   61     wsgi_multithread = apache.mpm_query(apache.AP_MPMQ_IS_THREADED) > 0
   62     wsgi_multiprocess = apache.mpm_query(apache.AP_MPMQ_IS_FORKED) > 0
   63 
   64     def __init__(self, req, options):
   65         environ = {}
   66         environ.update(apache.build_cgi_env(req))
   67 
   68         environ['trac.web.frontend'] = 'mod_python'
   69         environ['trac.web.version'] = version
   70 
   71         if 'TracEnv' in options:
   72             environ['trac.env_path'] = options['TracEnv']
   73         if 'TracEnvParentDir' in options:
   74             environ['trac.env_parent_dir'] = options['TracEnvParentDir']
   75         if 'TracEnvIndexTemplate' in options:
   76             environ['trac.env_index_template'] = options['TracEnvIndexTemplate']
   77         if 'TracTemplateVars' in options:
   78             environ['trac.template_vars'] = options['TracTemplateVars']
   79         if 'TracLocale' in options:
   80             environ['trac.locale'] = options['TracLocale']
   81 
   82         if 'TracUriRoot' in options:
   83             # Special handling of SCRIPT_NAME/PATH_INFO for mod_python, which
   84             # tends to get confused for whatever reason
   85             root_uri = options['TracUriRoot'].rstrip('/')
   86             request_uri = environ['REQUEST_URI'].split('?', 1)[0]
   87             if not request_uri.startswith(root_uri):
   88                 raise ValueError('TracUriRoot set to %s but request URL '
   89                                  'is %s' % (root_uri, request_uri))
   90             environ['SCRIPT_NAME'] = root_uri
   91             environ['PATH_INFO'] = urllib.unquote(request_uri[len(root_uri):])
   92 
   93         WSGIGateway.__init__(self, environ, InputWrapper(req),
   94                              _ErrorsWrapper(lambda x: req.log_error(x)))
   95         self.req = req
   96 
   97     def _send_headers(self):
   98         assert self.headers_set, 'Response not started'
   99 
  100         if not self.headers_sent:
  101             status, headers = self.headers_sent = self.headers_set
  102             self.req.status = int(status[:3])
  103             for name, value in headers:
  104                 if name.lower() == 'content-length':
  105                     self.req.set_content_length(int(value))
  106                 elif name.lower() == 'content-type':
  107                     self.req.content_type = value
  108                 else:
  109                     self.req.headers_out.add(name, value)
  110 
  111     def _sendfile(self, fileobj):
  112         self._send_headers()
  113         try:
  114             self.req.sendfile(fileobj.name)
  115         except IOError as e:
  116             if 'client closed connection' not in str(e):
  117                 raise
  118 
  119     def _write(self, data):
  120         self._send_headers()
  121         try:
  122             self.req.write(data)
  123         except IOError as e:
  124             if 'client closed connection' not in str(e):
  125                 raise
  126 
  127 _first = True
  128 _first_lock = threading.Lock()
  129 
  130 def handler(req):
  131     global _first
  132     with _first_lock:
  133         if _first:
  134             _first = False
  135             options = req.get_options()
  136             egg_cache = options.get('PYTHON_EGG_CACHE')
  137             if not egg_cache and options.get('TracEnv'):
  138                 egg_cache = os.path.join(options.get('TracEnv'), '.egg-cache')
  139             if not egg_cache and options.get('TracEnvParentDir'):
  140                 egg_cache = os.path.join(options.get('TracEnvParentDir'), '.egg-cache')
  141             if not egg_cache and req.subprocess_env.get('PYTHON_EGG_CACHE'):
  142                 egg_cache = req.subprocess_env.get('PYTHON_EGG_CACHE')
  143             if egg_cache:
  144                 pkg_resources.set_extraction_path(egg_cache)
  145             reload(sys.modules['trac.web'])
  146     pkg_resources.require('Trac==%s' % VERSION)
  147     gateway = ModPythonGateway(req, req.get_options())
  148     from trac.web.main import dispatch_request
  149     gateway.run(dispatch_request)
  150     return apache.OK