def request_complete(self):
        """In the HTTP protocol, a request is complete if the "end of headers"
        sequence ('\r\n\r\n') has been received
        If the request is POST, stores the request body in a StringIO before
        returning True"""
        terminator = self.incoming.find('\r\n\r\n')
        if terminator == -1:
            return False
        lines = self.incoming[:terminator].split('\r\n')
        self.requestline = lines[0]
        try:
            self.method,self.url,self.protocol = lines[0].strip().split()
        except:
            self.method = None # indicates bad request
            return True
        # put request headers in a dictionary
        self.headers = {}
        for line in lines[1:]:
            k,v = line.split(':',1)
            self.headers[k.lower().strip()] = v.strip()
        # persistent connection
        close_conn = self.headers.get("connection","")
        if (self.protocol == "HTTP/1.1" 
            and close_conn.lower() == "keep-alive"):
            self.close_when_done = False
        # parse the url
        scheme,netloc,path,params,query,fragment = urlparse.urlparse(self.url)
        self.path,self.rest = path,(params,query,fragment)

        if self.method == 'POST':
            # for POST requests, read the request body
            # its length must be specified in the content-length header
            content_length = int(self.headers.get('content-length',0))
            body = self.incoming[terminator+4:]
            # request is incomplete if not all message body received
            if len(body)<content_length:
                return False
            f_body = cStringIO.StringIO(body)
            f_body.seek(0)
            sys.stdin = f_body # compatibility with CGI

        return True
In the HTTP protocol, a request is complete if the "end of headers"
        sequence ('

') has been received
        If the request is POST, stores the request body in a StringIO before
        returning True
recipes/Python/511454_asynchronous_HTTP_server_CGI/recipe-511454.py