<commit_before>import asyncio
import ssl
import warnings

from pypeman import endpoints, channels, nodes, message

import aiohttp
from aiohttp import web

class HTTPEndpoint(endpoints.BaseEndpoint):
    """
    Endpoint to receive HTTP connection from outside.

    :param http_args: dict to pass as **kwargs** to aiohttp.Application for example for
        `client_max_size`
    """

    def __init__(self, adress=None, address='127.0.0.1', port='8080', loop=None, http_args=None):
        super().__init__()
        self.http_args = http_args or {}
        self.ssl_context = self.http_args.pop('ssl_context', None)

        if adress is not None:
            warnings.warn("HTTPEndpoint 'adress' param is deprecated. Replace it by 'address'", DeprecationWarning)
            address = adress

        self._app = None
        self.address = address
        self.port = port
        self.loop = loop or asyncio.get_event_loop()

    def add_route(self, *args, **kwargs):
        if self._app is None:
            self._app = web.Application(**self.http_args)

        self._app.router.add_route(*args, **kwargs)

    async def start(self):
        if self._app is not None:
            srv = await self.loop.create_server(
                protocol_factory=self._app.make_handler(),
                host=self.address,
                port=self.port,
                ssl=self.ssl_context
            )
            print("Server started at http{}://{}:{}".format(
                's' if self.ssl_context else '',
                self.address,
                self.port
            ))
            return srv
        else:
            print("No HTTP route.")

class HttpChannel(channels.BaseChannel):
    """
    Channel that handles Http connection. The Http message is the message payload and some headers
    become metadata of message. Needs ``aiohttp`` python dependency to work.

    :param endpoint: HTTP endpoint used to get connections.
    :param method: Method filter.
    :param url: Only matching urls messages will be sent to this channel.
    :param encoding: Encoding of message. Default to 'utf-8'.

    """
    app = None

    def __init__(self, *args, endpoint=None, method='*', url='/', encoding=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.method = method
        self.url = url
        self.encoding = encoding
        if endpoint is None:
            raise TypeError('Missing "endpoint" argument')
        self.http_endpoint = endpoint

    async def start(self):
        await super().start()
        if self._first_start:
            self.http_endpoint.add_route(self.method, self.url, self.handle_request)

    async def handle_request(self, request):
        content = await request.text()
        msg = message.Message(content_type='http_request', payload=content, meta={'method': request.method})
        try:
            result = await self.handle(msg)
            encoding = self.encoding or 'utf-8'
            return web.Response(body=result.payload.encode(encoding), status=result.meta.get('status', 200))

        except channels.Dropped:<commit_msg>fixes gh-76 : http endpoint doesn't setup route<commit_after>import asyncio
import ssl
import warnings

from pypeman import endpoints, channels, nodes, message

import aiohttp
from aiohttp import web

class HTTPEndpoint(endpoints.BaseEndpoint):
    """
    Endpoint to receive HTTP connection from outside.

    :param http_args: dict to pass as **kwargs** to aiohttp.Application for example for
        `client_max_size`
    """

    def __init__(self, adress=None, address='127.0.0.1', port='8080', loop=None, http_args=None):
        super().__init__()
        self.http_args = http_args or {}
        self.ssl_context = self.http_args.pop('ssl_context', None)

        if adress is not None:
            warnings.warn("HTTPEndpoint 'adress' param is deprecated. Replace it by 'address'", DeprecationWarning)
            address = adress

        self._app = None
        self.address = address
        self.port = port
        self.loop = loop or asyncio.get_event_loop()

    def add_route(self, *args, **kwargs):
        if self._app is None:
            self._app = web.Application(**self.http_args)

        self._app.router.add_route(*args, **kwargs)

    async def start(self):
        if self._app is not None:
            srv = await self.loop.create_server(
                protocol_factory=self._app.make_handler(),
                host=self.address,
                port=self.port,
                ssl=self.ssl_context
            )
            print("Server started at http{}://{}:{}".format(
                's' if self.ssl_context else '',
                self.address,
                self.port
            ))
            return srv
        else:
            print("No HTTP route.")

class HttpChannel(channels.BaseChannel):
    """
    Channel that handles Http connection. The Http message is the message payload and some headers
    become metadata of message. Needs ``aiohttp`` python dependency to work.

    :param endpoint: HTTP endpoint used to get connections.
    :param method: Method filter.
    :param url: Only matching urls messages will be sent to this channel.
    :param encoding: Encoding of message. Default to 'utf-8'.

    """
    app = None

    def __init__(self, *args, endpoint=None, method='*', url='/', encoding=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.method = method
        self.url = url
        self.encoding = encoding
        if endpoint is None:
            raise TypeError('Missing "endpoint" argument')
        self.http_endpoint = endpoint

    async def start(self):
        first_start = self._first_start
        await super().start()
        if first_start:
            self.http_endpoint.add_route(self.method, self.url, self.handle_request)

    async def handle_request(self, request):
        content = await request.text()
        msg = message.Message(content_type='http_request', payload=content, meta={'method': request.method})
        try:
            result = await self.handle(msg)
            encoding = self.encoding or 'utf-8'
            return web.Response(body=result.payload.encode(encoding), status=result.meta.get('status', 200))

        except channels.Dropped:
{'fraction_non_alphanumeric': 0.10372255806554248, 'fraction_numerical': 0.005090677696468342, 'mean_word_length': 2.3764769065520945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5289405', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1702, 'n_words': 503}