diff --git a/.gitattributes b/.gitattributes index 90eaec19..0198fdf1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,4 @@ * text eol=lf *.png binary +*.gif binary diff --git a/copyparty/__init__.py b/copyparty/__init__.py index 60b4e450..1d427577 100644 --- a/copyparty/__init__.py +++ b/copyparty/__init__.py @@ -14,6 +14,7 @@ if PY2: class EnvParams(object): def __init__(self): + self.mod = os.path.dirname(os.path.realpath(__file__)) if sys.platform == "win32": self.cfg = os.path.normpath(os.environ["APPDATA"] + "/copyparty") elif sys.platform == "darwin": diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 983f362d..97cd0199 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -4,6 +4,7 @@ from __future__ import print_function import time import hashlib +import mimetypes from .__init__ import * from .util import * @@ -84,9 +85,9 @@ class HttpCli(object): return ret[:-4].decode("utf-8", "replace").split("\r\n") - def reply(self, body): - header = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: text/html\r\nContent-Length: {0}\r\n\r\n".format( - len(body) + def reply(self, body, status="200 OK", mime="text/html"): + header = "HTTP/1.1 {}\r\nConnection: Keep-Alive\r\nContent-Type: {}\r\nContent-Length: {}\r\n\r\n".format( + status, mime, len(body) ).encode( "utf-8" ) @@ -95,16 +96,43 @@ class HttpCli(object): return body - def loud_reply(self, body): + def loud_reply(self, body, **kwargs): self.log(body.rstrip()) - self.reply(b"
" + body.encode("utf-8")) + self.reply(b"" + body.encode("utf-8"), **kwargs) + + def send_file(self, path): + sz = os.path.getsize(path) + mime = mimetypes.guess_type(path)[0] + header = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: {}\r\nContent-Length: {}\r\n\r\n".format( + mime, sz + ).encode( + "utf-8" + ) + + if self.ok: + self.s.send(header) + + with open(path, "rb") as f: + while self.ok: + buf = f.read(4096) + if not buf: + break + + self.s.send(buf) def handle_get(self): self.log("") self.log("GET {0} {1}".format(self.addr[0], self.req)) - self.reply( - b'