2022-02-14 21:43:12 +00:00
global email
import email . utils
global fileHandlers
fileHandlers = { }
global indexFiles
indexFiles = [ ]
global pathHandlers
pathHandlers = { }
global clientLoopIn
def clientLoopIn ( self ) :
2022-06-21 01:30:34 +00:00
while True :
env = { }
env [ " self " ] = self
env [ " requestTime " ] = time . time ( )
env [ " header " ] = getHeaderFromConnection ( self . connection )
env [ " protocolHeaderList " ] , env [ " headerList " ] = parseHeader ( env [ " header " ] )
env [ " cmd " ] = env [ " protocolHeaderList " ] [ 0 ]
env [ " path " ] , env [ " args " ] = parseHeaderPath ( env [ " protocolHeaderList " ] [ 1 ] )
env [ " pathFixed " ] = fixUserPath ( env [ " path " ] )
2022-02-14 21:43:12 +00:00
2022-06-21 01:30:34 +00:00
env [ " lPath " ] = env [ " pathFixed " ] . replace ( " / " , os . path . sep )
env [ " fPath " ] = p ( indexPath , env [ " lPath " ] )
env [ " fileExt " ] = " . "
2022-02-14 21:43:12 +00:00
2022-06-21 01:30:34 +00:00
if not env [ " pathFixed " ] == " " and not os . path . isfile ( env [ " fPath " ] ) and os . path . isdir ( env [ " fPath " ] ) and env [ " pathFixed " ] [ - 1 ] != " / " : env [ " pathFixed " ] + = " / " # This is dirty, since it possibly circumvents .fhtpyaccess (You can see if a folder exists or not by probing)
if " / " + env [ " pathFixed " ] != env [ " path " ] :
newPath = " / " + pathToURL ( env [ " pathFixed " ] )
rawArgs = env [ " protocolHeaderList " ] [ 1 ] . split ( " ? " , 1 )
if len ( rawArgs ) > 1 :
newPath + = " ? " + rawArgs [ - 1 ]
refer ( self . connection , newPath )
2022-02-14 21:43:12 +00:00
return
2022-06-21 01:30:34 +00:00
if env [ " pathFixed " ] in pathHandlers :
pathHandlers [ env [ " pathFixed " ] ] ( env )
return
2022-02-14 21:43:12 +00:00
2022-06-21 01:30:34 +00:00
if not os . path . isfile ( env [ " fPath " ] ) :
if not os . path . isdir ( env [ " fPath " ] ) :
handle404 ( env )
return
found = False
for file in indexFiles :
if os . path . isfile ( p ( env [ " fPath " ] , file ) ) :
found = file
break
if found == False :
env [ " fileExt " ] = " .d "
env [ " lPath " ] = p ( env [ " lPath " ] , " . " )
env [ " fPath " ] = p ( indexPath , env [ " lPath " ] )
else :
env [ " lPath " ] = p ( env [ " lPath " ] , found )
env [ " fPath " ] = p ( indexPath , env [ " lPath " ] )
lPathSplit = env [ " lPath " ] . rsplit ( os . path . sep , 1 ) [ - 1 ] . rsplit ( " . " , 1 )
if len ( lPathSplit ) > 1 :
env [ " fileExt " ] = lPathSplit [ - 1 ] . lower ( )
2022-02-14 21:43:12 +00:00
else :
lPathSplit = env [ " lPath " ] . rsplit ( os . path . sep , 1 ) [ - 1 ] . rsplit ( " . " , 1 )
if len ( lPathSplit ) > 1 :
env [ " fileExt " ] = lPathSplit [ - 1 ] . lower ( )
2022-06-21 01:30:34 +00:00
env [ " fPathDir " ] = pUp ( env [ " fPath " ] )
env [ " requestTimeFormatted " ] = email . utils . formatdate ( int ( env [ " requestTime " ] ) ) . replace ( " -0000 " , " GMT " )
env [ " handler " ] = False
if env [ " fileExt " ] in fileHandlers :
env [ " handler " ] = fileHandlers [ env [ " fileExt " ] ]
elif " .* " in fileHandlers :
env [ " handler " ] = fileHandlers [ " .* " ]
if triggerEvent ( " handleHTTP " , env ) == False : return
if env [ " handler " ] :
env [ " handler " ] ( env )
else :
handle404 ( env )
if allowKeepAlive :
if " connection " in env [ " headerList " ] and env [ " headerList " ] [ " connection " ] == " keep-alive " :
continue
break