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