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