<commit_before>import json
from urllib.parse import parse_qs
from cgi import parse_header


def form_params_middleware(func):
    def wrapper(request):
        ctype, pdict = parse_header(request.headers.get('Content-Type', ''))
        if ctype == "application/x-www-form-urlencoded":
            request.form_params = {}
            for key, value in parse_qs(request.body.decode("utf-8")).items():
                if len(value) == 1:
                    request.form_params[key] = value[0]
                else:
                    request.form_params[key] = value
        return func(request)
    return wrapper

def get_params_middleware(func):
    def wrapper(request):
        request.get_params = {}
        for key, value in parse_qs(request.query_string.decode("utf-8")).items():
            if len(value) == 1:
                request.get_params[key] = value[0]
            else:
                request.get_params[key] = value
        return func(request)
    return wrapper<commit_msg>Improve the params parsing and corresponding renames.<commit_after>import json
import functools

from anillo.utils.common import merge_dicts
from urllib.parse import parse_qs
from cgi import parse_header


def wrap_form_params(func):
    """
    A middleware that parses the url-encoded body and attach
    the result to the request `form_params` attribute.

    This middleware also merges the parsed value with the existing
    `params` attribute in same way as `wrap_query_params` is doing.
    """

    @functools.wraps(func)
    def wrapper(request):
        ctype, pdict = parse_header(request.headers.get('Content-Type', ''))
        if ctype == "application/x-www-form-urlencoded":
            params = {}
            for key, value in parse_qs(request.body.decode("utf-8")).items():
                if len(value) == 1:
                    params[key] = value[0]
                else:
                    params[key] = value

            request.params = merge_dicts(getattr(request, "params", None), params)
            request.form_params = params
        return func(request)
    return wrapper


def wrap_query_params(func):
    """
    A middleware that parses the urlencoded params from the querystring
    and attach it to the request `query_params` attribute.

    This middleware also merges the parsed value with the existing
    `params` attribute in same way as `wrap_form_params` is doing.
    """

    @functools.wraps(func)
    def wrapper(request):
        params = {}
        for key, value in parse_qs(request.query_string.decode("utf-8")).items():
            if len(value) == 1:
                params[key] = value[0]
            else:
                params[key] = value

        request.params = merge_dicts(getattr(request, "params", None), params)
        request.query_params = params
        return func(request)
    return wrapper
{'fraction_non_alphanumeric': 0.09849807893817673, 'fraction_numerical': 0.0041914076143905, 'mean_word_length': 2.3813459268004724, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '747219', 'n_tokens_mistral': 820, 'n_tokens_neox': 791, 'n_words': 219}