view MoinMoin/ @ 4185:68bcf39ae1d3

General cleanups
author Florian Krupicka <>
date Sat, 21 Jun 2008 20:02:17 +0200
parents ca0cf44dab89
children 105c5469ac05
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
    MoinMoin - WSGI application

    @copyright: 2003-2008 MoinMoin:ThomasWaldmann,
                2008-2008 MoinMoin:FlorianKrupicka
    @license: GNU GPL, see COPYING for details.
from werkzeug.utils import responder
from werkzeug.wrappers import Response
from werkzeug.exceptions import NotFound

from MoinMoin.web.contexts import HTTPContext, RenderContext, AllContext
from MoinMoin.web.request import Request
from MoinMoin.web.utils import check_spider, check_forbidden, check_setuid
from MoinMoin.web.utils import check_surge_protect, handle_auth_form
from MoinMoin.web.apps import HTTPExceptionsMiddleware

from MoinMoin.Page import Page
from MoinMoin import config, wikiutil, user, caching, error
from MoinMoin.action import get_names, get_available_actions
from MoinMoin.config import multiconfig
from import set
from MoinMoin.util import IsWin9x
from MoinMoin.request import MoinMoinFinish, RemoteClosedConnection
from MoinMoin import auth

from MoinMoin import log
logging = log.getLogger(__name__)

def init(request):
    request = AllContext(request)

    user_obj = request.cfg.session_handler.start(request, request.cfg.session_id_handler)
    request.user = handle_auth_form(user_obj, request)

    request.cfg.session_handler.after_auth(request, request.cfg.session_id_handler, request.user)

    if not request.user:
        request.user = user.User(request, auth_method='request:invalid')



    return request

def run(request):
    _ = request.getText


    action_name = request.action
    if request.cfg.log_timing:
        request.timing_log(True, action_name)

    # parse request data
        # The last component in path_info is the page name, if any
        path = request.path

        # we can have all action URLs like this: /action/ActionName/PageName?action=ActionName&...
        # this is just for robots.txt being able to forbid them for crawlers
        prefix = request.cfg.url_prefix_action
        if prefix is not None:
            prefix = '/%s/' % prefix # e.g. '/action/'
            if path.startswith(prefix):
                # remove prefix and action name
                path = path[len(prefix):]
                action, path = (path.split('/', 1) + ['', ''])[:2]
                path = '/' + path

        if path.startswith('/'):
            pagename = wikiutil.normalize_pagename(path, request.cfg)
            pagename = None

        # need to inform caches that content changes based on:
        # * cookie (even if we aren't sending one now)
        # * User-Agent (because a bot might be denied and get no content)
        # * Accept-Language (except if moin is told to ignore browser language)
        if request.cfg.language_ignore_browser:
            request.setHttpHeader("Vary: Cookie,User-Agent")
            request.setHttpHeader("Vary: Cookie,User-Agent,Accept-Language")

        # Handle request. We have these options:
        # 1. jump to page where user left off
        if not pagename and request.user.remember_last_visit and action_name == 'show':
            pagetrail = request.user.getTrail()
            if pagetrail:
                # Redirect to last page visited
                last_visited = pagetrail[-1]
                wikiname, pagename = wikiutil.split_interwiki(last_visited)
                if wikiname != 'Self':
                    wikitag, wikiurl, wikitail, error = wikiutil.resolve_interwiki(request, wikiname, pagename)
                    url = wikiurl + wikiutil.quoteWikinameURL(wikitail)
                    url = Page(request, pagename).url(request)
                # Or to localized FrontPage
                url = wikiutil.getFrontPage(request).url(request)
            return abort(redirect(url))

        # 2. handle action
            # pagename could be empty after normalization e.g. '///' -> ''
            # Use localized FrontPage if pagename is empty
            if not pagename:
       = wikiutil.getFrontPage(request)
       = Page(request, pagename)
                if '_' in pagename and not
                    pagename = pagename.replace('_', ' ')
                    page = Page(request, pagename)
                    if page.exists():
                        url = page.url(request)
                        return abort(redirect(url))

            msg = None
            # Complain about unknown actions
            if not action_name in get_names(request.cfg):
                msg = _("Unknown action %(action_name)s.") % {
                        'action_name': wikiutil.escape(action_name), }

            # Disallow non available actions
            elif action_name[0].isupper() and not action_name in request.getAvailableActions(
                msg = _("You are not allowed to do %(action_name)s on this page.") % {
                        'action_name': wikiutil.escape(action_name), }
                if not request.user.valid:
                    # Suggest non valid user to login
                    msg += " " + _("Login and try again.")

            if msg:
                request.theme.add_msg(msg, "error")
            # Try action
                from MoinMoin import action
                handler = action.getHandler(request, action_name)
                if handler is None:
                    msg = _("You are not allowed to do %(action_name)s on this page.") % {
                            'action_name': wikiutil.escape(action_name), }
                    if not request.user.valid:
                        # Suggest non valid user to login
                        msg += " " + _("Login and try again.")
                    request.theme.add_msg(msg, "error")
                    handler(, request)

        # every action that didn't use to raise MoinMoinFinish must call this now:
        # request.theme.send_closing_html()    

    except MoinMoinFinish:
    except RemoteClosedConnection:
        # at least clean up
    except SystemExit:
        raise # fcgi uses this to terminate a thread

    if request.cfg.log_timing:
        request.timing_log(False, action_name)

        #return request.finish()
    return request

def application(request):

    if getattr(request, '_send_file', None) is not None:
        # moin wants to send a file (e.g. AttachFile.do_get)
        def simple_wrapper(fileobj, bufsize):
            return iter(lambda:, '')
        file_wrapper = request.environ.get('wsgi.file_wrapper', simple_wrapper)
        request.response = file_wrapper(request._send_file, request._send_bufsize)
    return request

application = Request.application(application)
application = HTTPExceptionsMiddleware(application)