def test_limited_iter_reads_fail(self): fap = 'abcdefghij' fap = utils.limiting_iter(fap, 9) for i, chunk in enumerate(fap): if i == 8: break self.assertRaises(exception.ImageSizeLimitExceeded, fap.next)
def test_limited_iter_reads_succeed(self): fap = 'abcdefghij' fap = utils.limiting_iter(fap, 10) bytes_read = 0 for chunk in fap: bytes_read += len(chunk) self.assertEqual(10, bytes_read)
def _deserialize(self, request): result = {} try: result['image_meta'] = utils.get_image_meta_from_headers(request) except exception.Invalid: image_size_str = request.headers['x-image-meta-size'] msg = _("Incoming image size of %s was not convertible to " "an integer.") % image_size_str raise HTTPBadRequest(explanation=msg, request=request) image_meta = result['image_meta'] if request.content_length: image_size = request.content_length elif 'size' in image_meta: image_size = image_meta['size'] else: image_size = None data = request.body_file if self.has_body(request) else None if image_size is None and data is not None: data = utils.limiting_iter(data, CONF.image_size_cap) #NOTE(bcwaldon): this is a hack to make sure the downstream code # gets the correct image data request.body_file = data elif image_size > CONF.image_size_cap: max_image_size = CONF.image_size_cap msg = _("Denying attempt to upload image larger than %d.") LOG.warn(msg % max_image_size) raise HTTPBadRequest(explanation=msg % max_image_size, request=request) result['image_data'] = data return result