fastcgiHttpTransaction.py :  » Web-Frameworks » Maki » maki-20030512 » spb » 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 » Maki 
Maki » maki 20030512 » spb » fastcgiHttpTransaction.py
# Copyright (c) 2002 Samuel Brauer.  All Rights Reserved.  NO WARRANTY.
# $Revision: 1.5 $

import spb.genericHttpTransaction

import string
import sys
import types
import Cookie

class fastcgiHttpTransaction(spb.genericHttpTransaction.Transaction):

  def __init__(self, raw_transaction):
    self.raw_transaction = raw_transaction
    self.form = self.raw_transaction.getFieldStorage()
    self.env = self.raw_transaction.env
    self.inCookies = Cookie.SimpleCookie()
    if(self.env.has_key("HTTP_COOKIE")):
      http_cookie = self.env["HTTP_COOKIE"]
      self.inCookies.load(http_cookie)
    self.outCookies = None
    self.outHeaders = []
    self.headersSent = 0

  def getRawTransaction(self):
    return self.raw_transaction

  def getBackendName(self):
    return "fastcgi"

  def getRealPath(self):
    return self.getEnvironValue("PATH_TRANSLATED")

  def getEnvironNames(self):
    return self.env.keys()

  def getEnvironValue(self, name, default=None):
    val = ""
    if(self.env.has_key(name)):
      val = self.env[name]
    val = self.normalizeString(val)
    if(not val): val = default
    return val

  def getFormNames(self):
    return self.form.keys()

  def getFormValue(self, name, default=None):
    if(self.form.has_key(name)):
      data = self.form[name]
      if type(data) is type([]):
        if(len(data) > 0):
          val = self.normalizeString(data[0].value)
        else:
          val = None
      elif(hasattr(data, "filename") and data.filename):
        val = self.normalizeString(data.filename)
      else:
        val = self.normalizeString(data.value)
    else:
      val = None
    if(not(val)): val = default
    return val

  def getFormValues(self, name, keepNulls=0):
    """Returns a (possibly empty) list of strings"""
    valList = []
    if(self.form.has_key(name)):
      data = self.form[name]
      if type(data) is type([]):
        for item in data:
          val = self.normalizeString(item.value)
          if(val or keepNulls): valList.append(val)
      elif(hasattr(data, "filename") and data.filename):
        val = self.normalizeString(data.filename)
        if(val or keepNulls): valList.append(val)
      else:
        val = self.normalizeString(data.value)
        if(val or keepNulls): valList.append(val)
    return valList

  def getFilename(self, name):
    if(self.form.has_key(name)):
      if(hasattr(self.form[name], "filename")):
        return self.normalizeString(self.form[name].filename)
    return None

  def getFile(self, name):
    """Returns a file handle, or None"""
    filename = self.getFilename(name)
    if(filename):
      return self.form[name].file
    else:
      return None

  def getCookieNames(self):
    """Return names of cookies."""
    return self.inCookies.keys()

  def getCookieValue(self, name, default=None):
    """Return value of cookie."""
    if(self.inCookies.has_key(name)):
      val = str(self.inCookies[name].value)
    else:
      val = None
    val = self.normalizeString(val)
    if(not val): val = default
    return val
  
  def setCookieValue(self, name, val, path=None, expires=None, domain=None, secure=0):
    """ If specified, "expires" should be a number of seconds.
    """

    if(self.outCookies == None): self.outCookies = Cookie.SimpleCookie()
    if(val == None): val = ""
    self.outCookies[name] = str(val)
    if(path != None): self.outCookies[name]["path"] = path
    if(expires != None): self.outCookies[name]["expires"] = expires
    if(domain != None): self.outCookies[name]["domain"] = domain
    if(secure): self.outCookies[name]["secure"] = 1

  
  def setHeader(self, name, val):
    self.outHeaders.append("%s: %s" % (name, val))
  
  def sendRedirect(self, locationString):
    self.setHeader("Location", locationString)
    self.sendHeaders()


  def sendHeaders(self):
    if(self.headersSent): raise RuntimeError, "Response headers have already been sent!"
    if(self.outCookies != None):
      cookies = string.split(str(self.outCookies), "\n")
      for cookie in cookies:
        # assume each cookie begins with "Set-Cookie: "
        self.setHeader("Set-Cookie", cookie[12:].replace('"', ''))
    for header in self.outHeaders:
      self.raw_transaction.out.write(header)
      self.raw_transaction.out.write("\n")
    self.raw_transaction.out.write("\n")
    self.headersSent = 1
    self.finalizeSession()
  
  def setResponseContentType(self, contentType):
    self.setHeader("Content-type", contentType)

  def writeOutput(self, data):
    write_out = data
    if(isinstance(data, types.UnicodeType)): write_out = data.encode('utf-8')
    self.raw_transaction.out.write(write_out)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.