def text(self, charset=None, errors=None): """Decode content as a string. """ data = self.content if data is not None: if charset is None: ct = self.headers.get('content-type') if ct: ct, options = parse_options_header(ct) charset = options.get('charset') return data.decode(charset or 'utf-8', errors or 'strict')
def decode_content(self): '''Return the best possible representation of the response body. ''' ct = self.headers.get('content-type') if ct: ct, options = parse_options_header(ct) charset = options.get('charset') if ct in JSON_CONTENT_TYPES: return self.json(charset) elif ct.startswith('text/'): return self.content_string(charset) return self.get_content()
def decode_content(self): """Return the best possible representation of the response body. """ ct = self.headers.get('content-type') if ct: ct, options = parse_options_header(ct) charset = options.get('charset') if ct in JSON_CONTENT_TYPES: return self.json(charset) elif ct.startswith('text/'): return self.text(charset) elif ct == FORM_URL_ENCODED: return parse_qsl(self.content.decode(charset), keep_blank_values=True) return self.content
def parse_form_data(environ, stream=None, **kw): '''Parse form data from an environ dict and return a (forms, files) tuple. Both tuple values are dictionaries with the form-field name as a key (unicode) and lists as values (multiple values per key are possible). The forms-dictionary contains form-field values as unicode strings. The files-dictionary contains :class:`MultipartPart` instances, either because the form-field was a file-upload or the value is to big to fit into memory limits. :parameter environ: A WSGI environment dict. :parameter stream: Optional callable accepting one parameter only, the instance of :class:`FormDecoder` being parsed. If provided, the callable is invoked when data or partial data has been successfully parsed. ''' method = environ.get('REQUEST_METHOD', 'GET').upper() if method not in ENCODE_BODY_METHODS: raise HttpException(status=422) content_type = environ.get('CONTENT_TYPE') if content_type: content_type, options = parse_options_header(content_type) options.update(kw) else: options = kw if content_type == 'multipart/form-data': decoder = MultipartDecoder(environ, options, stream) elif content_type in FORM_ENCODED_TYPES: decoder = UrlEncodedDecoder(environ, options, stream) elif content_type in JSON_CONTENT_TYPES: decoder = JsonDecoder(environ, options, stream) else: decoder = BytesDecoder(environ, options, stream) return decoder.parse()
def parse_form_data(environ, stream=None, **kw): '''Parse form data from an environ dict and return a (forms, files) tuple. Both tuple values are dictionaries with the form-field name as a key (unicode) and lists as values (multiple values per key are possible). The forms-dictionary contains form-field values as unicode strings. The files-dictionary contains :class:`MultipartPart` instances, either because the form-field was a file-upload or the value is to big to fit into memory limits. :parameter environ: A WSGI environment dict. :parameter charset: The charset to use if unsure. (default: utf8) :parameter strict: If True, raise :exc:`MultipartError` on any parsing errors. These are silently ignored by default. :parameter stream_callback: a callback when the content is streamed ''' method = environ.get('REQUEST_METHOD', 'GET').upper() if method not in ENCODE_BODY_METHODS: raise HttpException(status=422) content_type = environ.get('CONTENT_TYPE') if content_type: content_type, options = parse_options_header(content_type) options.update(kw) else: options = kw if content_type == 'multipart/form-data': decoder = MultipartDecoder(environ, options, stream) elif content_type in FORM_ENCODED_TYPES: decoder = UrlEncodedDecoder(environ, options, stream) elif content_type in JSON_CONTENT_TYPES: decoder = JsonDecoder(environ, options, stream) else: decoder = BytesDecoder(environ, options, stream) return decoder.parse()
def content_type_options(self): content_type = self.environ.get('CONTENT_TYPE') if content_type: return parse_options_header(content_type) else: return None, {}
def encoding(self): ct = self.headers.get('content-type') if ct: ct, options = parse_options_header(ct) return options.get('charset')