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