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