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