X-Git-Url: http://www.dolda2000.com/gitweb/?p=ashd.git;a=blobdiff_plain;f=python%2Fashd%2Fproto.py;h=8dc5ecd133c2ff63cd54978e7879fa809d843010;hp=ab2152e0498fd76b4a67ddacf6a96fa29226d2ce;hb=f56b079086cb35e50697a4752b38679ae5e44679;hpb=55fa3f634594cedabf75182bd6404463c091ff63 diff --git a/python/ashd/proto.py b/python/ashd/proto.py index ab2152e..8dc5ecd 100644 --- a/python/ashd/proto.py +++ b/python/ashd/proto.py @@ -8,7 +8,7 @@ ashd.util module provides an easier-to-use interface. """ import os, socket -from . import htlib +import htlib __all__ = ["req", "recvreq", "sendreq"] @@ -46,14 +46,12 @@ class req(object): self.ver = ver self.rest = rest self.headers = headers - self.bsk = socket.fromfd(fd, socket.AF_UNIX, socket.SOCK_STREAM) - self.sk = self.bsk.makefile('rwb') + self.sk = socket.fromfd(fd, socket.AF_UNIX, socket.SOCK_STREAM).makefile('r+') os.close(fd) def close(self): "Close this request's response socket." self.sk.close() - self.bsk.close() def __getitem__(self, header): """Find a HTTP header case-insensitively. For example, @@ -61,8 +59,6 @@ class req(object): header regardlessly of whether the client specified it as "Content-Type", "content-type" or "Content-type". """ - if isinstance(header, str): - header = header.encode("ascii") header = header.lower() for key, val in self.headers: if key.lower() == header: @@ -73,8 +69,6 @@ class req(object): """Works analogously to the __getitem__ method for checking header presence case-insensitively. """ - if isinstance(header, str): - header = header.encode("ascii") header = header.lower() for key, val in self.headers: if key.lower() == header: @@ -85,7 +79,7 @@ class req(object): """Creates a duplicate of this request, referring to a duplicate of the response socket. """ - return req(self.method, self.url, self.ver, self.rest, self.headers, os.dup(self.bsk.fileno())) + return req(self.method, self.url, self.ver, self.rest, self.headers, os.dup(self.sk.fileno())) def match(self, match): """If the `match' argument matches exactly the leading part of @@ -101,17 +95,13 @@ class req(object): else: util.respond(req, "Not found", status = "404 Not Found", ctype = "text/plain") """ - if isinstance(match, str): - match = match.encode("utf-8") if self.rest[:len(match)] == match: self.rest = self.rest[len(match):] return True return False def __str__(self): - def dec(b): - return b.decode("ascii", errors="replace") - return "\"%s %s %s\"" % (dec(self.method), dec(self.url), dec(self.ver)) + return "\"%s %s %s\"" % (self.method, self.url, self.ver) def __enter__(self): return self @@ -129,22 +119,22 @@ def recvreq(sock = 0): done, to avoid leaking response sockets. If end-of-file is received on the socket, None is returned. - This function may either raise on OSError if an error occurs on - the socket, or a ashd.proto.protoerr if the incoming request is + This function may either raise an OSError if an error occurs on + the socket, or an ashd.proto.protoerr if the incoming request is invalidly encoded. """ data, fd = htlib.recvfd(sock) if fd is None: return None try: - parts = data.split(b'\0')[:-1] + parts = data.split('\0')[:-1] if len(parts) < 5: raise protoerr("Truncated request") method, url, ver, rest = parts[:4] headers = [] i = 4 while True: - if parts[i] == b"": break + if parts[i] == "": break if len(parts) - i < 3: raise protoerr("Truncated request") headers.append((parts[i], parts[i + 1])) @@ -161,13 +151,13 @@ def sendreq(sock, req): This function may raise an OSError if an error occurs on the socket. """ - data = b"" - data += req.method + b'\0' - data += req.url + b'\0' - data += req.ver + b'\0' - data += req.rest + b'\0' + data = "" + data += req.method + '\0' + data += req.url + '\0' + data += req.ver + '\0' + data += req.rest + '\0' for key, val in req.headers: - data += key + b'\0' - data += val + b'\0' - data += b'\0' + data += key + '\0' + data += val + '\0' + data += '\0' htlib.sendfd(sock, req.sk.fileno(), data)