<commit_before>import os
import gevent
from gevent import monkey; monkey.patch_all()
from gevent import http as ghttp

import settings

if settings.debug:
    import traceback

from geweb import log
from geweb.http import Request, Response, response_wrappers
from geweb.route import route
from geweb.exceptions import HTTPError, InternalServerError
from geweb.template import render, TemplateNotFound
from geweb.env import env

def _handler(http_request):
    """
    HTTP request handler.
    """
    env.request = Request(http_request)

    try:
        code = 200
        message = 'OK'
        response = route(env.request.path)

    except HTTPError, e:
        code = e.code
        message = e.message

        try:
            response = render('/%d.html' % code, code=code, message=message)
        except TemplateNotFound, e:
            response = render('/50x.html', code=code, message=message)

    except Exception, e:
        code = InternalServerError.code
        message = InternalServerError.message
        if settings.debug:
            response = render('/50x.debug.html', code=code, message=message,
                              trace=traceback.format_exc())
        else:
            response = render('/50x.html', code=code, message=message)

    log.info('%s %d %s' % (env.request.method, code, env.request.uri))

    if isinstance(response, (str, unicode)):
        response = Response(response)
    elif response is None:
        response = Response('')

    for fn in response_wrappers():
        fn(response)

    cookies = response.cookie_out()
    for c in cookies:
        http_request.add_output_header('Set-Cookie', c)

    redirect = response.redirect()
    if redirect:
        http_request.add_output_header('Location', redirect)
        http_request.send_reply(302, 'Moved Temporarily',
                                str('redirect %s' % redirect))
    else:
        http_request.add_output_header("Content-Type", response.mimetype)
        http_request.send_reply(code, message, response.body.encode('utf-8'))

def run_server():
    """
    Start the HTTP server.
    """
    log.info('Starting HTTP server at %s:%d' % settings.server_addr)

    httpd = ghttp.HTTPServer(settings.server_addr,
                             lambda req: gevent.spawn(_handler, req))
    httpd.pre_start()

    for i in xrange(settings.workers - 1):
        pid = gevent.fork()
        if pid == 0:
            break

    try:
        log.info('Starting worker PID=%d' % os.getpid())
        httpd.serve_forever()
    except Exception, e:
        log.error("%s: %s" % (e.__class__.__name__, str(e)))
        httpd.stop()
<commit_msg>Log request time in debug mode.<commit_after>import os
import gevent
from gevent import monkey; monkey.patch_all()
from gevent import http as ghttp

import settings

if settings.debug:
    import traceback
    from time import time

from geweb import log
from geweb.http import Request, Response, response_wrappers
from geweb.route import route
from geweb.exceptions import HTTPError, InternalServerError
from geweb.template import render, TemplateNotFound
from geweb.env import env

def _handler(http_request):
    """
    HTTP request handler.
    """

    if settings.debug:
        tm = time()

    env.request = Request(http_request)

    try:
        code = 200
        message = 'OK'
        response = route(env.request.path)

    except HTTPError, e:
        code = e.code
        message = e.message

        try:
            response = render('/%d.html' % code, code=code, message=message)
        except TemplateNotFound, e:
            response = render('/50x.html', code=code, message=message)

    except Exception, e:
        code = InternalServerError.code
        message = InternalServerError.message
        if settings.debug:
            response = render('/50x.debug.html', code=code, message=message,
                              trace=traceback.format_exc())
        else:
            response = render('/50x.html', code=code, message=message)

    if isinstance(response, (str, unicode)):
        response = Response(response)
    elif response is None:
        response = Response('')

    for fn in response_wrappers():
        fn(response)

    cookies = response.cookie_out()
    for c in cookies:
        http_request.add_output_header('Set-Cookie', c)

    redirect = response.redirect()
    if redirect:
        http_request.add_output_header('Location', redirect)
        http_request.send_reply(302, 'Moved Temporarily',
                                str('redirect %s' % redirect))
    else:
        http_request.add_output_header("Content-Type", response.mimetype)
        http_request.send_reply(code, message, response.body.encode('utf-8'))

    if settings.debug:
        tm = round(time() - tm, 4)
    else:
        tm = ''

    log.info('%s %s %s %d %s' % (tm, env.request.remote_host,
                              env.request.method, code, env.request.uri))

def run_server():
    """
    Start the HTTP server.
    """
    log.info('Starting HTTP server at %s:%d' % settings.server_addr)

    httpd = ghttp.HTTPServer(settings.server_addr,
                             lambda req: gevent.spawn(_handler, req))
    httpd.pre_start()

    for i in xrange(settings.workers - 1):
        pid = gevent.fork()
        if pid == 0:
            break

    try:
        log.info('Starting worker PID=%d' % os.getpid())
        httpd.serve_forever()
    except Exception, e:
        log.error("%s: %s" % (e.__class__.__name__, str(e)))
        httpd.stop()

{'fraction_non_alphanumeric': 0.10870740941049216, 'fraction_numerical': 0.005588606453939066, 'mean_word_length': 2.4120541205412054, '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': '5477506', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1593, 'n_words': 415}