175 lines
4 KiB
Python
175 lines
4 KiB
Python
|
import socketserver
|
||
|
import traceback
|
||
|
import io
|
||
|
import os.path
|
||
|
|
||
|
import hserver
|
||
|
import hserver.systemplates
|
||
|
|
||
|
import threading
|
||
|
import socket
|
||
|
|
||
|
from hserver.mime import MIME
|
||
|
from hserver.packages import PackageManager
|
||
|
|
||
|
import objectbroker
|
||
|
from objectbroker import AquisitionProxy
|
||
|
|
||
|
class HServerRequestHandler(socketserver.StreamRequestHandler):
|
||
|
""
|
||
|
|
||
|
def handle(self):
|
||
|
self.__keepalive = False
|
||
|
|
||
|
print("handle():")
|
||
|
|
||
|
try:
|
||
|
self.handle_request()
|
||
|
while (self.__keepalive):
|
||
|
self.handle_request()
|
||
|
|
||
|
except:
|
||
|
print("handle(): Exception:")
|
||
|
traceback.print_exc()
|
||
|
|
||
|
# self.rfile.close()
|
||
|
# self.wfile.close()
|
||
|
self.request.close()
|
||
|
|
||
|
print("handle(): finished.")
|
||
|
|
||
|
def handle_request(self):
|
||
|
print("handle_request():")
|
||
|
|
||
|
l = self.rfile.readline()
|
||
|
rline = str(l,"iso-8859-1")
|
||
|
toks = rline.split()
|
||
|
print("handle_request: requestline: %s" % (toks,))
|
||
|
|
||
|
if (len(toks)!=3):
|
||
|
return
|
||
|
|
||
|
method,uri,version = toks
|
||
|
|
||
|
if (not version.startswith("HTTP/")):
|
||
|
print("handle_request: Version String: %s" % (version,))
|
||
|
return
|
||
|
|
||
|
#self.rbfile = self.rfile
|
||
|
#self.rfile = io.TextIOWrapper(self.rbfile, "iso-8859-1")
|
||
|
|
||
|
print("MIMEing...")
|
||
|
|
||
|
mime = MIME(self.rfile, defaultlength=0)
|
||
|
r = hserver.Request( URI=uri, MIME=mime, method=method, read=self.rfile )
|
||
|
|
||
|
try:
|
||
|
self.server.hserver.handle(r)
|
||
|
except hserver.FinishRequestException:
|
||
|
pass
|
||
|
except hserver.HttpException as ex:
|
||
|
r.reset()
|
||
|
r.setResponseHeader("Content-Type","text/html")
|
||
|
|
||
|
t = self.server.hserver.packagemanager().hcore.Templates.provide("%s.html" % (ex.status,))
|
||
|
t.run( ex, outfile=r.getContentFile() )
|
||
|
|
||
|
except Exception as ex:
|
||
|
traceback.print_exception(type(ex),ex,ex.__traceback__)
|
||
|
|
||
|
r.reset()
|
||
|
r.setResponseHeader("Content-Type","text/html")
|
||
|
|
||
|
self.templ_exception = hserver.Template(source=hserver.systemplates.tmpl_exception, disable_sandbox=True)
|
||
|
self.templ_exception.run( ex, globals(), outfile=r.getContentFile() )
|
||
|
|
||
|
print("handle_request(): reply")
|
||
|
|
||
|
|
||
|
r.setResponseHeader("Connection","close")
|
||
|
|
||
|
w = io.TextIOWrapper(self.wfile)
|
||
|
w.write("%s %s %s\r\n" % (version,r.getStatus(),r.getStatus()))
|
||
|
|
||
|
c = r.getContent()
|
||
|
|
||
|
r.getResponseHeaders()['Content-Length'] = str(len(c))
|
||
|
|
||
|
for k in r.getResponseHeaders().keys():
|
||
|
w.write("%s: %s\r\n" % (k, r.getResponseHeaders()[k]))
|
||
|
|
||
|
w.write("\r\n")
|
||
|
w.flush()
|
||
|
|
||
|
print("handle_request(): sending content... (%s Bytes)" % (len(c),))
|
||
|
|
||
|
self.wfile.write( c )
|
||
|
#self.wfile.write( b"\r\n" );
|
||
|
print("handle_request(): finished.")
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
class HServer:
|
||
|
|
||
|
def __init__(self,listen = ('localhost',8080)):
|
||
|
self.__listen = (listen[0],listen[1])
|
||
|
|
||
|
self.__disksync = objectbroker.DiscObjectStore("./DiskSync")
|
||
|
self.__broker = objectbroker.ObjectBroker( self.__disksync )
|
||
|
|
||
|
self.__packages = PackageManager("./packages")
|
||
|
self.__packages.loadPackageFromPath( "{0}/../hcore".format( os.path.dirname(__file__) ) )
|
||
|
|
||
|
if "ROOT" in self.__disksync:
|
||
|
self.__root = self.__broker.load( self.__disksync["ROOT"].decode("UTF-8") )
|
||
|
else:
|
||
|
self.__root = hserver.api.WebFolder()
|
||
|
persistence_id = self.__broker.save( self.__root )
|
||
|
self.__disksync["ROOT"] = persistence_id.encode("UTF-8")
|
||
|
|
||
|
self.__packages.hcore.module().core.init(self)
|
||
|
|
||
|
|
||
|
def _shutdown(self):
|
||
|
self.__http.shutdown()
|
||
|
|
||
|
def shutdown(self):
|
||
|
t = threading.Thread(target=HServer._shutdown,args=(self,))
|
||
|
t.start()
|
||
|
|
||
|
def packagemanager(self):
|
||
|
return self.__packages
|
||
|
|
||
|
def getRoot(self):
|
||
|
return self.__root
|
||
|
|
||
|
def aq_root(self):
|
||
|
return AquisitionProxy( self.__root )
|
||
|
|
||
|
def run(self):
|
||
|
socketserver.TCPServer.allow_reuse_address = True
|
||
|
self.__http = socketserver.TCPServer(self.__listen,HServerRequestHandler)
|
||
|
self.__http.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
|
||
|
self.__http.hserver = self
|
||
|
self.__http.serve_forever()
|
||
|
|
||
|
|
||
|
|
||
|
def handle(self,request):
|
||
|
print("Hserver.handle(%s)" % (request,))
|
||
|
|
||
|
root = self.aq_root()
|
||
|
|
||
|
if (request.pathwalker().len()==0):
|
||
|
root( request )
|
||
|
else:
|
||
|
root.walk( request )
|
||
|
self.__broker.save( self.__root )
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|