[Python-checkins] python/dist/src/Lib httplib.py,1.51,1.52

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
Fri, 28 Jun 2002 15:38:04 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv28446

Modified Files:
	httplib.py 
Log Message:
Fixes for two separate HTTP/1.1 bugs: 100 responses and HTTPS connections.

The HTTPResponse class now handles 100 continue responses, instead of
choking on them.  It detects them internally in the _begin() method
and ignores them.  Based on a patch by Bob Kline.

This closes SF bugs 498149 and 551273.

The FakeSocket class (for SSL) is now usable with HTTP/1.1
connections.  The old version of the code could not work with
persistent connections, because the makefile() implementation read
until EOF before returning.  If the connection is persistent, the
server sends a response and leaves the connection open.  A client that
reads until EOF will block until the server gives up on the connection
-- more than a minute in my test case.

The problem was fixed by implementing a reasonable makefile().  It
reads data only when it is needed by the layers above it.  It's
implementation uses an internal buffer with a default size of 8192.

Also, rename begin() method of HTTPResponse to _begin() because it
should only be called by the HTTPConnection.

    


Index: httplib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/httplib.py,v
retrieving revision 1.51
retrieving revision 1.52
diff -C2 -d -r1.51 -r1.52
*** httplib.py	1 Jun 2002 14:18:45 -0000	1.51
--- httplib.py	28 Jun 2002 22:38:01 -0000	1.52
***************
*** 112,120 ****
          self.will_close = _UNKNOWN      # conn will close at end of response
  
!     def begin(self):
!         if self.msg is not None:
!             # we've already started reading the response
!             return
! 
          line = self.fp.readline()
          if self.debuglevel > 0:
--- 112,116 ----
          self.will_close = _UNKNOWN      # conn will close at end of response
  
!     def _read_status(self):
          line = self.fp.readline()
          if self.debuglevel > 0:
***************
*** 136,146 ****
          # The status code is a three-digit number
          try:
!             self.status = status = int(status)
              if status < 100 or status > 999:
                  raise BadStatusLine(line)
          except ValueError:
              raise BadStatusLine(line)
!         self.reason = reason.strip()
  
          if version == 'HTTP/1.0':
              self.version = 10
--- 132,162 ----
          # The status code is a three-digit number
          try:
!             status = int(status)
              if status < 100 or status > 999:
                  raise BadStatusLine(line)
          except ValueError:
              raise BadStatusLine(line)
!         return version, status, reason
! 
!     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 1:
+             version, status, reason = self._read_status()
+             if status != 100:
+                 break
+             # skip the header from the 100 response
+             while 1:
+                 skip = self.fp.readline().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
***************
*** 153,156 ****
--- 169,173 ----
  
          if self.version == 9:
+             self.chunked = 0
              self.msg = mimetools.Message(StringIO())
              return
***************
*** 234,237 ****
--- 251,255 ----
  
          if self.chunked:
+             assert self.chunked != _UNKNOWN
              chunk_left = self.chunk_left
              value = ''
***************
*** 364,368 ****
          """Connect to the host and port specified in __init__."""
          msg = "getaddrinfo returns an empty list"
!         for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
              af, socktype, proto, canonname, sa = res
              try:
--- 382,387 ----
          """Connect to the host and port specified in __init__."""
          msg = "getaddrinfo returns an empty list"
!         for res in socket.getaddrinfo(self.host, self.port, 0,
!                                       socket.SOCK_STREAM):
              af, socktype, proto, canonname, sa = res
              try:
***************
*** 596,600 ****
              response = self.response_class(self.sock)
  
!         response.begin()
          self.__state = _CS_IDLE
  
--- 615,620 ----
              response = self.response_class(self.sock)
  
!         response._begin()
!         assert response.will_close != _UNKNOWN
          self.__state = _CS_IDLE
  
***************
*** 608,633 ****
          return response
  
  
