Example #1
0
 def _parse_response(fp):
     '''A striped down version of httplib.HttpResponse.begin'''
     
     headers = {}
     # read until we get a non-100 response
     while True:
         version, status, reason = _read_status(fp)
         if status != httplib.CONTINUE:
             break
         # skip the header from the 100 response
         while True:
             skip = fp.readline(_MAXLINE + 1)
             if len(skip) > _MAXLINE:
                 raise httplib.LineTooLong("header line")
             skip = skip.strip()
             if not skip:
                 break
         
     # read the rest of the headers
     n_headers = 0
     while True:
         line = fp.readline(_MAXLINE +1)
         if len(line) > _MAXLINE:
             raise httplib.LineTooLong("header line")
         line = line.strip()
         #print(line)
         
         if line in (b'\r\n', b'\n', b''):
             break
             
         n_headers += 1
         if n_headers > httplib._MAXHEADERS:
             raise httplib.HTTPException("got more than %d headers" % httplib._MAXHEADERS)
         
         key, value = line.split(b': ')
         
         headers[key.strip().lower()] = value.strip()
             
             
     #print(headers)
                 
     #if six.PY2:
     #    msg = httplib.HTTPMessage(fp, 0)
     #    msg.fp = None #force the message to relinquish it's file pointer
     #    length = msg.getheader('content-length')
     #else:
     #    # noting that httplib here is actually http.client
     #    msg = httplib.parse_headers(fp)
         
     length = int(headers.get(b'content-length', 0))
     
     
     if length > 0:
         data = fp.read(length)
     else:
         data = b''
     
     return status, reason, data
Example #2
0
	def _read_chunked(self,amt):
		if not(self.read_by_chunks):
			return httplib.HTTPResponse._read_chunked(self,amt)
						
		assert self.chunked != httplib._UNKNOWN
		line = self.fp.readline(httplib._MAXLINE + 1)
		if len(line) > httplib._MAXLINE:
			raise httplib.LineTooLong("chunk size")
		i = line.find(';')
		if i >= 0:
			line = line[:i]
		try:
			chunk_left = int(line, 16)
		except ValueError:
			self.close()
			raise httplib.IncompleteRead('')
		if chunk_left == 0:
			return ''
		ret = self._safe_read(chunk_left)
		self._safe_read(2)
		return ret
Example #3
0
    def begin(self):
        if self.msg is not None:
            # we've already started reading the response
            return

        # read until we get a non-100 response
        while True:
            version, status, reason = self._read_status()
            if status != httplib.CONTINUE:
                break
            # skip the header from the 100 response
            while True:
                skip = self.fp.readline(httplib._MAXLINE + 1)
                if len(skip) > httplib._MAXLINE:
                    raise httplib.LineTooLong("header line")
                skip = skip.strip()
                if not skip:
                    break
                if self.debuglevel > 0:
                    print "header:", skip

        self.status = status
        self.reason = reason.strip()
        if version == 'HTTP/1.0':
            self.version = 10
        elif version.startswith('HTTP/1.'):
            self.version = 11  # use HTTP/1.1 code for HTTP/1.x where x>=1
        elif version == 'HTTP/0.9':
            self.version = 9
        else:
            raise httplib.UnknownProtocol(version)

        if self.version == 9:
            self.length = None
            self.chunked = 0
            self.will_close = 1
            self.msg = httplib.HTTPMessage(StringIO())
            return

        self.msg = httplib.HTTPMessage(self.fp, 0)
        if self.debuglevel > 0:
            for hdr in self.msg.headers:
                print "header:", hdr,

        # don't let the msg keep an fp
        self.msg.fp = None

        # are we using the chunked-style of transfer encoding?
        tr_enc = self.msg.getheader('transfer-encoding')
        if tr_enc and tr_enc.lower() == "chunked":
            self.chunked = 1
            self.chunk_left = None
        else:
            self.chunked = 0

        # will the connection close at the end of the response?
        self.will_close = self._check_close()

        # do we have a Content-Length?
        # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
        length = self._get_content_length()
        if length and not self.chunked:
            try:
                self.length = int(length)
            except (ValueError, TypeError):
                self.length = None
            else:
                if self.length < 0:  # ignore nonsensical negative lengths
                    self.length = None
        else:
            self.length = None

        # does the body have a fixed length? (of zero)
        if (status == NO_CONTENT or status == httplib.NOT_MODIFIED
                or 100 <= status < 200 or  # 1xx codes
                self._method == 'HEAD'):
            self.length = 0

        # if the connection remains open, and we aren't using chunked, and
        # a content-length was not provided, then assume that the connection
        # WILL close.
        if not self.will_close and \
           not self.chunked and \
           self.length is None:
            self.will_close = 1