
from cgi import parse_multipart
from http.cookies import SimpleCookie
from StringIO import StringIO
from urllib.parse import parse_qs

from .utils.functional import buffered_property
#from .utils import multipart

import logging
log = logging.getLogger(__name__)


class Request(object):
    def __init__(self, app, environ):
        self.app = app
        self.environ = environ
        self.path = self.environ.get('PATH_INFO', b'/')
        self.method = self.environ.get('REQUEST_METHOD')

        self.content_type, self.content_params = self.parse_content_type()

    @buffered_property
    def raw_cookies(self):
        '''Raw access to cookies'''
        cookie_data = self.environ('HTTP_COOKIE', '')
        if not cookie_data:
            return {}
        cookies = SimpleCookie()
        cookies.load(cookie_data)
        return cookies

    @buffered_property
    def cookies(self):
        '''Simplified Cookie access'''
        return {
            key: self.raw_cookies[key].value
            for key in self.raw_cookies.keys()
        }

    @buffered_property
    def query_data(self):
        return parse_qs(
            self.environ.get('QUERY_STRING', ''),
            keep_blank_values=True
        )

    @buffered_property
    def body(self):
        size = int(self.environ.get('CONTENT_LENGTH', 0))
        if not size:
            return ''
        return self.environ['wsgi.input'].read(size)

    @buffered_property
    def request_data(self):
        if self.content_type == 'application/x-www-form-urlencoded':
            return parse_qs(self.body)
        # Support multi-part
        elif self.content_type == 'multipart/form-data':
            return parse_multipart(StringIO(self.body), self.content_params['boundary'])
        return {}

    def parse_content_type(self):
        ctype = self.environ.get('CONTENT_TYPE', '')
        content_type, _, params = ctype.partition(';')
        content_params = {}
        for param in params.split(';'):
            k, _, v = param.strip().partition('=')
            content_params[k] = v
        return content_type, content_params

@@ -1,36 +1,15 @@
 %0A
-from cgi import parse_multipart
+import cgi
 %0Afro
@@ -520,32 +520,56 @@
 s = 
-self
+cgi
 .parse_
-content_type(
+header(environ.get('CONTENT_TYPE', '')
 )%0A%0A 
@@ -1681,24 +1681,28 @@
      return 
+cgi.
 parse_multip
@@ -1709,416 +1709,122 @@
 art(
-StringIO(self.body), self.content_params%5B'boundary'%5D)%0A        return %7B%7D%0A%0A    def parse_content_type(self):%0A        ctype = self.environ.get('CONTENT_TYPE', '')%0A        content_type, _, params = ctype.partition(';')%0A        content_params = %7B%7D%0A        for param in params.split(';'):%0A            k, _, v = param.strip().partition('=')%0A            content_params%5Bk%5D = v%0A        return content_type, content_params
+%0A                StringIO(self.body),%0A                self.content_params%5B'boundary'%5D%0A            )%0A        return %7B%7D
 %0A

Use cgi.parse_header