! class FakeSocket:
!     def __init__(self, sock, ssl):
!         self.__sock = sock
!         self.__ssl = ssl
! 
!     def makefile(self, mode, bufsize=None):
!         """Return a readable file-like object with data from socket.
! 
!         This method offers only partial support for the makefile
!         interface of a real socket.  It only supports modes 'r' and
!         'rb' and the bufsize argument is ignored.
! 
!         The returned object contains *all* of the file data
!         """
!         if mode != 'r' and mode != 'rb':
!             raise UnimplementedFileMode()
  
!         msgbuf = []
          while 1:
              try:
!                 buf = self.__ssl.read()
              except socket.sslerror, err:
                  if (err[0] == socket.SSL_ERROR_WANT_READ
--- 628,648 ----
          return response
  
+ class SSLFile:
+     """File-like object wrapping an SSL socket."""
  
!     BUFSIZE = 8192
!     
!     def __init__(self, sock, ssl, bufsize=None):
!         self._sock = sock
!         self._ssl = ssl
!         self._buf = ''
!         self._bufsize = bufsize or self.__class__.BUFSIZE
  
!     def _read(self):
!         buf = ''
!         # put in a loop so that we retry on transient errors
          while 1:
              try:
!                 buf = self._ssl.read(self._bufsize)
              except socket.sslerror, err:
                  if (err[0] == socket.SSL_ERROR_WANT_READ
***************
*** 641,649 ****
                  if err[0] == errno.EINTR:
                      continue
                  raise
!             if buf == '':
                  break
!             msgbuf.append(buf)
!         return StringIO("".join(msgbuf))
  
      def send(self, stuff, flags = 0):
--- 656,718 ----
                  if err[0] == errno.EINTR:
                      continue
+                 if err[0] == errno.EBADF:
+                     # XXX socket was closed?
+                     break
                  raise
!             else:
                  break
!         return buf
! 
!     def read(self, size=None):
!         L = [self._buf]
!         avail = len(self._buf)
!         while size is None or avail < size:
!             s = self._read()
!             if s == '':
!                 break
!             L.append(s)
!             avail += len(s)
!         all = "".join(L)
!         if size is None:
!             self._buf = ''
!             return all
!         else:
!             self._buf = all[size:]
!             return all[:size]
! 
!     def readline(self):
!         L = [self._buf]
!         self._buf = ''
!         while 1:
!             i = L[-1].find("\n")
!             if i >= 0:
!                 break
!             s = self._read()
!             if s == '':
!                 break
!             L.append(s)
!         if i == -1:
!             # loop exited because there is no more data
!             return "".join(L)
!         else:
!             all = "".join(L)
!             # XXX could do enough bookkeeping not to do a 2nd search
!             i = all.find("\n") + 1
!             line = all[:i]
!             self._buf = all[i:]
!             return line
! 
!     def close(self):
!         self._sock.close()
! 
! class FakeSocket:
!     def __init__(self, sock, ssl):
!         self.__sock = sock
!         self.__ssl = ssl
! 
!     def makefile(self, mode, bufsize=None):
!         if mode != 'r' and mode != 'rb':
!             raise UnimplementedFileMode()
!         return SSLFile(self.__sock, self.__ssl, bufsize)
  
      def send(self, stuff, flags = 0):
***************
*** 886,890 ****
          for header in headers.headers: print header.strip()
      print
!     print h.getfile().read()
  
      # minimal test that code to extract host from url works
--- 955,959 ----
          for header in headers.headers: print header.strip()
      print
!     print "read", len(h.getfile().read())
  
      # minimal test that code to extract host from url works
***************
*** 907,910 ****
--- 976,980 ----
          hs.endheaders()
          status, reason, headers = hs.getreply()
+         # XXX why does this give a 302 response?
          print 'status =', status
          print 'reason =', reason
***************
*** 913,917 ****
              for header in headers.headers: print header.strip()
          print
!         print hs.getfile().read()
  
  
--- 983,987 ----
              for header in headers.headers: print header.strip()
          print
!         print "read", len(hs.getfile().read())