Callback for .execute_command() for PATCH/POST/PUT requests

def data_from_payload(self, cmd):
        """
        Callback for .execute_command() for PATCH/POST/PUT requests
        """
        multipart = False
        ckey      = "%s /%s" % (self.command, cmd)

        if not isinstance(self._query_params, dict):
            self._query_params = {}

        if ckey in _NCMD:
            self._cmd = _NCMD[ckey]
        else:
            for key in sorted(_RCMD, key=len, reverse=True):
                if not key.startswith("%s " % self.command):
                    continue

                m = _RCMD[key].name.match(cmd)
                if m:
                    self._cmd = _RCMD[key]
                    self._query_params.update(m.groupdict())
                    break

        try:
            if not self._cmd:
                raise self.req_error(404)

            tenc     = self.headers.get('Transfer-Encoding')
            if tenc and tenc.lower() != 'identity':
                raise self.req_error(501, "Not supported; Transfer-Encoding: %s" % tenc)

            ctype    = self.headers.get('Content-Type')
            if ctype:
                ctype = ctype.lower().split(';', 1)[0]
                if ctype == 'multipart/form-data':
                    if not self._ALLOWED_MULTIPART_FORM:
                        raise self.req_error(501, "Not supported; Content-Type: %s", ctype)
                    multipart = True
                elif self._ALLOWED_CONTENT_TYPES:
                    ct_found = False
                    for x in self._ALLOWED_CONTENT_TYPES:
                        if ctype == x:
                            ct_found = True
                            break
                    if not ct_found:
                        raise self.req_error(501, "Not supported; Content-Type: %s" % ctype)

            try:
                clen = long(self.headers.get('Content-Length') or 0)
            except (ValueError, TypeError):
                raise self.req_error(411)

            if clen < 0:
                raise self.req_error(411)
            elif clen > long(_OPTIONS['max_body_size']):
                raise self.req_error(413)

            if self._cmd.to_auth:
                self.authenticate(self._cmd.auth_users)

            if clen > 0:
                payload       = self.rfile.read(clen)
                self._payload = StringIO(payload)

                if multipart:
                    try:
                        self._payload_params = cgi.FieldStorage(environ = {'REQUEST_METHOD': 'POST'},
                                                                fp      = self._payload,
                                                                headers = self.headers)
                    except Exception, e:
                        raise self.req_error(415, text=str(e))
                else:
                    charset = self._cmd.charset or DEFAULT_CHARSET

                    try:
                        if ctype == 'application/x-www-form-urlencoded':
                            self._payload_params = urlparse.parse_qsl(payload)
                        else:
                            self._payload_params = self.parse_payload(payload, charset)
                    except ValueError, e:
                        raise self.req_error(415, text=str(e))

            res = self._cmd.handler(self)

            if not isinstance(res, HttpResponse):
                return self.response_dumps(res)

            return res
        finally:
            payload                 = None
            self._payload           = None
            self._payload_params    = None
            self._query_params      = {}