<commit_before>
#
# Copyright 2015 University of Southern California
# Distributed under the Apache License, Version 2.0. See LICENSE for more info.
#

import core

# these modify core.dispatch_rules
import acl
import name
import transfer

urls = list(core.dispatch_rules.items())

# sort longest patterns first where prefixes match
urls.sort(reverse=True)

# flatten list of pairs into one long tuple for web.py
urls = tuple(
    [ item for rule in urls for item in rule ]
)
<commit_msg>add custom URI dispatcher to workaround broken handling of url-escapes<commit_after>
#
# Copyright 2015 University of Southern California
# Distributed under the Apache License, Version 2.0. See LICENSE for more info.
#

import re
import itertools
import web

import core

# these modify core.dispatch_rules
import acl
import name
import transfer

rules = list(core.dispatch_rules.items())

# sort longest patterns first where prefixes match
rules.sort(reverse=True)

# flatten list of pairs into one long tuple for web.py
rules = [
    # anchor pattern to end of string too for full matching
    (re.compile(pattern + (pattern[-1] != '$' and '$' or '')), handler)
    for pattern, handler in rules
]

class Dispatcher (object):
    """Helper class to handle parser-based URL dispatch

       Does what mod_wsgi + web.py should do if the standards around
       url-escaping weren't horribly broken.

    """

    def prepare_dispatch(self):
        """computes web dispatch from REQUEST_URI
        """
        uri = web.ctx.env['REQUEST_URI']
        uribase = web.ctx.env['SCRIPT_NAME']
        assert uri[0:len(uribase)] == uribase
        uri = uri[len(uribase):]

        for pattern, handler in rules:
            m = pattern.match(uri)
            if m:
                return handler(), m.groups()
        raise core.NotFound('%s does not map to any REST API.' % uri)

    def METHOD(self, methodname):
        handler, matchgroups = self.prepare_dispatch()

        if not hasattr(handler, methodname):
            raise rest.NoMethod()

        method = getattr(handler, methodname)
        return method(*matchgroups)

    def HEAD(self):
        return self.METHOD('HEAD')

    def GET(self):
        return self.METHOD('GET')
        
    def PUT(self):
        return self.METHOD('PUT')

    def DELETE(self):
        return self.METHOD('DELETE')

    def POST(self):
        return self.METHOD('POST')


# bypass web.py URI-dispatching because of broken handling of url-escapes!
urls = ('.*', Dispatcher)

{'fraction_non_alphanumeric': 0.09366281387006775, 'fraction_numerical': 0.005579912315663611, 'mean_word_length': 2.977812995245642, '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': '3064560', 'n_tokens_mistral': 735, 'n_tokens_neox': 694, 'n_words': 257}