root / pithos / api / functions.py @ 104626e3
History | View | Annotate | Download (29.9 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 os |
35 |
import logging |
36 |
import hashlib |
37 |
|
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 |
|
43 |
from pithos.api.faults import (Fault, NotModified, BadRequest, Unauthorized, ItemNotFound, Conflict, |
44 |
LengthRequired, PreconditionFailed, RangeNotSatisfiable, UnprocessableEntity) |
45 |
from pithos.api.util import (format_meta_key, printable_meta_dict, get_account_meta, |
46 |
put_account_meta, get_container_meta, put_container_meta, get_object_meta, put_object_meta, |
47 |
update_manifest_meta, update_sharing_meta, validate_modification_preconditions, |
48 |
validate_matching_preconditions, split_container_object_string, copy_or_move_object, |
49 |
get_int_parameter, get_content_length, get_content_range, get_sharing, raw_input_socket, |
50 |
socket_read_iterator, object_data_response, put_object_block, hashmap_hash, api_method) |
51 |
from pithos.backends import backend |
52 |
from pithos.backends.base import NotAllowedError |
53 |
|
54 |
|
55 |
logger = logging.getLogger(__name__) |
56 |
|
57 |
|
58 |
def top_demux(request): |
59 |
if request.method == 'GET': |
60 |
return authenticate(request)
|
61 |
else:
|
62 |
return method_not_allowed(request)
|
63 |
|
64 |
def account_demux(request, v_account): |
65 |
if request.method == 'HEAD': |
66 |
return account_meta(request, v_account)
|
67 |
elif request.method == 'POST': |
68 |
return account_update(request, v_account)
|
69 |
elif request.method == 'GET': |
70 |
return container_list(request, v_account)
|
71 |
else:
|
72 |
return method_not_allowed(request)
|
73 |
|
74 |
def container_demux(request, v_account, v_container): |
75 |
if request.method == 'HEAD': |
76 |
return container_meta(request, v_account, v_container)
|
77 |
elif request.method == 'PUT': |
78 |
return container_create(request, v_account, v_container)
|
79 |
elif request.method == 'POST': |
80 |
return container_update(request, v_account, v_container)
|
81 |
elif request.method == 'DELETE': |
82 |
return container_delete(request, v_account, v_container)
|
83 |
elif request.method == 'GET': |
84 |
return object_list(request, v_account, v_container)
|
85 |
else:
|
86 |
return method_not_allowed(request)
|
87 |
|
88 |
def object_demux(request, v_account, v_container, v_object): |
89 |
if request.method == 'HEAD': |
90 |
return object_meta(request, v_account, v_container, v_object)
|
91 |
elif request.method == 'GET': |
92 |
return object_read(request, v_account, v_container, v_object)
|
93 |
elif request.method == 'PUT': |
94 |
return object_write(request, v_account, v_container, v_object)
|
95 |
elif request.method == 'COPY': |
96 |
return object_copy(request, v_account, v_container, v_object)
|
97 |
elif request.method == 'MOVE': |
98 |
return object_move(request, v_account, v_container, v_object)
|
99 |
elif request.method == 'POST': |
100 |
return object_update(request, v_account, v_container, v_object)
|
101 |
elif request.method == 'DELETE': |
102 |
return object_delete(request, v_account, v_container, v_object)
|
103 |
else:
|
104 |
return method_not_allowed(request)
|
105 |
|
106 |
@api_method('GET') |
107 |
def authenticate(request): |
108 |
# Normal Response Codes: 204
|
109 |
# Error Response Codes: serviceUnavailable (503),
|
110 |
# unauthorized (401),
|
111 |
# badRequest (400)
|
112 |
|
113 |
x_auth_user = request.META.get('HTTP_X_AUTH_USER')
|
114 |
x_auth_key = request.META.get('HTTP_X_AUTH_KEY')
|
115 |
if not x_auth_user or not x_auth_key: |
116 |
raise BadRequest('Missing X-Auth-User or X-Auth-Key header') |
117 |
response = HttpResponse(status=204)
|
118 |
response['X-Auth-Token'] = '0000' |
119 |
response['X-Storage-Url'] = os.path.join(request.build_absolute_uri(), 'demo') |
120 |
return response
|
121 |
|
122 |
@api_method('HEAD') |
123 |
def account_meta(request, v_account): |
124 |
# Normal Response Codes: 204
|
125 |
# Error Response Codes: serviceUnavailable (503),
|
126 |
# unauthorized (401),
|
127 |
# badRequest (400)
|
128 |
|
129 |
until = get_int_parameter(request, 'until')
|
130 |
try:
|
131 |
meta = backend.get_account_meta(request.user, v_account, until) |
132 |
except NotAllowedError:
|
133 |
raise Unauthorized('Access denied') |
134 |
|
135 |
response = HttpResponse(status=204)
|
136 |
put_account_meta(response, meta) |
137 |
return response
|
138 |
|
139 |
@api_method('POST') |
140 |
def account_update(request, v_account): |
141 |
# Normal Response Codes: 202
|
142 |
# Error Response Codes: serviceUnavailable (503),
|
143 |
# unauthorized (401),
|
144 |
# badRequest (400)
|
145 |
|
146 |
meta = get_account_meta(request) |
147 |
replace = True
|
148 |
if 'update' in request.GET: |
149 |
replace = False
|
150 |
try:
|
151 |
backend.update_account_meta(request.user, v_account, meta, replace) |
152 |
except NotAllowedError:
|
153 |
raise Unauthorized('Access denied') |
154 |
return HttpResponse(status=202) |
155 |
|
156 |
@api_method('GET', format_allowed=True) |
157 |
def container_list(request, v_account): |
158 |
# Normal Response Codes: 200, 204
|
159 |
# Error Response Codes: serviceUnavailable (503),
|
160 |
# itemNotFound (404),
|
161 |
# unauthorized (401),
|
162 |
# badRequest (400)
|
163 |
|
164 |
until = get_int_parameter(request, 'until')
|
165 |
try:
|
166 |
meta = backend.get_account_meta(request.user, v_account, until) |
167 |
except NotAllowedError:
|
168 |
raise Unauthorized('Access denied') |
169 |
|
170 |
validate_modification_preconditions(request, meta) |
171 |
|
172 |
response = HttpResponse() |
173 |
put_account_meta(response, meta) |
174 |
|
175 |
marker = request.GET.get('marker')
|
176 |
limit = request.GET.get('limit')
|
177 |
if limit:
|
178 |
try:
|
179 |
limit = int(limit)
|
180 |
if limit <= 0: |
181 |
raise ValueError |
182 |
except ValueError: |
183 |
limit = 10000
|
184 |
|
185 |
try:
|
186 |
containers = backend.list_containers(request.user, v_account, marker, limit, until) |
187 |
except NotAllowedError:
|
188 |
raise Unauthorized('Access denied') |
189 |
except NameError: |
190 |
containers = [] |
191 |
|
192 |
if request.serialization == 'text': |
193 |
if len(containers) == 0: |
194 |
# The cloudfiles python bindings expect 200 if json/xml.
|
195 |
response.status_code = 204
|
196 |
return response
|
197 |
response.status_code = 200
|
198 |
response.content = '\n'.join([x[0] for x in containers]) + '\n' |
199 |
return response
|
200 |
|
201 |
container_meta = [] |
202 |
for x in containers: |
203 |
if x[1] is not None: |
204 |
try:
|
205 |
meta = backend.get_container_meta(request.user, v_account, x[0], until)
|
206 |
container_meta.append(printable_meta_dict(meta)) |
207 |
except NotAllowedError:
|
208 |
raise Unauthorized('Access denied') |
209 |
except NameError: |
210 |
pass
|
211 |
if request.serialization == 'xml': |
212 |
data = render_to_string('containers.xml', {'account': v_account, 'containers': container_meta}) |
213 |
elif request.serialization == 'json': |
214 |
data = json.dumps(container_meta) |
215 |
response.status_code = 200
|
216 |
response.content = data |
217 |
return response
|
218 |
|
219 |
@api_method('HEAD') |
220 |
def container_meta(request, v_account, v_container): |
221 |
# Normal Response Codes: 204
|
222 |
# Error Response Codes: serviceUnavailable (503),
|
223 |
# itemNotFound (404),
|
224 |
# unauthorized (401),
|
225 |
# badRequest (400)
|
226 |
|
227 |
until = get_int_parameter(request, 'until')
|
228 |
try:
|
229 |
meta = backend.get_container_meta(request.user, v_account, v_container, until) |
230 |
meta['object_meta'] = backend.list_object_meta(request.user, v_account, v_container, until)
|
231 |
except NotAllowedError:
|
232 |
raise Unauthorized('Access denied') |
233 |
except NameError: |
234 |
raise ItemNotFound('Container does not exist') |
235 |
|
236 |
response = HttpResponse(status=204)
|
237 |
put_container_meta(response, meta) |
238 |
return response
|
239 |
|
240 |
@api_method('PUT') |
241 |
def container_create(request, v_account, v_container): |
242 |
# Normal Response Codes: 201, 202
|
243 |
# Error Response Codes: serviceUnavailable (503),
|
244 |
# itemNotFound (404),
|
245 |
# unauthorized (401),
|
246 |
# badRequest (400)
|
247 |
|
248 |
meta = get_container_meta(request) |
249 |
|
250 |
try:
|
251 |
backend.put_container(request.user, v_account, v_container) |
252 |
ret = 201
|
253 |
except NotAllowedError:
|
254 |
raise Unauthorized('Access denied') |
255 |
except NameError: |
256 |
ret = 202
|
257 |
|
258 |
if len(meta) > 0: |
259 |
try:
|
260 |
backend.update_container_meta(request.user, v_account, v_container, meta, replace=True)
|
261 |
except NotAllowedError:
|
262 |
raise Unauthorized('Access denied') |
263 |
except NameError: |
264 |
raise ItemNotFound('Container does not exist') |
265 |
|
266 |
return HttpResponse(status=ret)
|
267 |
|
268 |
@api_method('POST') |
269 |
def container_update(request, v_account, v_container): |
270 |
# Normal Response Codes: 202
|
271 |
# Error Response Codes: serviceUnavailable (503),
|
272 |
# itemNotFound (404),
|
273 |
# unauthorized (401),
|
274 |
# badRequest (400)
|
275 |
|
276 |
meta = get_container_meta(request) |
277 |
replace = True
|
278 |
if 'update' in request.GET: |
279 |
replace = False
|
280 |
try:
|
281 |
backend.update_container_meta(request.user, v_account, v_container, meta, replace) |
282 |
except NotAllowedError:
|
283 |
raise Unauthorized('Access denied') |
284 |
except NameError: |
285 |
raise ItemNotFound('Container does not exist') |
286 |
return HttpResponse(status=202) |
287 |
|
288 |
@api_method('DELETE') |
289 |
def container_delete(request, v_account, v_container): |
290 |
# Normal Response Codes: 204
|
291 |
# Error Response Codes: serviceUnavailable (503),
|
292 |
# conflict (409),
|
293 |
# itemNotFound (404),
|
294 |
# unauthorized (401),
|
295 |
# badRequest (400)
|
296 |
|
297 |
try:
|
298 |
backend.delete_container(request.user, v_account, v_container) |
299 |
except NotAllowedError:
|
300 |
raise Unauthorized('Access denied') |
301 |
except NameError: |
302 |
raise ItemNotFound('Container does not exist') |
303 |
except IndexError: |
304 |
raise Conflict('Container is not empty') |
305 |
return HttpResponse(status=204) |
306 |
|
307 |
@api_method('GET', format_allowed=True) |
308 |
def object_list(request, v_account, v_container): |
309 |
# Normal Response Codes: 200, 204
|
310 |
# Error Response Codes: serviceUnavailable (503),
|
311 |
# itemNotFound (404),
|
312 |
# unauthorized (401),
|
313 |
# badRequest (400)
|
314 |
|
315 |
until = get_int_parameter(request, 'until')
|
316 |
try:
|
317 |
meta = backend.get_container_meta(request.user, v_account, v_container, until) |
318 |
meta['object_meta'] = backend.list_object_meta(request.user, v_account, v_container, until)
|
319 |
except NotAllowedError:
|
320 |
raise Unauthorized('Access denied') |
321 |
except NameError: |
322 |
raise ItemNotFound('Container does not exist') |
323 |
|
324 |
validate_modification_preconditions(request, meta) |
325 |
|
326 |
response = HttpResponse() |
327 |
put_container_meta(response, meta) |
328 |
|
329 |
path = request.GET.get('path')
|
330 |
prefix = request.GET.get('prefix')
|
331 |
delimiter = request.GET.get('delimiter')
|
332 |
|
333 |
# Path overrides prefix and delimiter.
|
334 |
virtual = True
|
335 |
if path:
|
336 |
prefix = path |
337 |
delimiter = '/'
|
338 |
virtual = False
|
339 |
|
340 |
# Naming policy.
|
341 |
if prefix and delimiter: |
342 |
prefix = prefix + delimiter |
343 |
if not prefix: |
344 |
prefix = ''
|
345 |
prefix = prefix.lstrip('/')
|
346 |
|
347 |
marker = request.GET.get('marker')
|
348 |
limit = request.GET.get('limit')
|
349 |
if limit:
|
350 |
try:
|
351 |
limit = int(limit)
|
352 |
if limit <= 0: |
353 |
raise ValueError |
354 |
except ValueError: |
355 |
limit = 10000
|
356 |
|
357 |
keys = request.GET.get('meta')
|
358 |
if keys:
|
359 |
keys = keys.split(',')
|
360 |
keys = [format_meta_key('X-Object-Meta-' + x.strip()) for x in keys if x.strip() != ''] |
361 |
else:
|
362 |
keys = [] |
363 |
|
364 |
try:
|
365 |
objects = backend.list_objects(request.user, v_account, v_container, prefix, delimiter, marker, limit, virtual, keys, until) |
366 |
except NotAllowedError:
|
367 |
raise Unauthorized('Access denied') |
368 |
except NameError: |
369 |
raise ItemNotFound('Container does not exist') |
370 |
|
371 |
if request.serialization == 'text': |
372 |
if len(objects) == 0: |
373 |
# The cloudfiles python bindings expect 200 if json/xml.
|
374 |
response.status_code = 204
|
375 |
return response
|
376 |
response.status_code = 200
|
377 |
response.content = '\n'.join([x[0] for x in objects]) + '\n' |
378 |
return response
|
379 |
|
380 |
object_meta = [] |
381 |
for x in objects: |
382 |
if x[1] is None: |
383 |
# Virtual objects/directories.
|
384 |
object_meta.append({'subdir': x[0]}) |
385 |
else:
|
386 |
try:
|
387 |
meta = backend.get_object_meta(request.user, v_account, v_container, x[0], x[1]) |
388 |
if until is None: |
389 |
permissions = backend.get_object_permissions(request.user, v_account, v_container, x[0])
|
390 |
else:
|
391 |
permissions = None
|
392 |
except NotAllowedError:
|
393 |
raise Unauthorized('Access denied') |
394 |
except NameError: |
395 |
pass
|
396 |
update_sharing_meta(permissions, v_account, v_container, x[0], meta)
|
397 |
object_meta.append(printable_meta_dict(meta)) |
398 |
if request.serialization == 'xml': |
399 |
data = render_to_string('objects.xml', {'container': v_container, 'objects': object_meta}) |
400 |
elif request.serialization == 'json': |
401 |
data = json.dumps(object_meta) |
402 |
response.status_code = 200
|
403 |
response.content = data |
404 |
return response
|
405 |
|
406 |
@api_method('HEAD') |
407 |
def object_meta(request, v_account, v_container, v_object): |
408 |
# Normal Response Codes: 204
|
409 |
# Error Response Codes: serviceUnavailable (503),
|
410 |
# itemNotFound (404),
|
411 |
# unauthorized (401),
|
412 |
# badRequest (400)
|
413 |
|
414 |
version = request.GET.get('version')
|
415 |
try:
|
416 |
meta = backend.get_object_meta(request.user, v_account, v_container, v_object, version) |
417 |
if version is None: |
418 |
permissions = backend.get_object_permissions(request.user, v_account, v_container, v_object) |
419 |
else:
|
420 |
permissions = None
|
421 |
except NotAllowedError:
|
422 |
raise Unauthorized('Access denied') |
423 |
except NameError: |
424 |
raise ItemNotFound('Object does not exist') |
425 |
except IndexError: |
426 |
raise ItemNotFound('Version does not exist') |
427 |
|
428 |
update_manifest_meta(request, v_account, meta) |
429 |
update_sharing_meta(permissions, v_account, v_container, v_object, meta) |
430 |
|
431 |
response = HttpResponse(status=200)
|
432 |
put_object_meta(response, meta) |
433 |
return response
|
434 |
|
435 |
@api_method('GET', format_allowed=True) |
436 |
def object_read(request, v_account, v_container, v_object): |
437 |
# Normal Response Codes: 200, 206
|
438 |
# Error Response Codes: serviceUnavailable (503),
|
439 |
# rangeNotSatisfiable (416),
|
440 |
# preconditionFailed (412),
|
441 |
# itemNotFound (404),
|
442 |
# unauthorized (401),
|
443 |
# badRequest (400),
|
444 |
# notModified (304)
|
445 |
|
446 |
version = request.GET.get('version')
|
447 |
|
448 |
# Reply with the version list. Do this first, as the object may be deleted.
|
449 |
if version == 'list': |
450 |
if request.serialization == 'text': |
451 |
raise BadRequest('No format specified for version list.') |
452 |
|
453 |
try:
|
454 |
v = backend.list_versions(request.user, v_account, v_container, v_object) |
455 |
except NotAllowedError:
|
456 |
raise Unauthorized('Access denied') |
457 |
d = {'versions': v}
|
458 |
if request.serialization == 'xml': |
459 |
d['object'] = v_object
|
460 |
data = render_to_string('versions.xml', d)
|
461 |
elif request.serialization == 'json': |
462 |
data = json.dumps(d) |
463 |
|
464 |
response = HttpResponse(data, status=200)
|
465 |
response['Content-Length'] = len(data) |
466 |
return response
|
467 |
|
468 |
try:
|
469 |
meta = backend.get_object_meta(request.user, v_account, v_container, v_object, version) |
470 |
if version is None: |
471 |
permissions = backend.get_object_permissions(request.user, v_account, v_container, v_object) |
472 |
else:
|
473 |
permissions = None
|
474 |
except NotAllowedError:
|
475 |
raise Unauthorized('Access denied') |
476 |
except NameError: |
477 |
raise ItemNotFound('Object does not exist') |
478 |
except IndexError: |
479 |
raise ItemNotFound('Version does not exist') |
480 |
|
481 |
update_manifest_meta(request, v_account, meta) |
482 |
update_sharing_meta(permissions, v_account, v_container, v_object, meta) |
483 |
|
484 |
# Evaluate conditions.
|
485 |
validate_modification_preconditions(request, meta) |
486 |
try:
|
487 |
validate_matching_preconditions(request, meta) |
488 |
except NotModified:
|
489 |
response = HttpResponse(status=304)
|
490 |
response['ETag'] = meta['hash'] |
491 |
return response
|
492 |
|
493 |
sizes = [] |
494 |
hashmaps = [] |
495 |
if 'X-Object-Manifest' in meta: |
496 |
try:
|
497 |
src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest']) |
498 |
objects = backend.list_objects(request.user, v_account, src_container, prefix=src_name, virtual=False)
|
499 |
except NotAllowedError:
|
500 |
raise Unauthorized('Access denied') |
501 |
except ValueError: |
502 |
raise BadRequest('Invalid X-Object-Manifest header') |
503 |
except NameError: |
504 |
raise ItemNotFound('Container does not exist') |
505 |
|
506 |
try:
|
507 |
for x in objects: |
508 |
s, h = backend.get_object_hashmap(request.user, v_account, src_container, x[0], x[1]) |
509 |
sizes.append(s) |
510 |
hashmaps.append(h) |
511 |
except NotAllowedError:
|
512 |
raise Unauthorized('Access denied') |
513 |
except NameError: |
514 |
raise ItemNotFound('Object does not exist') |
515 |
except IndexError: |
516 |
raise ItemNotFound('Version does not exist') |
517 |
else:
|
518 |
try:
|
519 |
s, h = backend.get_object_hashmap(request.user, v_account, v_container, v_object, version) |
520 |
sizes.append(s) |
521 |
hashmaps.append(h) |
522 |
except NotAllowedError:
|
523 |
raise Unauthorized('Access denied') |
524 |
except NameError: |
525 |
raise ItemNotFound('Object does not exist') |
526 |
except IndexError: |
527 |
raise ItemNotFound('Version does not exist') |
528 |
|
529 |
# Reply with the hashmap.
|
530 |
if request.serialization != 'text': |
531 |
size = sum(sizes)
|
532 |
hashmap = sum(hashmaps, [])
|
533 |
d = {'block_size': backend.block_size, 'block_hash': backend.hash_algorithm, 'bytes': size, 'hashes': hashmap} |
534 |
if request.serialization == 'xml': |
535 |
d['object'] = v_object
|
536 |
data = render_to_string('hashes.xml', d)
|
537 |
elif request.serialization == 'json': |
538 |
data = json.dumps(d) |
539 |
|
540 |
response = HttpResponse(data, status=200)
|
541 |
put_object_meta(response, meta) |
542 |
response['Content-Length'] = len(data) |
543 |
return response
|
544 |
|
545 |
return object_data_response(request, sizes, hashmaps, meta)
|
546 |
|
547 |
@api_method('PUT') |
548 |
def object_write(request, v_account, v_container, v_object): |
549 |
# Normal Response Codes: 201
|
550 |
# Error Response Codes: serviceUnavailable (503),
|
551 |
# unprocessableEntity (422),
|
552 |
# lengthRequired (411),
|
553 |
# conflict (409),
|
554 |
# itemNotFound (404),
|
555 |
# unauthorized (401),
|
556 |
# badRequest (400)
|
557 |
|
558 |
copy_from = request.META.get('HTTP_X_COPY_FROM')
|
559 |
move_from = request.META.get('HTTP_X_MOVE_FROM')
|
560 |
if copy_from or move_from: |
561 |
# TODO: Why is this required? Copy this ammount?
|
562 |
content_length = get_content_length(request) |
563 |
|
564 |
if move_from:
|
565 |
try:
|
566 |
src_container, src_name = split_container_object_string(move_from) |
567 |
except ValueError: |
568 |
raise BadRequest('Invalid X-Move-From header') |
569 |
copy_or_move_object(request, v_account, src_container, src_name, v_container, v_object, move=True)
|
570 |
else:
|
571 |
try:
|
572 |
src_container, src_name = split_container_object_string(copy_from) |
573 |
except ValueError: |
574 |
raise BadRequest('Invalid X-Copy-From header') |
575 |
copy_or_move_object(request, v_account, src_container, src_name, v_container, v_object, move=False)
|
576 |
return HttpResponse(status=201) |
577 |
|
578 |
meta = get_object_meta(request) |
579 |
permissions = get_sharing(request) |
580 |
content_length = -1
|
581 |
if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked': |
582 |
content_length = get_content_length(request) |
583 |
# Should be BadRequest, but API says otherwise.
|
584 |
if 'Content-Type' not in meta: |
585 |
raise LengthRequired('Missing Content-Type header') |
586 |
|
587 |
md5 = hashlib.md5() |
588 |
size = 0
|
589 |
hashmap = [] |
590 |
sock = raw_input_socket(request) |
591 |
for data in socket_read_iterator(sock, content_length, backend.block_size): |
592 |
# TODO: Raise 408 (Request Timeout) if this takes too long.
|
593 |
# TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
|
594 |
size += len(data)
|
595 |
hashmap.append(backend.put_block(data)) |
596 |
md5.update(data) |
597 |
|
598 |
meta['hash'] = md5.hexdigest().lower()
|
599 |
etag = request.META.get('HTTP_ETAG')
|
600 |
if etag and parse_etags(etag)[0].lower() != meta['hash']: |
601 |
raise UnprocessableEntity('Object ETag does not match') |
602 |
|
603 |
try:
|
604 |
backend.update_object_hashmap(request.user, v_account, v_container, v_object, size, hashmap, meta, True, permissions)
|
605 |
except NotAllowedError:
|
606 |
raise Unauthorized('Access denied') |
607 |
except NameError: |
608 |
raise ItemNotFound('Container does not exist') |
609 |
except ValueError: |
610 |
raise BadRequest('Invalid sharing header') |
611 |
except AttributeError: |
612 |
raise Conflict('Sharing already set above or below this path in the hierarchy') |
613 |
|
614 |
response = HttpResponse(status=201)
|
615 |
response['ETag'] = meta['hash'] |
616 |
return response
|
617 |
|
618 |
@api_method('COPY') |
619 |
def object_copy(request, v_account, v_container, v_object): |
620 |
# Normal Response Codes: 201
|
621 |
# Error Response Codes: serviceUnavailable (503),
|
622 |
# itemNotFound (404),
|
623 |
# unauthorized (401),
|
624 |
# badRequest (400)
|
625 |
|
626 |
dest_path = request.META.get('HTTP_DESTINATION')
|
627 |
if not dest_path: |
628 |
raise BadRequest('Missing Destination header') |
629 |
try:
|
630 |
dest_container, dest_name = split_container_object_string(dest_path) |
631 |
except ValueError: |
632 |
raise BadRequest('Invalid Destination header') |
633 |
copy_or_move_object(request, v_account, v_container, v_object, dest_container, dest_name, move=False)
|
634 |
return HttpResponse(status=201) |
635 |
|
636 |
@api_method('MOVE') |
637 |
def object_move(request, v_account, v_container, v_object): |
638 |
# Normal Response Codes: 201
|
639 |
# Error Response Codes: serviceUnavailable (503),
|
640 |
# itemNotFound (404),
|
641 |
# unauthorized (401),
|
642 |
# badRequest (400)
|
643 |
|
644 |
dest_path = request.META.get('HTTP_DESTINATION')
|
645 |
if not dest_path: |
646 |
raise BadRequest('Missing Destination header') |
647 |
try:
|
648 |
dest_container, dest_name = split_container_object_string(dest_path) |
649 |
except ValueError: |
650 |
raise BadRequest('Invalid Destination header') |
651 |
copy_or_move_object(request, v_account, v_container, v_object, dest_container, dest_name, move=True)
|
652 |
return HttpResponse(status=201) |
653 |
|
654 |
@api_method('POST') |
655 |
def object_update(request, v_account, v_container, v_object): |
656 |
# Normal Response Codes: 202, 204
|
657 |
# Error Response Codes: serviceUnavailable (503),
|
658 |
# conflict (409),
|
659 |
# itemNotFound (404),
|
660 |
# unauthorized (401),
|
661 |
# badRequest (400)
|
662 |
|
663 |
meta = get_object_meta(request) |
664 |
permissions = get_sharing(request) |
665 |
content_type = meta.get('Content-Type')
|
666 |
if content_type:
|
667 |
del(meta['Content-Type']) # Do not allow changing the Content-Type. |
668 |
|
669 |
try:
|
670 |
prev_meta = backend.get_object_meta(request.user, v_account, v_container, v_object) |
671 |
except NotAllowedError:
|
672 |
raise Unauthorized('Access denied') |
673 |
except NameError: |
674 |
raise ItemNotFound('Object does not exist') |
675 |
# If replacing, keep previous values of 'Content-Type' and 'hash'.
|
676 |
replace = True
|
677 |
if 'update' in request.GET: |
678 |
replace = False
|
679 |
if replace:
|
680 |
for k in ('Content-Type', 'hash'): |
681 |
if k in prev_meta: |
682 |
meta[k] = prev_meta[k] |
683 |
|
684 |
# A Content-Type header indicates data updates.
|
685 |
if not content_type or content_type != 'application/octet-stream': |
686 |
# Do permissions first, as it may fail easier.
|
687 |
if permissions is not None: |
688 |
try:
|
689 |
backend.update_object_permissions(request.user, v_account, v_container, v_object, permissions) |
690 |
except NotAllowedError:
|
691 |
raise Unauthorized('Access denied') |
692 |
except NameError: |
693 |
raise ItemNotFound('Object does not exist') |
694 |
except ValueError: |
695 |
raise BadRequest('Invalid sharing header') |
696 |
except AttributeError: |
697 |
raise Conflict('Sharing already set above or below this path in the hierarchy') |
698 |
try:
|
699 |
backend.update_object_meta(request.user, v_account, v_container, v_object, meta, replace) |
700 |
except NotAllowedError:
|
701 |
raise Unauthorized('Access denied') |
702 |
except NameError: |
703 |
raise ItemNotFound('Object does not exist') |
704 |
return HttpResponse(status=202) |
705 |
|
706 |
# Single range update. Range must be in Content-Range.
|
707 |
# Based on: http://code.google.com/p/gears/wiki/ContentRangePostProposal
|
708 |
# (with the addition that '*' is allowed for the range - will append).
|
709 |
content_range = request.META.get('HTTP_CONTENT_RANGE')
|
710 |
if not content_range: |
711 |
raise BadRequest('Missing Content-Range header') |
712 |
ranges = get_content_range(request) |
713 |
if not ranges: |
714 |
raise RangeNotSatisfiable('Invalid Content-Range header') |
715 |
# Require either a Content-Length, or 'chunked' Transfer-Encoding.
|
716 |
content_length = -1
|
717 |
if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked': |
718 |
content_length = get_content_length(request) |
719 |
|
720 |
try:
|
721 |
size, hashmap = backend.get_object_hashmap(request.user, v_account, v_container, v_object) |
722 |
except NotAllowedError:
|
723 |
raise Unauthorized('Access denied') |
724 |
except NameError: |
725 |
raise ItemNotFound('Object does not exist') |
726 |
|
727 |
offset, length, total = ranges |
728 |
if offset is None: |
729 |
offset = size |
730 |
elif offset > size:
|
731 |
raise RangeNotSatisfiable('Supplied offset is beyond object limits') |
732 |
if length is None or content_length == -1: |
733 |
length = content_length # Nevermind the error.
|
734 |
elif length != content_length:
|
735 |
raise BadRequest('Content length does not match range length') |
736 |
if total is not None and (total != size or offset >= size or (length > 0 and offset + length >= size)): |
737 |
raise RangeNotSatisfiable('Supplied range will change provided object limits') |
738 |
|
739 |
sock = raw_input_socket(request) |
740 |
data = ''
|
741 |
for d in socket_read_iterator(sock, length, backend.block_size): |
742 |
# TODO: Raise 408 (Request Timeout) if this takes too long.
|
743 |
# TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
|
744 |
data += d |
745 |
bytes = put_object_block(hashmap, data, offset) |
746 |
offset += bytes
|
747 |
data = data[bytes:]
|
748 |
if len(data) > 0: |
749 |
put_object_block(hashmap, data, offset) |
750 |
|
751 |
if offset > size:
|
752 |
size = offset |
753 |
meta.update({'hash': hashmap_hash(hashmap)}) # Update ETag. |
754 |
try:
|
755 |
backend.update_object_hashmap(request.user, v_account, v_container, v_object, size, hashmap, meta, replace, permissions) |
756 |
except NotAllowedError:
|
757 |
raise Unauthorized('Access denied') |
758 |
except NameError: |
759 |
raise ItemNotFound('Container does not exist') |
760 |
except ValueError: |
761 |
raise BadRequest('Invalid sharing header') |
762 |
except AttributeError: |
763 |
raise Conflict('Sharing already set above or below this path in the hierarchy') |
764 |
|
765 |
response = HttpResponse(status=204)
|
766 |
response['ETag'] = meta['hash'] |
767 |
return response
|
768 |
|
769 |
@api_method('DELETE') |
770 |
def object_delete(request, v_account, v_container, v_object): |
771 |
# Normal Response Codes: 204
|
772 |
# Error Response Codes: serviceUnavailable (503),
|
773 |
# itemNotFound (404),
|
774 |
# unauthorized (401),
|
775 |
# badRequest (400)
|
776 |
|
777 |
try:
|
778 |
backend.delete_object(request.user, v_account, v_container, v_object) |
779 |
except NotAllowedError:
|
780 |
raise Unauthorized('Access denied') |
781 |
except NameError: |
782 |
raise ItemNotFound('Object does not exist') |
783 |
return HttpResponse(status=204) |
784 |
|
785 |
@api_method()
|
786 |
def method_not_allowed(request): |
787 |
raise BadRequest('Method not allowed') |