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