#!/usr/bin/python3
###################################################################################
# Read all cookies
# Using Python http.cookies module
###################################################################################
import os
import re
#import Cookie                      #use for Python 2x
from http   import cookies          #use for Python 3x
from urllib import parse            #for URL encoding/decoding
import cgitb
cgitb.enable()

print('Content-Type: text/html')
print('\n')

print("<h2>Reading Cookies - Using http.cookies Module</h2>")

cookiesStr = os.environ.get('HTTP_COOKIE')          #obtain the HTTP cookies

if cookiesStr is None:                              #if there are no cookies
    print("<h3>There are no cookies.</h3>")
    exit() 

print(cookiesStr, '<br><br>')                       #print(all cookies, '; ' delimited

#==========================================================================
# display_cookies(): Using http.cookies module  and SimpleCookie function
#                    SimpleCookie() function
#==========================================================================
def display_cookies(cookiesStr):
    
    cookiesStr = re.sub('(\w) ',r'\1%20',cookiesStr)    #cookies.SimpleCookie() parse bug. Does not like ' ' in cookie values
                                                        #substitute 'char ' with 'same_char%20'

    allCookies = cookies.SimpleCookie(cookiesStr)       #create an object to help read cookies

    try:
        value = allCookies['name4'].value               #getting the value of a single cookie
    except:
        value = ""
    print('Cookie <b>name4</b>, value: <b>', value, '</b>')
    print('<br><br>')

    print('<table border=1>')
    print('<tr bgcolor=tan><th>Cookie Name<th>Cookie Value</tr>')

#   cookiesSortedByName = sorted(allCookies.items())    #sort cookies by name 

    for (name, cookieHeader) in allCookies.items():     #iterate through all the cookies
        print('<tr>', end='')
        print('<td><b>',name,'</td>', end='')           #cookie name
#       print('<td>', cookieHeader.key, '</td>')        #cookie name (a different way)
        value         = cookieHeader.value              #cookie value (encoded) 
        value_decoded = parse.unquote(value)            #cokkie value (decoded)
        print('<td><b>Encoded:</b>', value, end='')               
        print('<br><b>Decoded:</b>', value_decoded, '</td>')    
        
    print('</table>')
#==========================================================================

display_cookies(cookiesStr)




#=== 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
#=================================================================================