summaryrefslogtreecommitdiff
path: root/paste/urlparser.py
blob: b906084713f619c6c5e3a3d8db05194840a61230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import os
import sys
import imp
import wsgilib
from paste.docsupport import metadata

class NoDefault:
    pass

__all__ = ['URLParser']

class URLParser(object):

    """
    WSGI middleware

    Application dispatching, based on URL.  An instance of `URLParser` is
    an application that loads and delegates to other applications.  It
    looks for files in its directory that match the first part of
    PATH_INFO; these may have an extension, but are not required to have
    one, in which case the available files are searched to find the
    appropriate file.  If it is ambiguous, a 404 is returned and an error
    logged.

    By default there is a constructor for .py files that loads the module,
    and looks for an attribute ``application``, which is a ready
    application object, or an attribute that matches the module name,
    which is a factory for building applications, and is called with no
    arguments.

    URLParser will also look in __init__.py for special overrides.
    Currently the only override is urlparser_hook(environ), which can
    modify the environment; its return value is ignored.  You can use
    this, for example, to manipulate SCRIPT_NAME/PATH_INFO (try to
    keep them consistent with the original URL -- but consuming
    PATH_INFO and moving that to SCRIPT_NAME is ok).
    """

    _config_index_name = metadata.Config(
        """
        A list of allowed names for the index file (the file served
        for requests that end in ``/``).
        """, default=['index', 'Index', 'main', 'Main'])

    _config_hide_extensions = metadata.Config(
        """
        A list of extensions (with leading ``.``) that should not ever
        be served.""", default=['.pyc', '.bak', '.py~'])

    _config_ignore_extensions = metadata.Config(
        """
        Extensions that will be ignored when searching for a file.  If
        the extension is given explicitly, files with these extensions
        will still be served.""", default=[])

    _config_constructors = metadata.Config(
        """
        A dictionary of extensions as keys, and application constructors
        as values.  Also the key ``dir`` for directories, and ``*`` when
        no other constructor is found.

        Each constructor is called like ``constructor(environ, filename)``
        and should return an application or ``None``.
        """)

    default_options = {
        'index_names': ['index', 'Index', 'main', 'Main'],
        'hide_extensions': ['.pyc', '.bak', '.py~'],
        'ignore_extensions': [],
        'constructors': {},
        }

    parsers_by_directory = {}

    # This is lazily initialized
    init_module = NoDefault

    def __init__(self, directory, base_python_name, add_options=None):
        """
        Create a URLParser object that looks at `directory`.
        `base_python_name` is the package that this directory
        represents, thus any Python modules in this directory will
        be given names under this package.

        `add_options` overrides individual configuration options for
        this instance.
        """
        if os.path.sep != '/':
            directory = directory.replace(os.path.sep, '/')
        self.directory = directory
        self.add_options = add_options
        self.base_python_name = base_python_name

    def __call__(self, environ, start_response):
        environ['paste.urlparser.base_python_name'] = self.base_python_name
        if self.add_options:
            if environ.has_key('paste.urlparser.options'):
                environ['paste.urlparser.options'].update(self.add_options)
            else:
                environ['paste.urlparser.options'] = self.add_options.copy()
        if self.init_module is NoDefault:
            self.init_module = self.find_init_module(environ)
        path_info = environ.get('PATH_INFO', '')
        if not path_info:
            return self.add_slash(environ, start_response)
        if (self.init_module
            and getattr(self.init_module, 'urlparser_hook', None)):
            self.init_module.urlparser_hook(environ)
        name, rest_of_path = wsgilib.path_info_split(environ['PATH_INFO'])
        orig_path_info = environ['PATH_INFO']
        orig_script_name = environ['SCRIPT_NAME']
        environ['PATH_INFO'] = rest_of_path
        if name is not None:
            environ['SCRIPT_NAME'] = environ.get('SCRIPT_NAME', '') + '/' + name
        if not name:
            names = self.option(environ, 'index_names') or []
            for index_name in names:
                filename = self.find_file(environ, index_name)
                if filename:
                    break
            else:
                # None of the index files found
                filename = None
        else:
            filename = self.find_file(environ, name)
        if filename is None:
            application = None
        else:
            application = self.get_application(environ, filename)
        if not application:
            if (self.init_module
                and getattr(self.init_module, 'not_found_hook', None)
                and environ.get('paste.urlparser.not_found_parser') is not self):
                not_found_hook = self.init_module.not_found_hook
                environ['paste.urlparser.not_found_parser'] = self
                environ['PATH_INFO'] = orig_path_info
                environ['SCRIPT_NAME'] = orig_script_name
                return not_found_hook(environ, start_response)
            if filename is None:
                if not name:
                    desc = 'one of %s' % ', '.join(
                        self.option(environ, 'index_names') or
                        ['(no index_names defined)'])
                else:
                    desc = name

                return self.not_found(
                    environ, start_response,
                    'Tried to load %s from directory %s'
                    % (desc, self.directory))
            else:
                environ['wsgi.errors'].write(
                    'Found resource %s, but could not construct application\n'
                    % filename)
                return self.not_found(
                    environ, start_response,
                    'Tried to load %s from directory %s'
                    % (filename, self.directory))
        if (self.init_module
            and getattr(self.init_module, 'urlparser_wrap', None)):
            return self.init_module.urlparser_wrap(
                environ, start_response, application)
        else:
            return application(environ, start_response)

    def not_found(self, environ, start_response, debug_message=None):
        status, headers, body = wsgilib.error_response(
            environ,
            '404 Not Found',
            'The resource at %s could not be found'
            % wsgilib.construct_url(environ),
            debug_message=debug_message)
        start_response(status, headers)
        return [body]

    def option(self, environ, name):
        return environ.get('paste.urlparser.options', {}).get(
            name, self.default_options.get(name))

    def add_slash(self, environ, start_response):
        """
        This happens when you try to get to a directory
        without a trailing /
        """
        url = wsgilib.construct_url(environ, with_query_string=False)
        url += '/'
        if environ.get('QUERY_STRING'):
            url += '?' + environ['QUERY_STRING']
        status = '301 Moved Permanently'
        status, headers, body = wsgilib.error_response(
            environ,
            status,
            '''
            <p>The resource has moved to <a href="%s">%s</a>.  You
            should be redirected automatically.</p>''' % (url, url))
        start_response(status, headers + [('Location', url)])
        return [body]

    def find_file(self, environ, base_filename):
        possible = []
        """Cache a few values to reduce function call overhead"""
        ignore_extensions = self.option(environ, 'ignore_extensions')
        hide_extensions = self.option(environ, 'hide_extensions')
        for filename in os.listdir(self.directory):
            base, ext = os.path.splitext(filename)
            full_filename = os.path.join(self.directory, filename)
            if (ext in hide_extensions
                or not base):
                continue
            if filename == base_filename:
                possible.append(full_filename)
                continue
            if ext in ignore_extensions:
                continue
            if base == base_filename:
                possible.append(full_filename)
        if not possible:
            #environ['wsgi.errors'].write(
            #    'No file found matching %r in %s\n'
            #    % (base_filename, self.directory))
            return None
        if len(possible) > 1:
            environ['wsgi.errors'].write(
                'Ambiguous URL: %s; matches files %s\n'
                % (wsgilib.construct_url(environ),
                   ', '.join(possible)))
            return None
        return possible[0]

    def get_application(self, environ, filename):
        constructors = self.option(environ, 'constructors')
        if os.path.isdir(filename):
            t = 'dir'
        else:
            t = os.path.splitext(filename)[1]
        constructor = constructors.get(t, constructors.get('*'))
        if constructor is None:
            #environ['wsgi.errors'].write(
            #    'No constructor found for %s\n' % t)
            return constructor
        app = constructor(environ, filename)
        if app is None:
            #environ['wsgi.errors'].write(
            #    'Constructor %s return None for %s\n' %
            #    (constructor, filename))
            pass
        return app

    def register_constructor(cls, extension, constructor):
        """
        Register a function as a constructor.  Registered constructors
        apply to all instances of `URLParser`.

        The extension should have a leading ``.``, or the special
        extensions ``dir`` (for directories) and ``*`` (a catch-all).

        `constructor` must be a callable that takes two arguments:
        ``environ`` and ``filename``, and returns a WSGI application.
        """
        d = cls.default_options['constructors']
        assert not d.has_key(extension), (
            "A constructor already exists for the extension %r (%r) "
            "when attemption to register constructor %r"
            % (extension, d[extension], constructor))
        d[extension] = constructor
    register_constructor = classmethod(register_constructor)

    def get_parser(cls, directory, base_python_name):
        """
        Get a parser for the given directory, or create one if
        necessary.  This way parsers can be cached and reused.
        """
        try:
            return cls.parsers_by_directory[(directory, base_python_name)]
        except KeyError:
            parser = cls(directory, base_python_name)
            cls.parsers_by_directory[(directory, base_python_name)] = parser
            return parser
    get_parser = classmethod(get_parser)

    def find_init_module(self, environ):
        filename = os.path.join(self.directory, '__init__.py')
        if not os.path.exists(filename):
            return None
        return load_module(environ, filename)

    def __repr__(self):
        return '<%s directory=%r; module=%s at %s>' % (
            self.__class__.__name__,
            self.directory,
            self.base_python_name,
            hex(abs(id(self))))

