<commit_before># encoding: utf-8

import sys
import cgi

import time
from functools import partial
from inspect import getouterframes, currentframe

from marrow.server.protocol import Protocol
from marrow.server.http import release

from marrow.util.compat import binary, unicode, IO

try:
    from email.utils import formatdate

except ImportError:
    from rfc822 import formatdate


__all__ = ['HTTPProtocol', 'HTTPServer']
log = __import__('logging').getLogger(__name__)


CRLF = b"\r\n"
dCRLF = b"\r\n\r\n"
HTTP_INTERNAL_ERROR = b" 500 Internal Server Error\r\nContent-Type: text/plain\r\nContent-Length: 48\r\n\r\nThe server encountered an unrecoverable error.\r\n"
__versionstring__ = b'marrow.httpd/' + release.release



# TODO: Separate out into marrow.util.
try:
    range = xrange

except:
    pass


# TODO: Separate out into marrow.util.
def bytestring(s, encoding="iso-8859-1", fallback="iso-8859-1"):
    if not isinstance(s, unicode):
        fname, line = getouterframes(currentframe())[1][1:3]
        log.warn("Value is already byte string.\n%s:%d", fname, line)
        return s
    
    try:
        s.encode(encoding)
    
    except UnicodeDecodeError:
        s.encode(fallback)


# TODO: Separate out into marrow.util.
def native(s, encoding="iso-8859-1", fallback="iso-8859-1"):
    if isinstance(s, str):
        fname, line = getouterframes(currentframe())[1][1:3]
        log.warn("Value is already native string.\n%s:%d", fname, line)
        return s
    
    try:
        return s.encode(encoding)
    
    except:
        return s.encode(fallback)


# TODO: Separate out into marrow.util.
class LoggingFile(object): # pragma: no cover
    def __init__(self, logger=None, level=logging.ERROR):
        logger = logger if logger else logging.getLogger('wsgi.errors')
        self.logger = partial(logger.log, level)
    
    def write(self, text):
        self.logger(text)
    
    def writelines(self, lines):
        for line in lines:
            self.logger(line)
    
    def close(self, *args, **kw): 
        """A no-op method used for several of the file-like object methods."""
        pass
    
    def next(self, *args, **kw):
        """An error-raising exception usedbfor several of the methods."""
        raise IOError("Logging files can not be read.")
    
    flush = close
    read = next
    readline = next
    readlines = next

errorlog = LoggingFile(__import__('logging').getLogger('wsgi.errors'))


class HTTPProtocol(Protocol):
    def __init__(self, server, testing, application, ingress=None, egress=None, pedantic=True, **options):
        super(HTTPProtocol, self).__init__(server, testing, **options)
        
        self.application = application
        self.ingress = ingress if ingress else []
        self.egress = egress if egress else []
        self.pedantic = pedantic
        
        if sys.version_info < (3, 0):
            self._name = server.name
            self._addr = server.address[0] if isinstance(server.address, tuple) else b''
            self._port = str(server.address[1]) if isinstance(server.address, tuple) else b'80'
        
        else:
            self._name = server.name.encode()
            self._addr = (server.address[0] if isinstance(server.address, tuple) else b'').encode()
            self._port = (str(server.address[1]) if isinstance(server.address, tuple) else b'80').encode()
    
    def accept(self, client):
        self.Connection(self.server, self, client)
    
    class Connection(object):
        def __init__(self, server, protocol, client):
            self.server = server
            self.protocol = protocol
            self.client = client
            
            env = dict()
            env['REMOTE_ADDR'] = native(client.address[0])
            env['SERVER_NAME'] = native(protocol._name)
            env['SERVER_ADDR'] = native(protocol._addr)
            env['SERVER_PORT'] = native(protocol._port)
            
            env['wsgi.input'] = IO()
            env['wsgi.errors'] = errorlog
            env['wsgi.version'] = (2, 0)
            env['wsgi.multithread'] = getattr(server, 'threaded', False) # TODO: Temporary hack until marrow.server 1.0 release.
            env['wsgi.multiprocess'] = server.fork != 1
            env['wsgi.run_once'] = False
            env['wsgi.url_scheme'] = 'http' # TODO: Remove unicode_literals.
            
            # env['wsgi.script_name'] = b''
            # env['wsgi.path_info'] = b''
            
            self.environ = None
            self.environ_template = env
            
            self.finished = False<commit_msg>Simplified __init__ to utilize the native function.<commit_after># encoding: utf-8

