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