annotate MoinMoin/wsgiapp.py @ 5829:a744f573fe30

advancedsearch: fix problem with non-ascii chars breaking up words for OR and NOT terms
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 04 Feb 2012 16:52:24 +0100
parents 1dff6cfdcf90
children 9bd355d893f4
rev   line source
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
2 """
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
3 MoinMoin - WSGI application
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
4
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4134
diff changeset
5 @copyright: 2003-2008 MoinMoin:ThomasWaldmann,
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
6 2008-2008 MoinMoin:FlorianKrupicka
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
7 @license: GNU GPL, see COPYING for details.
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
8 """
5393
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
9 import os
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
10
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
11 from MoinMoin import log
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
12 logging = log.getLogger(__name__)
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
13
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
14 from MoinMoin.web.contexts import AllContext, Context, XMLRPCContext
4321
4476d5ca521c Move imports of werkzeug solely into the MoinMoin.web package (to make bundled werkzeug really work)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4309
diff changeset
15 from MoinMoin.web.exceptions import HTTPException
4476d5ca521c Move imports of werkzeug solely into the MoinMoin.web package (to make bundled werkzeug really work)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4309
diff changeset
16 from MoinMoin.web.request import Request, MoinMoinFinish, HeaderSet
4476d5ca521c Move imports of werkzeug solely into the MoinMoin.web package (to make bundled werkzeug really work)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4309
diff changeset
17 from MoinMoin.web.utils import check_forbidden, check_surge_protect, fatal_response, \
4476d5ca521c Move imports of werkzeug solely into the MoinMoin.web package (to make bundled werkzeug really work)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4309
diff changeset
18 redirect_last_visited
4144
0a62a439d81b Trying to port some of RequestBase.__init__ to a WSGI app (very crude right now)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4141
diff changeset
19 from MoinMoin.Page import Page
5393
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
20 from MoinMoin import auth, config, i18n, user, wikiutil, xmlrpc, error
4144
0a62a439d81b Trying to port some of RequestBase.__init__ to a WSGI app (very crude right now)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4141
diff changeset
21 from MoinMoin.action import get_names, get_available_actions
0a62a439d81b Trying to port some of RequestBase.__init__ to a WSGI app (very crude right now)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4141
diff changeset
22
5393
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
23
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
24 def set_umask(new_mask=0777^config.umask):
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
25 """ Set the OS umask value (and ignore potential failures on OSes where
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
26 this is not supported).
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
27 Default: the bitwise inverted value of config.umask
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
28 """
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
29 try:
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
30 old_mask = os.umask(new_mask)
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
31 except:
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
32 # maybe we are on win32?
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
33 pass
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
34
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
35
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
36 def init(request):
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
37 """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
38 Wraps an incoming WSGI request in a Context object and initializes
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
39 several important attributes.
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
40 """
5393
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
41 set_umask() # do it once per request because maybe some server
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
42 # software sets own umask
1472141fa7be added missing config.umask support code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5373
diff changeset
43
4256
c9483e402491 Wrap request only if it's not already a Context-object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4251
diff changeset
44 if isinstance(request, Context):
c9483e402491 Wrap request only if it's not already a Context-object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4251
diff changeset
45 context, request = request, request.request
c9483e402491 Wrap request only if it's not already a Context-object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4251
diff changeset
46 else:
c9483e402491 Wrap request only if it's not already a Context-object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4251
diff changeset
47 context = AllContext(request)
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
48 context.clock.start('total')
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
49 context.clock.start('init')
4144
0a62a439d81b Trying to port some of RequestBase.__init__ to a WSGI app (very crude right now)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4141
diff changeset
50
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
51 context.lang = setup_i18n_preauth(context)
4144
0a62a439d81b Trying to port some of RequestBase.__init__ to a WSGI app (very crude right now)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4141
diff changeset
52
4531
83666cc9dc31 new cfg.session_dir setting, store sessions into cache_dir/__session__ by default
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4530
diff changeset
53 context.session = context.cfg.session_service.get_session(context)
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
54
4530
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4441
diff changeset
55 context.user = setup_user(context, context.session)
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
56
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
57 context.lang = setup_i18n_postauth(context)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
58
4751
09c345615cd8 Put the finish method back on the context object so we can delay process such as thread joined until the end
Andy Dawkins <andydawkins@gmail.com>
parents: 4605
diff changeset
59 def finish():
09c345615cd8 Put the finish method back on the context object so we can delay process such as thread joined until the end
Andy Dawkins <andydawkins@gmail.com>
parents: 4605
diff changeset
60 pass
4753
5db0151a2df3 Done PEP-8 compilance
Cezary Krzy?anowski <cezary.krzyzanowski@gmail.com>
parents: 4751
diff changeset
61
4751
09c345615cd8 Put the finish method back on the context object so we can delay process such as thread joined until the end
Andy Dawkins <andydawkins@gmail.com>
parents: 4605
diff changeset
62 context.finish = finish
4753
5db0151a2df3 Done PEP-8 compilance
Cezary Krzy?anowski <cezary.krzyzanowski@gmail.com>
parents: 4751
diff changeset
63
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
64 context.reset()
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
65
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
66 context.clock.stop('init')
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
67 return context
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
68
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
69 def run(context):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
70 """ Run a context trough the application. """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
71 context.clock.start('run')
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
72 request = context.request
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
73
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
74 # preliminary access checks (forbidden, bots, surge protection)
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
75 try:
4330
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
76 try:
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
77 check_forbidden(context)
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
78 check_surge_protect(context)
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
79
4330
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
80 action_name = context.action
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
81
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
82 # handle XMLRPC calls
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
83 if action_name == 'xmlrpc':
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
84 response = xmlrpc.xmlrpc(XMLRPCContext(request))
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
85 elif action_name == 'xmlrpc2':
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
86 response = xmlrpc.xmlrpc2(XMLRPCContext(request))
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
87 else:
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
88 response = dispatch(request, context, action_name)
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
89 context.cfg.session_service.finalize(context, context.session)
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
90 return response
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
91 except MoinMoinFinish:
b6d5bf04b9e6 try/except/finally is 2.5+ only, use try/try/except/finally
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4321
diff changeset
92 return request
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
93 finally:
4751
09c345615cd8 Put the finish method back on the context object so we can delay process such as thread joined until the end
Andy Dawkins <andydawkins@gmail.com>
parents: 4605
diff changeset
94 context.finish()
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
95 context.clock.stop('run')
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
96
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
97 def remove_prefix(path, prefix=None):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
98 """ Remove an url prefix from the path info and return shortened path. """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
99 # we can have all action URLs like this: /action/ActionName/PageName?action=ActionName&...
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
100 # this is just for robots.txt being able to forbid them for crawlers
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
101 if prefix is not None:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
102 prefix = '/%s/' % prefix # e.g. '/action/'
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
103 if path.startswith(prefix):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
104 # remove prefix and action name
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
105 path = path[len(prefix):]
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
106 action, path = (path.split('/', 1) + ['', ''])[:2]
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
107 path = '/' + path
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
108 return path
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
109
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
110 def dispatch(request, context, action_name='show'):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
111 cfg = context.cfg
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
112
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
113 # The last component in path_info is the page name, if any
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
114 path = remove_prefix(request.path, cfg.url_prefix_action)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
115
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
116 if path.startswith('/'):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
117 pagename = wikiutil.normalize_pagename(path, cfg)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
118 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
119 pagename = None
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
120
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
121 # need to inform caches that content changes based on:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
122 # * cookie (even if we aren't sending one now)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
123 # * User-Agent (because a bot might be denied and get no content)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
124 # * Accept-Language (except if moin is told to ignore browser language)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
125 hs = HeaderSet(('Cookie', 'User-Agent'))
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
126 if not cfg.language_ignore_browser:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
127 hs.add('Accept-Language')
5591
1dff6cfdcf90 http headers: for most cases, do not use .add, but .__setitem__
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5536
diff changeset
128 request.headers['Vary'] = str(hs)
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
129
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
130 # Handle request. We have these options:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
131 # 1. jump to page where user left off
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
132 if not pagename and context.user.remember_last_visit and action_name == 'show':
4321
4476d5ca521c Move imports of werkzeug solely into the MoinMoin.web package (to make bundled werkzeug really work)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4309
diff changeset
133 response = redirect_last_visited(context)
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
134 # 2. handle action
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
135 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
136 response = handle_action(context, pagename, action_name)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
137 if isinstance(response, Context):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
138 response = response.request
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
139 return response
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
140
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
141 def handle_action(context, pagename, action_name='show'):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
142 """ Actual dispatcher function for non-XMLRPC actions.
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
143
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
144 Also sets up the Page object for this request, normalizes and
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
145 redirects to canonical pagenames and checks for non-allowed
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
146 actions.
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
147 """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
148 _ = context.getText
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
149 cfg = context.cfg
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
150
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
151 # pagename could be empty after normalization e.g. '///' -> ''
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
152 # Use localized FrontPage if pagename is empty
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
153 if not pagename:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
154 context.page = wikiutil.getFrontPage(context)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
155 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
156 context.page = Page(context, pagename)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
157 if '_' in pagename and not context.page.exists():
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
158 pagename = pagename.replace('_', ' ')
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
159 page = Page(context, pagename)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
160 if page.exists():
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
161 url = page.url(context)
4321
4476d5ca521c Move imports of werkzeug solely into the MoinMoin.web package (to make bundled werkzeug really work)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4309
diff changeset
162 return context.http_redirect(url)
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
163
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
164 msg = None
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
165 # Complain about unknown actions
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
166 if not action_name in get_names(cfg):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
167 msg = _("Unknown action %(action_name)s.") % {
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
168 'action_name': wikiutil.escape(action_name), }
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
169
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
170 # Disallow non available actions
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
171 elif action_name[0].isupper() and not action_name in \
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
172 get_available_actions(cfg, context.page, context.user):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
173 msg = _("You are not allowed to do %(action_name)s on this page.") % {
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
174 'action_name': wikiutil.escape(action_name), }
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
175 if not context.user.valid:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
176 # Suggest non valid user to login
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
177 msg += " " + _("Login and try again.")
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
178
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
179 if msg:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
180 context.theme.add_msg(msg, "error")
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
181 context.page.send_page()
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
182 # Try action
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
183 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
184 from MoinMoin import action
5168
61ce73eb13a0 action.getHandler(): make it moin 1.8 compatible again
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4860
diff changeset
185 handler = action.getHandler(context, action_name)
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
186 if handler is None:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
187 msg = _("You are not allowed to do %(action_name)s on this page.") % {
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
188 'action_name': wikiutil.escape(action_name), }
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
189 if not context.user.valid:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
190 # Suggest non valid user to login
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
191 msg += " " + _("Login and try again.")
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
192 context.theme.add_msg(msg, "error")
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
193 context.page.send_page()
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
194 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
195 handler(context.page.page_name, context)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
196
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
197 return context
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
198
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
199 def setup_user(context, session):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
200 """ Try to retrieve a valid user object from the request, be it
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
201 either through the session or through a login. """
4194
9c80451df643 Setup user from stored session
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
202 # first try setting up from session
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
203 userobj = auth.setup_from_session(context, session)
4530
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4441
diff changeset
204 userobj, olduser = auth.setup_setuid(context, userobj)
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4441
diff changeset
205 context._setuid_real_user = olduser
4194
9c80451df643 Setup user from stored session
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
206
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4197
diff changeset
207 # then handle login/logout forms
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
208 form = context.request.values
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
209
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
210 if 'login' in form:
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
211 params = {
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
212 'username': form.get('name'),
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
213 'password': form.get('password'),
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
214 'attended': True,
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
215 'openid_identifier': form.get('openid_identifier'),
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
216 'stage': form.get('stage')
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
217 }
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
218 userobj = auth.handle_login(context, userobj, **params)
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
219 elif 'logout' in form:
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
220 userobj = auth.handle_logout(context, userobj)
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
221 else:
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
222 userobj = auth.handle_request(context, userobj)
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
223
4534
0b176807b07c bugfix: create dummy user obj at the right place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4531
diff changeset
224 # if we still have no user obj, create a dummy:
0b176807b07c bugfix: create dummy user obj at the right place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4531
diff changeset
225 if not userobj:
0b176807b07c bugfix: create dummy user obj at the right place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4531
diff changeset
226 userobj = user.User(context, auth_method='invalid')
0b176807b07c bugfix: create dummy user obj at the right place
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4531
diff changeset
227
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
228 return userobj
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
229
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
230 def setup_i18n_preauth(context):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
231 """ Determine language for the request in absence of any user info. """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
232 if i18n.languages is None:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
233 i18n.i18n_init(context)
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
234
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
235 lang = None
5373
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
236 if i18n.languages:
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
237 cfg = context.cfg
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
238 if not cfg.language_ignore_browser:
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
239 for l, w in context.request.accept_languages:
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
240 logging.debug("client accepts language %r, weight %r" % (l, w))
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
241 if l in i18n.languages:
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
242 logging.debug("moin supports language %r" % l)
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
243 lang = l
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
244 break
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
245 else:
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
246 logging.debug("moin does not support any language client accepts")
5536
01dbe5f901e4 make sure to use language_default when language_ignore_browser is set
Radomir Dopieralski <moindev@sheep.art.pl>
parents: 5393
diff changeset
247 if not lang:
01dbe5f901e4 make sure to use language_default when language_ignore_browser is set
Radomir Dopieralski <moindev@sheep.art.pl>
parents: 5393
diff changeset
248 if cfg.language_default in i18n.languages:
01dbe5f901e4 make sure to use language_default when language_ignore_browser is set
Radomir Dopieralski <moindev@sheep.art.pl>
parents: 5393
diff changeset
249 lang = cfg.language_default
01dbe5f901e4 make sure to use language_default when language_ignore_browser is set
Radomir Dopieralski <moindev@sheep.art.pl>
parents: 5393
diff changeset
250 logging.debug("fall back to cfg.language_default (%r)" % lang)
5373
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
251 if not lang:
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
252 lang = 'en'
5373
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
253 logging.debug("emergency fallback to 'en'")
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
254 logging.debug("setup_i18n_preauth returns %r" % lang)
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
255 return lang
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
256
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
257 def setup_i18n_postauth(context):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
258 """ Determine language for the request after user-id is established. """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
259 user = context.user
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
260 if user and user.valid and user.language:
5373
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
261 logging.debug("valid user that has configured some specific language to use in his user profile")
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
262 lang = user.language
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
263 else:
5373
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
264 logging.debug("either no valid user or no specific language configured in user profile, using lang setup by setup_i18n_preauth")
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
265 lang = context.lang
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
266 logging.debug("setup_i18n_postauth returns %r" % lang)
7c9372ef5428 fix browser language detection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5275
diff changeset
267 return lang
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
268
4441
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
269 class Application(object):
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
270 def __init__(self, app_config=None):
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
271
4441
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
272 class AppRequest(Request):
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
273 given_config = app_config
4228
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
274
4441
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
275 self.Request = AppRequest
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
276
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
277 def __call__(self, environ, start_response):
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
278 try:
5275
adb58cd3ece2 main exception handler: include request url in log output
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5168
diff changeset
279 request = None
4441
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
280 request = self.Request(environ)
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
281 context = init(request)
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
282 response = run(context)
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
283 context.clock.stop('total')
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
284 except HTTPException, e:
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
285 response = e
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
286 except error.ConfigurationError, e:
4605
edb6cb2cac43 Environment variable MOIN_DEBUGGER=True will enable werkzeug's debugger for the standalone wikiserver
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4534
diff changeset
287 # this is stuff the user should see on the web interface:
4441
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
288 response = fatal_response(e)
4605
edb6cb2cac43 Environment variable MOIN_DEBUGGER=True will enable werkzeug's debugger for the standalone wikiserver
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4534
diff changeset
289 except Exception, e:
5275
adb58cd3ece2 main exception handler: include request url in log output
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5168
diff changeset
290 # we avoid raising more exceptions here to preserve the original exception
adb58cd3ece2 main exception handler: include request url in log output
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5168
diff changeset
291 url_info = request and ' [%s]' % request.url or ''
4605
edb6cb2cac43 Environment variable MOIN_DEBUGGER=True will enable werkzeug's debugger for the standalone wikiserver
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4534
diff changeset
292 # have exceptions logged within the moin logging framework:
5275
adb58cd3ece2 main exception handler: include request url in log output
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5168
diff changeset
293 logging.exception("An exception has occurred%s." % url_info)
4605
edb6cb2cac43 Environment variable MOIN_DEBUGGER=True will enable werkzeug's debugger for the standalone wikiserver
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4534
diff changeset
294 # re-raise exception, so e.g. the debugger middleware gets it
edb6cb2cac43 Environment variable MOIN_DEBUGGER=True will enable werkzeug's debugger for the standalone wikiserver
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4534
diff changeset
295 raise
4441
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
296
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
297 return response(environ, start_response)
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
298
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
299 #XXX: default application using the default config from disk
f02b3e7dffe6 fixed wsgiapplication tests
Ronny Pfannschmidt <Ronny.Pfannschmidt@gmx.de>
parents: 4330
diff changeset
300 application = Application()