from StringIO import StringIO
from httplib import HTTPResponse

class FakeSocket(StringIO):
    def makefile(self, *args, **kw):
        return self


def parse_http_response(sock):

    try:
        # H4ck to standardize the API between sockets and SSLConnection objects
        # sock is a Python socket
        sock.read = sock.recv
    except AttributeError:
        # sock is an SSLConnection
        pass

    response = sock.read(4096)
    if 'HTTP/' not in response:
        # Try to get the rest of the response
        response += sock.read(4096)

    fake_sock = FakeSocket(response)
    response = HTTPResponse(fake_sock)
    response.begin()

    return response


from StringIO import StringIO
from httplib import HTTPResponse

class FakeSocket(StringIO):
    def makefile(self, *args, **kw):
        return self


def parse_http_response(sock):

    try:
        # H4ck to standardize the API between sockets and SSLConnection objects
        response = sock.read(4096)
    except AttributeError:
        response = sock.recv(4096)

    if 'HTTP/' not in response:
        # Try to get the rest of the response
        try:
            response += sock.read(4096)
        except AttributeError:
            response += sock.recv(4096)

    fake_sock = FakeSocket(response)
    response = HTTPResponse(fake_sock)
    response.begin()

    return response


Tweak the hack to fix bug when scanning through a proxy
Tweak the hack to fix bug when scanning through a proxy

  from StringIO import StringIO
  from httplib import HTTPResponse
  
  class FakeSocket(StringIO):
      def makefile(self, *args, **kw):
          return self
  
  
  def parse_http_response(sock):
  
      try:
          # H4ck to standardize the API between sockets and SSLConnection objects
+         response = sock.read(4096)
-         # sock is a Python socket
-         sock.read = sock.recv
      except AttributeError:
+         response = sock.recv(4096)
-         # sock is an SSLConnection
-         pass
  
-     response = sock.read(4096)
      if 'HTTP/' not in response:
          # Try to get the rest of the response
+         try:
-         response += sock.read(4096)
+             response += sock.read(4096)
+         except AttributeError:
+             response += sock.recv(4096)
  
      fake_sock = FakeSocket(response)
      response = HTTPResponse(fake_sock)
      response.begin()
  
      return response
  
  
Tweak the hack to fix bug when scanning through a proxy
## Code Before:
from StringIO import StringIO
from httplib import HTTPResponse

class FakeSocket(StringIO):
    def makefile(self, *args, **kw):
        return self


def parse_http_response(sock):

    try:
        # H4ck to standardize the API between sockets and SSLConnection objects
        # sock is a Python socket
        sock.read = sock.recv
    except AttributeError:
        # sock is an SSLConnection
        pass

    response = sock.read(4096)
    if 'HTTP/' not in response:
        # Try to get the rest of the response
        response += sock.read(4096)

    fake_sock = FakeSocket(response)
    response = HTTPResponse(fake_sock)
    response.begin()

    return response


## Instruction:
Tweak the hack to fix bug when scanning through a proxy
## Code After:
from StringIO import StringIO
from httplib import HTTPResponse

class FakeSocket(StringIO):
    def makefile(self, *args, **kw):
        return self


def parse_http_response(sock):

    try:
        # H4ck to standardize the API between sockets and SSLConnection objects
        response = sock.read(4096)
    except AttributeError:
        response = sock.recv(4096)

    if 'HTTP/' not in response:
        # Try to get the rest of the response
        try:
            response += sock.read(4096)
        except AttributeError:
            response += sock.recv(4096)

    fake_sock = FakeSocket(response)
    response = HTTPResponse(fake_sock)
    response.begin()

    return response

