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