def make_directory(environ, filename):
    base_python_name = environ['paste.urlparser.base_python_name']
    if base_python_name:
        base_python_name += "." + os.path.basename(filename)
    else:
        base_python_name = os.path.basename(filename)
    return URLParser.get_parser(filename, base_python_name)

URLParser.register_constructor('dir', make_directory)

def make_unknown(environ, filename):
    return wsgilib.send_file(filename)

URLParser.register_constructor('*', make_unknown)

def load_module(environ, filename):
    base_python_name = environ['paste.urlparser.base_python_name']
    module_name = os.path.splitext(os.path.basename(filename))[0]
    if base_python_name:
        module_name = base_python_name + '.' + module_name
    return load_module_from_name(environ, filename, module_name,
                                 environ['wsgi.errors'])

def load_module_from_name(environ, filename, module_name, errors):
    if sys.modules.has_key(module_name):
        return sys.modules[module_name]
    init_filename = os.path.join(os.path.dirname(filename), '__init__.py')
    if not os.path.exists(init_filename):
        try:
            f = open(init_filename, 'w')
        except (OSError, IOError), e:
            errors.write(
                'Cannot write __init__.py file into directory %s (%s)\n'
                % (os.path.dirname(filename), e))
            return None
        f.write('#\n')
        f.close()
    fp = None
    if sys.modules.has_key(module_name):
        return sys.modules[module_name]
    if '.' in module_name:
        parent_name = '.'.join(module_name.split('.')[:-1])
        base_name = module_name.split('.')[-1]
        parent = load_module_from_name(environ, os.path.dirname(filename),
                                       parent_name, errors)
    else:
        base_name = module_name
    fp = None
    try:
        fp, pathname, stuff = imp.find_module(
            base_name, [os.path.dirname(filename)])
        module = imp.load_module(module_name, fp, pathname, stuff)
    finally:
        if fp is not None:
            fp.close()
    return module

def make_py(environ, filename):
    module = load_module(environ, filename)
    if not module:
        return None
    if hasattr(module, 'application') and module.application:
        return module.application
    base_name = module.__name__.split('.')[-1]
    if hasattr(module, base_name):
        return getattr(module, base_name)()
    environ['wsgi.errors'].write(
        "Cound not find application or %s in %s\n"
        % (base_name, module))
    return None
    
URLParser.register_constructor('.py', make_py)