<commit_before>#
# Copyright (c) 2011 rPath, Inc.
#

import logging
import sys
from mod_python import apache

from mint import config
from mint.lib import mintutils
from mint.lib import profile
from mint import mint_error
from mint import maintenance
from mint.db.repository import RepositoryManager
from mint.helperfuncs import extractBasePath
from mint.logerror import logWebErrorAndEmail
from mint.rest.server import restHandler
from mint.web import app
from mint.web.rpchooks import rpcHandler
from mint.web.catalog import catalogHandler
from mint.web.hooks.conaryhooks import conaryHandler
from mint.web.webhandler import normPath, setCacheControl, HttpError

from conary import dbstore
from conary.lib import coveragehook

log = logging.getLogger(__name__)


# Global cached objects
_cfg = None


class Context(object):
    db = None
    cfg = None
    req = None
    pathInfo = None
    manager = None


def mintHandler(context):
    webfe = app.MintApp(context.req, context.cfg, db=context.db)
    return webfe._handle(context.pathInfo)


urls = (
    ('/changeset/',        conaryHandler),
    ('/conary/',           conaryHandler),
    ('/repos/',            conaryHandler),
    ('/catalog/',          catalogHandler),
    ('/api/',              restHandler),
    ('/xmlrpc/',           rpcHandler),
    ('/jsonrpc/',          rpcHandler),
    ('/xmlrpc-private/',   rpcHandler),
    ('/',                  mintHandler),
)


def handler(req):
    coveragehook.install()
    if not req.hostname:
        return apache.HTTP_BAD_REQUEST

    # Direct logging to httpd error_log.
    mintutils.setupLogging(consoleLevel=logging.INFO, consoleFormat='apache')
    # Silence some noisy third-party components.
    for name in ('stomp.py', 'boto'):
        logging.getLogger(name).setLevel(logging.ERROR)

    context = Context()
    context.req = req

    # only reload the configuration file if it's changed
    # since our last read
    cfgPath = req.get_options().get("rbuilderConfig", config.RBUILDER_CONFIG)

    global _cfg
    if not _cfg:
        _cfg = config.getConfig(cfgPath)
    context.cfg = cfg = _cfg

    if "basePath" not in req.get_options():
        cfg.basePath = extractBasePath(normPath(req.uri),
                normPath(req.path_info))
        pathInfo = req.path_info
    else:
        cfg.basePath = req.get_options()['basePath']
        # chop off the provided base path
        pathInfo = normPath(req.uri[len(cfg.basePath):])
    context.pathInfo = pathInfo

    if not req.uri.startswith(cfg.basePath + 'setup/') and not cfg.configured:
        if req.uri == cfg.basePath + 'pwCheck':
            # allow pwCheck requests to go through without being
            # redirected - they will simply return "False"
            pass
        else:
            req.headers_out['Location'] = cfg.basePath + "setup/"
            raise apache.SERVER_RETURN, apache.HTTP_MOVED_TEMPORARILY

    context.db = db = dbstore.connect(cfg.dbPath, cfg.dbDriver)
    context.manager = RepositoryManager(cfg, db)

    prof = profile.Profile(cfg)
    prof.startHttp(req.uri)

    ret = apache.HTTP_NOT_FOUND
    try:
        for match, urlHandler in urls:
            if pathInfo.startswith(match):
                ret = _tryHandler(urlHandler, context)<commit_msg>Use x-conary-servername header to detect repository requests.<commit_after>#
# Copyright (c) 2011 rPath, Inc.
#

import logging
import sys
from mod_python import apache

from mint import config
from mint.lib import mintutils
from mint.lib import profile
from mint import mint_error
from mint import maintenance
from mint.db.repository import RepositoryManager
from mint.helperfuncs import extractBasePath
from mint.logerror import logWebErrorAndEmail
from mint.rest.server import restHandler
from mint.web import app
from mint.web.rpchooks import rpcHandler
from mint.web.catalog import catalogHandler
from mint.web.hooks.conaryhooks import conaryHandler
from mint.web.webhandler import normPath, setCacheControl, HttpError

from conary import dbstore
from conary.lib import coveragehook

log = logging.getLogger(__name__)


# Global cached objects
_cfg = None


class Context(object):
    db = None
    cfg = None
    req = None
    pathInfo = None
    manager = None


def mintHandler(context):
    webfe = app.MintApp(context.req, context.cfg, db=context.db)
    return webfe._handle(context.pathInfo)


urls = (
    ('/changeset/',        conaryHandler),
    ('/conary/',           conaryHandler),
    ('/repos/',            conaryHandler),
    ('/catalog/',          catalogHandler),
    ('/api/',              restHandler),
    ('/xmlrpc/',           rpcHandler),
    ('/jsonrpc/',          rpcHandler),
    ('/xmlrpc-private/',   rpcHandler),
    ('/',                  mintHandler),
)


def handler(req):
    coveragehook.install()
    if not req.hostname:
        return apache.HTTP_BAD_REQUEST

    # Direct logging to httpd error_log.
    mintutils.setupLogging(consoleLevel=logging.INFO, consoleFormat='apache')
    # Silence some noisy third-party components.
    for name in ('stomp.py', 'boto'):
        logging.getLogger(name).setLevel(logging.ERROR)

    context = Context()
    context.req = req

    # only reload the configuration file if it's changed
    # since our last read
    cfgPath = req.get_options().get("rbuilderConfig", config.RBUILDER_CONFIG)

    global _cfg
    if not _cfg:
        _cfg = config.getConfig(cfgPath)
    context.cfg = cfg = _cfg

    if "basePath" not in req.get_options():
        cfg.basePath = extractBasePath(normPath(req.uri),
                normPath(req.path_info))
        pathInfo = req.path_info
    else:
        cfg.basePath = req.get_options()['basePath']
        # chop off the provided base path
        pathInfo = normPath(req.uri[len(cfg.basePath):])
    context.pathInfo = pathInfo

    if not req.uri.startswith(cfg.basePath + 'setup/') and not cfg.configured:
        if req.uri == cfg.basePath + 'pwCheck':
            # allow pwCheck requests to go through without being
            # redirected - they will simply return "False"
            pass
        else:
            req.headers_out['Location'] = cfg.basePath + "setup/"
            raise apache.SERVER_RETURN, apache.HTTP_MOVED_TEMPORARILY

    context.db = db = dbstore.connect(cfg.dbPath, cfg.dbDriver)
    context.manager = RepositoryManager(cfg, db)

    prof = profile.Profile(cfg)
    prof.startHttp(req.uri)

    ret = apache.HTTP_NOT_FOUND
    try:
        # Proxied Conary requests can have all sorts of paths, so look for a
        # header instead.
        if 'x-conary-servername' in req.headers_in:
            return _tryHandler(conaryHandler, context)
        for match, urlHandler in urls:
            if pathInfo.startswith(match):
                ret = _tryHandler(urlHandler, context)
{'fraction_non_alphanumeric': 0.09606345475910694, 'fraction_numerical': 0.0011750881316098707, 'mean_word_length': 2.7869855394883203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4409560', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1965, 'n_words': 545}