annotate MoinMoin/wsgiapp.py @ 4227:b459b036f263

Changed some setHttpHeader-calls to new header API
author Florian Krupicka <florian.krupicka@googlemail.com>
date Fri, 18 Jul 2008 01:28:31 +0200
parents 54b6f63ae411
children d9b3c6585585
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
4138
6bbc4beed793 Started with the HTTPContext (currently a reimplementation of request_wsgi with werkzeug)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4134
diff changeset
10 from werkzeug.utils import responder
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
11 from werkzeug.wrappers import Response
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
12 from werkzeug.exceptions import NotFound
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
13
4185
68bcf39ae1d3 General cleanups
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4182
diff changeset
14 from MoinMoin.web.contexts import HTTPContext, RenderContext, AllContext
4152
ddcd459c3255 Restructured contexts classes for wrapping contexts in each other
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4151
diff changeset
15 from MoinMoin.web.request import Request
4161
67f0fc696d97 Factored out setuid-code
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4160
diff changeset
16 from MoinMoin.web.utils import check_spider, check_forbidden, check_setuid
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
17 from MoinMoin.web.utils import check_surge_protect
4151
30ac757e72de Added HTTPException catching middleware
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4149
diff changeset
18 from MoinMoin.web.apps import HTTPExceptionsMiddleware
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
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
20 from MoinMoin.Page import Page
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 import config, wikiutil, user, caching, error
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 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
23 from MoinMoin.config import multiconfig
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
24 from MoinMoin.support.python_compatibility import set
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
25 from MoinMoin.util import IsWin9x
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
26 from MoinMoin.request import MoinMoinFinish, RemoteClosedConnection
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
27 from MoinMoin import auth
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
28
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
29 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
30 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
31
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
32 def init(request):
4185
68bcf39ae1d3 General cleanups
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4182
diff changeset
33 request = AllContext(request)
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 request.clock.start('total')
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
35 request.clock.start('base__init__')
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
4192
105c5469ac05 Use session service in the WSGI app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4185
diff changeset
37 request.session = request.cfg.session_service.get_session(request)
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
38
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
39 # auth & user handling
4194
9c80451df643 Setup user from stored session
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
40 # first try setting up from session
9c80451df643 Setup user from stored session
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
41 userobj = auth.setup_from_session(request, request.session)
9c80451df643 Setup user from stored session
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
42
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4197
diff changeset
43 # then handle login/logout forms
4197
4acb272b1759 Fixed: use combined GET/POST dictionary (needed for logout)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4194
diff changeset
44 form = 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
45
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
46 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
47 params = {
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
48 '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
49 '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
50 'attended': True,
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
51 '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
52 '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
53 }
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
54 userobj = auth.handle_login(request, userobj, **params)
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
55 elif 'logout' in form:
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
56 userobj = auth.handle_logout(request, userobj)
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
57 else:
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
58 userobj = auth.handle_request(request, userobj)
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
59
4194
9c80451df643 Setup user from stored session
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
60 # check for setuid-handling of users
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
61 userobj, olduser = check_setuid(request, userobj)
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
62
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
63 if not userobj:
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
64 userobj = user.User(request, auth_method='request:invalid')
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
65
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
66 request.user = userobj
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
67 request._setuid_real_user = olduser
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
68
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
69 # preliminary access control
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
70 # check against spiders, blacklists and request-spam
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
71 check_forbidden(request)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
72 check_surge_protect(request)
4164
0be4fb46e492 Moved even more attributes into (lazy) properties
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4161
diff changeset
73
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
74 request.reset()
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
75
4156
40501b3e2176 Moved some attributes on Request into RenderContext
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4155
diff changeset
76 request.clock.stop('base__init__')
4164
0be4fb46e492 Moved even more attributes into (lazy) properties
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4161
diff changeset
77 return request
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
78
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
79 def run(request):
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4197
diff changeset
80
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
81 _ = request.getText
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
82 request.clock.start('run')
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
83
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
84 action_name = request.action
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
85 if request.cfg.log_timing:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
86 request.timing_log(True, action_name)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
87
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
88 # parse request data
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
89 try:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
90 # The last component in path_info is the page name, if any
4181
eacb4c9e83e5 Reflect changes to Context-model and Request/Response in wsgiapp
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4166
diff changeset
91 path = request.path
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
92
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
93 # we can have all action URLs like this: /action/ActionName/PageName?action=ActionName&...
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
94 # this is just for robots.txt being able to forbid them for crawlers
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
95 prefix = request.cfg.url_prefix_action
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
96 if prefix is not None:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
97 prefix = '/%s/' % prefix # e.g. '/action/'
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
98 if path.startswith(prefix):
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
99 # remove prefix and action name
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
100 path = path[len(prefix):]
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
101 action, path = (path.split('/', 1) + ['', ''])[:2]
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
102 path = '/' + path
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
103
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
104 if path.startswith('/'):
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
105 pagename = wikiutil.normalize_pagename(path, request.cfg)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
106 else:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
107 pagename = None
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
108
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
109 # need to inform caches that content changes based on:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
110 # * cookie (even if we aren't sending one now)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
111 # * User-Agent (because a bot might be denied and get no content)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
112 # * Accept-Language (except if moin is told to ignore browser language)
4227
b459b036f263 Changed some setHttpHeader-calls to new header API
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4225
diff changeset
113 hs = HeaderSet(('Cookie', 'User-Agent'))
b459b036f263 Changed some setHttpHeader-calls to new header API
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4225
diff changeset
114 if not request.cfg.language_ignore_browser:
b459b036f263 Changed some setHttpHeader-calls to new header API
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4225
diff changeset
115 hs.add('Accept-Language')
b459b036f263 Changed some setHttpHeader-calls to new header API
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4225
diff changeset
116 request.headers.add('Vary', str(hs))
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
117
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
118 # Handle request. We have these options:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
119 # 1. jump to page where user left off
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
120 if not pagename and request.user.remember_last_visit and action_name == 'show':
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
121 pagetrail = request.user.getTrail()
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
122 if pagetrail:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
123 # Redirect to last page visited
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
124 last_visited = pagetrail[-1]
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
125 wikiname, pagename = wikiutil.split_interwiki(last_visited)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
126 if wikiname != 'Self':
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
127 wikitag, wikiurl, wikitail, error = wikiutil.resolve_interwiki(request, wikiname, pagename)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
128 url = wikiurl + wikiutil.quoteWikinameURL(wikitail)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
129 else:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
130 url = Page(request, pagename).url(request)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
131 else:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
132 # Or to localized FrontPage
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
133 url = wikiutil.getFrontPage(request).url(request)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
134 return abort(redirect(url))
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
135
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
136 # 2. handle action
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
137 else:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
138 # pagename could be empty after normalization e.g. '///' -> ''
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
139 # Use localized FrontPage if pagename is empty
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
140 if not pagename:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
141 request.page = wikiutil.getFrontPage(request)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
142 else:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
143 request.page = Page(request, pagename)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
144 if '_' in pagename and not request.page.exists():
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
145 pagename = pagename.replace('_', ' ')
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
146 page = Page(request, pagename)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
147 if page.exists():
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
148 url = page.url(request)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
149 return abort(redirect(url))
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
150
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
151 msg = None
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
152 # Complain about unknown actions
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
153 if not action_name in get_names(request.cfg):
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
154 msg = _("Unknown action %(action_name)s.") % {
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
155 'action_name': wikiutil.escape(action_name), }
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
156
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
157 # Disallow non available actions
4216
84cdf829d881 Remove deprecated call to request.getAvailableActions
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4214
diff changeset
158 elif action_name[0].isupper() and not action_name in \
84cdf829d881 Remove deprecated call to request.getAvailableActions
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4214
diff changeset
159 get_available_actions(request.cfg, request.page, request.user):
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
160 msg = _("You are not allowed to do %(action_name)s on this page.") % {
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
161 'action_name': wikiutil.escape(action_name), }
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
162 if not request.user.valid:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
163 # Suggest non valid user to login
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
164 msg += " " + _("Login and try again.")
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
165
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
166 if msg:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
167 request.theme.add_msg(msg, "error")
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
168 request.page.send_page()
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
169 # Try action
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
170 else:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
171 from MoinMoin import action
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
172 handler = action.getHandler(request, action_name)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
173 if handler is None:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
174 msg = _("You are not allowed to do %(action_name)s on this page.") % {
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
175 'action_name': wikiutil.escape(action_name), }
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
176 if not request.user.valid:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
177 # Suggest non valid user to login
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
178 msg += " " + _("Login and try again.")
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
179 request.theme.add_msg(msg, "error")
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
180 request.page.send_page()
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
181 else:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
182 handler(request.page.page_name, request)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
183
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
184 # every action that didn't use to raise MoinMoinFinish must call this now:
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4197
diff changeset
185 # request.theme.send_closing_html()
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
186
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
187 except MoinMoinFinish:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
188 pass
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
189 except RemoteClosedConnection:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
190 # at least clean up
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
191 pass
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
192 except SystemExit:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
193 raise # fcgi uses this to terminate a thread
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
194
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
195 if request.cfg.log_timing:
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
196 request.timing_log(False, action_name)
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
197
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
198 #return request.finish()
4192
105c5469ac05 Use session service in the WSGI app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4185
diff changeset
199 request.cfg.session_service.finalize(request, request.session)
4185
68bcf39ae1d3 General cleanups
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4182
diff changeset
200 return request
4166
500c58d0269b Port RequestBase.run() into a function for use in wsgi-app
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4164
diff changeset
201
4181
eacb4c9e83e5 Reflect changes to Context-model and Request/Response in wsgiapp
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4166
diff changeset
202 def application(request):
4185
68bcf39ae1d3 General cleanups
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4182
diff changeset
203 run(init(request))
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
204
4181
eacb4c9e83e5 Reflect changes to Context-model and Request/Response in wsgiapp
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4166
diff changeset
205 return request
4134
719256a8db51 Initial code to track down the use of request objects in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
206
4181
eacb4c9e83e5 Reflect changes to Context-model and Request/Response in wsgiapp
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4166
diff changeset
207 application = Request.application(application)
4151
30ac757e72de Added HTTPException catching middleware
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4149
diff changeset
208 application = HTTPExceptionsMiddleware(application)