annotate MoinMoin/wsgiapp.py @ 4251:3c6e893424f6

Fixed: log format and line-doubling in standalone server
author Florian Krupicka <florian.krupicka@googlemail.com>
date Sun, 27 Jul 2008 17:40:11 +0200
parents 43da090ab838
children c9483e402491
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 """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
28 context = AllContext(request)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
29 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
30 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
31
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
32 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
33
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
34 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
35
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
36 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
37 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
38
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
39 if not userobj:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
40 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
41
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
42 context.user = userobj
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
43 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
44
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
45 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
46
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
47 context.reset()
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
48
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
49 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
50 return context
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 def run(context):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
53 """ 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
54 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
55 request = context.request
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
56
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
57 # 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
58 try:
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
59 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
60 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
61
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
62 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
63
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
64 # 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
65 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
66 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
67 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
68 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
69 else:
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
70 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
71 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
72 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
73 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
74 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
75 finally:
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
76 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
77
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
78 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
79 """ 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
80 # 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
81 # 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
82 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
83 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
84 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
85 # 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
86 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
87 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
88 path = '/' + path
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
89 return path
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
90
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
91 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
92 cfg = context.cfg
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 # 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
95 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
96
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
97 if path.startswith('/'):
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
98 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
99 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
100 pagename = None
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
101
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
102 # 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
103 # * 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
104 # * 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
105 # * 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
106 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
107 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
108 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
109 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
110
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
111 # 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
112 # 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
113 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
114 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
115 # 2. handle action
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
116 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
117 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
118 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
119 response = response.request
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
120 return response
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
121
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
122 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
123 """ 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
124
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
125 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
126 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
127 actions.
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
128 """
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
129 _ = context.getText
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
130 cfg = context.cfg
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 # 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
133 # 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
134 if not pagename:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
135 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
136 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
137 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
138 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
139 pagename = pagename.replace('_', ' ')
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
140 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 page.exists():
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
142 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
143 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
144
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
145 msg = None
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
146 # 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
147 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
148 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
149 '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
150
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
151 # 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
152 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
153 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
154 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
155 '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
156 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
157 # 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
158 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
159
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
160 if msg:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
161 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
162 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
163 # Try action
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
164 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
165 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
166 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
167 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
168 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
169 '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
170 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
171 # 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
172 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
173 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
174 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
175 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
176 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
177
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
178 return context
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
179
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
180 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
181 """ 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
182 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
183 # 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
184 userobj = auth.setup_from_session(context, session)
4194
9c80451df643 Setup user from stored session
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
185
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4197
diff changeset
186 # 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
187 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
188
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
189 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
190 params = {
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
191 '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
192 '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
193 'attended': True,
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4192
diff changeset
194 '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
195 '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
196 }
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
197 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
198 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
199 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
200 else:
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
201 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
202
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
203 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
204
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
205 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
206 """ 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
207 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
208 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
209
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
210 cfg = context.cfg
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
211 lang = None
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
212 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
213 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
214 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
215 lang = l
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
216 break
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
217 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
218 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
219 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
220 lang = 'en'
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
221 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
222
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
223 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
224 """ 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
225 user = context.user
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
226 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
227 return user.language
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
228 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
229 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
230
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
231 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
232 """ 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
233 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
234 if pagetrail:
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 page visited
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
236 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
237 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
238 if wikiname != 'Self':
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
239 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
240 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
241 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
242 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
243 else:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
244 # 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
245 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
246 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
247
4239
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
248 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
249 try:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
250 request = Request(environ)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
251 context = init(request)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
252 response = run(context)
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
253 except HTTPException, e:
db15f98e3228 Split up wsgiapp.init and wsgiapp.run into smaller chunks
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4228
diff changeset
254 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
255
4250
43da090ab838 Fixed exception-handling for MoinMoinFinish and relocated it to MoinMoin.web.request
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4248
diff changeset
256 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
257 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
258
4245
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
259 class ProxyTrust(object):
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
260 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
261 self.app = app
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
262 self.proxies = proxies
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
263
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
264 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
265 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
266 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
267 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
268 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
269 addrs = [environ['REMOTE_ADDR']]
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
270 else:
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
271 addrs = [None]
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
272 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
273 if result:
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
274 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
275 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
276 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
277 else:
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
278 del environ['REMOTE_ADDR']
2b21ec54a5c9 Middleware to fix up REMOTE_ADDR for trusted proxies
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4239
diff changeset
279 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
280
4228
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
281 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
282 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
283 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
284 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
285 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
286 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
287
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
288 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
289 '/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
290 '/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
291
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
292 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
293
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
294 params = {}
4248
f35fdfdc1995 Fix missing attributes for standalone server config, remove reloader (crashes currently)
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4245
diff changeset
295 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
296 params['threaded'] = True
4251
3c6e893424f6 Fixed: log format and line-doubling in standalone server
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4250
diff changeset
297 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
298
d9b3c6585585 First implementation of standalone server using werkzeug.serving.run_simple
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4227
diff changeset
299 run_simple(config.interface, config.port, app, **params)