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