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