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