"""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"]
|