root / snf-pithos-app / pithos / api / functions.py @ b90584d0
History | View | Annotate | Download (58.8 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from xml.dom import minidom |
35 |
|
36 |
from django.http import HttpResponse |
37 |
from django.template.loader import render_to_string |
38 |
from django.utils import simplejson as json |
39 |
from django.utils.http import parse_etags |
40 |
from django.utils.encoding import smart_str |
41 |
from django.views.decorators.csrf import csrf_exempt |
42 |
|
43 |
from astakosclient import AstakosClient |
44 |
|
45 |
from snf_django.lib import api |
46 |
from snf_django.lib.api import faults |
47 |
|
48 |
from pithos.api.util import ( |
49 |
json_encode_decimal, rename_meta_key, format_header_key, |
50 |
printable_header_dict, get_account_headers, put_account_headers, |
51 |
get_container_headers, put_container_headers, get_object_headers, |
52 |
put_object_headers, update_manifest_meta, update_sharing_meta, |
53 |
update_public_meta, validate_modification_preconditions, |
54 |
validate_matching_preconditions, split_container_object_string, |
55 |
copy_or_move_object, get_int_parameter, get_content_length, |
56 |
get_content_range, socket_read_iterator, SaveToBackendHandler, |
57 |
object_data_response, put_object_block, hashmap_md5, simple_list_response, |
58 |
api_method, is_uuid, |
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 |
lock_container_path=True)
|
976 |
def object_write(request, v_account, v_container, v_object): |
977 |
# Normal Response Codes: 201
|
978 |
# Error Response Codes: internalServerError (500),
|
979 |
# unprocessableEntity (422),
|
980 |
# lengthRequired (411),
|
981 |
# conflict (409),
|
982 |
# itemNotFound (404),
|
983 |
# forbidden (403),
|
984 |
# badRequest (400)
|
985 |
# requestentitytoolarge (413)
|
986 |
|
987 |
# Evaluate conditions.
|
988 |
if (request.META.get('HTTP_IF_MATCH') |
989 |
or request.META.get('HTTP_IF_NONE_MATCH')): |
990 |
try:
|
991 |
meta = request.backend.get_object_meta( |
992 |
request.user_uniq, v_account, |
993 |
v_container, v_object, 'pithos')
|
994 |
except NotAllowedError:
|
995 |
raise faults.Forbidden('Not allowed') |
996 |
except NameError: |
997 |
meta = {} |
998 |
validate_matching_preconditions(request, meta) |
999 |
|
1000 |
copy_from = request.META.get('HTTP_X_COPY_FROM')
|
1001 |
move_from = request.META.get('HTTP_X_MOVE_FROM')
|
1002 |
if copy_from or move_from: |
1003 |
delimiter = request.GET.get('delimiter')
|
1004 |
content_length = get_content_length(request) # Required by the API.
|
1005 |
|
1006 |
src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
|
1007 |
|
1008 |
if not src_account: |
1009 |
src_account = request.user_uniq |
1010 |
else:
|
1011 |
if TRANSLATE_UUIDS:
|
1012 |
try:
|
1013 |
src_account = retrieve_uuid(getattr(request, 'token', None), |
1014 |
src_account) |
1015 |
except ItemNotExists:
|
1016 |
faults.ItemNotFound('Invalid source account')
|
1017 |
|
1018 |
if move_from:
|
1019 |
try:
|
1020 |
src_container, src_name = split_container_object_string( |
1021 |
move_from) |
1022 |
except ValueError: |
1023 |
raise faults.BadRequest('Invalid X-Move-From header') |
1024 |
version_id = copy_or_move_object( |
1025 |
request, src_account, src_container, src_name, |
1026 |
v_account, v_container, v_object, |
1027 |
move=True, delimiter=delimiter)
|
1028 |
else:
|
1029 |
try:
|
1030 |
src_container, src_name = split_container_object_string( |
1031 |
copy_from) |
1032 |
except ValueError: |
1033 |
raise faults.BadRequest('Invalid X-Copy-From header') |
1034 |
version_id = copy_or_move_object( |
1035 |
request, src_account, src_container, src_name, |
1036 |
v_account, v_container, v_object, |
1037 |
move=False, delimiter=delimiter)
|
1038 |
response = HttpResponse(status=201)
|
1039 |
response['X-Object-Version'] = version_id
|
1040 |
return response
|
1041 |
|
1042 |
content_type, meta, permissions, public = get_object_headers(request) |
1043 |
content_length = -1
|
1044 |
if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked': |
1045 |
content_length = get_content_length(request) |
1046 |
# Should be BadRequest, but API says otherwise.
|
1047 |
if content_type is None: |
1048 |
raise faults.LengthRequired('Missing Content-Type header') |
1049 |
|
1050 |
if 'hashmap' in request.GET: |
1051 |
if request.serialization not in ('json', 'xml'): |
1052 |
raise faults.BadRequest('Invalid hashmap format') |
1053 |
|
1054 |
data = ''
|
1055 |
for block in socket_read_iterator(request, content_length, |
1056 |
request.backend.block_size): |
1057 |
data = '%s%s' % (data, block)
|
1058 |
|
1059 |
if request.serialization == 'json': |
1060 |
d = json.loads(data) |
1061 |
if not hasattr(d, '__getitem__'): |
1062 |
raise faults.BadRequest('Invalid data formating') |
1063 |
try:
|
1064 |
hashmap = d['hashes']
|
1065 |
size = int(d['bytes']) |
1066 |
except:
|
1067 |
raise faults.BadRequest('Invalid data formatting') |
1068 |
elif request.serialization == 'xml': |
1069 |
try:
|
1070 |
xml = minidom.parseString(data) |
1071 |
obj = xml.getElementsByTagName('object')[0] |
1072 |
size = int(obj.attributes['bytes'].value) |
1073 |
|
1074 |
hashes = xml.getElementsByTagName('hash')
|
1075 |
hashmap = [] |
1076 |
for hash in hashes: |
1077 |
hashmap.append(hash.firstChild.data)
|
1078 |
except:
|
1079 |
raise faults.BadRequest('Invalid data formatting') |
1080 |
|
1081 |
checksum = '' # Do not set to None (will copy previous value). |
1082 |
else:
|
1083 |
md5 = hashlib.md5() |
1084 |
size = 0
|
1085 |
hashmap = [] |
1086 |
for data in socket_read_iterator(request, content_length, |
1087 |
request.backend.block_size): |
1088 |
# TODO: Raise 408 (Request Timeout) if this takes too long.
|
1089 |
# TODO: Raise 499 (Client Disconnect) if a length is defined
|
1090 |
# and we stop before getting this much data.
|
1091 |
size += len(data)
|
1092 |
hashmap.append(request.backend.put_block(data)) |
1093 |
md5.update(data) |
1094 |
|
1095 |
checksum = md5.hexdigest().lower() |
1096 |
etag = request.META.get('HTTP_ETAG')
|
1097 |
if etag and parse_etags(etag)[0].lower() != checksum: |
1098 |
raise faults.UnprocessableEntity('Object ETag does not match') |
1099 |
|
1100 |
try:
|
1101 |
version_id = \ |
1102 |
request.backend.update_object_hashmap(request.user_uniq, |
1103 |
v_account, v_container, |
1104 |
v_object, size, content_type, |
1105 |
hashmap, checksum, |
1106 |
'pithos', meta, True, |
1107 |
permissions) |
1108 |
except NotAllowedError:
|
1109 |
raise faults.Forbidden('Not allowed') |
1110 |
except IndexError, e: |
1111 |
missing_blocks = e.data |
1112 |
response = HttpResponse(status=409)
|
1113 |
response.content = simple_list_response(request, missing_blocks) |
1114 |
return response
|
1115 |
except ItemNotExists:
|
1116 |
raise faults.ItemNotFound('Container does not exist') |
1117 |
except ValueError: |
1118 |
raise faults.BadRequest('Invalid sharing header') |
1119 |
except QuotaError, e:
|
1120 |
raise faults.RequestEntityTooLarge('Quota error: %s' % e) |
1121 |
if not checksum and UPDATE_MD5: |
1122 |
# Update the MD5 after the hashmap, as there may be missing hashes.
|
1123 |
checksum = hashmap_md5(request.backend, hashmap, size) |
1124 |
try:
|
1125 |
request.backend.update_object_checksum(request.user_uniq, |
1126 |
v_account, v_container, |
1127 |
v_object, version_id, |
1128 |
checksum) |
1129 |
except NotAllowedError:
|
1130 |
raise faults.Forbidden('Not allowed') |
1131 |
if public is not None: |
1132 |
try:
|
1133 |
request.backend.update_object_public(request.user_uniq, v_account, |
1134 |
v_container, v_object, public) |
1135 |
except NotAllowedError:
|
1136 |
raise faults.Forbidden('Not allowed') |
1137 |
except ItemNotExists:
|
1138 |
raise faults.ItemNotFound('Object does not exist') |
1139 |
|
1140 |
response = HttpResponse(status=201)
|
1141 |
if checksum:
|
1142 |
response['ETag'] = checksum
|
1143 |
response['X-Object-Version'] = version_id
|
1144 |
return response
|
1145 |
|
1146 |
|
1147 |
@api_method('POST', user_required=True, logger=logger, lock_container_path=True) |
1148 |
def object_write_form(request, v_account, v_container, v_object): |
1149 |
# Normal Response Codes: 201
|
1150 |
# Error Response Codes: internalServerError (500),
|
1151 |
# itemNotFound (404),
|
1152 |
# forbidden (403),
|
1153 |
# badRequest (400)
|
1154 |
# requestentitytoolarge (413)
|
1155 |
|
1156 |
request.upload_handlers = [SaveToBackendHandler(request)] |
1157 |
if 'X-Object-Data' not in request.FILES: |
1158 |
raise faults.BadRequest('Missing X-Object-Data field') |
1159 |
file = request.FILES['X-Object-Data']
|
1160 |
|
1161 |
checksum = file.etag
|
1162 |
try:
|
1163 |
version_id = \ |
1164 |
request.backend.update_object_hashmap(request.user_uniq, |
1165 |
v_account, v_container, |
1166 |
v_object, file.size,
|
1167 |
file.content_type,
|
1168 |
file.hashmap, checksum,
|
1169 |
'pithos', {}, True) |
1170 |
except NotAllowedError:
|
1171 |
raise faults.Forbidden('Not allowed') |
1172 |
except ItemNotExists:
|
1173 |
raise faults.ItemNotFound('Container does not exist') |
1174 |
except QuotaError, e:
|
1175 |
raise faults.RequestEntityTooLarge('Quota error: %s' % e) |
1176 |
|
1177 |
response = HttpResponse(status=201)
|
1178 |
response['ETag'] = checksum
|
1179 |
response['X-Object-Version'] = version_id
|
1180 |
response.content = checksum |
1181 |
return response
|
1182 |
|
1183 |
|
1184 |
@api_method('COPY', format_allowed=True, user_required=True, logger=logger, |
1185 |
lock_container_path=True)
|
1186 |
def object_copy(request, v_account, v_container, v_object): |
1187 |
# Normal Response Codes: 201
|
1188 |
# Error Response Codes: internalServerError (500),
|
1189 |
# itemNotFound (404),
|
1190 |
# forbidden (403),
|
1191 |
# badRequest (400)
|
1192 |
# requestentitytoolarge (413)
|
1193 |
|
1194 |
dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
|
1195 |
if not dest_account: |
1196 |
dest_account = request.user_uniq |
1197 |
dest_path = request.META.get('HTTP_DESTINATION')
|
1198 |
if not dest_path: |
1199 |
raise faults.BadRequest('Missing Destination header') |
1200 |
try:
|
1201 |
dest_container, dest_name = split_container_object_string(dest_path) |
1202 |
except ValueError: |
1203 |
raise faults.BadRequest('Invalid Destination header') |
1204 |
|
1205 |
# Evaluate conditions.
|
1206 |
if (request.META.get('HTTP_IF_MATCH') |
1207 |
or request.META.get('HTTP_IF_NONE_MATCH')): |
1208 |
src_version = request.META.get('HTTP_X_SOURCE_VERSION')
|
1209 |
try:
|
1210 |
meta = request.backend.get_object_meta( |
1211 |
request.user_uniq, v_account, |
1212 |
v_container, v_object, 'pithos', src_version)
|
1213 |
except NotAllowedError:
|
1214 |
raise faults.Forbidden('Not allowed') |
1215 |
except (ItemNotExists, VersionNotExists):
|
1216 |
raise faults.ItemNotFound('Container or object does not exist') |
1217 |
validate_matching_preconditions(request, meta) |
1218 |
|
1219 |
delimiter = request.GET.get('delimiter')
|
1220 |
|
1221 |
version_id = copy_or_move_object(request, v_account, v_container, v_object, |
1222 |
dest_account, dest_container, dest_name, |
1223 |
move=False, delimiter=delimiter)
|
1224 |
response = HttpResponse(status=201)
|
1225 |
response['X-Object-Version'] = version_id
|
1226 |
return response
|
1227 |
|
1228 |
|
1229 |
@api_method('MOVE', format_allowed=True, user_required=True, logger=logger, |
1230 |
lock_container_path=True)
|
1231 |
def object_move(request, v_account, v_container, v_object): |
1232 |
# Normal Response Codes: 201
|
1233 |
# Error Response Codes: internalServerError (500),
|
1234 |
# itemNotFound (404),
|
1235 |
# forbidden (403),
|
1236 |
# badRequest (400)
|
1237 |
# requestentitytoolarge (413)
|
1238 |
|
1239 |
dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
|
1240 |
if not dest_account: |
1241 |
dest_account = request.user_uniq |
1242 |
dest_path = request.META.get('HTTP_DESTINATION')
|
1243 |
if not dest_path: |
1244 |
raise faults.BadRequest('Missing Destination header') |
1245 |
try:
|
1246 |
dest_container, dest_name = split_container_object_string(dest_path) |
1247 |
except ValueError: |
1248 |
raise faults.BadRequest('Invalid Destination header') |
1249 |
|
1250 |
# Evaluate conditions.
|
1251 |
if (request.META.get('HTTP_IF_MATCH') |
1252 |
or request.META.get('HTTP_IF_NONE_MATCH')): |
1253 |
try:
|
1254 |
meta = request.backend.get_object_meta( |
1255 |
request.user_uniq, v_account, |
1256 |
v_container, v_object, 'pithos')
|
1257 |
except NotAllowedError:
|
1258 |
raise faults.Forbidden('Not allowed') |
1259 |
except ItemNotExists:
|
1260 |
raise faults.ItemNotFound('Container or object does not exist') |
1261 |
validate_matching_preconditions(request, meta) |
1262 |
|
1263 |
delimiter = request.GET.get('delimiter')
|
1264 |
|
1265 |
version_id = copy_or_move_object(request, v_account, v_container, v_object, |
1266 |
dest_account, dest_container, dest_name, |
1267 |
move=True, delimiter=delimiter)
|
1268 |
response = HttpResponse(status=201)
|
1269 |
response['X-Object-Version'] = version_id
|
1270 |
return response
|
1271 |
|
1272 |
|
1273 |
@api_method('POST', format_allowed=True, user_required=True, logger=logger, |
1274 |
lock_container_path=True)
|
1275 |
def object_update(request, v_account, v_container, v_object): |
1276 |
# Normal Response Codes: 202, 204
|
1277 |
# Error Response Codes: internalServerError (500),
|
1278 |
# conflict (409),
|
1279 |
# itemNotFound (404),
|
1280 |
# forbidden (403),
|
1281 |
# badRequest (400)
|
1282 |
|
1283 |
content_type, meta, permissions, public = get_object_headers(request) |
1284 |
|
1285 |
try:
|
1286 |
prev_meta = request.backend.get_object_meta( |
1287 |
request.user_uniq, v_account, |
1288 |
v_container, v_object, 'pithos')
|
1289 |
except NotAllowedError:
|
1290 |
raise faults.Forbidden('Not allowed') |
1291 |
except ItemNotExists:
|
1292 |
raise faults.ItemNotFound('Object does not exist') |
1293 |
|
1294 |
# Evaluate conditions.
|
1295 |
if (request.META.get('HTTP_IF_MATCH') |
1296 |
or request.META.get('HTTP_IF_NONE_MATCH')): |
1297 |
validate_matching_preconditions(request, prev_meta) |
1298 |
|
1299 |
replace = True
|
1300 |
if 'update' in request.GET: |
1301 |
replace = False
|
1302 |
|
1303 |
# A Content-Type or X-Source-Object header indicates data updates.
|
1304 |
src_object = request.META.get('HTTP_X_SOURCE_OBJECT')
|
1305 |
if ((not content_type or content_type != 'application/octet-stream') |
1306 |
and not src_object): |
1307 |
response = HttpResponse(status=202)
|
1308 |
|
1309 |
# Do permissions first, as it may fail easier.
|
1310 |
if permissions is not None: |
1311 |
try:
|
1312 |
request.backend.update_object_permissions(request.user_uniq, |
1313 |
v_account, |
1314 |
v_container, v_object, |
1315 |
permissions) |
1316 |
except NotAllowedError:
|
1317 |
raise faults.Forbidden('Not allowed') |
1318 |
except ItemNotExists:
|
1319 |
raise faults.ItemNotFound('Object does not exist') |
1320 |
except ValueError: |
1321 |
raise faults.BadRequest('Invalid sharing header') |
1322 |
if public is not None: |
1323 |
try:
|
1324 |
request.backend.update_object_public( |
1325 |
request.user_uniq, v_account, |
1326 |
v_container, v_object, public) |
1327 |
except NotAllowedError:
|
1328 |
raise faults.Forbidden('Not allowed') |
1329 |
except ItemNotExists:
|
1330 |
raise faults.ItemNotFound('Object does not exist') |
1331 |
if meta or replace: |
1332 |
try:
|
1333 |
version_id = request.backend.update_object_meta( |
1334 |
request.user_uniq, |
1335 |
v_account, v_container, v_object, 'pithos', meta, replace)
|
1336 |
except NotAllowedError:
|
1337 |
raise faults.Forbidden('Not allowed') |
1338 |
except ItemNotExists:
|
1339 |
raise faults.ItemNotFound('Object does not exist') |
1340 |
response['X-Object-Version'] = version_id
|
1341 |
|
1342 |
return response
|
1343 |
|
1344 |
# Single range update. Range must be in Content-Range.
|
1345 |
# Based on: http://code.google.com/p/gears/wiki/ContentRangePostProposal
|
1346 |
# (with the addition that '*' is allowed for the range - will append).
|
1347 |
content_range = request.META.get('HTTP_CONTENT_RANGE')
|
1348 |
if not content_range: |
1349 |
raise faults.BadRequest('Missing Content-Range header') |
1350 |
ranges = get_content_range(request) |
1351 |
if not ranges: |
1352 |
raise faults.RangeNotSatisfiable('Invalid Content-Range header') |
1353 |
|
1354 |
try:
|
1355 |
size, hashmap = \ |
1356 |
request.backend.get_object_hashmap(request.user_uniq, |
1357 |
v_account, v_container, v_object) |
1358 |
except NotAllowedError:
|
1359 |
raise faults.Forbidden('Not allowed') |
1360 |
except ItemNotExists:
|
1361 |
raise faults.ItemNotFound('Object does not exist') |
1362 |
|
1363 |
offset, length, total = ranges |
1364 |
if offset is None: |
1365 |
offset = size |
1366 |
elif offset > size:
|
1367 |
raise faults.RangeNotSatisfiable(
|
1368 |
'Supplied offset is beyond object limits')
|
1369 |
if src_object:
|
1370 |
src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
|
1371 |
if not src_account: |
1372 |
src_account = request.user_uniq |
1373 |
src_container, src_name = split_container_object_string(src_object) |
1374 |
src_version = request.META.get('HTTP_X_SOURCE_VERSION')
|
1375 |
try:
|
1376 |
src_size, src_hashmap = request.backend.get_object_hashmap( |
1377 |
request.user_uniq, |
1378 |
src_account, src_container, src_name, src_version) |
1379 |
except NotAllowedError:
|
1380 |
raise faults.Forbidden('Not allowed') |
1381 |
except ItemNotExists:
|
1382 |
raise faults.ItemNotFound('Source object does not exist') |
1383 |
|
1384 |
if length is None: |
1385 |
length = src_size |
1386 |
elif length > src_size:
|
1387 |
raise faults.BadRequest(
|
1388 |
'Object length is smaller than range length')
|
1389 |
else:
|
1390 |
# Require either a Content-Length, or 'chunked' Transfer-Encoding.
|
1391 |
content_length = -1
|
1392 |
if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked': |
1393 |
content_length = get_content_length(request) |
1394 |
|
1395 |
if length is None: |
1396 |
length = content_length |
1397 |
else:
|
1398 |
if content_length == -1: |
1399 |
# TODO: Get up to length bytes in chunks.
|
1400 |
length = content_length |
1401 |
elif length != content_length:
|
1402 |
raise faults.BadRequest(
|
1403 |
'Content length does not match range length')
|
1404 |
if (total is not None |
1405 |
and (total != size or offset >= size |
1406 |
or (length > 0 and offset + length >= size))): |
1407 |
raise faults.RangeNotSatisfiable(
|
1408 |
'Supplied range will change provided object limits')
|
1409 |
|
1410 |
dest_bytes = request.META.get('HTTP_X_OBJECT_BYTES')
|
1411 |
if dest_bytes is not None: |
1412 |
dest_bytes = get_int_parameter(dest_bytes) |
1413 |
if dest_bytes is None: |
1414 |
raise faults.BadRequest('Invalid X-Object-Bytes header') |
1415 |
|
1416 |
if src_object:
|
1417 |
if offset % request.backend.block_size == 0: |
1418 |
# Update the hashes only.
|
1419 |
sbi = 0
|
1420 |
while length > 0: |
1421 |
bi = int(offset / request.backend.block_size)
|
1422 |
bl = min(length, request.backend.block_size)
|
1423 |
if bi < len(hashmap): |
1424 |
if bl == request.backend.block_size:
|
1425 |
hashmap[bi] = src_hashmap[sbi] |
1426 |
else:
|
1427 |
data = request.backend.get_block(src_hashmap[sbi]) |
1428 |
hashmap[bi] = request.backend.update_block(hashmap[bi], |
1429 |
data[:bl], 0)
|
1430 |
else:
|
1431 |
hashmap.append(src_hashmap[sbi]) |
1432 |
offset += bl |
1433 |
length -= bl |
1434 |
sbi += 1
|
1435 |
else:
|
1436 |
data = ''
|
1437 |
sbi = 0
|
1438 |
while length > 0: |
1439 |
data += request.backend.get_block(src_hashmap[sbi]) |
1440 |
if length < request.backend.block_size:
|
1441 |
data = data[:length] |
1442 |
bytes = put_object_block(request, hashmap, data, offset) |
1443 |
offset += bytes
|
1444 |
data = data[bytes:]
|
1445 |
length -= bytes
|
1446 |
sbi += 1
|
1447 |
else:
|
1448 |
data = ''
|
1449 |
for d in socket_read_iterator(request, length, |
1450 |
request.backend.block_size): |
1451 |
# TODO: Raise 408 (Request Timeout) if this takes too long.
|
1452 |
# TODO: Raise 499 (Client Disconnect) if a length is defined
|
1453 |
# and we stop before getting this much data.
|
1454 |
data += d |
1455 |
bytes = put_object_block(request, hashmap, data, offset) |
1456 |
offset += bytes
|
1457 |
data = data[bytes:]
|
1458 |
if len(data) > 0: |
1459 |
put_object_block(request, hashmap, data, offset) |
1460 |
|
1461 |
if offset > size:
|
1462 |
size = offset |
1463 |
if dest_bytes is not None and dest_bytes < size: |
1464 |
size = dest_bytes |
1465 |
hashmap = hashmap[:(int((size - 1) / request.backend.block_size) + 1)] |
1466 |
checksum = hashmap_md5( |
1467 |
request.backend, hashmap, size) if UPDATE_MD5 else '' |
1468 |
try:
|
1469 |
version_id = \ |
1470 |
request.backend.update_object_hashmap(request.user_uniq, |
1471 |
v_account, v_container, |
1472 |
v_object, size, |
1473 |
prev_meta['type'],
|
1474 |
hashmap, checksum, 'pithos',
|
1475 |
meta, replace, permissions) |
1476 |
except NotAllowedError:
|
1477 |
raise faults.Forbidden('Not allowed') |
1478 |
except ItemNotExists:
|
1479 |
raise faults.ItemNotFound('Container does not exist') |
1480 |
except ValueError: |
1481 |
raise faults.BadRequest('Invalid sharing header') |
1482 |
except QuotaError, e:
|
1483 |
raise faults.RequestEntityTooLarge('Quota error: %s' % e) |
1484 |
if public is not None: |
1485 |
try:
|
1486 |
request.backend.update_object_public(request.user_uniq, v_account, |
1487 |
v_container, v_object, public) |
1488 |
except NotAllowedError:
|
1489 |
raise faults.Forbidden('Not allowed') |
1490 |
except ItemNotExists:
|
1491 |
raise faults.ItemNotFound('Object does not exist') |
1492 |
|
1493 |
response = HttpResponse(status=204)
|
1494 |
response['ETag'] = checksum
|
1495 |
response['X-Object-Version'] = version_id
|
1496 |
return response
|
1497 |
|
1498 |
|
1499 |
@api_method('DELETE', user_required=True, logger=logger, |
1500 |
lock_container_path=True)
|
1501 |
def object_delete(request, v_account, v_container, v_object): |
1502 |
# Normal Response Codes: 204
|
1503 |
# Error Response Codes: internalServerError (500),
|
1504 |
# itemNotFound (404),
|
1505 |
# forbidden (403),
|
1506 |
# badRequest (400)
|
1507 |
# requestentitytoolarge (413)
|
1508 |
|
1509 |
until = get_int_parameter(request.GET.get('until'))
|
1510 |
delimiter = request.GET.get('delimiter')
|
1511 |
|
1512 |
try:
|
1513 |
request.backend.delete_object( |
1514 |
request.user_uniq, v_account, v_container, |
1515 |
v_object, until, delimiter=delimiter) |
1516 |
except NotAllowedError:
|
1517 |
raise faults.Forbidden('Not allowed') |
1518 |
except ItemNotExists:
|
1519 |
raise faults.ItemNotFound('Object does not exist') |
1520 |
except QuotaError, e:
|
1521 |
raise faults.RequestEntityTooLarge('Quota error: %s' % e) |
1522 |
return HttpResponse(status=204) |