<commit_before>#!/usr/bin/env python3
import asyncio
import signal
import datetime
from email.utils import formatdate


class HTTPRequest(object):

    def __init__(self, raw):
        self.raw = raw

    @property
    def body(self):
        pass

    @property
    def method(self):
        return self.raw.split('\r\n')[0].split(' ')[0]

    @property
    def path(self):
        return self.raw.split('\r\n')[0].split(' ')[1]

    @property
    def headers(self):
        pass


class HTTPResponse(object):

    def __init__(self, body='', code=200, additional_headers={}):
        self.body = body
        self.code = code
        self.headers = {
            'Content-Type': 'text/html; charset=utf-8',
            'Server': 'hurricane/python',
            'Date': formatdate(timeval=None, localtime=False, usegmt=True),
        }
        self.headers.update(additional_headers)

    def compose(self):    
        header = 'HTTP/1.1 {code} OK\r\n'.format(code=self.code)
        for head, value in self.headers.items():
            header += '{}: {}\r\n'.format(head, value)
        return '{}\r\n{}'.format(header, self.body)


class HTTPHandler(object):

    def handler(self, request):
        raise NotImplementedError()

    def render(self, text):
        return text

    def __call__(self, request):
        response = HTTPResponse(code=200)
        body = self.handler(request)
        if body:
            response.body = body
        return response


class HTTPServer(asyncio.Protocol):

    TIMEOUT = 5.0
    _handlers = {}

    def timeout(self):
        #print('connection timeout, closing.')
        self.transport.close()

    def connection_made(self, transport):
        self.transport = transport

        # start 5 seconds timeout timer
        self.h_timeout = asyncio.get_event_loop().call_later(
            self.TIMEOUT, self.timeout
        )

    def data_received(self, data):
        decoded_data = data.decode()<commit_msg>jinja2 support added<commit_after>#!/usr/bin/env python3
import asyncio
import signal
import jinja2
import datetime
from email.utils import formatdate


class HTTPRequest(object):

    def __init__(self, raw):
        self.raw = raw

    @property
    def body(self):
        pass

    @property
    def method(self):
        return self.raw.split('\r\n')[0].split(' ')[0]

    @property
    def path(self):
        return self.raw.split('\r\n')[0].split(' ')[1]

    @property
    def headers(self):
        pass


class HTTPResponse(object):

    def __init__(self, body='', code=200, additional_headers={}):
        self.body = body
        self.code = code
        self.headers = {
            'Content-Type': 'text/html; charset=utf-8',
            'Server': 'hurricane/python',
            'Date': formatdate(timeval=None, localtime=False, usegmt=True),
        }
        self.headers.update(additional_headers)

    def compose(self):    
        header = 'HTTP/1.1 {code} OK\r\n'.format(code=self.code)
        for head, value in self.headers.items():
            header += '{}: {}\r\n'.format(head, value)
        return '{}\r\n{}'.format(header, self.body)


class HTTPHandler(object):

    def handler(self, request):
        raise NotImplementedError()

    def render(self, text, **kwargs):
        result = ''
        if kwargs:
            template = jinja2.Template(text)
            result = template.render(kwargs)
        return result or text

    def __call__(self, request):
        response = HTTPResponse(code=200)
        body = self.handler(request)
        if body:
            response.body = body
        return response


class HTTPServer(asyncio.Protocol):

    TIMEOUT = 5.0
    _handlers = {}

    def timeout(self):
        #print('connection timeout, closing.')
        self.transport.close()

    def connection_made(self, transport):
        self.transport = transport

        # start 5 seconds timeout timer
        self.h_timeout = asyncio.get_event_loop().call_later(
            self.TIMEOUT, self.timeout
        )

    def data_received(self, data):
        decoded_data = data.decode()
{'fraction_non_alphanumeric': 0.12738386308068458, 'fraction_numerical': 0.009046454767726161, 'mean_word_length': 2.307194826192401, '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': '4063401', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1205, 'n_words': 279}