#!/usr/bin/env python3
#=====================================================================================
# Generic responder for any html form 
#=====================================================================================
import os
from urllib import parse
import HTTPparam


print("Content-Type: text/html \n")          #required cgi header

print('''                                                
    <html>
    <head>
    <title>Sample Python Forms-responder</title>
    </head>
    <body>
    <h2>Your Cookies and Input Fields Are...</h2>
    <p>    
    <table border=2 bgcolor=white>   
''')

cookiesDict = {}  

#====================================================================================
# getCookies(): Get all cookies  
#      returns: all cookies as a dictionary
#====================================================================================
def getCookies():
    global cookiesDict                             
    cookieStr = os.environ.get('HTTP_COOKIE')     #obtain the HTTP cookies
    if cookieStr is None: 
        return 

    cookiesList = cookieStr.split('; ')           #split on semi-colon space
    for cookie in cookiesList:
        (name, value) = cookie.split('=',1)       #split on first =
        value2 = parse.unquote(value)             #cookie value (decoded)
        cookiesDict[name] = value2                #build the cookies dictionary
    return  

#====================================================================================
# Generic loop to access all form elements
#====================================================================================
def printCookies():                    #define a function

    print('<tr bgcolor=tan><th>Cookie Name</th><th>Cookie Value</th>')
    if not cookiesDict:
         print('<tr><th>   <td>  ') 
    for name in cookiesDict:    
        print('<tr><th>', name, '<td>', cookiesDict[name] ) 

#====================================================================================
# Generic loop to access all form elements
#====================================================================================
def printElements():                    #define a function

    print('<tr bgcolor=tan><th>Field Name</th><th>Field Value</th>')

    elements = HTTPparam.getAll()       #obtain all http parameter names/values
    if not elements:
         print('<tr><th>   <td>  ') 
    for (name,value) in elements.items():    
        print('<tr><th>', name, '<td>', value, end='') 
 
#====================================================================================
# main code
#====================================================================================
cookies = getCookies()
printCookies()
printElements()                         #call the function

print('</table>')
print('</body>')
print('</html>')

    

#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/staff/sultan/public_html/cgi-bin/python')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================