# HG changeset patch # User paulb # Date 1076264165 0 # Node ID 51e935f05b4b1c4d905b3a4dd6d8fbae85055056 # Parent 88fe8cca77bdb16d6c7274a3be7cab9589fa8527 [project @ 2004-02-08 18:16:05 by paulb] Added a special stream object which understands part of the HTTP specification. diff -r 88fe8cca77bd -r 51e935f05b4b WebStack/Helpers/Request.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebStack/Helpers/Request.py Sun Feb 08 18:16:05 2004 +0000 @@ -0,0 +1,74 @@ +#!/usr/bin/env python + +""" +Request helper classes. +""" + +class MessageBodyStream: + + """ + A naive stream class, providing a non-blocking stream for transactions when + reading the message body. According to the HTTP standard, the following + things decide how long the message is: + + * Use of the Content-Length header field (see 4.4 Message Length). + * Use of the Transfer-Coding header field (see 3.6 Transfer Codings), + particularly when the "chunked" coding is used. + + NOTE: For now, we don't support the Transfer-Coding business. + """ + + def __init__(self, stream, headers): + + """ + Initialise the object with the given underlying 'stream'. The supplied + 'headers' in a dictionary-style object are used to examine the nature of + the request. + """ + + self.stream = stream + self.headers = headers + self.length = int(headers.get("Content-Length") or 0) + + def read(self, limit=None): + + "Reads all remaining data from the message body." + + if limit is not None: + limit = min(limit, self.length) + else: + limit = self.length + data = self.stream.read(limit) + self.length = self.length - len(data) + return data + + def readline(self): + + "Reads a single line of data from the message body." + + data = [] + while self.length > 0: + data.append(self.read(1)) + if data[-1] == "\n": + break + return "".join(data) + + def readlines(self): + + """ + Reads all remaining data from the message body, splitting it into lines + and returning the data as a list of lines. + """ + + lines = self.read().split("\n") + for i in range(0, len(lines) - 1): + lines[i] = lines[i] + "\n" + return lines + + def close(self): + + "Closes the stream." + + self.stream.close() + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 88fe8cca77bd -r 51e935f05b4b WebStack/Helpers/__init__.py