1 # Copyright 2011-2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
37 from django.conf import settings
38 from django.http import HttpResponse
39 from django.template.loader import render_to_string
40 from django.utils import simplejson as json
41 from django.utils.http import parse_etags
42 from django.utils.encoding import smart_str
43 from xml.dom import minidom
45 from pithos.lib.filter import parse_filters
47 from pithos.api.faults import (Fault, NotModified, BadRequest, Unauthorized, Forbidden, ItemNotFound, Conflict,
48 LengthRequired, PreconditionFailed, RequestEntityTooLarge, RangeNotSatisfiable, UnprocessableEntity)
49 from pithos.api.util import (json_encode_decimal, rename_meta_key, format_header_key, printable_header_dict,
50 get_account_headers, put_account_headers, get_container_headers, put_container_headers, get_object_headers,
51 put_object_headers, update_manifest_meta, update_sharing_meta, update_public_meta,
52 validate_modification_preconditions, validate_matching_preconditions, split_container_object_string,
53 copy_or_move_object, get_int_parameter, get_content_length, get_content_range, socket_read_iterator,
54 SaveToBackendHandler, object_data_response, put_object_block, hashmap_md5, simple_list_response, api_method)
55 from pithos.backends.base import NotAllowedError, QuotaError
58 logger = logging.getLogger(__name__)
61 def top_demux(request):
62 if request.method == 'GET':
63 if getattr(request, 'user', None) is not None:
64 return account_list(request)
65 return authenticate(request)
67 return method_not_allowed(request)
69 def account_demux(request, v_account):
70 if request.method == 'HEAD':
71 return account_meta(request, v_account)
72 elif request.method == 'POST':
73 return account_update(request, v_account)
74 elif request.method == 'GET':
75 return container_list(request, v_account)
77 return method_not_allowed(request)
79 def container_demux(request, v_account, v_container):
80 if request.method == 'HEAD':
81 return container_meta(request, v_account, v_container)
82 elif request.method == 'PUT':
83 return container_create(request, v_account, v_container)
84 elif request.method == 'POST':
85 return container_update(request, v_account, v_container)
86 elif request.method == 'DELETE':
87 return container_delete(request, v_account, v_container)
88 elif request.method == 'GET':
89 return object_list(request, v_account, v_container)
91 return method_not_allowed(request)
93 def object_demux(request, v_account, v_container, v_object):
94 if request.method == 'HEAD':
95 return object_meta(request, v_account, v_container, v_object)
96 elif request.method == 'GET':
97 return object_read(request, v_account, v_container, v_object)
98 elif request.method == 'PUT':
99 return object_write(request, v_account, v_container, v_object)
100 elif request.method == 'COPY':
101 return object_copy(request, v_account, v_container, v_object)
102 elif request.method == 'MOVE':
103 return object_move(request, v_account, v_container, v_object)
104 elif request.method == 'POST':
105 if request.META.get('CONTENT_TYPE', '').startswith('multipart/form-data'):
106 return object_write_form(request, v_account, v_container, v_object)
107 return object_update(request, v_account, v_container, v_object)
108 elif request.method == 'DELETE':
109 return object_delete(request, v_account, v_container, v_object)
111 return method_not_allowed(request)
113 @api_method('GET', user_required=False)
114 def authenticate(request):
115 # Normal Response Codes: 204
116 # Error Response Codes: internalServerError (500),
120 x_auth_user = request.META.get('HTTP_X_AUTH_USER')
121 x_auth_key = request.META.get('HTTP_X_AUTH_KEY')
122 if not x_auth_user or not x_auth_key:
123 raise BadRequest('Missing X-Auth-User or X-Auth-Key header')
124 response = HttpResponse(status=204)
126 uri = request.build_absolute_uri()
128 uri = uri[:uri.find('?')]
130 response['X-Auth-Token'] = x_auth_key
131 response['X-Storage-Url'] = uri + ('' if uri.endswith('/') else '/') + x_auth_user
134 @api_method('GET', format_allowed=True)
135 def account_list(request):
136 # Normal Response Codes: 200, 204
137 # Error Response Codes: internalServerError (500),
140 response = HttpResponse()
142 marker = request.GET.get('marker')
143 limit = get_int_parameter(request.GET.get('limit'))
147 accounts = request.backend.list_accounts(request.user_uniq, marker, limit)
149 if request.serialization == 'text':
150 if len(accounts) == 0:
151 # The cloudfiles python bindings expect 200 if json/xml.
152 response.status_code = 204
154 response.status_code = 200
155 response.content = '\n'.join(accounts) + '\n'
160 if x == request.user_uniq:
163 meta = request.backend.get_account_meta(request.user_uniq, x, 'pithos')
164 groups = request.backend.get_account_groups(request.user_uniq, x)
165 except NotAllowedError:
166 raise Forbidden('Not allowed')
168 rename_meta_key(meta, 'modified', 'last_modified')
169 rename_meta_key(meta, 'until_timestamp', 'x_account_until_timestamp')
170 m = dict([(k[15:], v) for k, v in meta.iteritems() if k.startswith('X-Account-Meta-')])
172 del(meta['X-Account-Meta-' + k])
174 meta['X-Account-Meta'] = printable_header_dict(m)
176 meta['X-Account-Group'] = printable_header_dict(dict([(k, ','.join(v)) for k, v in groups.iteritems()]))
177 account_meta.append(printable_header_dict(meta))
178 if request.serialization == 'xml':
179 data = render_to_string('accounts.xml', {'accounts': account_meta})
180 elif request.serialization == 'json':
181 data = json.dumps(account_meta)
182 response.status_code = 200
183 response.content = data
187 def account_meta(request, v_account):
188 # Normal Response Codes: 204
189 # Error Response Codes: internalServerError (500),
193 until = get_int_parameter(request.GET.get('until'))
195 meta = request.backend.get_account_meta(request.user_uniq, v_account, 'pithos', until)
196 groups = request.backend.get_account_groups(request.user_uniq, v_account)
197 policy = request.backend.get_account_policy(request.user_uniq, v_account)
198 except NotAllowedError:
199 raise Forbidden('Not allowed')
201 validate_modification_preconditions(request, meta)
203 response = HttpResponse(status=204)
204 put_account_headers(response, meta, groups, policy)
208 def account_update(request, v_account):
209 # Normal Response Codes: 202
210 # Error Response Codes: internalServerError (500),
214 meta, groups = get_account_headers(request)
216 if 'update' in request.GET:
220 request.backend.update_account_groups(request.user_uniq, v_account,
222 except NotAllowedError:
223 raise Forbidden('Not allowed')
225 raise BadRequest('Invalid groups header')
228 request.backend.update_account_meta(request.user_uniq, v_account,
229 'pithos', meta, replace)
230 except NotAllowedError:
231 raise Forbidden('Not allowed')
232 return HttpResponse(status=202)
234 @api_method('GET', format_allowed=True)
235 def container_list(request, v_account):
236 # Normal Response Codes: 200, 204
237 # Error Response Codes: internalServerError (500),
238 # itemNotFound (404),
242 until = get_int_parameter(request.GET.get('until'))
244 meta = request.backend.get_account_meta(request.user_uniq, v_account, 'pithos', until)
245 groups = request.backend.get_account_groups(request.user_uniq, v_account)
246 policy = request.backend.get_account_policy(request.user_uniq, v_account)
247 except NotAllowedError:
248 raise Forbidden('Not allowed')
250 validate_modification_preconditions(request, meta)
252 response = HttpResponse()
253 put_account_headers(response, meta, groups, policy)
255 marker = request.GET.get('marker')
256 limit = get_int_parameter(request.GET.get('limit'))
261 if 'shared' in request.GET:
265 containers = request.backend.list_containers(request.user_uniq, v_account,
266 marker, limit, shared, until)
267 except NotAllowedError:
268 raise Forbidden('Not allowed')
272 if request.serialization == 'text':
273 if len(containers) == 0:
274 # The cloudfiles python bindings expect 200 if json/xml.
275 response.status_code = 204
277 response.status_code = 200
278 response.content = '\n'.join(containers) + '\n'
284 meta = request.backend.get_container_meta(request.user_uniq, v_account,
286 policy = request.backend.get_container_policy(request.user_uniq,
288 except NotAllowedError:
289 raise Forbidden('Not allowed')
293 rename_meta_key(meta, 'modified', 'last_modified')
294 rename_meta_key(meta, 'until_timestamp', 'x_container_until_timestamp')
295 m = dict([(k[17:], v) for k, v in meta.iteritems() if k.startswith('X-Container-Meta-')])
297 del(meta['X-Container-Meta-' + k])
299 meta['X-Container-Meta'] = printable_header_dict(m)
301 meta['X-Container-Policy'] = printable_header_dict(dict([(k, v) for k, v in policy.iteritems()]))
302 container_meta.append(printable_header_dict(meta))
303 if request.serialization == 'xml':
304 data = render_to_string('containers.xml', {'account': v_account, 'containers': container_meta})
305 elif request.serialization == 'json':
306 data = json.dumps(container_meta)
307 response.status_code = 200
308 response.content = data
312 def container_meta(request, v_account, v_container):
313 # Normal Response Codes: 204
314 # Error Response Codes: internalServerError (500),
315 # itemNotFound (404),
319 until = get_int_parameter(request.GET.get('until'))
321 meta = request.backend.get_container_meta(request.user_uniq, v_account,
322 v_container, 'pithos', until)
323 meta['object_meta'] = request.backend.list_object_meta(request.user_uniq,
324 v_account, v_container, 'pithos', until)
325 policy = request.backend.get_container_policy(request.user_uniq, v_account,
327 except NotAllowedError:
328 raise Forbidden('Not allowed')
330 raise ItemNotFound('Container does not exist')
332 validate_modification_preconditions(request, meta)
334 response = HttpResponse(status=204)
335 put_container_headers(request, response, meta, policy)
339 def container_create(request, v_account, v_container):
340 # Normal Response Codes: 201, 202
341 # Error Response Codes: internalServerError (500),
342 # itemNotFound (404),
346 meta, policy = get_container_headers(request)
349 request.backend.put_container(request.user_uniq, v_account, v_container, policy)
351 except NotAllowedError:
352 raise Forbidden('Not allowed')
354 raise BadRequest('Invalid policy header')
358 if ret == 202 and policy:
360 request.backend.update_container_policy(request.user_uniq, v_account,
361 v_container, policy, replace=False)
362 except NotAllowedError:
363 raise Forbidden('Not allowed')
365 raise ItemNotFound('Container does not exist')
367 raise BadRequest('Invalid policy header')
370 request.backend.update_container_meta(request.user_uniq, v_account,
371 v_container, 'pithos', meta, replace=False)
372 except NotAllowedError:
373 raise Forbidden('Not allowed')
375 raise ItemNotFound('Container does not exist')
377 return HttpResponse(status=ret)
379 @api_method('POST', format_allowed=True)
380 def container_update(request, v_account, v_container):
381 # Normal Response Codes: 202
382 # Error Response Codes: internalServerError (500),
383 # itemNotFound (404),
387 meta, policy = get_container_headers(request)
389 if 'update' in request.GET:
393 request.backend.update_container_policy(request.user_uniq, v_account,
394 v_container, policy, replace)
395 except NotAllowedError:
396 raise Forbidden('Not allowed')
398 raise ItemNotFound('Container does not exist')
400 raise BadRequest('Invalid policy header')
403 request.backend.update_container_meta(request.user_uniq, v_account,
404 v_container, 'pithos', meta, replace)
405 except NotAllowedError:
406 raise Forbidden('Not allowed')
408 raise ItemNotFound('Container does not exist')
411 if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
412 content_length = get_int_parameter(request.META.get('CONTENT_LENGTH', 0))
413 content_type = request.META.get('CONTENT_TYPE')
415 if content_type and content_type == 'application/octet-stream' and content_length != 0:
416 for data in socket_read_iterator(request, content_length,
417 request.backend.block_size):
418 # TODO: Raise 408 (Request Timeout) if this takes too long.
419 # TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
420 hashmap.append(request.backend.put_block(data))
422 response = HttpResponse(status=202)
424 response.content = simple_list_response(request, hashmap)
427 @api_method('DELETE')
428 def container_delete(request, v_account, v_container):
429 # Normal Response Codes: 204
430 # Error Response Codes: internalServerError (500),
432 # itemNotFound (404),
436 until = get_int_parameter(request.GET.get('until'))
438 request.backend.delete_container(request.user_uniq, v_account, v_container,
440 except NotAllowedError:
441 raise Forbidden('Not allowed')
443 raise ItemNotFound('Container does not exist')
445 raise Conflict('Container is not empty')
446 return HttpResponse(status=204)
448 @api_method('GET', format_allowed=True)
449 def object_list(request, v_account, v_container):
450 # Normal Response Codes: 200, 204
451 # Error Response Codes: internalServerError (500),
452 # itemNotFound (404),
456 until = get_int_parameter(request.GET.get('until'))
458 meta = request.backend.get_container_meta(request.user_uniq, v_account,
459 v_container, 'pithos', until)
460 meta['object_meta'] = request.backend.list_object_meta(request.user_uniq,
461 v_account, v_container, 'pithos', until)
462 policy = request.backend.get_container_policy(request.user_uniq, v_account,
464 except NotAllowedError:
465 raise Forbidden('Not allowed')
467 raise ItemNotFound('Container does not exist')
469 validate_modification_preconditions(request, meta)
471 response = HttpResponse()
472 put_container_headers(request, response, meta, policy)
474 path = request.GET.get('path')
475 prefix = request.GET.get('prefix')
476 delimiter = request.GET.get('delimiter')
478 # Path overrides prefix and delimiter.
486 if prefix and delimiter:
487 prefix = prefix + delimiter
490 prefix = prefix.lstrip('/')
492 marker = request.GET.get('marker')
493 limit = get_int_parameter(request.GET.get('limit'))
497 keys = request.GET.get('meta')
499 keys = [smart_str(x.strip()) for x in keys.split(',') if x.strip() != '']
500 included, excluded, opers = parse_filters(keys)
502 keys += [format_header_key('X-Object-Meta-' + x) for x in included]
503 keys += ['!'+format_header_key('X-Object-Meta-' + x) for x in excluded]
504 keys += ['%s%s%s' % (format_header_key('X-Object-Meta-' + k), o, v) for k, o, v in opers]
509 if 'shared' in request.GET:
513 objects = request.backend.list_objects(request.user_uniq, v_account,
514 v_container, prefix, delimiter, marker,
515 limit, virtual, 'pithos', keys, shared, until)
516 except NotAllowedError:
517 raise Forbidden('Not allowed')
519 raise ItemNotFound('Container does not exist')
521 if request.serialization == 'text':
522 if len(objects) == 0:
523 # The cloudfiles python bindings expect 200 if json/xml.
524 response.status_code = 204
526 response.status_code = 200
527 response.content = '\n'.join([x[0] for x in objects]) + '\n'
533 # Virtual objects/directories.
534 object_meta.append({'subdir': x[0]})
537 meta = request.backend.get_object_meta(request.user_uniq, v_account,
538 v_container, x[0], 'pithos', x[1])
540 permissions = request.backend.get_object_permissions(
541 request.user_uniq, v_account, v_container, x[0])
542 public = request.backend.get_object_public(request.user_uniq,
543 v_account, v_container, x[0])
547 except NotAllowedError:
548 raise Forbidden('Not allowed')
552 rename_meta_key(meta, 'hash', 'x_object_hash') # Will be replaced by ETag.
553 rename_meta_key(meta, 'ETag', 'hash')
554 rename_meta_key(meta, 'uuid', 'x_object_uuid')
555 rename_meta_key(meta, 'modified', 'last_modified')
556 rename_meta_key(meta, 'modified_by', 'x_object_modified_by')
557 rename_meta_key(meta, 'version', 'x_object_version')
558 rename_meta_key(meta, 'version_timestamp', 'x_object_version_timestamp')
559 m = dict([(k[14:], v) for k, v in meta.iteritems() if k.startswith('X-Object-Meta-')])
561 del(meta['X-Object-Meta-' + k])
563 meta['X-Object-Meta'] = printable_header_dict(m)
564 update_sharing_meta(request, permissions, v_account, v_container, x[0], meta)
565 update_public_meta(public, meta)
566 object_meta.append(printable_header_dict(meta))
567 if request.serialization == 'xml':
568 data = render_to_string('objects.xml', {'container': v_container, 'objects': object_meta})
569 elif request.serialization == 'json':
570 data = json.dumps(object_meta, default=json_encode_decimal)
571 response.status_code = 200
572 response.content = data
576 def object_meta(request, v_account, v_container, v_object):
577 # Normal Response Codes: 204
578 # Error Response Codes: internalServerError (500),
579 # itemNotFound (404),
583 version = request.GET.get('version')
585 meta = request.backend.get_object_meta(request.user_uniq, v_account,
586 v_container, v_object, 'pithos', version)
588 permissions = request.backend.get_object_permissions(request.user_uniq,
589 v_account, v_container, v_object)
590 public = request.backend.get_object_public(request.user_uniq, v_account,
591 v_container, v_object)
595 except NotAllowedError:
596 raise Forbidden('Not allowed')
598 raise ItemNotFound('Object does not exist')
600 raise ItemNotFound('Version does not exist')
602 update_manifest_meta(request, v_account, meta)
603 update_sharing_meta(request, permissions, v_account, v_container, v_object, meta)
604 update_public_meta(public, meta)
606 # Evaluate conditions.
607 validate_modification_preconditions(request, meta)
609 validate_matching_preconditions(request, meta)
611 response = HttpResponse(status=304)
612 response['ETag'] = meta['ETag']
615 response = HttpResponse(status=200)
616 put_object_headers(response, meta)
619 @api_method('GET', format_allowed=True)
620 def object_read(request, v_account, v_container, v_object):
621 # Normal Response Codes: 200, 206
622 # Error Response Codes: internalServerError (500),
623 # rangeNotSatisfiable (416),
624 # preconditionFailed (412),
625 # itemNotFound (404),
630 version = request.GET.get('version')
632 # Reply with the version list. Do this first, as the object may be deleted.
633 if version == 'list':
634 if request.serialization == 'text':
635 raise BadRequest('No format specified for version list.')
638 v = request.backend.list_versions(request.user_uniq, v_account,
639 v_container, v_object)
640 except NotAllowedError:
641 raise Forbidden('Not allowed')
643 if request.serialization == 'xml':
644 d['object'] = v_object
645 data = render_to_string('versions.xml', d)
646 elif request.serialization == 'json':
647 data = json.dumps(d, default=json_encode_decimal)
649 response = HttpResponse(data, status=200)
650 response['Content-Length'] = len(data)
654 meta = request.backend.get_object_meta(request.user_uniq, v_account,
655 v_container, v_object, 'pithos', version)
657 permissions = request.backend.get_object_permissions(request.user_uniq,
658 v_account, v_container, v_object)
659 public = request.backend.get_object_public(request.user_uniq, v_account,
660 v_container, v_object)
664 except NotAllowedError:
665 raise Forbidden('Not allowed')
667 raise ItemNotFound('Object does not exist')
669 raise ItemNotFound('Version does not exist')
671 update_manifest_meta(request, v_account, meta)
672 update_sharing_meta(request, permissions, v_account, v_container, v_object, meta)
673 update_public_meta(public, meta)
675 # Evaluate conditions.
676 validate_modification_preconditions(request, meta)
678 validate_matching_preconditions(request, meta)
680 response = HttpResponse(status=304)
681 response['ETag'] = meta['ETag']
686 if 'X-Object-Manifest' in meta:
688 src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest'])
689 objects = request.backend.list_objects(request.user_uniq, v_account,
690 src_container, prefix=src_name, virtual=False)
691 except NotAllowedError:
692 raise Forbidden('Not allowed')
694 raise BadRequest('Invalid X-Object-Manifest header')
696 raise ItemNotFound('Container does not exist')
700 s, h = request.backend.get_object_hashmap(request.user_uniq,
701 v_account, src_container, x[0], x[1])
704 except NotAllowedError:
705 raise Forbidden('Not allowed')
707 raise ItemNotFound('Object does not exist')
709 raise ItemNotFound('Version does not exist')
712 s, h = request.backend.get_object_hashmap(request.user_uniq, v_account,
713 v_container, v_object, version)
716 except NotAllowedError:
717 raise Forbidden('Not allowed')
719 raise ItemNotFound('Object does not exist')
721 raise ItemNotFound('Version does not exist')
723 # Reply with the hashmap.
724 if 'hashmap' in request.GET and request.serialization != 'text':
726 hashmap = sum(hashmaps, [])
728 'block_size': request.backend.block_size,
729 'block_hash': request.backend.hash_algorithm,
732 if request.serialization == 'xml':
733 d['object'] = v_object
734 data = render_to_string('hashes.xml', d)
735 elif request.serialization == 'json':
738 response = HttpResponse(data, status=200)
739 put_object_headers(response, meta)
740 response['Content-Length'] = len(data)
743 request.serialization = 'text' # Unset.
744 return object_data_response(request, sizes, hashmaps, meta)
746 @api_method('PUT', format_allowed=True)
747 def object_write(request, v_account, v_container, v_object):
748 # Normal Response Codes: 201
749 # Error Response Codes: internalServerError (500),
750 # unprocessableEntity (422),
751 # lengthRequired (411),
753 # itemNotFound (404),
757 # Evaluate conditions.
758 if request.META.get('HTTP_IF_MATCH') or request.META.get('HTTP_IF_NONE_MATCH'):
760 meta = request.backend.get_object_meta(request.user_uniq, v_account,
761 v_container, v_object, 'pithos')
762 except NotAllowedError:
763 raise Forbidden('Not allowed')
766 validate_matching_preconditions(request, meta)
768 copy_from = request.META.get('HTTP_X_COPY_FROM')
769 move_from = request.META.get('HTTP_X_MOVE_FROM')
770 if copy_from or move_from:
771 content_length = get_content_length(request) # Required by the API.
773 src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
775 src_account = request.user_uniq
778 src_container, src_name = split_container_object_string(move_from)
780 raise BadRequest('Invalid X-Move-From header')
781 version_id = copy_or_move_object(request, src_account, src_container, src_name,
782 v_account, v_container, v_object, move=True)
785 src_container, src_name = split_container_object_string(copy_from)
787 raise BadRequest('Invalid X-Copy-From header')
788 version_id = copy_or_move_object(request, src_account, src_container, src_name,
789 v_account, v_container, v_object, move=False)
790 response = HttpResponse(status=201)
791 response['X-Object-Version'] = version_id
794 meta, permissions, public = get_object_headers(request)
796 if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
797 content_length = get_content_length(request)
798 # Should be BadRequest, but API says otherwise.
799 if 'Content-Type' not in meta:
800 raise LengthRequired('Missing Content-Type header')
802 if 'hashmap' in request.GET:
803 if request.serialization not in ('json', 'xml'):
804 raise BadRequest('Invalid hashmap format')
807 for block in socket_read_iterator(request, content_length,
808 request.backend.block_size):
809 data = '%s%s' % (data, block)
811 if request.serialization == 'json':
813 if not hasattr(d, '__getitem__'):
814 raise BadRequest('Invalid data formating')
816 hashmap = d['hashes']
817 size = int(d['bytes'])
819 raise BadRequest('Invalid data formatting')
820 elif request.serialization == 'xml':
822 xml = minidom.parseString(data)
823 obj = xml.getElementsByTagName('object')[0]
824 size = int(obj.attributes['bytes'].value)
826 hashes = xml.getElementsByTagName('hash')
829 hashmap.append(hash.firstChild.data)
831 raise BadRequest('Invalid data formatting')
836 for data in socket_read_iterator(request, content_length,
837 request.backend.block_size):
838 # TODO: Raise 408 (Request Timeout) if this takes too long.
839 # TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
841 hashmap.append(request.backend.put_block(data))
844 meta['ETag'] = md5.hexdigest().lower()
845 etag = request.META.get('HTTP_ETAG')
846 if etag and parse_etags(etag)[0].lower() != meta['ETag']:
847 raise UnprocessableEntity('Object ETag does not match')
850 version_id = request.backend.update_object_hashmap(request.user_uniq,
851 v_account, v_container, v_object, size, hashmap,
852 'pithos', meta, True, permissions)
853 except NotAllowedError:
854 raise Forbidden('Not allowed')
855 except IndexError, e:
856 raise Conflict(simple_list_response(request, e.data))
858 raise ItemNotFound('Container does not exist')
860 raise BadRequest('Invalid sharing header')
861 except AttributeError, e:
862 raise Conflict(simple_list_response(request, e.data))
864 raise RequestEntityTooLarge('Quota exceeded')
865 if 'ETag' not in meta:
866 # Update the MD5 after the hashmap, as there may be missing hashes.
867 # TODO: This will create a new version, even if done synchronously...
868 etag = hashmap_md5(request, hashmap, size)
869 meta.update({'ETag': etag}) # Update ETag.
871 version_id = request.backend.update_object_meta(request.user_uniq,
872 v_account, v_container, v_object, 'pithos', {'ETag': etag}, False)
873 except NotAllowedError:
874 raise Forbidden('Not allowed')
875 if public is not None:
877 request.backend.update_object_public(request.user_uniq, v_account,
878 v_container, v_object, public)
879 except NotAllowedError:
880 raise Forbidden('Not allowed')
882 raise ItemNotFound('Object does not exist')
884 response = HttpResponse(status=201)
885 response['ETag'] = meta['ETag']
886 response['X-Object-Version'] = version_id
890 def object_write_form(request, v_account, v_container, v_object):
891 # Normal Response Codes: 201
892 # Error Response Codes: internalServerError (500),
893 # itemNotFound (404),
897 request.upload_handlers = [SaveToBackendHandler(request)]
898 if not request.FILES.has_key('X-Object-Data'):
899 raise BadRequest('Missing X-Object-Data field')
900 file = request.FILES['X-Object-Data']
903 meta['Content-Type'] = file.content_type
904 meta['ETag'] = file.etag
907 version_id = request.backend.update_object_hashmap(request.user_uniq,
908 v_account, v_container, v_object, file.size, file.hashmap,
909 'pithos', meta, True)
910 except NotAllowedError:
911 raise Forbidden('Not allowed')
913 raise ItemNotFound('Container does not exist')
915 raise RequestEntityTooLarge('Quota exceeded')
917 response = HttpResponse(status=201)
918 response['ETag'] = meta['ETag']
919 response['X-Object-Version'] = version_id
922 @api_method('COPY', format_allowed=True)
923 def object_copy(request, v_account, v_container, v_object):
924 # Normal Response Codes: 201
925 # Error Response Codes: internalServerError (500),
926 # itemNotFound (404),
930 dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
932 dest_account = request.user_uniq
933 dest_path = request.META.get('HTTP_DESTINATION')
935 raise BadRequest('Missing Destination header')
937 dest_container, dest_name = split_container_object_string(dest_path)
939 raise BadRequest('Invalid Destination header')
941 # Evaluate conditions.
942 if request.META.get('HTTP_IF_MATCH') or request.META.get('HTTP_IF_NONE_MATCH'):
943 src_version = request.META.get('HTTP_X_SOURCE_VERSION')
945 meta = request.backend.get_object_meta(request.user_uniq, v_account,
946 v_container, v_object, 'pithos', src_version)
947 except NotAllowedError:
948 raise Forbidden('Not allowed')
949 except (NameError, IndexError):
950 raise ItemNotFound('Container or object does not exist')
951 validate_matching_preconditions(request, meta)
953 version_id = copy_or_move_object(request, v_account, v_container, v_object,
954 dest_account, dest_container, dest_name, move=False)
955 response = HttpResponse(status=201)
956 response['X-Object-Version'] = version_id
959 @api_method('MOVE', format_allowed=True)
960 def object_move(request, v_account, v_container, v_object):
961 # Normal Response Codes: 201
962 # Error Response Codes: internalServerError (500),
963 # itemNotFound (404),
967 dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
969 dest_account = request.user_uniq
970 dest_path = request.META.get('HTTP_DESTINATION')
972 raise BadRequest('Missing Destination header')
974 dest_container, dest_name = split_container_object_string(dest_path)
976 raise BadRequest('Invalid Destination header')
978 # Evaluate conditions.
979 if request.META.get('HTTP_IF_MATCH') or request.META.get('HTTP_IF_NONE_MATCH'):
981 meta = request.backend.get_object_meta(request.user_uniq, v_account,
982 v_container, v_object, 'pithos')
983 except NotAllowedError:
984 raise Forbidden('Not allowed')
986 raise ItemNotFound('Container or object does not exist')
987 validate_matching_preconditions(request, meta)
989 version_id = copy_or_move_object(request, v_account, v_container, v_object,
990 dest_account, dest_container, dest_name, move=True)
991 response = HttpResponse(status=201)
992 response['X-Object-Version'] = version_id
995 @api_method('POST', format_allowed=True)
996 def object_update(request, v_account, v_container, v_object):
997 # Normal Response Codes: 202, 204
998 # Error Response Codes: internalServerError (500),
1000 # itemNotFound (404),
1004 meta, permissions, public = get_object_headers(request)
1005 content_type = meta.get('Content-Type')
1007 del(meta['Content-Type']) # Do not allow changing the Content-Type.
1010 prev_meta = request.backend.get_object_meta(request.user_uniq, v_account,
1011 v_container, v_object, 'pithos')
1012 except NotAllowedError:
1013 raise Forbidden('Not allowed')
1015 raise ItemNotFound('Object does not exist')
1017 # Evaluate conditions.
1018 if request.META.get('HTTP_IF_MATCH') or request.META.get('HTTP_IF_NONE_MATCH'):
1019 validate_matching_preconditions(request, prev_meta)
1021 # If replacing, keep previous values of 'Content-Type' and 'ETag'.
1023 if 'update' in request.GET:
1026 for k in ('Content-Type', 'ETag'):
1028 meta[k] = prev_meta[k]
1030 # A Content-Type or X-Source-Object header indicates data updates.
1031 src_object = request.META.get('HTTP_X_SOURCE_OBJECT')
1032 if (not content_type or content_type != 'application/octet-stream') and not src_object:
1033 response = HttpResponse(status=202)
1035 # Do permissions first, as it may fail easier.
1036 if permissions is not None:
1038 request.backend.update_object_permissions(request.user_uniq,
1039 v_account, v_container, v_object, permissions)
1040 except NotAllowedError:
1041 raise Forbidden('Not allowed')
1043 raise ItemNotFound('Object does not exist')
1045 raise BadRequest('Invalid sharing header')
1046 except AttributeError, e:
1047 raise Conflict(simple_list_response(request, e.data))
1048 if public is not None:
1050 request.backend.update_object_public(request.user_uniq, v_account,
1051 v_container, v_object, public)
1052 except NotAllowedError:
1053 raise Forbidden('Not allowed')
1055 raise ItemNotFound('Object does not exist')
1058 version_id = request.backend.update_object_meta(request.user_uniq,
1059 v_account, v_container, v_object, 'pithos', meta, replace)
1060 except NotAllowedError:
1061 raise Forbidden('Not allowed')
1063 raise ItemNotFound('Object does not exist')
1064 response['X-Object-Version'] = version_id
1068 # Single range update. Range must be in Content-Range.
1069 # Based on: http://code.google.com/p/gears/wiki/ContentRangePostProposal
1070 # (with the addition that '*' is allowed for the range - will append).
1071 content_range = request.META.get('HTTP_CONTENT_RANGE')
1072 if not content_range:
1073 raise BadRequest('Missing Content-Range header')
1074 ranges = get_content_range(request)
1076 raise RangeNotSatisfiable('Invalid Content-Range header')
1079 size, hashmap = request.backend.get_object_hashmap(request.user_uniq,
1080 v_account, v_container, v_object)
1081 except NotAllowedError:
1082 raise Forbidden('Not allowed')
1084 raise ItemNotFound('Object does not exist')
1086 offset, length, total = ranges
1090 raise RangeNotSatisfiable('Supplied offset is beyond object limits')
1092 src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
1094 src_account = request.user_uniq
1095 src_container, src_name = split_container_object_string(src_object)
1096 src_version = request.META.get('HTTP_X_SOURCE_VERSION')
1098 src_size, src_hashmap = request.backend.get_object_hashmap(request.user_uniq,
1099 src_account, src_container, src_name, src_version)
1100 except NotAllowedError:
1101 raise Forbidden('Not allowed')
1103 raise ItemNotFound('Source object does not exist')
1107 elif length > src_size:
1108 raise BadRequest('Object length is smaller than range length')
1110 # Require either a Content-Length, or 'chunked' Transfer-Encoding.
1112 if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
1113 content_length = get_content_length(request)
1116 length = content_length
1118 if content_length == -1:
1119 # TODO: Get up to length bytes in chunks.
1120 length = content_length
1121 elif length != content_length:
1122 raise BadRequest('Content length does not match range length')
1123 if total is not None and (total != size or offset >= size or (length > 0 and offset + length >= size)):
1124 raise RangeNotSatisfiable('Supplied range will change provided object limits')
1126 dest_bytes = request.META.get('HTTP_X_OBJECT_BYTES')
1127 if dest_bytes is not None:
1128 dest_bytes = get_int_parameter(dest_bytes)
1129 if dest_bytes is None:
1130 raise BadRequest('Invalid X-Object-Bytes header')
1133 if offset % request.backend.block_size == 0:
1134 # Update the hashes only.
1137 bi = int(offset / request.backend.block_size)
1138 bl = min(length, request.backend.block_size)
1139 if bi < len(hashmap):
1140 if bl == request.backend.block_size:
1141 hashmap[bi] = src_hashmap[sbi]
1143 data = request.backend.get_block(src_hashmap[sbi])
1144 hashmap[bi] = request.backend.update_block(hashmap[bi],
1147 hashmap.append(src_hashmap[sbi])
1155 data += request.backend.get_block(src_hashmap[sbi])
1156 if length < request.backend.block_size:
1157 data = data[:length]
1158 bytes = put_object_block(request, hashmap, data, offset)
1165 for d in socket_read_iterator(request, length,
1166 request.backend.block_size):
1167 # TODO: Raise 408 (Request Timeout) if this takes too long.
1168 # TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
1170 bytes = put_object_block(request, hashmap, data, offset)
1174 put_object_block(request, hashmap, data, offset)
1178 if dest_bytes is not None and dest_bytes < size:
1180 hashmap = hashmap[:(int((size - 1) / request.backend.block_size) + 1)]
1181 meta.update({'ETag': hashmap_md5(request, hashmap, size)}) # Update ETag.
1183 version_id = request.backend.update_object_hashmap(request.user_uniq,
1184 v_account, v_container, v_object, size, hashmap,
1185 'pithos', meta, replace, permissions)
1186 except NotAllowedError:
1187 raise Forbidden('Not allowed')
1189 raise ItemNotFound('Container does not exist')
1191 raise BadRequest('Invalid sharing header')
1192 except AttributeError, e:
1193 raise Conflict(simple_list_response(request, e.data))
1195 raise RequestEntityTooLarge('Quota exceeded')
1196 if public is not None:
1198 request.backend.update_object_public(request.user_uniq, v_account,
1199 v_container, v_object, public)
1200 except NotAllowedError:
1201 raise Forbidden('Not allowed')
1203 raise ItemNotFound('Object does not exist')
1205 response = HttpResponse(status=204)
1206 response['ETag'] = meta['ETag']
1207 response['X-Object-Version'] = version_id
1210 @api_method('DELETE')
1211 def object_delete(request, v_account, v_container, v_object):
1212 # Normal Response Codes: 204
1213 # Error Response Codes: internalServerError (500),
1214 # itemNotFound (404),
1218 until = get_int_parameter(request.GET.get('until'))
1220 request.backend.delete_object(request.user_uniq, v_account, v_container,
1222 except NotAllowedError:
1223 raise Forbidden('Not allowed')
1225 raise ItemNotFound('Object does not exist')
1226 return HttpResponse(status=204)
1229 def method_not_allowed(request):
1230 raise BadRequest('Method not allowed')