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