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