<commit_before>#
# Copyright (c) 2011 <NAME> <EMAIL>
#
# request.py 07-Jul-2011
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# under the License.
#
try:
    import simplejson as json
except ImportError:
    import json

import tnetstring


class MongrelRequest(object):
    """
    A simple wrapper around the standard `HTTPRequest`.
    """

    def __init__(self, sender, conn_id, path, headers, body):
        self.sender = sender
        self.path = path
        self.conn_id = conn_id
        self.headers = headers
        self.body = body
        
        if self.headers['METHOD'] == 'JSON':
            self.data = json.loads(body)
        else:
            self.data = {}

    @staticmethod
    def parse(msg):
        sender, conn_id, path, rest = msg.split(' ', 3)
        headers, rest = tnetstring.loads(rest)
        body, _ = tnetstring.loads(rest)

        if type(headers) is str:
            headers = json.loads(headers)

        return Request(sender, conn_id, path, headers, body)

    def is_disconnect(self):
        if self.headers.get('METHOD') == 'JSON':
            return self.data['type'] == 'disconnect'

    def should_close(self):
        if self.headers.get('connection') == 'close':
            return True
        elif 'content-length' in self.headers or \
            self.headers.get('METHOD') in ['HEAD', 'GET']:
            return self.headers.get('connection') != 'Kepp-Alive'
        elif self.headers.get('VERSION') == 'HTTP/1.0':
            return True
        else:
            return False
<commit_msg>fixing mongrel request parsing<commit_after>#
# Copyright (c) 2011 <NAME> <EMAIL>
#
# request.py 07-Jul-2011
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# under the License.
#
try:
    import simplejson as json
except ImportError:
    import json

import tnetstring


class MongrelRequest(object):
    """
    A simple wrapper around the standard `HTTPRequest`.
    """

    def __init__(self, sender, conn_id, path, headers, body):
        self.sender = sender
        self.path = path
        self.conn_id = conn_id
        self.headers = headers
        self.body = body
        
        if self.headers['METHOD'] == 'JSON':
            self.data = json.loads(body)
        else:
            self.data = {}

    @staticmethod
    def parse(msg):
        sender, conn_id, path, rest = msg.split(' ', 3)
        headers, rest = tnetstring.pop(rest)
        body, _ = tnetstring.pop(rest)

        if type(headers) is str:
            headers = json.loads(headers)

        return MongrelRequest(sender, conn_id, path, headers, body)

    def is_disconnect(self):
        if self.headers.get('METHOD') == 'JSON':
            return self.data['type'] == 'disconnect'

    def should_close(self):
        if self.headers.get('connection') == 'close':
            return True
        elif 'content-length' in self.headers or \
            self.headers.get('METHOD') in ['HEAD', 'GET']:
            return self.headers.get('connection') != 'Kepp-Alive'
        elif self.headers.get('VERSION') == 'HTTP/1.0':
            return True
        else:
            return False

{'fraction_non_alphanumeric': 0.10943579766536965, 'fraction_numerical': 0.008268482490272374, 'mean_word_length': 2.5153846153846153, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3201345', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1149, 'n_words': 391}