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