<commit_before>from __future__ import unicode_literals

import sys
import logging
from threading import Lock

from django import http
from django.core.handlers import base
from django.core import signals
from django.core.urlresolvers import set_script_prefix
from django.utils.functional import cached_property

logger = logging.getLogger('django.request')


class AsgiRequest(http.HttpRequest):
    """
    Custom request subclass that decodes from an ASGI-standard request
    dict, and wraps request body handling.
    """

    def __init__(self, message):
        self.message = message
        self.reply_channel = self.message['reply_channel']
        self._content_length = 0
        # Path info
        self.path = self.message['path']
        self.script_name = self.message.get('root_path', '')
        if self.script_name:
            # TODO: Better is-prefix checking, slash handling?
            self.path_info = self.path[len(self.script_name):]
        else:
            self.path_info = self.path
        # HTTP basics
        self.method = self.message['method'].upper()
        self.META = {
            "REQUEST_METHOD": self.method,
            "QUERY_STRING": self.message.get('query_string', ''),
        }
        if self.message.get('client', None):
            self.META['REMOTE_ADDR'] = self.message['client'][0]
            self.META['REMOTE_HOST'] = self.META['REMOTE_ADDR']
            self.META['REMOTE_PORT'] = self.message['client'][1]
        if self.message.get('server', None):
            self.META['SERVER_NAME'] = self.message['server'][0]
            self.META['SERVER_PORT'] = self.message['server'][1]
        # Headers go into META
        for name, value in self.message.get('headers', {}).items():
            if name == "content_length":
                corrected_name = "CONTENT_LENGTH"
            elif name == "content_type":
                corrected_name = "CONTENT_TYPE"
            else:
                corrected_name = 'HTTP_%s' % name.upper().replace("-", "_")
            self.META[corrected_name] = value
        # Pull out content length info
        if self.META.get('CONTENT_LENGTH', None):
            try:
                self._content_length = int(self.META['CONTENT_LENGTH'])
            except (ValueError, TypeError):
                pass
        # TODO: body handling
        self._body = ""
        # Other bits
        self.resolver_match = None

    @cached_property
    def GET(self):
        return http.QueryDict(
            self.message.get('query_string', ''),
            encoding=self._encoding,
        )

    def _get_post(self):
        if not hasattr(self, '_post'):
            self._load_post_and_files()
        return self._post
<commit_msg>Correct body/POST handling<commit_after>from __future__ import unicode_literals

import sys
import logging
from threading import Lock

from django import http
from django.core.handlers import base
from django.core import signals
from django.core.urlresolvers import set_script_prefix
from django.utils.functional import cached_property

logger = logging.getLogger('django.request')


class AsgiRequest(http.HttpRequest):
    """
    Custom request subclass that decodes from an ASGI-standard request
    dict, and wraps request body handling.
    """

    def __init__(self, message):
        self.message = message
        self.reply_channel = self.message['reply_channel']
        self._content_length = 0
        # Path info
        self.path = self.message['path']
        self.script_name = self.message.get('root_path', '')
        if self.script_name:
            # TODO: Better is-prefix checking, slash handling?
            self.path_info = self.path[len(self.script_name):]
        else:
            self.path_info = self.path
        # HTTP basics
        self.method = self.message['method'].upper()
        self.META = {
            "REQUEST_METHOD": self.method,
            "QUERY_STRING": self.message.get('query_string', ''),
        }
        if self.message.get('client', None):
            self.META['REMOTE_ADDR'] = self.message['client'][0]
            self.META['REMOTE_HOST'] = self.META['REMOTE_ADDR']
            self.META['REMOTE_PORT'] = self.message['client'][1]
        if self.message.get('server', None):
            self.META['SERVER_NAME'] = self.message['server'][0]
            self.META['SERVER_PORT'] = self.message['server'][1]
        # Headers go into META
        for name, value in self.message.get('headers', {}).items():
            if name == "content-length":
                corrected_name = "CONTENT_LENGTH"
            elif name == "content-type":
                corrected_name = "CONTENT_TYPE"
            else:
                corrected_name = 'HTTP_%s' % name.upper().replace("-", "_")
            self.META[corrected_name] = value
        # Pull out content length info
        if self.META.get('CONTENT_LENGTH', None):
            try:
                self._content_length = int(self.META['CONTENT_LENGTH'])
            except (ValueError, TypeError):
                pass
        # Body handling
        self._body = message.get("body", "")
        if message.get("body_channel", None):
            while True:
                # Get the next chunk from the request body channel
                chunk = None
                while chunk is None:
                    _, chunk = message.channel_layer.receive_many(
                        [message['body_channel']],
                        block=True,
                    )
                # Add content to body
                self._body += chunk.get("content", "")
                # Exit loop if this was the last
                if not chunk.get("more_content", False):
                    break
        # Other bits
        self.resolver_match = None

    @cached_property
    def GET(self):
        return http.QueryDict(
            self.message.get('query_string', ''),
            encoding=self._encoding,
        )

    def _get_post(self):
        if not hasattr(self, '_post'):
            self._read_started = False
            self._load_post_and_files()
        return self._post

{'fraction_non_alphanumeric': 0.1116734693877551, 'fraction_numerical': 0.0016326530612244899, 'mean_word_length': 2.078391959798995, 'pattern_counts': {'":': 8, '<': 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': '5243775', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1658, 'n_words': 384}