# Alexander Sherwin
# 08/14/2005 v1.2
#
# This class is designed to look for and parse miscellaneous data
# that is not handled internally through Python (i.e. POST, GET.)
#
# Currently, upon instantiation of the class it automatically parses
# all POST, GET and SERVER data available to the script.
#
# Report any errors, bugs or contributions & additions
# to alex.sherwin@gmail.com. This class has been developed and tested on
# nearlyfreespeech.net servers (FreeBSD, Apache 2.x.)
import sys
import os
class HTTPWrap:
"""Takes the sys.stdin sent from the webserver and parses it for POST variables
This class may not work if your webserver sends other types of information to
serverside scripts as sys.stdin
.process_post() - Initiates the parsing of raw data in std.in
.process_get() - Initiates the parsing of ENV var QUERY_STRING
.server(str) - Checks SERVER vars in server_dict and returns the value
- if the key exists, False if it doesn't
.post_keys - List of POST keys (form names)
.post_vals - List of POST values (form values)
.post_dict - Dict of POST keys and values
.get_keys - List of GET keys
.get_vals - List of GET values
.get_dict - Dict of GET keys and values
.server_keys - List of SERVER keys
.server_vals - List of SERVER values
.server_dict - Dict of SERVER keys and values
.has_post - True if script has received POST data
.has_get - True if script has received GET data
.server_dict - Dict of SERVER keys and values"""
post_keys = [] # init all lists & dictionaries
post_vals = []
post_dict = {}
get_keys = []
get_vals = []
get_dict = {}
server_keys = []
server_vals = []
server_dict = {}
has_post = False # Instantiates with no POST vars
has_get = False # Instantiates with no GET vars
def __init__(self):
"""Initialize by determining if there is any POST or GET vars"""
self.__raw = sys.stdin.readline() # read from sys.stdin
if len(self.__raw):
self.process_post() # if POST string length > 0, parse it
self.__raw = "" # init empty string
for k in os.environ.items():
if "QUERY_STRING" in k: # find GET data
self.__raw = k[1] # save GET data
else:
self.server_dict[k[0]] = k[1] # save SERVER vars
self.server_keys.append(k[0])
self.server_vals.append(k[1])
if len(self.__raw):
self.process_get() # if GET string length > 0, parse it
def server(self, serverVar):
"""Check server_dict for a server variable, return it's value"""
if self.server_dict.has_key(serverVar):
return self.server_dict[serverVar] # return value
else: return False
def process_post(self):
"""Read sys.stdin and parse it to obtain orderly POST data"""
self.has_post = True # set boolean for exterior uses
rawlist = self.__raw.split('&') # obtain list of key=val strings
for k in rawlist:
tmpli = k.split('=') # get key, val list
self.post_keys.append(tmpli[0]) # save key
self.post_vals.append(tmpli[1]) # save value
self.post_dict[tmpli[0]] = tmpli[1] # save pair in dict
def process_get(self):
"""Parse self.__raw to obtain orderly GET data"""
self.has_get = True # set boolean for exterior uses
rawlist = self.__raw.split('&') # obtain list of key=val strings
for k in rawlist:
tmpli = k.split('=') # get key, val list
self.get_keys.append(tmpli[0]) # save key
self.get_vals.append(tmpli[1]) # save value
self.get_dict[tmpli[0]] = tmpli[1] # save pair in dict