def _read_request(self, reader):

        self.state = self.STATE_WAIT_FOR_REQUEST
        
        #setup readline iterator        
        lines = reader.read_lines()
        
        #parse status line, this will block to read the first request line
        line = lines.next().split()
        
        self.state = self.STATE_READING_HEADER
        
        u = urlparse.urlparse(line[1])
        
        self.method = line[0]
        if self.method not in ['GET', 'POST']:
            raise HTTPError('Unsupported method: %s' % self.method)

        #TODO validate version
        self.version = line[2]
        self.uri = line[1]

        #build up the WSGI environment
        self.environ['REQUEST_METHOD'] = line[0]
        self.environ['SCRIPT_NAME'] = '' #TODO
        self.environ['PATH_INFO'] = u[2]
        self.environ['QUERY_STRING'] = u[4]
        
        self.environ['wsgi.url_scheme'] = 'http'
        self.environ['wsgi.multiprocess'] = False
        self.environ['wsgi.multithread'] = True
        self.environ['wsgi.run_once'] = False
        self.environ['wsgi.version'] = (1, 0)
        
        #rest of request headers
        for line in lines:
            if not line: break
            key, value = line.split(': ')
            key = key.replace('-', '_').upper()
            value = value.strip()
            
            http_key = 'HTTP_' + key 
            if http_key in self.environ:
                self.environ[http_key] += ',' + value # comma-separate multiple headers
            else:
                self.environ[http_key] = value

        #wsgi complience 
        if 'HTTP_CONTENT_LENGTH' in self.environ:
            self.environ['CONTENT_LENGTH'] = self.environ['HTTP_CONTENT_LENGTH']

        if 'HTTP_CONTENT_TYPE' in self.environ:
            self.environ['CONTENT_TYPE'] = self.environ['HTTP_CONTENT_TYPE']

        #setup required wsgi streams
        self.environ['wsgi.input'] = WSGIInputStream(self, reader)
        self.environ['wsgi.errors'] = WSGIErrorStream()
        
        if not 'HTTP_HOST' in self.environ:
            if self.version == 'HTTP/1.0':
                #ok in version 1.0, TODO what should host in wsgi environ be?
                host = 'localhost'
            else:
                raise HTTPError('Host header field is required in HTTP version > 1.0')
        else:
            host = self.environ['HTTP_HOST']

        if ':' in host:
            host, port = host.split(':')
        else:
            host, port = host, 80

        self.environ['SERVER_NAME'] = host
        self.environ['SERVER_PORT'] = port
        self.environ['SERVER_PROTOCOL'] = self.version
        
        self.state = self.STATE_REQUEST_READ
dataset/ETHPy150Open concurrence/concurrence/lib/concurrence/http/server.py/WSGIRequest._read_request/original