180 lines
3.7 KiB
Python
180 lines
3.7 KiB
Python
|
import http.server
|
||
|
import hserver
|
||
|
|
||
|
from hserver.formdata import FormData
|
||
|
|
||
|
import io
|
||
|
|
||
|
class PathWalker:
|
||
|
|
||
|
def __init__(self,path_info):
|
||
|
self.__walk = path_info
|
||
|
self.__walked = []
|
||
|
|
||
|
def len(self):
|
||
|
return len(self.__walk)
|
||
|
|
||
|
def next(self):
|
||
|
return self.__walk[0]
|
||
|
|
||
|
def walk(self):
|
||
|
n = self.__walk[0]
|
||
|
del self.__walk[0]
|
||
|
self.__walked.append(n)
|
||
|
return (n,self.__walk)
|
||
|
|
||
|
def walked(self, limit=None):
|
||
|
if limit is None:
|
||
|
return "%s" % ("/".join(self.__walked),)
|
||
|
return "%s" % ("/".join(self.__walked[:limit]),)
|
||
|
|
||
|
def stack(self):
|
||
|
return self.__walk
|
||
|
|
||
|
def wstack(self,ind = None):
|
||
|
if ind is None:
|
||
|
return self.__walked
|
||
|
return self.__walked[ind]
|
||
|
|
||
|
|
||
|
|
||
|
class Request:
|
||
|
|
||
|
def __init__(self, MIME=None, URI = None, vars = {}, method = "", read = None):
|
||
|
self.reset()
|
||
|
|
||
|
self.__mime = MIME
|
||
|
|
||
|
self.__headers = self.__mime.headers()
|
||
|
self.__vars = vars
|
||
|
self.__form = None
|
||
|
self.__read = read
|
||
|
|
||
|
self.set("REQUEST_URI", URI)
|
||
|
self.set("REQUEST_METHOD", method)
|
||
|
|
||
|
self.__initialize()
|
||
|
|
||
|
|
||
|
def __resetContent(self):
|
||
|
self.__bcontent = io.BytesIO()
|
||
|
self.__content = io.TextIOWrapper(self.__bcontent,write_through=True)
|
||
|
|
||
|
def reset(self):
|
||
|
self.__resetContent()
|
||
|
self.__status = 200
|
||
|
self.__rheaders = { "Content-Type": "text/html" }
|
||
|
|
||
|
def mime(self):
|
||
|
return self.__mime
|
||
|
|
||
|
def getContentFile(self):
|
||
|
return self.__content
|
||
|
|
||
|
def getBinaryContentFile(self):
|
||
|
return self.__bcontent
|
||
|
|
||
|
def getContent(self):
|
||
|
return self.__bcontent.getvalue()
|
||
|
|
||
|
|
||
|
def __getattr__(self,name):
|
||
|
if name in self.__vars.keys():
|
||
|
return self.__vars[name]
|
||
|
raise AttributeError()
|
||
|
|
||
|
def set(self,name,value):
|
||
|
self.__vars[name] = value
|
||
|
|
||
|
def keys(self):
|
||
|
return self.__vars.keys()
|
||
|
|
||
|
def __getitem__(self,name):
|
||
|
if name in self.__vars.keys():
|
||
|
return self.__vars[ name ]
|
||
|
|
||
|
def getForm(self):
|
||
|
return self.__form
|
||
|
|
||
|
|
||
|
def getResponseHeaders(self):
|
||
|
return self.__rheaders
|
||
|
|
||
|
def setResponseHeader(self,name,value):
|
||
|
self.__rheaders[ name ] = value
|
||
|
|
||
|
def setStatus(self,status):
|
||
|
self.__status = status
|
||
|
def getStatus(self):
|
||
|
return self.__status
|
||
|
|
||
|
|
||
|
def self(self,limit = None,trail=False):
|
||
|
p = self.SCRIPT_NAME + "/" + self.__pathwalker.walked( limit )
|
||
|
if trail and (p!="/"):
|
||
|
p = p + "/"
|
||
|
return p
|
||
|
|
||
|
def pathwalker(self):
|
||
|
return self.__pathwalker
|
||
|
|
||
|
def __initvar(self,name):
|
||
|
if not name in self.__vars.keys():
|
||
|
self.__vars[name] = ""
|
||
|
|
||
|
def __initialize(self):
|
||
|
self.__initvar("SCRIPT_NAME")
|
||
|
self.__vars["CLIMBPATH"] = []
|
||
|
|
||
|
ind = self.REQUEST_URI.find("?")
|
||
|
if (ind >= 0):
|
||
|
self.set("QUERY_STRING", self.REQUEST_URI[ ind + 1 :])
|
||
|
else:
|
||
|
self.set("QUERY_STRING", "")
|
||
|
ind = len(self.REQUEST_URI)
|
||
|
|
||
|
self.set("PATH_INFO", self.REQUEST_URI[:ind])
|
||
|
|
||
|
l = int( self.__headers.value("Content-Length",0) )
|
||
|
|
||
|
print("Request: Content-Length: %s" % (l,))
|
||
|
|
||
|
self.set("CONTENT_TYPE", self.__headers.value("Content-Type", ""))
|
||
|
self.set("REFERER", self.__headers.value("Referer", ""))
|
||
|
|
||
|
self.__construct_path_info()
|
||
|
|
||
|
self.__form = FormData(self)
|
||
|
|
||
|
self.__pathwalker = PathWalker(self.__path_info)
|
||
|
|
||
|
def __construct_path_info(self):
|
||
|
path_info = self.PATH_INFO.split("/")
|
||
|
del path_info[0]
|
||
|
if (len(path_info)>0) and (path_info[0] == ""):
|
||
|
del path_info[0]
|
||
|
if (len(path_info)>0) and (path_info[-1] == ""):
|
||
|
del path_info[-1]
|
||
|
self.__path_info = path_info
|
||
|
|
||
|
def redirect(self,uri = "",append = None):
|
||
|
if (len(uri)>0 and uri[0]!="/"):
|
||
|
uri = "/" + uri
|
||
|
|
||
|
if not append is None:
|
||
|
uri = "%s/%s" % (uri,append)
|
||
|
|
||
|
self.__status = 307
|
||
|
self.setResponseHeader("Location", "%s%s" % (self.SCRIPT_NAME,uri))
|
||
|
|
||
|
raise hserver.FinishRequestException()
|
||
|
|
||
|
|
||
|
def __str__(self):
|
||
|
b = io.StringIO()
|
||
|
|
||
|
b.write("<Request(M:%s U:%s)>" % (self.REQUEST_METHOD,self.REQUEST_URI))
|
||
|
|
||
|
return b.getvalue()
|
||
|
|