error handling

This commit is contained in:
ed 2019-06-18 21:54:49 +00:00
parent d59ad1b119
commit a25a6946a7
2 changed files with 21 additions and 16 deletions

View file

@ -115,8 +115,6 @@ class HttpCli(object):
return False return False
return True
def reply(self, body, status="200 OK", mime="text/html", headers=[]): def reply(self, body, status="200 OK", mime="text/html", headers=[]):
# TODO something to reply with user-supplied values safely # TODO something to reply with user-supplied values safely
response = [ response = [
@ -195,12 +193,10 @@ class HttpCli(object):
act = self.parser.require("act", 64) act = self.parser.require("act", 64)
if act == "bput": if act == "bput":
self.handle_plain_upload() return self.handle_plain_upload()
return
if act == "login": if act == "login":
self.handle_login() return self.handle_login()
return
raise Pebkac('invalid action "{}"'.format(act)) raise Pebkac('invalid action "{}"'.format(act))
@ -217,6 +213,7 @@ class HttpCli(object):
h = ["Set-Cookie: cppwd={}; Path=/".format(pwd)] h = ["Set-Cookie: cppwd={}; Path=/".format(pwd)]
html = self.conn.tpl_msg.render(h1=msg, h2='<a href="/">ack</a>', redir="/") html = self.conn.tpl_msg.render(h1=msg, h2='<a href="/">ack</a>', redir="/")
self.reply(html.encode("utf-8"), headers=h) self.reply(html.encode("utf-8"), headers=h)
return True
def handle_plain_upload(self): def handle_plain_upload(self):
nullwrite = self.args.nw nullwrite = self.args.nw
@ -311,8 +308,8 @@ class HttpCli(object):
pre=msg, pre=msg,
) )
self.reply(html.encode("utf-8")) self.reply(html.encode("utf-8"))
self.parser.drop() self.parser.drop()
return True
def tx_file(self, path): def tx_file(self, path):
file_ts = os.path.getmtime(fsenc(path)) file_ts = os.path.getmtime(fsenc(path))
@ -334,10 +331,12 @@ class HttpCli(object):
if not do_send: if not do_send:
status = "304 Not Modified" status = "304 Not Modified"
mime = mimetypes.guess_type(path)[0] or "application/octet-stream"
headers = [ headers = [
"HTTP/1.1 " + status, "HTTP/1.1 " + status,
"Connection: Keep-Alive", "Connection: Keep-Alive",
"Content-Type: " + mimetypes.guess_type(path)[0], "Content-Type: " + mime,
"Content-Length: " + str(os.path.getsize(fsenc(path))), "Content-Length: " + str(os.path.getsize(fsenc(path))),
"Last-Modified: " + file_lastmod, "Last-Modified: " + file_lastmod,
] ]
@ -359,7 +358,7 @@ class HttpCli(object):
try: try:
self.s.send(buf) self.s.send(buf)
except ConnectionResetError: except:
return False return False
self.log(logmsg) self.log(logmsg)
@ -368,11 +367,13 @@ class HttpCli(object):
def tx_mounts(self): def tx_mounts(self):
html = self.conn.tpl_mounts.render(this=self) html = self.conn.tpl_mounts.render(this=self)
self.reply(html.encode("utf-8")) self.reply(html.encode("utf-8"))
return True
def tx_upper(self): def tx_upper(self):
# return html for basic uploader; # return html for basic uploader;
# js rewrites to up2k unless uparam['b'] # js rewrites to up2k unless uparam['b']
self.loud_reply("TODO jupper {}".format(self.vpath)) self.loud_reply("TODO jupper {}".format(self.vpath))
return True
def tx_browser(self): def tx_browser(self):
vpath = "" vpath = ""
@ -430,3 +431,4 @@ class HttpCli(object):
vdir=self.vpath, vpnodes=vpnodes, files=dirs, can_upload=self.writable vdir=self.vpath, vpnodes=vpnodes, files=dirs, can_upload=self.writable
) )
self.reply(html.encode("utf-8", "replace")) self.reply(html.encode("utf-8", "replace"))
return True

View file

@ -41,14 +41,17 @@ class TcpSrv(object):
try: try:
self.srv.bind((self.args.i, self.args.p)) self.srv.bind((self.args.i, self.args.p))
except (OSError, socket.error) as ex: except (OSError, socket.error) as ex:
if ex.errno != 98: if ex.errno == 98:
raise raise Exception(
"\033[1;31mport {} is busy on interface {}\033[0m".format(
raise Exception( self.args.p, self.args.i
"\033[1;31mport {} is busy on interface {}\033[0m".format( )
self.args.p, self.args.i )
if ex.errno == 99:
raise Exception(
"\033[1;31minterface {} does not exist\033[0m".format(self.args.i)
) )
)
def run(self): def run(self):
self.srv.listen(self.args.nc) self.srv.listen(self.args.nc)