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