annotate MoinMoin/wsgiapp.py @ 4256:c9483e402491

Wrap request only if it's not already a Context-object
author Florian Krupicka <florian.krupicka@googlemail.com>
date Thu, 31 Jul 2008 15:59:57 +0200
parents 3c6e893424f6
children e1a63fb9bb4d
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 """
4227
b459b036f263 Changed some setHttpHeader-calls to new header API
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4225
diff changeset
9 from werkzeug.http import HeaderSet
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
10 from werkzeug.exceptions import HTTPException
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
11
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
12 from MoinMoin.web.contexts import AllContext, Context, XMLRPCContext
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
13 from MoinMoin.web.request import Request, MoinMoinFinish
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.utils import check_forbidden, check_setuid, check_surge_protect
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
15
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
16 from MoinMoin.Page import Page
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
17 from MoinMoin import auth, i18n, user, wikiutil, xmlrpc
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
18 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
19
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
20 from MoinMoin import log
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
21 logging = log.getLogger(__name__)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
22
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
23 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
24 """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
25 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
26 several important attributes.
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
27 """
4256
c9483e402491 Wrap request only if it's not already a Context-object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4251
diff changeset
28 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
29 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
30 else:
c9483e402491 Wrap request only if it's not already a Context-object
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4251
diff changeset
31 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
32 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
33 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
34
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
35 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
36
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
37 context.session = context.cfg.session_service.get_session(request)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
38
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
39 userobj = setup_user(context, context.session)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
40 userobj, olduser = check_setuid(context, userobj)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
41
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
42 if not userobj:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
43 userobj = user.User(context, auth_method='request:invalid')
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
44
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
45 context.user = userobj
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
46 context._setuid_realuser = olduser
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
47
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
48 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
49
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
50 context.reset()
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
51
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
52 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
53 return context
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
54
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
55 def run(context):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
56 """ 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
57 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
58 request = context.request
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
59
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
60 # 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
61 try:
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
62 check_forbidden(context)
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
63 check_surge_protect(context)
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
64
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
65 action_name = context.action
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
66
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
67 # handle XMLRPC calls
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
68 if action_name == 'xmlrpc':
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
69 response = xmlrpc.xmlrpc(XMLRPCContext(request))
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
70 elif action_name == 'xmlrpc2':
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
71 response = xmlrpc.xmlrpc2(XMLRPCContext(request))
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
72 else:
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
73 response = dispatch(request, context, action_name)
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
74 context.cfg.session_service.finalize(context, context.session)
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
75 return response
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
76 except MoinMoinFinish:
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
77 return request
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
78 finally:
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
79 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
80
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
81 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
82 """ 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
83 # 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
84 # 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
85 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
86 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
87 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
88 # 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
89 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
90 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
91 path = '/' + path
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
92 return path
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
93
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
94 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
95 cfg = context.cfg
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 # 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
98 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
99
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
100 if path.startswith('/'):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
101 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
102 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
103 pagename = None
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
104
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
105 # 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
106 # * 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
107 # * 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
108 # * 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
109 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
110 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
111 hs.add('Accept-Language')
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
112 request.headers.add('Vary', str(hs))
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
113
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
114 # 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
115 # 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
116 if not pagename and context.user.remember_last_visit and action_name == 'show':
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
117 response = handle_last_visit(context)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
118 # 2. handle action
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
119 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
120 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
121 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
122 response = response.request
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
123 return response
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
124
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
125 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
126 """ 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
127
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
128 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
129 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
130 actions.
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
131 """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
132 _ = context.getText
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
133 cfg = context.cfg
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
134
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
135 # 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
136 # 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
137 if not pagename:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
138 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
139 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
140 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
141 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
142 pagename = pagename.replace('_', ' ')
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
143 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
144 if page.exists():
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
145 url = page.url(context)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
146 return abort(redirect(url))
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 msg = None
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
149 # 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
150 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
151 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
152 '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
153
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
154 # 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
155 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
156 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
157 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
158 '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
159 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
160 # 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
161 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
162
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
163 if msg:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
164 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
165 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
166 # Try action
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
167 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
168 from MoinMoin import action
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
169 handler = action.getHandler(cfg, action_name)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
170 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
171 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
172 '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
173 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
174 # 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
175 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
176 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
177 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
178 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
179 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
180
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
181 return context
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
182
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
183 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
184 """ 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
185 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
186 # 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
187 userobj = auth.setup_from_session(context, session)
4194
9c80451df643 Setup user from stored session
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
188
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4197
diff changeset
189 # 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
190 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
191
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
192 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
193 params = {
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
194 '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
195 '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
196 'attended': True,
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
197 '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
198 '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
199 }
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
200 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
201 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
202 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
203 else:
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
204 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
205
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
206 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
207
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
208 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
209 """ 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
210 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
211 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
212
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
213 cfg = context.cfg
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
214 lang = None
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
215 if i18n.languages and 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
216 for l in context.request.accept_languages:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
217 if l in i18n.languages:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
218 lang = l
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
219 break
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
220 if lang is None and cfg.language_default in i18n.languages:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
221 lang = cfg.language_default
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
222 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
223 lang = 'en'
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
224 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
225
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
226 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
227 """ 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
228 user = context.user
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
229 if user and user.valid and user.language:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
230 return user.language
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
231 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
232 return context.lang
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
233
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
234 def handle_last_visit(request, context):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
235 """ Redirect to last visited page (or frontpage) on missing pagename. """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
236 pagetrail = context.user.getTrail()
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
237 if pagetrail:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
238 # Redirect to last page visited
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
239 last_visited = pagetrail[-1]
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
240 wikiname, pagename = wikiutil.split_interwiki(last_visited)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
241 if wikiname != 'Self':
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
242 wikitag, wikiurl, wikitail, error = wikiutil.resolve_interwiki(context, wikiname, pagename)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
243 url = wikiurl + wikiutil.quoteWikinameURL(wikitail)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
244 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
245 url = Page(context, pagename).url(context)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
246 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
247 # Or to localized FrontPage
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
248 url = wikiutil.getFrontPage(context).url(context)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
249 return abort(redirect(url))
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
250
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
251 def application(environ, start_response):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
252 try:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
253 request = Request(environ)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
254 context = init(request)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
255 response = run(context)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
256 except HTTPException, e:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
257 response = e
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
258
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
259 context.clock.stop('total')
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
260 return response(environ, start_response)
4228
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
261
4245
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
262 class ProxyTrust(object):
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
263 def __init__(self, app, proxies):
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
264 self.app = app
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
265 self.proxies = proxies
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
266
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
267 def __call__(environ, start_response):
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
268 if 'HTTP_X_FORWARDED_FOR' in environ:
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
269 addrs = environ.pop('HTTP_X_FORWARDED_FOR').split(',')
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
270 addrs = [x.strip() for addr in addrs]
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
271 elif 'REMOTE_ADDR' in environ:
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
272 addrs = [environ['REMOTE_ADDR']]
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
273 else:
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
274 addrs = [None]
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
275 result = [addr for addr in addrs if addr not in self.proxies]
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
276 if result:
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
277 environ['REMOTE_ADDR'] = result[-1]
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
278 elif addrs[-1] is not None:
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
279 environ['REMOTE_ADDR'] = addrs[-1]
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
280 else:
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
281 del environ['REMOTE_ADDR']
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
282 return self.app(environ, start_response)
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
283
4228
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
284 def run_server(config):
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
285 from os import path
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
286 from MoinMoin.config import url_prefix_static
4251
3c6e893424f6 Fixed: log format and line-doubling in standalone server
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
287 from MoinMoin.web.serving import RequestHandler
4228
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
288 from werkzeug.serving import run_simple
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
289 from werkzeug.utils import SharedDataMiddleware
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
290
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
291 shared = {url_prefix_static: config.docs,
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
292 '/favicon.ico': path.join(config.docs, 'favicon.ico'),
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
293 '/robots.txt': path.join(config.docs, 'robots.txt')}
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
294
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
295 app = SharedDataMiddleware(application, shared)
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
296
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
297 params = {}
4248
f35fdfdc1995 Fix missing attributes for standalone server config, remove reloader (crashes currently)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4245
diff changeset
298 params['use_debugger'] = config.debug
4228
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
299 params['threaded'] = True
4251
3c6e893424f6 Fixed: log format and line-doubling in standalone server
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
300 params['request_handler'] = RequestHandler
4228
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
301
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
302 run_simple(config.interface, config.port, app, **params)