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