#!/usr/bin/env python3
#################################################################################################
# Obtain HTTP Request
# Get all request headers
# If request headers are cutom headers (starting with SESSION_), add them to response headers 
#################################################################################################
import os,cgi, cgitb                                #import os and cgi modules

cgitb.enable() 

elements = cgi.FieldStorage()                       #obtain the http for elements
env      = elements.getvalue('env') or 'no'         #entry fields             

#Get REQUEST HEADERS...................................
reqHeaders={}
for name in os.environ:                             #get all environment variables
    if name.startswith('HTTP_'):                    #if they start with HTTP_, they are request headers
         reqHeaders[name] = os.environ[name]        #build request headers dictionary 


#Get CUSTOM REQUEST HEADERS............................
custHeaders={}
for name in reqHeaders:                             #get all headers
    if name.startswith('HTTP_CUSTOM'):              #if they start with HTTP_CUSTOM, they are my custom headers
        custHeaders[name] = reqHeaders[name]        #build a custom headers dictionary 


#--Put CUSTOM REQ.HEADERS into RESPONSE HEADERS.........
#--This allows for full communication between client and server

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

for name in custHeaders:                            #get all custom headers
    name2 = name[5:]                                #strip off the leading HTTP_
    print(name2 +":", custHeaders[name])            #add those to response headers 

cookName  = 'fullname'
cookValue = 'Sam Sultan'
print("Set-Cookie:", cookName +"="+ cookValue)      #add a cookie to response headers 

print("\n")                                         #end the headers


#--PRINT THE OUTPUT.........

print("<br>")
print("<table border=1 width=350>")
print("<tr bgcolor=tan><td colspan=2><b>Request Method Received by the Server")
print("<tr><td width=230><b>", "REQUEST_METHOD", "<td>", os.environ['REQUEST_METHOD'])
print("</table>")        

print("<br>")
print("<table border=1 width=600>")
print("<tr bgcolor=tan><td colspan=2><b>Request Headers Received by the Server")
for name in reqHeaders:
    print("<tr><td width=230><b>", name, "<td>", reqHeaders[name])
print("</table>")        

print("<br>")
print("<table border=1 width=600>")
print("<tr bgcolor=tan><th colspan=2>Custom Request Headers Received by the Server")
for name in custHeaders :
    print("<tr><td width=230><b>", name, "<td>", custHeaders[name])
print("</table>")        

print("<br>")
print("<table border=1 width=600>")
print("<tr bgcolor=cyan><th colspan=2>Additional Response Headers Returned by the Server")
for name in custHeaders:
    name2 = name[5:]                                        #strip off the leading HTTP_
    print("<tr><td><b>", name2, "<td>", custHeaders[name])
print("<tr><td><b>Set-Cookie <td>", cookName +'='+ cookValue)
print("</table>")   

if env != 'no':
    print("<br><br><br>")
    print("<table border=1>")
    print("<tr bgcolor=lightblue><th colspan=2>Environment Variables")
    for name in os.environ :
        print("<tr><td width=230><b>", name, "<td>", os.environ[name])
    print("</table>")        



#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================