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