<commit_before>
import os
from http.server import BaseHTTPRequestHandler
from urllib.parse import urlparse
from urllib.request import Request
from urllib.request import urlopen
import logging

from fluxghost.http_websocket_route import get_match_ws_service
from fluxghost import __version__
from io import StringIO
import urllib.error

logger = logging.getLogger("HTTP")


class HttpHandler(BaseHTTPRequestHandler):
    server_version = "FLUXGhost/%s" % __version__
    protocol_version = "HTTP/1.1"

    def __init__(self, request, client, server):
        request.settimeout(60.)
        try:
            BaseHTTPRequestHandler.__init__(self, request, client, server)
        except OSError as e:
            if server.debug:
                logger.exception("OSError in http request")
            else:
                logger.error("%s", e)
        except Exception:
            logger.exception("Unhandle Error")

    def version_string(self):
        return self.server_version

    def log_error(self, format, *args):
        self.log_message(format, *args, error=True)

    def log_message(self, format, *args, **kw):
        if kw.get("error"):
            logger.warn("%s %s" % (self.address_string(), format % args))
        else:
            logger.info("%s %s" % (self.address_string(), format % args))

    def do_GET(self):  # noqa
        if self.path.startswith("/ws/"):
            klass, kwargs = get_match_ws_service(self.path[4:])

            if klass:
                self.serve_websocket(klass, kwargs)
            else:
                logger.exception("Websocket route error: %s" % self.path[4:])
                self.response_404()

        elif self.path == "/":
            return self.serve_assets("index.html")
        elif self.path.startswith("/api"):
            try:
                hostname = os.environ.get("proxy_api_host")
                print("Proxying %s" % hostname)
                url = 'http://{}{}'.format(hostname, self.path)
                req = Request(url=url)
                req_headers = self.headers.items()
                for header, value in req_headers:
                    if str(header).startswith("Host"):
                        continue
                    if str(header).startswith("Accept-Encoding"):
                        continue
                    req.add_header(header, value)
                try:
                    resp = urlopen(req)
                except urllib.error.HTTPError as e:
                    if e.getcode():
                        resp = e
                    else:
                        self.send_error(599, u'error proxying: {}'.format(unicode(e)))
                        return
                self.send_response(resp.getcode())
                respheaders = resp.getheaders()
                for header, value in respheaders:
                    if str(header).startswith("Transfer-Encoding"):
                        continue
                    self.send_header(header, value)
                self.end_headers()
                resp_content = resp.read(10240)
                while resp_content:
                    self.wfile.write(resp_content)
                    resp_content = resp.read(10240)
                self.wfile.flush()
            except IOError as e:
                self.send_error(404, 'error trying to proxy: {}'.format(str(e)))
        else:
            #self.send_response(200)
            #self.end_headers()
            #self.wfile.write('sadvd'.encode('utf-8'))<commit_msg>Adjust proxy pack size<commit_after>
import os
from http.server import BaseHTTPRequestHandler
from urllib.parse import urlparse
from urllib.request import Request
from urllib.request import urlopen
import logging

from fluxghost.http_websocket_route import get_match_ws_service
from fluxghost import __version__
from io import StringIO
import urllib.error

logger = logging.getLogger("HTTP")


class HttpHandler(BaseHTTPRequestHandler):
    server_version = "FLUXGhost/%s" % __version__
    protocol_version = "HTTP/1.1"

    def __init__(self, request, client, server):
        request.settimeout(60.)
        try:
            BaseHTTPRequestHandler.__init__(self, request, client, server)
        except OSError as e:
            if server.debug:
                logger.exception("OSError in http request")
            else:
                logger.error("%s", e)
        except Exception:
            logger.exception("Unhandle Error")

    def version_string(self):
        return self.server_version

    def log_error(self, format, *args):
        self.log_message(format, *args, error=True)

    def log_message(self, format, *args, **kw):
        if kw.get("error"):
            logger.warn("%s %s" % (self.address_string(), format % args))
        else:
            logger.info("%s %s" % (self.address_string(), format % args))

    def do_GET(self):  # noqa
        if self.path.startswith("/ws/"):
            klass, kwargs = get_match_ws_service(self.path[4:])

            if klass:
                self.serve_websocket(klass, kwargs)
            else:
                logger.exception("Websocket route error: %s" % self.path[4:])
                self.response_404()

        elif self.path == "/":
            return self.serve_assets("index.html")
        elif self.path.startswith("/api"):
            try:
                hostname = os.environ.get("proxy_api_host")
                print("Proxying %s" % hostname)
                url = 'http://{}{}'.format(hostname, self.path)
                req = Request(url=url)
                req_headers = self.headers.items()
                for header, value in req_headers:
                    if str(header).startswith("Host"):
                        continue
                    if str(header).startswith("Accept-Encoding"):
                        continue
                    req.add_header(header, value)
                try:
                    resp = urlopen(req)
                except urllib.error.HTTPError as e:
                    if e.getcode():
                        resp = e
                    else:
                        self.send_error(599, u'error proxying: {}'.format(unicode(e)))
                        return
                self.send_response(resp.getcode())
                respheaders = resp.getheaders()
                for header, value in respheaders:
                    if str(header).startswith("Transfer-Encoding"):
                        continue
                    self.send_header(header, value)
                self.end_headers()
                resp_content = resp.read(4192)
                while resp_content:
                    self.wfile.write(resp_content)
                    resp_content = resp.read(4192)
                self.wfile.flush()
            except IOError as e:
                self.send_error(404, 'error trying to proxy: {}'.format(str(e)))
        else:
            #self.send_response(200)
            #self.end_headers()
            #self.wfile.write('sadvd'.encode('utf-8'))
{'fraction_non_alphanumeric': 0.1039554476652863, 'fraction_numerical': 0.007996572897329716, 'mean_word_length': 1.6876438986953184, 'pattern_counts': {'":': 2, '<': 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': '740537', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1857, 'n_words': 412}