QueryParameters.py :  » Web-Frameworks » Aquarium » aquarium-2.3 » aquarium » urlscheme » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Aquarium 
Aquarium » aquarium 2.3 » aquarium » urlscheme » QueryParameters.py
"""This URL scheme is based on query parameters."""

__docformat__ = "restructuredtext"

# Created: Wed Feb 25 10:31:51 PST 2004
# Author: Shannon -jj Behrens
# Email: jjinux@users.sourceforge.net
#
# Copyright (c) Shannon -jj Behrens.  All rights reserved.

import urllib

from aquarium.parse.Host import parseHost
from aquarium.util.AquariumClass import AquariumClass
import aquarium.util.Ports as ports
import aquarium.conf.AquariumProperties as properties


class QueryParameters(AquariumClass):

    """This URL scheme is based on query parameters.

    I.e. the name of the screen is set via a GET or POST variable named
    ``screen``.  Furthermore, there is only entry point to the site usually
    named ``index.py`` which is in the root directory of the site.  This is
    appropriate for CGI situations, etc.

    See aquarium.urlscheme.UrlSchemeAPI_.

    .. _aquarium.urlscheme.UrlSchemeAPI:
      aquarium.urlscheme.UrlSchemeAPI-module.html

    """

    def __init__(self, ctx):
        """Extend ``__init__`` to handle proxying.

        If present, use the ``X_FORWARDED_HOST`` header (i.e.
        ``cgiEnv["HTTP_X_FORWARDED_HOST"]) in order to override
        ``cgiEnv["SERVER_NAME"]`` and ``cgiEnv["SERVER_PORT"]``.

        """
        AquariumClass.__init__(self, ctx)
        cgiEnv = self._ctx.wsa.getCgiEnv()
        defaultReturnValue = (cgiEnv["SERVER_NAME"], cgiEnv["SERVER_PORT"])
        isSecure = cgiEnv.get("HTTPS", "off").lower() == "on"
        (cgiEnv["SERVER_NAME"], cgiEnv["SERVER_PORT"]) = parseHost(
            header=cgiEnv.get("HTTP_X_FORWARDED_HOST"),
            defaultReturnValue=defaultReturnValue, isSecure=isSecure)

    def getRootUrl(self, secure=-1):
        """Figure out the URL for the root of this site."""
        cgiEnv = self._ctx.wsa.getCgiEnv()
        url = scheme = self.getScheme(secure)
        url += cgiEnv["SERVER_NAME"]
        url += self.getPort(scheme)
        scriptName = cgiEnv['SCRIPT_NAME']
        if scriptName == "":
            url += "/"
        else:
            url += scriptName[:scriptName.rindex("/") + 1]
        return url

    def static(self, file, secure=-1):
        """Return an URL for something relative to the root URL."""
        return self.getRootUrl(secure) + file

    def img(self, img, secure=-1):
        """Return an URL to an image."""
        return self.static("images/" + img, secure)

    def screen(self, screen, vars=None, secure=-1):
        """Return a dynamic URL for the given screen."""
        if vars is None:
            vars = {}
        ctx = self._ctx
        cgiEnv = ctx.wsa.getCgiEnv()
        scriptName = cgiEnv["SCRIPT_NAME"]
        scriptFile = scriptName[scriptName.rindex("/") + 1:]
        url = self.getRootUrl(secure) + scriptFile
        vars["screen"] = screen
        return self.addVars(url, vars)

    def hiddenFormFields(self, screen, vars=None):
        """Return necessary hidden form fields."""
        if vars is None:
            vars = {}
        ctx = self._ctx
        vars["screen"] = screen
        self.addSid(vars)
        return ctx.iLib.call("widget.HiddenFormFields", vars)

    def whichScreen(self):
        """Which screen does the user want to view?"""
        return self._ctx.form.get("screen")

    def getDefaultScheme(self):
        """Return the default scheme, either ``http://`` or ``https://``.

        The default scheme is the scheme currently in use.

        """
        if not hasattr(self, "_defaultScheme"):
            https = self._ctx.wsa.getCgiEnv().get("HTTPS", "off").lower()
            if https == "on":
                self._defaultScheme = "https://"
            else:
                self._defaultScheme = "http://"
        return self._defaultScheme

    def getScheme(self, secure=-1):
        """Return ``http://`` or ``https://``.

        Interpret secure like aquarium.urlscheme.UrlSchemeAPI.getRootUrl_.

        .. _aquarium.urlscheme.UrlSchemeAPI.getRootUrl:
           aquarium.urlscheme.UrlSchemeAPI-module.html#getRootUrl

        """
        if secure == -1:
            return self.getDefaultScheme()
        if secure == 0:
            return "http://"
        return "https://"

    def getPort(self, scheme):
        """Given a scheme, return the port string, as appropriate.

        Naturally, the string is not necessary for HTTP or HTTPS when they're
        on the canonical ports.

        If scheme given is the same as the current scheme, make use of
        ``cgiEnv["SERVER_PORT"]``.  If it isn't, then I don't know what port to
        use.  If ``properties.HTTP_PORT`` or ``properties.HTTPS_PORT`` exists,
        I'll use those as appropriate.  Otherwise, I'll have to fall back to
        their canonical values.

        """
        cgiEnv = self._ctx.wsa.getCgiEnv()
        if scheme == self.getDefaultScheme():
            port = cgiEnv["SERVER_PORT"]
        elif scheme == "http://":
            port = getattr(properties, "HTTP_PORT", ports.HTTP_PORT)
        else:
            assert scheme == "https://"
            port = getattr(properties, "HTTPS_PORT", ports.HTTPS_PORT)
        port = int(port)
        if ((scheme == "http://" and port == ports.HTTP_PORT) or
            (scheme == "https://" and port == ports.HTTPS_PORT)):
            return ""
        return ":%s" % port

    def addVars(self, url, vars):
        """Add the given vars to the url and return it.

        This will also take care of calling ``addSid``.

        """
        self.addSid(vars)
        if len(vars) > 0:
            url += "?" + urllib.urlencode(vars, doseq=True)
        return url

    def addSid(self, vars):
        """Add the sid to a given dictionary of ``vars``, if appropriate."""
        ctx = self._ctx
        if (properties.USE_SESSIONS
            and (not properties.USE_COOKIES or
                 not properties.USE_SESSION_COOKIES or
                 not ctx.cookiesVerified)
            and not properties.FORCE_SESSION_COOKIES):
            vars["sid"] = ctx.session["sid"]
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.