root / pithos / api / functions.py @ 15a96c3e
History | View | Annotate | Download (50.1 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
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.
|
15 |
#
|
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.
|
28 |
#
|
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.
|
33 |
|
34 |
from xml.dom import minidom |
35 |
|
36 |
from django.conf import settings |
37 |
from django.http import HttpResponse |
38 |
from django.template.loader import render_to_string |
39 |
from django.utils import simplejson as json |
40 |
from django.utils.http import parse_etags |
41 |
from django.utils.encoding import smart_str |
42 |
from django.views.decorators.csrf import csrf_exempt |
43 |
|
44 |
from pithos.lib.filter import parse_filters |
45 |
|
46 |
from pithos.api.faults import (Fault, NotModified, BadRequest, Unauthorized, Forbidden, ItemNotFound, Conflict, |
47 |
LengthRequired, PreconditionFailed, RequestEntityTooLarge, RangeNotSatisfiable, UnprocessableEntity) |
48 |
from pithos.api.util import (json_encode_decimal, rename_meta_key, format_header_key, printable_header_dict, |
49 |
get_account_headers, put_account_headers, get_container_headers, put_container_headers, get_object_headers, |
50 |
put_object_headers, update_manifest_meta, update_sharing_meta, update_public_meta, |
51 |
validate_modification_preconditions, validate_matching_preconditions, split_container_object_string, |
52 |
copy_or_move_object, get_int_parameter, get_content_length, get_content_range, socket_read_iterator, |
53 |
SaveToBackendHandler, object_data_response, put_object_block, hashmap_md5, simple_list_response, api_method) |
54 |
from pithos.backends.base import NotAllowedError, QuotaError |
55 |
|
56 |
import logging |
57 |
import hashlib |
58 |
|
59 |
|
60 |
logger = logging.getLogger(__name__) |
61 |
|
62 |
|
63 |
@csrf_exempt
|
64 |
def top_demux(request): |
65 |
if request.method == 'GET': |
66 |
if getattr(request, 'user', None) is not None: |
67 |
return account_list(request)
|
68 |
return authenticate(request)
|
69 |
else:
|
70 |
return method_not_allowed(request)
|
71 |
|
72 |
@csrf_exempt
|
73 |
def account_demux(request, v_account): |
74 |
if request.method == 'HEAD': |
75 |
return account_meta(request, v_account)
|
76 |
elif request.method == 'POST': |
77 |
return account_update(request, v_account)
|
78 |
elif request.method == 'GET': |
79 |
return container_list(request, v_account)
|
80 |
else:
|
81 |
return method_not_allowed(request)
|
82 |
|
83 |
@csrf_exempt
|
84 |
def container_demux(request, v_account, v_container): |
85 |
if request.method == 'HEAD': |
86 |
return container_meta(request, v_account, v_container)
|
87 |
elif request.method == 'PUT': |
88 |
return container_create(request, v_account, v_container)
|
89 |
elif request.method == 'POST': |
90 |
return container_update(request, v_account, v_container)
|
91 |
elif request.method == 'DELETE': |
92 |
return container_delete(request, v_account, v_container)
|
93 |
elif request.method == 'GET': |
94 |
return object_list(request, v_account, v_container)
|
95 |
else:
|
96 |
return method_not_allowed(request)
|
97 |
|
98 |
@csrf_exempt
|
99 |
def object_demux(request, v_account, v_container, v_object): |
100 |
if request.method == 'HEAD': |
101 |
return object_meta(request, v_account, v_container, v_object)
|
102 |
elif request.method == 'GET': |
103 |
return object_read(request, v_account, v_container, v_object)
|
104 |
elif request.method == 'PUT': |
105 |
return object_write(request, v_account, v_container, v_object)
|
106 |
elif request.method == 'COPY': |
107 |
return object_copy(request, v_account, v_container, v_object)
|
108 |
elif request.method == 'MOVE': |
109 |
return object_move(request, v_account, v_container, v_object)
|
110 |
elif request.method == 'POST': |
111 |
if request.META.get('CONTENT_TYPE', '').startswith('multipart/form-data'): |
112 |
return object_write_form(request, v_account, v_container, v_object)
|
113 |
return object_update(request, v_account, v_container, v_object)
|
114 |
elif request.method == 'DELETE': |
115 |
return object_delete(request, v_account, v_container, v_object)
|
116 |
else:
|
117 |
return method_not_allowed(request)
|
118 |
|
119 |
@api_method('GET', user_required=False) |
120 |
def authenticate(request): |
121 |
# Normal Response Codes: 204
|
122 |
# Error Response Codes: internalServerError (500),
|
123 |
# forbidden (403),
|
124 |
# badRequest (400)
|
125 |
|
126 |
x_auth_user = request.META.get('HTTP_X_AUTH_USER')
|
127 |
x_auth_key = request.META.get('HTTP_X_AUTH_KEY')
|
128 |
if not x_auth_user or not x_auth_key: |
129 |
raise BadRequest('Missing X-Auth-User or X-Auth-Key header') |
130 |
response = HttpResponse(status=204)
|
131 |
|
132 |
uri = request.build_absolute_uri() |
133 |
if '?' in uri: |
134 |
uri = uri[:uri.find('?')]
|
135 |
|
136 |
response['X-Auth-Token'] = x_auth_key
|
137 |
response['X-Storage-Url'] = uri + ('' if uri.endswith('/') else '/') + x_auth_user |
138 |
return response
|
139 |
|
140 |
@api_method('GET', format_allowed=True) |
141 |
def account_list(request): |
142 |
# Normal Response Codes: 200, 204
|
143 |
# Error Response Codes: internalServerError (500),
|
144 |
# badRequest (400)
|
145 |
|
146 |
response = HttpResponse() |
147 |
|
148 |
marker = request.GET.get('marker')
|
149 |
limit = get_int_parameter(request.GET.get('limit'))
|
150 |
if not limit: |
151 |
limit = 10000
|
152 |
|
153 |
accounts = request.backend.list_accounts(request.user_uniq, marker, limit) |
154 |
|
155 |
if request.serialization == 'text': |
156 |
if len(accounts) == 0: |
157 |
# The cloudfiles python bindings expect 200 if json/xml.
|
158 |
response.status_code = 204
|
159 |
return response
|
160 |
response.status_code = 200
|
161 |
response.content = '\n'.join(accounts) + '\n' |
162 |
return response
|
163 |
|
164 |
account_meta = [] |
165 |
for x in accounts: |
166 |
if x == request.user_uniq:
|
167 |
continue
|
168 |
try:
|
169 |
meta = request.backend.get_account_meta(request.user_uniq, x, 'pithos')
|
170 |
groups = request.backend.get_account_groups(request.user_uniq, x) |
171 |
except NotAllowedError:
|
172 |
raise Forbidden('Not allowed') |
173 |
else:
|
174 |
rename_meta_key(meta, 'modified', 'last_modified') |
175 |
rename_meta_key(meta, 'until_timestamp', 'x_account_until_timestamp') |
176 |
m = dict([(k[15:], v) for k, v in meta.iteritems() if k.startswith('X-Account-Meta-')]) |
177 |
for k in m: |
178 |
del(meta['X-Account-Meta-' + k]) |
179 |
if m:
|
180 |
meta['X-Account-Meta'] = printable_header_dict(m)
|
181 |
if groups:
|
182 |
meta['X-Account-Group'] = printable_header_dict(dict([(k, ','.join(v)) for k, v in groups.iteritems()])) |
183 |
account_meta.append(printable_header_dict(meta)) |
184 |
if request.serialization == 'xml': |
185 |
data = render_to_string('accounts.xml', {'accounts': account_meta}) |
186 |
elif request.serialization == 'json': |
187 |
data = json.dumps(account_meta) |
188 |
response.status_code = 200
|
189 |
response.content = data |
190 |
return response
|
191 |
|
192 |
@api_method('HEAD') |
193 |
def account_meta(request, v_account): |
194 |
# Normal Response Codes: 204
|
195 |
# Error Response Codes: internalServerError (500),
|
196 |
# forbidden (403),
|
197 |
# badRequest (400)
|
198 |
|
199 |
until = get_int_parameter(request.GET.get('until'))
|
200 |
try:
|
201 |
meta = request.backend.get_account_meta(request.user_uniq, v_account, 'pithos', until)
|
202 |
groups = request.backend.get_account_groups(request.user_uniq, v_account) |
203 |
policy = request.backend.get_account_policy(request.user_uniq, v_account) |
204 |
except NotAllowedError:
|
205 |
raise Forbidden('Not allowed') |
206 |
|
207 |
validate_modification_preconditions(request, meta) |
208 |
|
209 |
response = HttpResponse(status=204)
|
210 |
put_account_headers(response, meta, groups, policy) |
211 |
return response
|
212 |
|
213 |
@api_method('POST') |
214 |
def account_update(request, v_account): |
215 |
# Normal Response Codes: 202
|
216 |
# Error Response Codes: internalServerError (500),
|
217 |
# forbidden (403),
|
218 |
# badRequest (400)
|
219 |
|
220 |
meta, groups = get_account_headers(request) |
221 |
replace = True
|
222 |
if 'update' in request.GET: |
223 |
replace = False
|
224 |
if groups:
|
225 |
try:
|
226 |
request.backend.update_account_groups(request.user_uniq, v_account, |
227 |
groups, replace) |
228 |
except NotAllowedError:
|
229 |
raise Forbidden('Not allowed') |
230 |
except ValueError: |
231 |
raise BadRequest('Invalid groups header') |
232 |
if meta or replace: |
233 |
try:
|
234 |
request.backend.update_account_meta(request.user_uniq, v_account, |
235 |
'pithos', meta, replace)
|
236 |
except NotAllowedError:
|
237 |
raise Forbidden('Not allowed') |
238 |
return HttpResponse(status=202) |
239 |
|
240 |
@api_method('GET', format_allowed=True) |
241 |
def container_list(request, v_account): |
242 |
# Normal Response Codes: 200, 204
|
243 |
# Error Response Codes: internalServerError (500),
|
244 |
# itemNotFound (404),
|
245 |
# forbidden (403),
|
246 |
# badRequest (400)
|
247 |
|
248 |
until = get_int_parameter(request.GET.get('until'))
|
249 |
try:
|
250 |
meta = request.backend.get_account_meta(request.user_uniq, v_account, 'pithos', until)
|
251 |
groups = request.backend.get_account_groups(request.user_uniq, v_account) |
252 |
policy = request.backend.get_account_policy(request.user_uniq, v_account) |
253 |
except NotAllowedError:
|
254 |
raise Forbidden('Not allowed') |
255 |
|
256 |
validate_modification_preconditions(request, meta) |
257 |
|
258 |
response = HttpResponse() |
259 |
put_account_headers(response, meta, groups, policy) |
260 |
|
261 |
marker = request.GET.get('marker')
|
262 |
limit = get_int_parameter(request.GET.get('limit'))
|
263 |
if not limit: |
264 |
limit = 10000
|
265 |
|
266 |
shared = False
|
267 |
if 'shared' in request.GET: |
268 |
shared = True
|
269 |
|
270 |
try:
|
271 |
containers = request.backend.list_containers(request.user_uniq, v_account, |
272 |
marker, limit, shared, until) |
273 |
except NotAllowedError:
|
274 |
raise Forbidden('Not allowed') |
275 |
except NameError: |
276 |
containers = [] |
277 |
|
278 |
if request.serialization == 'text': |
279 |
if len(containers) == 0: |
280 |
# The cloudfiles python bindings expect 200 if json/xml.
|
281 |
response.status_code = 204
|
282 |
return response
|
283 |
response.status_code = 200
|
284 |
response.content = '\n'.join(containers) + '\n' |
285 |
return response
|
286 |
|
287 |
container_meta = [] |
288 |
for x in containers: |
289 |
try:
|
290 |
meta = request.backend.get_container_meta(request.user_uniq, v_account, |
291 |
x, 'pithos', until)
|
292 |
policy = request.backend.get_container_policy(request.user_uniq, |
293 |
v_account, x) |
294 |
except NotAllowedError:
|
295 |
raise Forbidden('Not allowed') |
296 |
except NameError: |
297 |
pass
|
298 |
else:
|
299 |
rename_meta_key(meta, 'modified', 'last_modified') |
300 |
rename_meta_key(meta, 'until_timestamp', 'x_container_until_timestamp') |
301 |
m = dict([(k[17:], v) for k, v in meta.iteritems() if k.startswith('X-Container-Meta-')]) |
302 |
for k in m: |
303 |
del(meta['X-Container-Meta-' + k]) |
304 |
if m:
|
305 |
meta['X-Container-Meta'] = printable_header_dict(m)
|
306 |
if policy:
|
307 |
meta['X-Container-Policy'] = printable_header_dict(dict([(k, v) for k, v in policy.iteritems()])) |
308 |
container_meta.append(printable_header_dict(meta)) |
309 |
if request.serialization == 'xml': |
310 |
data = render_to_string('containers.xml', {'account': v_account, 'containers': container_meta}) |
311 |
elif request.serialization == 'json': |
312 |
data = json.dumps(container_meta) |
313 |
response.status_code = 200
|
314 |
response.content = data |
315 |
return response
|
316 |
|
317 |
@api_method('HEAD') |
318 |
def container_meta(request, v_account, v_container): |
319 |
# Normal Response Codes: 204
|
320 |
# Error Response Codes: internalServerError (500),
|
321 |
# itemNotFound (404),
|
322 |
# forbidden (403),
|
323 |
# badRequest (400)
|
324 |
|
325 |
until = get_int_parameter(request.GET.get('until'))
|
326 |
try:
|
327 |
meta = request.backend.get_container_meta(request.user_uniq, v_account, |
328 |
v_container, 'pithos', until)
|
329 |
meta['object_meta'] = request.backend.list_container_meta(request.user_uniq,
|
330 |
v_account, v_container, 'pithos', until)
|
331 |
policy = request.backend.get_container_policy(request.user_uniq, v_account, |
332 |
v_container) |
333 |
except NotAllowedError:
|
334 |
raise Forbidden('Not allowed') |
335 |
except NameError: |
336 |
raise ItemNotFound('Container does not exist') |
337 |
|
338 |
validate_modification_preconditions(request, meta) |
339 |
|
340 |
response = HttpResponse(status=204)
|
341 |
put_container_headers(request, response, meta, policy) |
342 |
return response
|
343 |
|
344 |
@api_method('PUT') |
345 |
def container_create(request, v_account, v_container): |
346 |
# Normal Response Codes: 201, 202
|
347 |
# Error Response Codes: internalServerError (500),
|
348 |
# itemNotFound (404),
|
349 |
# forbidden (403),
|
350 |
# badRequest (400)
|
351 |
|
352 |
meta, policy = get_container_headers(request) |
353 |
|
354 |
try:
|
355 |
request.backend.put_container(request.user_uniq, v_account, v_container, policy) |
356 |
ret = 201
|
357 |
except NotAllowedError:
|
358 |
raise Forbidden('Not allowed') |
359 |
except ValueError: |
360 |
raise BadRequest('Invalid policy header') |
361 |
except NameError: |
362 |
ret = 202
|
363 |
|
364 |
if ret == 202 and policy: |
365 |
try:
|
366 |
request.backend.update_container_policy(request.user_uniq, v_account, |
367 |
v_container, policy, replace=False)
|
368 |
except NotAllowedError:
|
369 |
raise Forbidden('Not allowed') |
370 |
except NameError: |
371 |
raise ItemNotFound('Container does not exist') |
372 |
except ValueError: |
373 |
raise BadRequest('Invalid policy header') |
374 |
if meta:
|
375 |
try:
|
376 |
request.backend.update_container_meta(request.user_uniq, v_account, |
377 |
v_container, 'pithos', meta, replace=False) |
378 |
except NotAllowedError:
|
379 |
raise Forbidden('Not allowed') |
380 |
except NameError: |
381 |
raise ItemNotFound('Container does not exist') |
382 |
|
383 |
return HttpResponse(status=ret)
|
384 |
|
385 |
@api_method('POST', format_allowed=True) |
386 |
def container_update(request, v_account, v_container): |
387 |
# Normal Response Codes: 202
|
388 |
# Error Response Codes: internalServerError (500),
|
389 |
# itemNotFound (404),
|
390 |
# forbidden (403),
|
391 |
# badRequest (400)
|
392 |
|
393 |
meta, policy = get_container_headers(request) |
394 |
replace = True
|
395 |
if 'update' in request.GET: |
396 |
replace = False
|
397 |
if policy:
|
398 |
try:
|
399 |
request.backend.update_container_policy(request.user_uniq, v_account, |
400 |
v_container, policy, replace) |
401 |
except NotAllowedError:
|
402 |
raise Forbidden('Not allowed') |
403 |
except NameError: |
404 |
raise ItemNotFound('Container does not exist') |
405 |
except ValueError: |
406 |
raise BadRequest('Invalid policy header') |
407 |
if meta or replace: |
408 |
try:
|
409 |
request.backend.update_container_meta(request.user_uniq, v_account, |
410 |
v_container, 'pithos', meta, replace)
|
411 |
except NotAllowedError:
|
412 |
raise Forbidden('Not allowed') |
413 |
except NameError: |
414 |
raise ItemNotFound('Container does not exist') |
415 |
|
416 |
content_length = -1
|
417 |
if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked': |
418 |
content_length = get_int_parameter(request.META.get('CONTENT_LENGTH', 0)) |
419 |
content_type = request.META.get('CONTENT_TYPE')
|
420 |
hashmap = [] |
421 |
if content_type and content_type == 'application/octet-stream' and content_length != 0: |
422 |
for data in socket_read_iterator(request, content_length, |
423 |
request.backend.block_size): |
424 |
# TODO: Raise 408 (Request Timeout) if this takes too long.
|
425 |
# TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
|
426 |
hashmap.append(request.backend.put_block(data)) |
427 |
|
428 |
response = HttpResponse(status=202)
|
429 |
if hashmap:
|
430 |
response.content = simple_list_response(request, hashmap) |
431 |
return response
|
432 |
|
433 |
@api_method('DELETE') |
434 |
def container_delete(request, v_account, v_container): |
435 |
# Normal Response Codes: 204
|
436 |
# Error Response Codes: internalServerError (500),
|
437 |
# conflict (409),
|
438 |
# itemNotFound (404),
|
439 |
# forbidden (403),
|
440 |
# badRequest (400)
|
441 |
|
442 |
until = get_int_parameter(request.GET.get('until'))
|
443 |
try:
|
444 |
request.backend.delete_container(request.user_uniq, v_account, v_container, |
445 |
until) |
446 |
except NotAllowedError:
|
447 |
raise Forbidden('Not allowed') |
448 |
except NameError: |
449 |
raise ItemNotFound('Container does not exist') |
450 |
except IndexError: |
451 |
raise Conflict('Container is not empty') |
452 |
return HttpResponse(status=204) |
453 |
|
454 |
@api_method('GET', format_allowed=True) |
455 |
def object_list(request, v_account, v_container): |
456 |
# Normal Response Codes: 200, 204
|
457 |
# Error Response Codes: internalServerError (500),
|
458 |
# itemNotFound (404),
|
459 |
# forbidden (403),
|
460 |
# badRequest (400)
|
461 |
|
462 |
until = get_int_parameter(request.GET.get('until'))
|
463 |
try:
|
464 |
meta = request.backend.get_container_meta(request.user_uniq, v_account, |
465 |
v_container, 'pithos', until)
|
466 |
meta['object_meta'] = request.backend.list_container_meta(request.user_uniq,
|
467 |
v_account, v_container, 'pithos', until)
|
468 |
policy = request.backend.get_container_policy(request.user_uniq, v_account, |
469 |
v_container) |
470 |
except NotAllowedError:
|
471 |
raise Forbidden('Not allowed') |
472 |
except NameError: |
473 |
raise ItemNotFound('Container does not exist') |
474 |
|
475 |
validate_modification_preconditions(request, meta) |
476 |
|
477 |
response = HttpResponse() |
478 |
put_container_headers(request, response, meta, policy) |
479 |
|
480 |
path = request.GET.get('path')
|
481 |
prefix = request.GET.get('prefix')
|
482 |
delimiter = request.GET.get('delimiter')
|
483 |
|
484 |
# Path overrides prefix and delimiter.
|
485 |
virtual = True
|
486 |
if path:
|
487 |
prefix = path |
488 |
delimiter = '/'
|
489 |
virtual = False
|
490 |
|
491 |
# Naming policy.
|
492 |
if prefix and delimiter: |
493 |
prefix = prefix + delimiter |
494 |
if not prefix: |
495 |
prefix = ''
|
496 |
prefix = prefix.lstrip('/')
|
497 |
|
498 |
marker = request.GET.get('marker')
|
499 |
limit = get_int_parameter(request.GET.get('limit'))
|
500 |
if not limit: |
501 |
limit = 10000
|
502 |
|
503 |
keys = request.GET.get('meta')
|
504 |
if keys:
|
505 |
keys = [smart_str(x.strip()) for x in keys.split(',') if x.strip() != ''] |
506 |
included, excluded, opers = parse_filters(keys) |
507 |
keys = [] |
508 |
keys += [format_header_key('X-Object-Meta-' + x) for x in included] |
509 |
keys += ['!'+format_header_key('X-Object-Meta-' + x) for x in excluded] |
510 |
keys += ['%s%s%s' % (format_header_key('X-Object-Meta-' + k), o, v) for k, o, v in opers] |
511 |
else:
|
512 |
keys = [] |
513 |
|
514 |
shared = False
|
515 |
if 'shared' in request.GET: |
516 |
shared = True
|
517 |
|
518 |
if request.serialization == 'text': |
519 |
try:
|
520 |
objects = request.backend.list_objects(request.user_uniq, v_account, |
521 |
v_container, prefix, delimiter, marker, |
522 |
limit, virtual, 'pithos', keys, shared, until)
|
523 |
except NotAllowedError:
|
524 |
raise Forbidden('Not allowed') |
525 |
except NameError: |
526 |
raise ItemNotFound('Container does not exist') |
527 |
|
528 |
if len(objects) == 0: |
529 |
# The cloudfiles python bindings expect 200 if json/xml.
|
530 |
response.status_code = 204
|
531 |
return response
|
532 |
response.status_code = 200
|
533 |
response.content = '\n'.join([x[0] for x in objects]) + '\n' |
534 |
return response
|
535 |
|
536 |
try:
|
537 |
objects = request.backend.list_object_meta(request.user_uniq, v_account, |
538 |
v_container, prefix, delimiter, marker, |
539 |
limit, virtual, 'pithos', keys, shared, until)
|
540 |
object_permissions = {} |
541 |
object_public = {} |
542 |
if until is None: |
543 |
name_idx = len('/'.join((v_account, v_container, ''))) |
544 |
for x in request.backend.list_object_permissions(request.user_uniq, |
545 |
v_account, v_container, prefix): |
546 |
object = x[name_idx:] |
547 |
object_permissions[object] = request.backend.get_object_permissions(
|
548 |
request.user_uniq, v_account, v_container, object)
|
549 |
for k, v in request.backend.list_object_public(request.user_uniq, |
550 |
v_account, v_container, prefix).iteritems(): |
551 |
object_public[k[name_idx:]] = v |
552 |
except NotAllowedError:
|
553 |
raise Forbidden('Not allowed') |
554 |
except NameError: |
555 |
raise ItemNotFound('Container does not exist') |
556 |
|
557 |
object_meta = [] |
558 |
for meta in objects: |
559 |
if len(meta) == 1: |
560 |
# Virtual objects/directories.
|
561 |
object_meta.append(meta) |
562 |
else:
|
563 |
rename_meta_key(meta, 'hash', 'x_object_hash') # Will be replaced by checksum. |
564 |
rename_meta_key(meta, 'checksum', 'hash') |
565 |
rename_meta_key(meta, 'type', 'content_type') |
566 |
rename_meta_key(meta, 'uuid', 'x_object_uuid') |
567 |
if until is not None and 'modified' in meta: |
568 |
del(meta['modified']) |
569 |
else:
|
570 |
rename_meta_key(meta, 'modified', 'last_modified') |
571 |
rename_meta_key(meta, 'modified_by', 'x_object_modified_by') |
572 |
rename_meta_key(meta, 'version', 'x_object_version') |
573 |
rename_meta_key(meta, 'version_timestamp', 'x_object_version_timestamp') |
574 |
permissions = object_permissions.get(meta['name'], None) |
575 |
if permissions:
|
576 |
update_sharing_meta(request, permissions, v_account, v_container, meta['name'], meta)
|
577 |
public = object_public.get(meta['name'], None) |
578 |
if public:
|
579 |
update_public_meta(public, meta) |
580 |
object_meta.append(printable_header_dict(meta)) |
581 |
if request.serialization == 'xml': |
582 |
data = render_to_string('objects.xml', {'container': v_container, 'objects': object_meta}) |
583 |
elif request.serialization == 'json': |
584 |
data = json.dumps(object_meta, default=json_encode_decimal) |
585 |
response.status_code = 200
|
586 |
response.content = data |
587 |
return response
|
588 |
|
589 |
@api_method('HEAD') |
590 |
def object_meta(request, v_account, v_container, v_object): |
591 |
# Normal Response Codes: 204
|
592 |
# Error Response Codes: internalServerError (500),
|
593 |
# itemNotFound (404),
|
594 |
# forbidden (403),
|
595 |
# badRequest (400)
|
596 |
|
597 |
version = request.GET.get('version')
|
598 |
try:
|
599 |
meta = request.backend.get_object_meta(request.user_uniq, v_account, |
600 |
v_container, v_object, 'pithos', version)
|
601 |
if version is None: |
602 |
permissions = request.backend.get_object_permissions(request.user_uniq, |
603 |
v_account, v_container, v_object) |
604 |
public = request.backend.get_object_public(request.user_uniq, v_account, |
605 |
v_container, v_object) |
606 |
else:
|
607 |
permissions = None
|
608 |
public = None
|
609 |
except NotAllowedError:
|
610 |
raise Forbidden('Not allowed') |
611 |
except NameError: |
612 |
raise ItemNotFound('Object does not exist') |
613 |
except IndexError: |
614 |
raise ItemNotFound('Version does not exist') |
615 |
|
616 |
update_manifest_meta(request, v_account, meta) |
617 |
update_sharing_meta(request, permissions, v_account, v_container, v_object, meta) |
618 |
update_public_meta(public, meta) |
619 |
|
620 |
# Evaluate conditions.
|
621 |
validate_modification_preconditions(request, meta) |
622 |
try:
|
623 |
validate_matching_preconditions(request, meta) |
624 |
except NotModified:
|
625 |
response = HttpResponse(status=304)
|
626 |
response['ETag'] = meta['checksum'] |
627 |
return response
|
628 |
|
629 |
response = HttpResponse(status=200)
|
630 |
put_object_headers(response, meta) |
631 |
return response
|
632 |
|
633 |
@api_method('GET', format_allowed=True) |
634 |
def object_read(request, v_account, v_container, v_object): |
635 |
# Normal Response Codes: 200, 206
|
636 |
# Error Response Codes: internalServerError (500),
|
637 |
# rangeNotSatisfiable (416),
|
638 |
# preconditionFailed (412),
|
639 |
# itemNotFound (404),
|
640 |
# forbidden (403),
|
641 |
# badRequest (400),
|
642 |
# notModified (304)
|
643 |
|
644 |
version = request.GET.get('version')
|
645 |
|
646 |
# Reply with the version list. Do this first, as the object may be deleted.
|
647 |
if version == 'list': |
648 |
if request.serialization == 'text': |
649 |
raise BadRequest('No format specified for version list.') |
650 |
|
651 |
try:
|
652 |
v = request.backend.list_versions(request.user_uniq, v_account, |
653 |
v_container, v_object) |
654 |
except NotAllowedError:
|
655 |
raise Forbidden('Not allowed') |
656 |
d = {'versions': v}
|
657 |
if request.serialization == 'xml': |
658 |
d['object'] = v_object
|
659 |
data = render_to_string('versions.xml', d)
|
660 |
elif request.serialization == 'json': |
661 |
data = json.dumps(d, default=json_encode_decimal) |
662 |
|
663 |
response = HttpResponse(data, status=200)
|
664 |
response['Content-Length'] = len(data) |
665 |
return response
|
666 |
|
667 |
try:
|
668 |
meta = request.backend.get_object_meta(request.user_uniq, v_account, |
669 |
v_container, v_object, 'pithos', version)
|
670 |
if version is None: |
671 |
permissions = request.backend.get_object_permissions(request.user_uniq, |
672 |
v_account, v_container, v_object) |
673 |
public = request.backend.get_object_public(request.user_uniq, v_account, |
674 |
v_container, v_object) |
675 |
else:
|
676 |
permissions = None
|
677 |
public = None
|
678 |
except NotAllowedError:
|
679 |
raise Forbidden('Not allowed') |
680 |
except NameError: |
681 |
raise ItemNotFound('Object does not exist') |
682 |
except IndexError: |
683 |
raise ItemNotFound('Version does not exist') |
684 |
|
685 |
update_manifest_meta(request, v_account, meta) |
686 |
update_sharing_meta(request, permissions, v_account, v_container, v_object, meta) |
687 |
update_public_meta(public, meta) |
688 |
|
689 |
# Evaluate conditions.
|
690 |
validate_modification_preconditions(request, meta) |
691 |
try:
|
692 |
validate_matching_preconditions(request, meta) |
693 |
except NotModified:
|
694 |
response = HttpResponse(status=304)
|
695 |
response['ETag'] = meta['checksum'] |
696 |
return response
|
697 |
|
698 |
sizes = [] |
699 |
hashmaps = [] |
700 |
if 'X-Object-Manifest' in meta: |
701 |
try:
|
702 |
src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest']) |
703 |
objects = request.backend.list_objects(request.user_uniq, v_account, |
704 |
src_container, prefix=src_name, virtual=False)
|
705 |
except NotAllowedError:
|
706 |
raise Forbidden('Not allowed') |
707 |
except ValueError: |
708 |
raise BadRequest('Invalid X-Object-Manifest header') |
709 |
except NameError: |
710 |
raise ItemNotFound('Container does not exist') |
711 |
|
712 |
try:
|
713 |
for x in objects: |
714 |
s, h = request.backend.get_object_hashmap(request.user_uniq, |
715 |
v_account, src_container, x[0], x[1]) |
716 |
sizes.append(s) |
717 |
hashmaps.append(h) |
718 |
except NotAllowedError:
|
719 |
raise Forbidden('Not allowed') |
720 |
except NameError: |
721 |
raise ItemNotFound('Object does not exist') |
722 |
except IndexError: |
723 |
raise ItemNotFound('Version does not exist') |
724 |
else:
|
725 |
try:
|
726 |
s, h = request.backend.get_object_hashmap(request.user_uniq, v_account, |
727 |
v_container, v_object, version) |
728 |
sizes.append(s) |
729 |
hashmaps.append(h) |
730 |
except NotAllowedError:
|
731 |
raise Forbidden('Not allowed') |
732 |
except NameError: |
733 |
raise ItemNotFound('Object does not exist') |
734 |
except IndexError: |
735 |
raise ItemNotFound('Version does not exist') |
736 |
|
737 |
# Reply with the hashmap.
|
738 |
if 'hashmap' in request.GET and request.serialization != 'text': |
739 |
size = sum(sizes)
|
740 |
hashmap = sum(hashmaps, [])
|
741 |
d = { |
742 |
'block_size': request.backend.block_size,
|
743 |
'block_hash': request.backend.hash_algorithm,
|
744 |
'bytes': size,
|
745 |
'hashes': hashmap}
|
746 |
if request.serialization == 'xml': |
747 |
d['object'] = v_object
|
748 |
data = render_to_string('hashes.xml', d)
|
749 |
elif request.serialization == 'json': |
750 |
data = json.dumps(d) |
751 |
|
752 |
response = HttpResponse(data, status=200)
|
753 |
put_object_headers(response, meta) |
754 |
response['Content-Length'] = len(data) |
755 |
return response
|
756 |
|
757 |
request.serialization = 'text' # Unset. |
758 |
return object_data_response(request, sizes, hashmaps, meta)
|
759 |
|
760 |
@api_method('PUT', format_allowed=True) |
761 |
def object_write(request, v_account, v_container, v_object): |
762 |
# Normal Response Codes: 201
|
763 |
# Error Response Codes: internalServerError (500),
|
764 |
# unprocessableEntity (422),
|
765 |
# lengthRequired (411),
|
766 |
# conflict (409),
|
767 |
# itemNotFound (404),
|
768 |
# forbidden (403),
|
769 |
# badRequest (400)
|
770 |
|
771 |
# Evaluate conditions.
|
772 |
if request.META.get('HTTP_IF_MATCH') or request.META.get('HTTP_IF_NONE_MATCH'): |
773 |
try:
|
774 |
meta = request.backend.get_object_meta(request.user_uniq, v_account, |
775 |
v_container, v_object, 'pithos')
|
776 |
except NotAllowedError:
|
777 |
raise Forbidden('Not allowed') |
778 |
except NameError: |
779 |
meta = {} |
780 |
validate_matching_preconditions(request, meta) |
781 |
|
782 |
copy_from = request.META.get('HTTP_X_COPY_FROM')
|
783 |
move_from = request.META.get('HTTP_X_MOVE_FROM')
|
784 |
if copy_from or move_from: |
785 |
content_length = get_content_length(request) # Required by the API.
|
786 |
|
787 |
src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
|
788 |
if not src_account: |
789 |
src_account = request.user_uniq |
790 |
if move_from:
|
791 |
try:
|
792 |
src_container, src_name = split_container_object_string(move_from) |
793 |
except ValueError: |
794 |
raise BadRequest('Invalid X-Move-From header') |
795 |
version_id = copy_or_move_object(request, src_account, src_container, src_name, |
796 |
v_account, v_container, v_object, move=True)
|
797 |
else:
|
798 |
try:
|
799 |
src_container, src_name = split_container_object_string(copy_from) |
800 |
except ValueError: |
801 |
raise BadRequest('Invalid X-Copy-From header') |
802 |
version_id = copy_or_move_object(request, src_account, src_container, src_name, |
803 |
v_account, v_container, v_object, move=False)
|
804 |
response = HttpResponse(status=201)
|
805 |
response['X-Object-Version'] = version_id
|
806 |
return response
|
807 |
|
808 |
content_type, meta, permissions, public = get_object_headers(request) |
809 |
content_length = -1
|
810 |
if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked': |
811 |
content_length = get_content_length(request) |
812 |
# Should be BadRequest, but API says otherwise.
|
813 |
if not content_type: |
814 |
raise LengthRequired('Missing Content-Type header') |
815 |
|
816 |
if 'hashmap' in request.GET: |
817 |
if request.serialization not in ('json', 'xml'): |
818 |
raise BadRequest('Invalid hashmap format') |
819 |
|
820 |
data = ''
|
821 |
for block in socket_read_iterator(request, content_length, |
822 |
request.backend.block_size): |
823 |
data = '%s%s' % (data, block)
|
824 |
|
825 |
if request.serialization == 'json': |
826 |
d = json.loads(data) |
827 |
if not hasattr(d, '__getitem__'): |
828 |
raise BadRequest('Invalid data formating') |
829 |
try:
|
830 |
hashmap = d['hashes']
|
831 |
size = int(d['bytes']) |
832 |
except:
|
833 |
raise BadRequest('Invalid data formatting') |
834 |
elif request.serialization == 'xml': |
835 |
try:
|
836 |
xml = minidom.parseString(data) |
837 |
obj = xml.getElementsByTagName('object')[0] |
838 |
size = int(obj.attributes['bytes'].value) |
839 |
|
840 |
hashes = xml.getElementsByTagName('hash')
|
841 |
hashmap = [] |
842 |
for hash in hashes: |
843 |
hashmap.append(hash.firstChild.data)
|
844 |
except:
|
845 |
raise BadRequest('Invalid data formatting') |
846 |
|
847 |
checksum = '' # Do not set to None (will copy previous value). |
848 |
else:
|
849 |
md5 = hashlib.md5() |
850 |
size = 0
|
851 |
hashmap = [] |
852 |
for data in socket_read_iterator(request, content_length, |
853 |
request.backend.block_size): |
854 |
# TODO: Raise 408 (Request Timeout) if this takes too long.
|
855 |
# TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
|
856 |
size += len(data)
|
857 |
hashmap.append(request.backend.put_block(data)) |
858 |
md5.update(data) |
859 |
|
860 |
checksum = md5.hexdigest().lower() |
861 |
etag = request.META.get('HTTP_ETAG')
|
862 |
if etag and parse_etags(etag)[0].lower() != checksum: |
863 |
raise UnprocessableEntity('Object ETag does not match') |
864 |
|
865 |
try:
|
866 |
version_id = request.backend.update_object_hashmap(request.user_uniq, |
867 |
v_account, v_container, v_object, size, content_type, |
868 |
hashmap, checksum, 'pithos', meta, True, permissions) |
869 |
except NotAllowedError:
|
870 |
raise Forbidden('Not allowed') |
871 |
except IndexError, e: |
872 |
raise Conflict(simple_list_response(request, e.data))
|
873 |
except NameError: |
874 |
raise ItemNotFound('Container does not exist') |
875 |
except ValueError: |
876 |
raise BadRequest('Invalid sharing header') |
877 |
except QuotaError:
|
878 |
raise RequestEntityTooLarge('Quota exceeded') |
879 |
if not checksum: |
880 |
# Update the MD5 after the hashmap, as there may be missing hashes.
|
881 |
checksum = hashmap_md5(request, hashmap, size) |
882 |
try:
|
883 |
version_id = request.backend.update_object_checksum(request.user_uniq, |
884 |
v_account, v_container, v_object, version_id, checksum) |
885 |
except NotAllowedError:
|
886 |
raise Forbidden('Not allowed') |
887 |
if public is not None: |
888 |
try:
|
889 |
request.backend.update_object_public(request.user_uniq, v_account, |
890 |
v_container, v_object, public) |
891 |
except NotAllowedError:
|
892 |
raise Forbidden('Not allowed') |
893 |
except NameError: |
894 |
raise ItemNotFound('Object does not exist') |
895 |
|
896 |
response = HttpResponse(status=201)
|
897 |
if checksum:
|
898 |
response['ETag'] = checksum
|
899 |
response['X-Object-Version'] = version_id
|
900 |
return response
|
901 |
|
902 |
@api_method('POST') |
903 |
def object_write_form(request, v_account, v_container, v_object): |
904 |
# Normal Response Codes: 201
|
905 |
# Error Response Codes: internalServerError (500),
|
906 |
# itemNotFound (404),
|
907 |
# forbidden (403),
|
908 |
# badRequest (400)
|
909 |
|
910 |
request.upload_handlers = [SaveToBackendHandler(request)] |
911 |
if not request.FILES.has_key('X-Object-Data'): |
912 |
raise BadRequest('Missing X-Object-Data field') |
913 |
file = request.FILES['X-Object-Data']
|
914 |
|
915 |
checksum = file.etag
|
916 |
try:
|
917 |
version_id = request.backend.update_object_hashmap(request.user_uniq, |
918 |
v_account, v_container, v_object, file.size, file.content_type, |
919 |
file.hashmap, checksum, 'pithos', {}, True) |
920 |
except NotAllowedError:
|
921 |
raise Forbidden('Not allowed') |
922 |
except NameError: |
923 |
raise ItemNotFound('Container does not exist') |
924 |
except QuotaError:
|
925 |
raise RequestEntityTooLarge('Quota exceeded') |
926 |
|
927 |
response = HttpResponse(status=201)
|
928 |
response['ETag'] = checksum
|
929 |
response['X-Object-Version'] = version_id
|
930 |
response.content = checksum |
931 |
return response
|
932 |
|
933 |
@api_method('COPY', format_allowed=True) |
934 |
def object_copy(request, v_account, v_container, v_object): |
935 |
# Normal Response Codes: 201
|
936 |
# Error Response Codes: internalServerError (500),
|
937 |
# itemNotFound (404),
|
938 |
# forbidden (403),
|
939 |
# badRequest (400)
|
940 |
|
941 |
dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
|
942 |
if not dest_account: |
943 |
dest_account = request.user_uniq |
944 |
dest_path = request.META.get('HTTP_DESTINATION')
|
945 |
if not dest_path: |
946 |
raise BadRequest('Missing Destination header') |
947 |
try:
|
948 |
dest_container, dest_name = split_container_object_string(dest_path) |
949 |
except ValueError: |
950 |
raise BadRequest('Invalid Destination header') |
951 |
|
952 |
# Evaluate conditions.
|
953 |
if request.META.get('HTTP_IF_MATCH') or request.META.get('HTTP_IF_NONE_MATCH'): |
954 |
src_version = request.META.get('HTTP_X_SOURCE_VERSION')
|
955 |
try:
|
956 |
meta = request.backend.get_object_meta(request.user_uniq, v_account, |
957 |
v_container, v_object, 'pithos', src_version)
|
958 |
except NotAllowedError:
|
959 |
raise Forbidden('Not allowed') |
960 |
except (NameError, IndexError): |
961 |
raise ItemNotFound('Container or object does not exist') |
962 |
validate_matching_preconditions(request, meta) |
963 |
|
964 |
version_id = copy_or_move_object(request, v_account, v_container, v_object, |
965 |
dest_account, dest_container, dest_name, move=False)
|
966 |
response = HttpResponse(status=201)
|
967 |
response['X-Object-Version'] = version_id
|
968 |
return response
|
969 |
|
970 |
@api_method('MOVE', format_allowed=True) |
971 |
def object_move(request, v_account, v_container, v_object): |
972 |
# Normal Response Codes: 201
|
973 |
# Error Response Codes: internalServerError (500),
|
974 |
# itemNotFound (404),
|
975 |
# forbidden (403),
|
976 |
# badRequest (400)
|
977 |
|
978 |
dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
|
979 |
if not dest_account: |
980 |
dest_account = request.user_uniq |
981 |
dest_path = request.META.get('HTTP_DESTINATION')
|
982 |
if not dest_path: |
983 |
raise BadRequest('Missing Destination header') |
984 |
try:
|
985 |
dest_container, dest_name = split_container_object_string(dest_path) |
986 |
except ValueError: |
987 |
raise BadRequest('Invalid Destination header') |
988 |
|
989 |
# Evaluate conditions.
|
990 |
if request.META.get('HTTP_IF_MATCH') or request.META.get('HTTP_IF_NONE_MATCH'): |
991 |
try:
|
992 |
meta = request.backend.get_object_meta(request.user_uniq, v_account, |
993 |
v_container, v_object, 'pithos')
|
994 |
except NotAllowedError:
|
995 |
raise Forbidden('Not allowed') |
996 |
except NameError: |
997 |
raise ItemNotFound('Container or object does not exist') |
998 |
validate_matching_preconditions(request, meta) |
999 |
|
1000 |
version_id = copy_or_move_object(request, v_account, v_container, v_object, |
1001 |
dest_account, dest_container, dest_name, move=True)
|
1002 |
response = HttpResponse(status=201)
|
1003 |
response['X-Object-Version'] = version_id
|
1004 |
return response
|
1005 |
|
1006 |
@api_method('POST', format_allowed=True) |
1007 |
def object_update(request, v_account, v_container, v_object): |
1008 |
# Normal Response Codes: 202, 204
|
1009 |
# Error Response Codes: internalServerError (500),
|
1010 |
# conflict (409),
|
1011 |
# itemNotFound (404),
|
1012 |
# forbidden (403),
|
1013 |
# badRequest (400)
|
1014 |
|
1015 |
content_type, meta, permissions, public = get_object_headers(request) |
1016 |
|
1017 |
try:
|
1018 |
prev_meta = request.backend.get_object_meta(request.user_uniq, v_account, |
1019 |
v_container, v_object, 'pithos')
|
1020 |
except NotAllowedError:
|
1021 |
raise Forbidden('Not allowed') |
1022 |
except NameError: |
1023 |
raise ItemNotFound('Object does not exist') |
1024 |
|
1025 |
# Evaluate conditions.
|
1026 |
if request.META.get('HTTP_IF_MATCH') or request.META.get('HTTP_IF_NONE_MATCH'): |
1027 |
validate_matching_preconditions(request, prev_meta) |
1028 |
|
1029 |
replace = True
|
1030 |
if 'update' in request.GET: |
1031 |
replace = False
|
1032 |
|
1033 |
# A Content-Type or X-Source-Object header indicates data updates.
|
1034 |
src_object = request.META.get('HTTP_X_SOURCE_OBJECT')
|
1035 |
if (not content_type or content_type != 'application/octet-stream') and not src_object: |
1036 |
response = HttpResponse(status=202)
|
1037 |
|
1038 |
# Do permissions first, as it may fail easier.
|
1039 |
if permissions is not None: |
1040 |
try:
|
1041 |
request.backend.update_object_permissions(request.user_uniq, |
1042 |
v_account, v_container, v_object, permissions) |
1043 |
except NotAllowedError:
|
1044 |
raise Forbidden('Not allowed') |
1045 |
except NameError: |
1046 |
raise ItemNotFound('Object does not exist') |
1047 |
except ValueError: |
1048 |
raise BadRequest('Invalid sharing header') |
1049 |
if public is not None: |
1050 |
try:
|
1051 |
request.backend.update_object_public(request.user_uniq, v_account, |
1052 |
v_container, v_object, public) |
1053 |
except NotAllowedError:
|
1054 |
raise Forbidden('Not allowed') |
1055 |
except NameError: |
1056 |
raise ItemNotFound('Object does not exist') |
1057 |
if meta or replace: |
1058 |
try:
|
1059 |
version_id = request.backend.update_object_meta(request.user_uniq, |
1060 |
v_account, v_container, v_object, 'pithos', meta, replace)
|
1061 |
except NotAllowedError:
|
1062 |
raise Forbidden('Not allowed') |
1063 |
except NameError: |
1064 |
raise ItemNotFound('Object does not exist') |
1065 |
response['X-Object-Version'] = version_id
|
1066 |
|
1067 |
return response
|
1068 |
|
1069 |
# Single range update. Range must be in Content-Range.
|
1070 |
# Based on: http://code.google.com/p/gears/wiki/ContentRangePostProposal
|
1071 |
# (with the addition that '*' is allowed for the range - will append).
|
1072 |
content_range = request.META.get('HTTP_CONTENT_RANGE')
|
1073 |
if not content_range: |
1074 |
raise BadRequest('Missing Content-Range header') |
1075 |
ranges = get_content_range(request) |
1076 |
if not ranges: |
1077 |
raise RangeNotSatisfiable('Invalid Content-Range header') |
1078 |
|
1079 |
try:
|
1080 |
size, hashmap = request.backend.get_object_hashmap(request.user_uniq, |
1081 |
v_account, v_container, v_object) |
1082 |
except NotAllowedError:
|
1083 |
raise Forbidden('Not allowed') |
1084 |
except NameError: |
1085 |
raise ItemNotFound('Object does not exist') |
1086 |
|
1087 |
offset, length, total = ranges |
1088 |
if offset is None: |
1089 |
offset = size |
1090 |
elif offset > size:
|
1091 |
raise RangeNotSatisfiable('Supplied offset is beyond object limits') |
1092 |
if src_object:
|
1093 |
src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
|
1094 |
if not src_account: |
1095 |
src_account = request.user_uniq |
1096 |
src_container, src_name = split_container_object_string(src_object) |
1097 |
src_version = request.META.get('HTTP_X_SOURCE_VERSION')
|
1098 |
try:
|
1099 |
src_size, src_hashmap = request.backend.get_object_hashmap(request.user_uniq, |
1100 |
src_account, src_container, src_name, src_version) |
1101 |
except NotAllowedError:
|
1102 |
raise Forbidden('Not allowed') |
1103 |
except NameError: |
1104 |
raise ItemNotFound('Source object does not exist') |
1105 |
|
1106 |
if length is None: |
1107 |
length = src_size |
1108 |
elif length > src_size:
|
1109 |
raise BadRequest('Object length is smaller than range length') |
1110 |
else:
|
1111 |
# Require either a Content-Length, or 'chunked' Transfer-Encoding.
|
1112 |
content_length = -1
|
1113 |
if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked': |
1114 |
content_length = get_content_length(request) |
1115 |
|
1116 |
if length is None: |
1117 |
length = content_length |
1118 |
else:
|
1119 |
if content_length == -1: |
1120 |
# TODO: Get up to length bytes in chunks.
|
1121 |
length = content_length |
1122 |
elif length != content_length:
|
1123 |
raise BadRequest('Content length does not match range length') |
1124 |
if total is not None and (total != size or offset >= size or (length > 0 and offset + length >= size)): |
1125 |
raise RangeNotSatisfiable('Supplied range will change provided object limits') |
1126 |
|
1127 |
dest_bytes = request.META.get('HTTP_X_OBJECT_BYTES')
|
1128 |
if dest_bytes is not None: |
1129 |
dest_bytes = get_int_parameter(dest_bytes) |
1130 |
if dest_bytes is None: |
1131 |
raise BadRequest('Invalid X-Object-Bytes header') |
1132 |
|
1133 |
if src_object:
|
1134 |
if offset % request.backend.block_size == 0: |
1135 |
# Update the hashes only.
|
1136 |
sbi = 0
|
1137 |
while length > 0: |
1138 |
bi = int(offset / request.backend.block_size)
|
1139 |
bl = min(length, request.backend.block_size)
|
1140 |
if bi < len(hashmap): |
1141 |
if bl == request.backend.block_size:
|
1142 |
hashmap[bi] = src_hashmap[sbi] |
1143 |
else:
|
1144 |
data = request.backend.get_block(src_hashmap[sbi]) |
1145 |
hashmap[bi] = request.backend.update_block(hashmap[bi], |
1146 |
data[:bl], 0)
|
1147 |
else:
|
1148 |
hashmap.append(src_hashmap[sbi]) |
1149 |
offset += bl |
1150 |
length -= bl |
1151 |
sbi += 1
|
1152 |
else:
|
1153 |
data = ''
|
1154 |
sbi = 0
|
1155 |
while length > 0: |
1156 |
data += request.backend.get_block(src_hashmap[sbi]) |
1157 |
if length < request.backend.block_size:
|
1158 |
data = data[:length] |
1159 |
bytes = put_object_block(request, hashmap, data, offset) |
1160 |
offset += bytes
|
1161 |
data = data[bytes:]
|
1162 |
length -= bytes
|
1163 |
sbi += 1
|
1164 |
else:
|
1165 |
data = ''
|
1166 |
for d in socket_read_iterator(request, length, |
1167 |
request.backend.block_size): |
1168 |
# TODO: Raise 408 (Request Timeout) if this takes too long.
|
1169 |
# TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
|
1170 |
data += d |
1171 |
bytes = put_object_block(request, hashmap, data, offset) |
1172 |
offset += bytes
|
1173 |
data = data[bytes:]
|
1174 |
if len(data) > 0: |
1175 |
put_object_block(request, hashmap, data, offset) |
1176 |
|
1177 |
if offset > size:
|
1178 |
size = offset |
1179 |
if dest_bytes is not None and dest_bytes < size: |
1180 |
size = dest_bytes |
1181 |
hashmap = hashmap[:(int((size - 1) / request.backend.block_size) + 1)] |
1182 |
checksum = hashmap_md5(request, hashmap, size) |
1183 |
try:
|
1184 |
version_id = request.backend.update_object_hashmap(request.user_uniq, |
1185 |
v_account, v_container, v_object, size, prev_meta['type'],
|
1186 |
hashmap, checksum, 'pithos', meta, replace, permissions)
|
1187 |
except NotAllowedError:
|
1188 |
raise Forbidden('Not allowed') |
1189 |
except NameError: |
1190 |
raise ItemNotFound('Container does not exist') |
1191 |
except ValueError: |
1192 |
raise BadRequest('Invalid sharing header') |
1193 |
except QuotaError:
|
1194 |
raise RequestEntityTooLarge('Quota exceeded') |
1195 |
if public is not None: |
1196 |
try:
|
1197 |
request.backend.update_object_public(request.user_uniq, v_account, |
1198 |
v_container, v_object, public) |
1199 |
except NotAllowedError:
|
1200 |
raise Forbidden('Not allowed') |
1201 |
except NameError: |
1202 |
raise ItemNotFound('Object does not exist') |
1203 |
|
1204 |
response = HttpResponse(status=204)
|
1205 |
response['ETag'] = checksum
|
1206 |
response['X-Object-Version'] = version_id
|
1207 |
return response
|
1208 |
|
1209 |
@api_method('DELETE') |
1210 |
def object_delete(request, v_account, v_container, v_object): |
1211 |
# Normal Response Codes: 204
|
1212 |
# Error Response Codes: internalServerError (500),
|
1213 |
# itemNotFound (404),
|
1214 |
# forbidden (403),
|
1215 |
# badRequest (400)
|
1216 |
|
1217 |
until = get_int_parameter(request.GET.get('until'))
|
1218 |
try:
|
1219 |
request.backend.delete_object(request.user_uniq, v_account, v_container, |
1220 |
v_object, until) |
1221 |
except NotAllowedError:
|
1222 |
raise Forbidden('Not allowed') |
1223 |
except NameError: |
1224 |
raise ItemNotFound('Object does not exist') |
1225 |
return HttpResponse(status=204) |
1226 |
|
1227 |
@api_method()
|
1228 |
def method_not_allowed(request): |
1229 |
raise BadRequest('Method not allowed') |