<commit_before>import io
import json
import logging
import logging.config
import sys
from urllib.parse import parse_qsl

from cookies_samesite_compat import CookiesSameSiteCompatMiddleware

import satosa
import satosa.logging_util as lu
from .base import SATOSABase
from .context import Context
from .response import ServiceError, NotFound
from .routing import SATOSANoBoundEndpointError
from saml2.s_utils import UnknownSystemEntity

logger = logging.getLogger(__name__)


def unpack_get(environ):
    """
    Unpacks a redirect request query string.
    :param environ: whiskey application environment.
    :return: A dictionary with parameters.
    """
    if "QUERY_STRING" in environ:
        return dict(parse_qsl(environ["QUERY_STRING"]))

    return None


def unpack_post(environ, content_length):
    """
    Unpacks a post request query string.
    :param environ: whiskey application environment.
    :return: A dictionary with parameters.
    """
    post_body = environ['wsgi.input'].read(content_length).decode("utf-8")
    data = None
    if "application/x-www-form-urlencoded" in environ["CONTENT_TYPE"]:
        data = dict(parse_qsl(post_body))
    elif "application/json" in environ["CONTENT_TYPE"]:
        data = json.loads(post_body)

    logline = "unpack_post:: {}".format(data)
    logger.debug(logline)
    return data


def unpack_request(environ, content_length=0):
    """
    Unpacks a get or post request query string.
    :param environ: whiskey application environment.
    :return: A dictionary with parameters.
    """
    data = None
    if environ["REQUEST_METHOD"] == "GET":
        data = unpack_get(environ)
    elif environ["REQUEST_METHOD"] == "POST":
        data = unpack_post(environ, content_length)

    logline = "read request data: {}".format(data)
    logger.debug(logline)<commit_msg>Abstract parsing of the query string as dictionary<commit_after>import io
import json
import logging
import logging.config
import sys
from urllib.parse import parse_qsl as _parse_query_string

from cookies_samesite_compat import CookiesSameSiteCompatMiddleware

import satosa
import satosa.logging_util as lu
from .base import SATOSABase
from .context import Context
from .response import ServiceError, NotFound
from .routing import SATOSANoBoundEndpointError
from saml2.s_utils import UnknownSystemEntity

logger = logging.getLogger(__name__)


def parse_query_string(data):
    query_param_pairs = _parse_query_string(data)
    query_param_dict = dict(query_param_pairs)
    return query_param_dict


def unpack_get(environ):
    """
    Unpacks a redirect request query string.
    :param environ: whiskey application environment.
    :return: A dictionary with parameters.
    """
    return parse_query_string(environ.get("QUERY_STRING"))


def unpack_post(environ, content_length):
    """
    Unpacks a post request query string.
    :param environ: whiskey application environment.
    :return: A dictionary with parameters.
    """
    post_body = environ['wsgi.input'].read(content_length).decode("utf-8")
    data = None
    if "application/x-www-form-urlencoded" in environ["CONTENT_TYPE"]:
        data = parse_query_string(post_body)
    elif "application/json" in environ["CONTENT_TYPE"]:
        data = json.loads(post_body)

    logline = "unpack_post:: {}".format(data)
    logger.debug(logline)
    return data


def unpack_request(environ, content_length=0):
    """
    Unpacks a get or post request query string.
    :param environ: whiskey application environment.
    :return: A dictionary with parameters.
    """
    data = None
    if environ["REQUEST_METHOD"] == "GET":
        data = unpack_get(environ)
    elif environ["REQUEST_METHOD"] == "POST":
        data = unpack_post(environ, content_length)

    logline = "read request data: {}".format(data)
    logger.debug(logline)
{'fraction_non_alphanumeric': 0.09079084287200832, 'fraction_numerical': 0.0015608740894901144, 'mean_word_length': 4.072559366754618, 'pattern_counts': {'":': 4, '<': 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': '894209', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1125, 'n_words': 318}