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