import logging
import sys
import cgi

import time
from functools import partial
from inspect import getouterframes, currentframe

from marrow.server.protocol import Protocol
from marrow.server.http import release

from marrow.util.compat import binary, unicode, IO

try:
    from email.utils import formatdate

except ImportError:
    from rfc822 import formatdate


__all__ = ['HTTPProtocol', 'HTTPServer']
log = logging.getLogger(__name__)


CRLF = b"\r\n"
dCRLF = b"\r\n\r\n"
HTTP_INTERNAL_ERROR = b" 500 Internal Server Error\r\nContent-Type: text/plain\r\nContent-Length: 48\r\n\r\nThe server encountered an unrecoverable error.\r\n"
__versionstring__ = b'marrow.httpd/' + release.release



# TODO: Separate out into marrow.util.
try:
    range = xrange

except:
    pass


# TODO: Separate out into marrow.util.
def bytestring(s, encoding="iso-8859-1", fallback="iso-8859-1"):
    if not isinstance(s, unicode):
        fname, line = getouterframes(currentframe())[1][1:3]
        log.warn("Value is already byte string.\n%s:%d", fname, line)
        return s
    
    try:
        s.encode(encoding)
    
    except UnicodeDecodeError:
        s.encode(fallback)


# TODO: Separate out into marrow.util.
def native(s, encoding="iso-8859-1", fallback="iso-8859-1"):
    if isinstance(s, str):
        fname, line = getouterframes(currentframe())[1][1:3]
        log.warn("Value is already native string.\n%s:%d", fname, line)
        return s
    
    try:
        return s.encode(encoding)
    
    except:
        return s.encode(fallback)


# TODO: Separate out into marrow.util.
class LoggingFile(object): # pragma: no cover
    def __init__(self, logger=None, level=logging.ERROR):
        logger = logger if logger else logging.getLogger('wsgi.errors')
        self.logger = partial(logger.log, level)
    
    def write(self, text):
        self.logger(text)
    
    def writelines(self, lines):
        for line in lines:
            self.logger(line)
    
    def close(self, *args, **kw): 
        """A no-op method used for several of the file-like object methods."""
        pass
    
    def next(self, *args, **kw):
        """An error-raising exception usedbfor several of the methods."""
        raise IOError("Logging files can not be read.")
    
    flush = close
    read = next
    readline = next
    readlines = next

errorlog = LoggingFile()


class HTTPProtocol(Protocol):
    def __init__(self, server, testing, application, ingress=None, egress=None, pedantic=True, **options):
        super(HTTPProtocol, self).__init__(server, testing, **options)
        
        self.application = application
        self.ingress = ingress if ingress else []
        self.egress = egress if egress else []
        self.pedantic = pedantic
        
        self._name = native(server.name)
        self._addr = native(server.address[0]) if isinstance(server.address, tuple) else ''
        self._port = str(server.address[1]) if isinstance(server.address, tuple) else '80'
    
    def accept(self, client):
        self.Connection(self.server, self, client)
    
    class Connection(object):
        def __init__(self, server, protocol, client):
            self.server = server
            self.protocol = protocol
            self.client = client
            
            env = dict()
            env['REMOTE_ADDR'] = native(client.address[0])
            env['SERVER_NAME'] = native(protocol._name)
            env['SERVER_ADDR'] = native(protocol._addr)
            env['SERVER_PORT'] = native(protocol._port)
            
            env['wsgi.input'] = IO()
            env['wsgi.errors'] = errorlog
            env['wsgi.version'] = (2, 0)
            env['wsgi.multithread'] = getattr(server, 'threaded', False) # TODO: Temporary hack until marrow.server 1.0 release.
            env['wsgi.multiprocess'] = server.fork != 1
            env['wsgi.run_once'] = False
            env['wsgi.url_scheme'] = 'http' # TODO: Remove unicode_literals.
            
            # env['wsgi.script_name'] = b''
            # env['wsgi.path_info'] = b''
            
            self.environ = None
            self.environ_template = env
            
            self.finished = False
{'fraction_non_alphanumeric': 0.11804384485666104, 'fraction_numerical': 0.010792580101180439, 'mean_word_length': 2.469578783151326, 'pattern_counts': {'":': 0, '<': 4, '<?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': '642682', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2628, 'n_words': 687}