<commit_before># -*- coding: utf-8 -*-
"""Utility to parse HTTP responses - http://pythonwise.blogspot.com/2010/02/parse-http-response.html.
"""

from StringIO import StringIO
from httplib import HTTPResponse


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


class HttpResponseParser(object):

    @staticmethod
    def parse(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
<commit_msg>Properly parse HTTP responses until the end of the headers<commit_after># -*- coding: utf-8 -*-
"""Utility to parse HTTP responses - http://pythonwise.blogspot.com/2010/02/parse-http-response.html.
"""

from StringIO import StringIO
from httplib import HTTPResponse


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


class HttpResponseParser(object):

    @staticmethod
    def parse(sock):

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

        while 'HTTP/' not in response or '\r\n\r\n' not in response:
            # Parse until the end of the headers
            try:
                response += sock.read(4096)
            except AttributeError:
                response += sock.recv(4096)

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

        return response

{'fraction_non_alphanumeric': 0.0897629853756934, 'fraction_numerical': 0.024205748865355523, 'mean_word_length': 2.220779220779221, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7042085', 'n_tokens_mistral': 566, 'n_tokens_neox': 521, 'n_words': 161}