def test_read_request_body(): data = 'content of handle' data_length = len(data) fh = StringIO(data) environ['wsgi.input'] = fh output = read_request_body(environ, data_length) assert output == data output = read_request_body(environ, data_length) assert output == '' fh.close() pytest.raises(HTTP400, 'read_request_body(environ, data_length)')
def _process_request_body(environ, tiddler): """ Read request body to set tiddler content. If a serializer exists for the content type, use it, otherwise treat the content as binary or pseudo-binary tiddler. """ length, content_type = content_length_and_type(environ) content = read_request_body(environ, length) try: try: serialize_type = get_serialize_type(environ)[0] serializer = Serializer(serialize_type, environ) # Short circuit de-serialization attempt to avoid # decoding content multiple times. if hasattr(serializer.serialization, 'as_tiddler'): serializer.object = tiddler try: serializer.from_string(content.decode('utf-8')) except TiddlerFormatError as exc: raise HTTP400('unable to put tiddler: %s' % exc) else: raise NoSerializationError() except NoSerializationError: tiddler.type = content_type if pseudo_binary(tiddler.type): tiddler.text = content.decode('utf-8') else: tiddler.text = content except UnicodeDecodeError as exc: raise HTTP400('unable to decode tiddler, utf-8 expected: %s' % exc)
def put(environ, start_response): """ Put a bag to the server, meaning the description and policy of the bag, if policy allows. """ bag_name = web.get_route_value(environ, 'bag_name') bag_name = web.handle_extension(environ, bag_name) bag = Bag(bag_name) store = environ['tiddlyweb.store'] length, _ = web.content_length_and_type(environ) usersign = environ['tiddlyweb.usersign'] try: bag = store.get(bag) bag.policy.allows(usersign, 'manage') except NoBagError: create_policy_check(environ, 'bag', usersign) try: serialize_type = web.get_serialize_type(environ)[0] serializer = Serializer(serialize_type, environ) serializer.object = bag content = web.read_request_body(environ, length) serializer.from_string(content.decode('utf-8')) bag.policy.owner = usersign['name'] _validate_bag(environ, bag) store.put(bag) except BagFormatError, exc: raise HTTP400('unable to put bag: %s' % exc)
def _process_encodedform(environ): """ Read ``application/x-www-form-urlencoded`` from the request body and parse for form data and return. """ try: length = environ['CONTENT_LENGTH'] content = read_request_body(environ, length) if not ENCODED_QUERY: content = content.decode('UTF-8') except KeyError as exc: raise HTTP400('Invalid post, unable to read content: %s' % exc) return parse_qs(content, keep_blank_values=True)
def put(environ, start_response): """ Handle ``PUT`` on a single recipe URI. Put a :py:class:`recipe <tiddlyweb.model.recipe.Recipe>` to the server, meaning the description, policy and recipe list of the recipe, if :py:class:`policy <tiddlyweb.model.policy.Policy>` allows. """ recipe_name = web.get_route_value(environ, 'recipe_name') recipe_name = web.handle_extension(environ, recipe_name) recipe = Recipe(recipe_name) store = environ['tiddlyweb.store'] length, _ = web.content_length_and_type(environ) usersign = environ['tiddlyweb.usersign'] try: recipe = store.get(recipe) recipe.policy.allows(usersign, 'manage') except NoRecipeError: create_policy_check(environ, 'recipe', usersign) try: serialize_type = web.get_serialize_type(environ)[0] except TypeError: raise HTTP400('Content-type header required') try: serializer = Serializer(serialize_type, environ) serializer.object = recipe content = web.read_request_body(environ, length) serializer.from_string(content.decode('utf-8')) recipe.policy.owner = usersign['name'] _validate_recipe(environ, recipe) store.put(recipe) except RecipeFormatError as exc: raise HTTP400('unable to put recipe: %s' % exc) except TypeError as exc: raise HTTP400('malformed input: %s' % exc) except NoSerializationError: raise HTTP415('Content type %s not supported' % serialize_type) start_response("204 No Content", [('Location', web.recipe_url(environ, recipe))]) return []
def put(environ, start_response): """ Handle ``PUT`` on a single bag URI. Put a :py:class:`bag <tiddlyweb.model.bag.Bag>` to the server, meaning the description and policy of the bag, if :py:class:`policy <tiddlyweb.model.policy.Policy>` allows. """ bag_name = web.get_route_value(environ, 'bag_name') bag_name = web.handle_extension(environ, bag_name) bag = Bag(bag_name) store = environ['tiddlyweb.store'] length, _ = web.content_length_and_type(environ) usersign = environ['tiddlyweb.usersign'] try: bag = store.get(bag) bag.policy.allows(usersign, 'manage') except NoBagError: create_policy_check(environ, 'bag', usersign) try: serialize_type = web.get_serialize_type(environ)[0] serializer = Serializer(serialize_type, environ) serializer.object = bag content = web.read_request_body(environ, length) serializer.from_string(content.decode('utf-8')) bag.policy.owner = usersign['name'] _validate_bag(environ, bag) store.put(bag) except BagFormatError as exc: raise HTTP400('unable to put bag: %s' % exc) except TypeError: raise HTTP400('Content-type header required') except NoSerializationError: raise HTTP415('Content type not supported: %s' % serialize_type) start_response("204 No Content", [('Location', web.bag_url(environ, bag))]) return []
def _process_request_body(environ, tiddler): """ Read request body to set tiddler.text. """ length, content_type = content_length_and_type(environ) content = read_request_body(environ, length) try: try: serialize_type = get_serialize_type(environ)[0] serializer = Serializer(serialize_type, environ) # Short circuit de-serialization attempt to avoid # decoding content multiple times. if hasattr(serializer.serialization, 'as_tiddler'): serializer.object = tiddler try: serializer.from_string(content.decode('utf-8')) except TiddlerFormatError, exc: raise HTTP400('unable to put tiddler: %s' % exc) else: raise NoSerializationError()
def extract_query(self, environ): """ Read the QUERY_STRING and body (if a POSTed form) to extract query paremeters. Put the results in tiddlyweb.query. """ content_type = environ.get('CONTENT_TYPE', '') environ['tiddlyweb.query'] = {} if environ['REQUEST_METHOD'].upper() == 'POST' and \ content_type.startswith('application/x-www-form-urlencoded'): try: length = environ['CONTENT_LENGTH'] content = read_request_body(environ, length) except KeyError, exc: raise HTTP400('Invalid post, unable to read content: %s' % exc) posted_data = parse_qs(content, keep_blank_values=True) try: _update_tiddlyweb_query(environ, posted_data) except UnicodeDecodeError, exc: raise HTTP400( 'Invalid encoding in query string, utf-8 required: %s', exc)
def extract_query(self, environ): """ Read the ``QUERY_STRING`` and body (if a POSTed form) to extract query parameters. Put the results in ``tiddlyweb.query`` in environ. The query names and values are decoded from UTF-8 to unicode. """ content_type = environ.get('CONTENT_TYPE', '') environ['tiddlyweb.query'] = {} if environ['REQUEST_METHOD'].upper() == 'POST' and \ content_type.startswith('application/x-www-form-urlencoded'): try: try: length = environ['CONTENT_LENGTH'] content = read_request_body(environ, length) if not ENCODED_QUERY: content = content.decode('UTF-8') except KeyError as exc: raise HTTP400('Invalid post, unable to read content: %s' % exc) posted_data = parse_qs(content, keep_blank_values=True) _update_tiddlyweb_query(environ, posted_data, encoded=ENCODED_QUERY) except UnicodeDecodeError as exc: raise HTTP400( 'Invalid encoding in query data, utf-8 required: %s', exc) filters, leftovers = parse_for_filters( environ.get('QUERY_STRING', ''), environ) query_data = parse_qs(leftovers, keep_blank_values=True) try: _update_tiddlyweb_query(environ, query_data, encoded=ENCODED_QUERY) except UnicodeDecodeError as exc: raise HTTP400( 'Invalid encoding in query string, utf-8 required: %s', exc) environ['tiddlyweb.filters'] = filters
def put(environ, start_response): """ Put a new recipe to the server. """ recipe_name = web.get_route_value(environ, 'recipe_name') recipe_name = web.handle_extension(environ, recipe_name) recipe = Recipe(recipe_name) store = environ['tiddlyweb.store'] length, _ = web.content_length_and_type(environ) usersign = environ['tiddlyweb.usersign'] try: recipe = store.get(recipe) recipe.policy.allows(usersign, 'manage') except NoRecipeError: create_policy_check(environ, 'recipe', usersign) try: serialize_type = web.get_serialize_type(environ)[0] except TypeError: raise HTTP400('Content-type header required') try: serializer = Serializer(serialize_type, environ) serializer.object = recipe content = web.read_request_body(environ, length) serializer.from_string(content.decode('utf-8')) recipe.policy.owner = usersign['name'] _validate_recipe(environ, recipe) store.put(recipe) except RecipeFormatError, exc: raise HTTP400('unable to put recipe: %s' % exc)