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