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