Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / functions.py @ 0c6ab9df

History | View | Annotate | Download (59.7 kB)

1
# Copyright 2011-2013 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

    
36
from django.http import HttpResponse
37
from django.template.loader import render_to_string
38
from django.utils import simplejson as json
39
from django.utils.http import parse_etags
40
from django.utils.encoding import smart_str
41
from django.views.decorators.csrf import csrf_exempt
42

    
43
from astakosclient import AstakosClient
44

    
45
from snf_django.lib import api
46
from snf_django.lib.api import faults
47

    
48
from pithos.api.util import (
49
    json_encode_decimal, rename_meta_key, format_header_key,
50
    printable_header_dict, get_account_headers, put_account_headers,
51
    get_container_headers, put_container_headers, get_object_headers,
52
    put_object_headers, update_manifest_meta, update_sharing_meta,
53
    update_public_meta, validate_modification_preconditions,
54
    validate_matching_preconditions, split_container_object_string,
55
    copy_or_move_object, get_int_parameter, get_content_length,
56
    get_content_range, socket_read_iterator, SaveToBackendHandler,
57
    object_data_response, put_object_block, hashmap_md5, simple_list_response,
58
    api_method, is_uuid, retrieve_uuid, retrieve_uuids,
59
    retrieve_displaynames, Checksum, NoChecksum
60
)
61

    
62
from pithos.api.settings import (UPDATE_MD5, TRANSLATE_UUIDS,
63
                                 SERVICE_TOKEN, ASTAKOS_AUTH_URL)
64

    
65
from pithos.api import settings
66

    
67
from pithos.backends.base import (
68
    NotAllowedError, QuotaError, ContainerNotEmpty, ItemNotExists,
69
    VersionNotExists, ContainerExists, InvalidHash)
70

    
71
from pithos.backends.filter import parse_filters
72

    
73
import logging
74
logger = logging.getLogger(__name__)
75

    
76

    
77
def get_uuids(names):
78
    try:
79
        astakos = AstakosClient(SERVICE_TOKEN, ASTAKOS_AUTH_URL,
80
                                retry=2, use_pool=True, logger=logger)
81
        uuids = astakos.service_get_uuids(names)
82
    except Exception, e:
83
        logger.exception(e)
84
        return {}
85

    
86
    return uuids
87

    
88

    
89
@csrf_exempt
90
def top_demux(request):
91
    if request.method == 'GET':
92
        try:
93
            request.GET['X-Auth-Token']
94
        except KeyError:
95
            try:
96
                request.META['HTTP_X_AUTH_TOKEN']
97
            except KeyError:
98
                return authenticate(request)
99
        return account_list(request)
100
    else:
101
        return api.api_method_not_allowed(request, allowed_methods=['GET'])
102

    
103

    
104
@csrf_exempt
105
def account_demux(request, v_account):
106
    if TRANSLATE_UUIDS:
107
        if not is_uuid(v_account):
108
            uuids = get_uuids([v_account])
109
            if not uuids or not v_account in uuids:
110
                return HttpResponse(status=404)
111
            v_account = uuids[v_account]
112

    
113
    if request.method == 'HEAD':
114
        return account_meta(request, v_account)
115
    elif request.method == 'POST':
116
        return account_update(request, v_account)
117
    elif request.method == 'GET':
118
        return container_list(request, v_account)
119
    else:
120
        return api.api_method_not_allowed(request,
121
                                          allowed_methods=['HEAD',
122
                                                           'POST',
123
                                                           'GET'])
124

    
125

    
126
@csrf_exempt
127
def container_demux(request, v_account, v_container):
128
    if TRANSLATE_UUIDS:
129
        if not is_uuid(v_account):
130
            uuids = get_uuids([v_account])
131
            if not uuids or not v_account in uuids:
132
                return HttpResponse(status=404)
133
            v_account = uuids[v_account]
134

    
135
    if request.method == 'HEAD':
136
        return container_meta(request, v_account, v_container)
137
    elif request.method == 'PUT':
138
        return container_create(request, v_account, v_container)
139
    elif request.method == 'POST':
140
        return container_update(request, v_account, v_container)
141
    elif request.method == 'DELETE':
142
        return container_delete(request, v_account, v_container)
143
    elif request.method == 'GET':
144
        return object_list(request, v_account, v_container)
145
    else:
146
        return api.api_method_not_allowed(request,
147
                                          allowed_methods=['HEAD',
148
                                                           'PUT',
149
                                                           'POST',
150
                                                           'DELETE',
151
                                                           'GET'])
152

    
153

    
154
@csrf_exempt
155
def object_demux(request, v_account, v_container, v_object):
156
    # Helper to avoid placing the token in the URL
157
    # when loading objects from a browser.
158
    if TRANSLATE_UUIDS:
159
        if not is_uuid(v_account):
160
            uuids = get_uuids([v_account])
161
            if not uuids or not v_account in uuids:
162
                return HttpResponse(status=404)
163
            v_account = uuids[v_account]
164

    
165
    if request.method == 'HEAD':
166
        return object_meta(request, v_account, v_container, v_object)
167
    elif request.method == 'GET':
168
        return object_read(request, v_account, v_container, v_object)
169
    elif request.method == 'PUT':
170
        return object_write(request, v_account, v_container, v_object)
171
    elif request.method == 'COPY':
172
        return object_copy(request, v_account, v_container, v_object)
173
    elif request.method == 'MOVE':
174
        return object_move(request, v_account, v_container, v_object)
175
    elif request.method == 'POST':
176
        if request.META.get(
177
                'CONTENT_TYPE', '').startswith('multipart/form-data'):
178
            return object_write_form(request, v_account, v_container, v_object)
179
        return object_update(request, v_account, v_container, v_object)
180
    elif request.method == 'DELETE':
181
        return object_delete(request, v_account, v_container, v_object)
182
    else:
183
        return api.api_method_not_allowed(request, allowed_methods=['HEAD',
184
                                                                    'GET',
185
                                                                    'PUT',
186
                                                                    'COPY',
187
                                                                    'MOVE',
188
                                                                    'POST',
189
                                                                    'DELETE'])
190

    
191

    
192
@api_method('GET', token_required=False, user_required=False, logger=logger)
193
def authenticate(request):
194
    # Normal Response Codes: 204
195
    # Error Response Codes: internalServerError (500),
196
    #                       forbidden (403),
197
    #                       badRequest (400)
198

    
199
    x_auth_user = request.META.get('HTTP_X_AUTH_USER')
200
    x_auth_key = request.META.get('HTTP_X_AUTH_KEY')
201
    if not x_auth_user or not x_auth_key:
202
        raise faults.BadRequest('Missing X-Auth-User or X-Auth-Key header')
203
    response = HttpResponse(status=204)
204

    
205
    uri = request.build_absolute_uri()
206
    if '?' in uri:
207
        uri = uri[:uri.find('?')]
208

    
209
    response['X-Auth-Token'] = x_auth_key
210
    response['X-Storage-Url'] = uri + ('' if uri.endswith('/')
211
                                       else '/') + x_auth_user
212
    return response
213

    
214

    
215
@api_method('GET', format_allowed=True, user_required=True, logger=logger,
216
            serializations=["text", "xml", "json"])
217
def account_list(request):
218
    # Normal Response Codes: 200, 204
219
    # Error Response Codes: internalServerError (500),
220
    #                       badRequest (400)
221
    response = HttpResponse()
222

    
223
    marker = request.GET.get('marker')
224
    limit = get_int_parameter(request.GET.get('limit'))
225
    if not limit:
226
        limit = settings.API_LIST_LIMIT
227

    
228
    accounts = request.backend.list_accounts(request.user_uniq, marker, limit)
229

    
230
    if request.serialization == 'text':
231
        if TRANSLATE_UUIDS:
232
            accounts = retrieve_displaynames(
233
                getattr(request, 'token', None), accounts)
234
        if len(accounts) == 0:
235
            # The cloudfiles python bindings expect 200 if json/xml.
236
            response.status_code = 204
237
            return response
238
        response.status_code = 200
239
        response.content = '\n'.join(accounts) + '\n'
240
        return response
241

    
242
    account_meta = []
243
    for x in accounts:
244
        if x == request.user_uniq:
245
            continue
246
        try:
247
            meta = request.backend.get_account_meta(
248
                request.user_uniq, x, 'pithos', include_user_defined=False)
249
            groups = request.backend.get_account_groups(request.user_uniq, x)
250
        except NotAllowedError:
251
            raise faults.Forbidden('Not allowed')
252
        else:
253
            rename_meta_key(meta, 'modified', 'last_modified')
254
            rename_meta_key(
255
                meta, 'until_timestamp', 'x_account_until_timestamp')
256
            if groups:
257
                meta['X-Account-Group'] = printable_header_dict(
258
                    dict([(k, ','.join(v)) for k, v in groups.iteritems()]))
259
            account_meta.append(printable_header_dict(meta))
260

    
261
    if TRANSLATE_UUIDS:
262
        uuids = list(d['name'] for d in account_meta)
263
        catalog = retrieve_displaynames(
264
            getattr(request, 'token', None), uuids, return_dict=True)
265
        for meta in account_meta:
266
            meta['name'] = catalog.get(meta.get('name'))
267

    
268
    if request.serialization == 'xml':
269
        data = render_to_string('accounts.xml', {'accounts': account_meta})
270
    elif request.serialization == 'json':
271
        data = json.dumps(account_meta)
272
    response.status_code = 200
273
    response.content = data
274
    return response
275

    
276

    
277
@api_method('HEAD', user_required=True, logger=logger)
278
def account_meta(request, v_account):
279
    # Normal Response Codes: 204
280
    # Error Response Codes: internalServerError (500),
281
    #                       forbidden (403),
282
    #                       badRequest (400)
283

    
284
    until = get_int_parameter(request.GET.get('until'))
285
    try:
286
        meta = request.backend.get_account_meta(
287
            request.user_uniq, v_account, 'pithos', until)
288
        groups = request.backend.get_account_groups(
289
            request.user_uniq, v_account)
290

    
291
        if TRANSLATE_UUIDS:
292
            for k in groups:
293
                groups[k] = retrieve_displaynames(
294
                    getattr(request, 'token', None), groups[k])
295
        policy = request.backend.get_account_policy(
296
            request.user_uniq, v_account)
297
    except NotAllowedError:
298
        raise faults.Forbidden('Not allowed')
299

    
300
    validate_modification_preconditions(request, meta)
301

    
302
    response = HttpResponse(status=204)
303
    put_account_headers(response, meta, groups, policy)
304
    return response
305

    
306

    
307
@api_method('POST', user_required=True, logger=logger)
308
def account_update(request, v_account):
309
    # Normal Response Codes: 202
310
    # Error Response Codes: internalServerError (500),
311
    #                       forbidden (403),
312
    #                       badRequest (400)
313

    
314
    meta, groups = get_account_headers(request)
315
    for k in groups:
316
        if TRANSLATE_UUIDS:
317
            try:
318
                groups[k] = retrieve_uuids(
319
                    getattr(request, 'token', None),
320
                    groups[k],
321
                    fail_silently=False)
322
            except ItemNotExists, e:
323
                raise faults.BadRequest(
324
                    'Bad X-Account-Group header value: %s' % e)
325
        else:
326
            try:
327
                retrieve_displaynames(
328
                    getattr(request, 'token', None),
329
                    groups[k],
330
                    fail_silently=False)
331
            except ItemNotExists, e:
332
                raise faults.BadRequest(
333
                    'Bad X-Account-Group header value: %s' % e)
334
    replace = True
335
    if 'update' in request.GET:
336
        replace = False
337
    if groups:
338
        try:
339
            request.backend.update_account_groups(request.user_uniq, v_account,
340
                                                  groups, replace)
341
        except NotAllowedError:
342
            raise faults.Forbidden('Not allowed')
343
        except ValueError:
344
            raise faults.BadRequest('Invalid groups header')
345
    if meta or replace:
346
        try:
347
            request.backend.update_account_meta(request.user_uniq, v_account,
348
                                                'pithos', meta, replace)
349
        except NotAllowedError:
350
            raise faults.Forbidden('Not allowed')
351
    return HttpResponse(status=202)
352

    
353

    
354
@api_method('GET', format_allowed=True, user_required=True, logger=logger,
355
            serializations=["text", "xml", "json"])
356
def container_list(request, v_account):
357
    # Normal Response Codes: 200, 204
358
    # Error Response Codes: internalServerError (500),
359
    #                       itemNotFound (404),
360
    #                       forbidden (403),
361
    #                       badRequest (400)
362

    
363
    until = get_int_parameter(request.GET.get('until'))
364
    try:
365
        meta = request.backend.get_account_meta(
366
            request.user_uniq, v_account, 'pithos', until)
367
        groups = request.backend.get_account_groups(
368
            request.user_uniq, v_account)
369
        policy = request.backend.get_account_policy(
370
            request.user_uniq, v_account)
371
    except NotAllowedError:
372
        raise faults.Forbidden('Not allowed')
373

    
374
    validate_modification_preconditions(request, meta)
375

    
376
    response = HttpResponse()
377
    put_account_headers(response, meta, groups, policy)
378

    
379
    marker = request.GET.get('marker')
380
    limit = get_int_parameter(request.GET.get('limit'))
381
    if not limit:
382
        limit = settings.API_LIST_LIMIT
383

    
384
    shared = False
385
    if 'shared' in request.GET:
386
        shared = True
387

    
388
    public_requested = 'public' in request.GET
389
    public_granted = public_requested and request.user_uniq == v_account
390

    
391
    if public_requested and not public_granted:
392
        raise faults.Forbidden(
393
            'PUblic container listing is not allowed to non path owners')
394

    
395
    try:
396
        containers = request.backend.list_containers(
397
            request.user_uniq, v_account,
398
            marker, limit, shared, until, public_granted)
399
    except NotAllowedError:
400
        raise faults.Forbidden('Not allowed')
401
    except NameError:
402
        containers = []
403

    
404
    if request.serialization == 'text':
405
        if len(containers) == 0:
406
            # The cloudfiles python bindings expect 200 if json/xml.
407
            response.status_code = 204
408
            return response
409
        response.status_code = 200
410
        response.content = '\n'.join(containers) + '\n'
411
        return response
412

    
413
    container_meta = []
414
    for x in containers:
415
        try:
416
            meta = request.backend.get_container_meta(
417
                request.user_uniq, v_account,
418
                x, 'pithos', until, include_user_defined=False)
419
            policy = request.backend.get_container_policy(request.user_uniq,
420
                                                          v_account, x)
421
        except NotAllowedError:
422
            raise faults.Forbidden('Not allowed')
423
        except NameError:
424
            pass
425
        else:
426
            rename_meta_key(meta, 'modified', 'last_modified')
427
            rename_meta_key(
428
                meta, 'until_timestamp', 'x_container_until_timestamp')
429
            if policy:
430
                meta['X-Container-Policy'] = printable_header_dict(
431
                    dict([(k, v) for k, v in policy.iteritems()]))
432
            container_meta.append(printable_header_dict(meta))
433
    if request.serialization == 'xml':
434
        data = render_to_string('containers.xml', {'account':
435
                                v_account, 'containers': container_meta})
436
    elif request.serialization == 'json':
437
        data = json.dumps(container_meta)
438
    response.status_code = 200
439
    response.content = data
440
    return response
441

    
442

    
443
@api_method('HEAD', user_required=True, logger=logger)
444
def container_meta(request, v_account, v_container):
445
    # Normal Response Codes: 204
446
    # Error Response Codes: internalServerError (500),
447
    #                       itemNotFound (404),
448
    #                       forbidden (403),
449
    #                       badRequest (400)
450

    
451
    until = get_int_parameter(request.GET.get('until'))
452
    try:
453
        meta = request.backend.get_container_meta(request.user_uniq, v_account,
454
                                                  v_container, 'pithos', until)
455
        meta['object_meta'] = \
456
            request.backend.list_container_meta(request.user_uniq,
457
                                                v_account, v_container,
458
                                                'pithos', until)
459
        policy = request.backend.get_container_policy(
460
            request.user_uniq, v_account,
461
            v_container)
462
    except NotAllowedError:
463
        raise faults.Forbidden('Not allowed')
464
    except ItemNotExists:
465
        raise faults.ItemNotFound('Container does not exist')
466

    
467
    validate_modification_preconditions(request, meta)
468

    
469
    response = HttpResponse(status=204)
470
    put_container_headers(request, response, meta, policy)
471
    return response
472

    
473

    
474
@api_method('PUT', user_required=True, logger=logger)
475
def container_create(request, v_account, v_container):
476
    # Normal Response Codes: 201, 202
477
    # Error Response Codes: internalServerError (500),
478
    #                       itemNotFound (404),
479
    #                       forbidden (403),
480
    #                       badRequest (400)
481

    
482
    meta, policy = get_container_headers(request)
483

    
484
    try:
485
        request.backend.put_container(
486
            request.user_uniq, v_account, v_container, policy)
487
        ret = 201
488
    except NotAllowedError:
489
        raise faults.Forbidden('Not allowed')
490
    except ValueError:
491
        raise faults.BadRequest('Invalid policy header')
492
    except ContainerExists:
493
        ret = 202
494

    
495
    if ret == 202 and policy:
496
        try:
497
            request.backend.update_container_policy(
498
                request.user_uniq, v_account,
499
                v_container, policy, replace=False)
500
        except NotAllowedError:
501
            raise faults.Forbidden('Not allowed')
502
        except ItemNotExists:
503
            raise faults.ItemNotFound('Container does not exist')
504
        except ValueError:
505
            raise faults.BadRequest('Invalid policy header')
506
    if meta:
507
        try:
508
            request.backend.update_container_meta(request.user_uniq, v_account,
509
                                                  v_container, 'pithos',
510
                                                  meta, replace=False)
511
        except NotAllowedError:
512
            raise faults.Forbidden('Not allowed')
513
        except ItemNotExists:
514
            raise faults.ItemNotFound('Container does not exist')
515

    
516
    return HttpResponse(status=ret)
517

    
518

    
519
@api_method('POST', format_allowed=True, user_required=True, logger=logger,
520
            lock_container_path=True)
521
def container_update(request, v_account, v_container):
522
    # Normal Response Codes: 202
523
    # Error Response Codes: internalServerError (500),
524
    #                       itemNotFound (404),
525
    #                       forbidden (403),
526
    #                       badRequest (400)
527

    
528
    meta, policy = get_container_headers(request)
529
    replace = True
530
    if 'update' in request.GET:
531
        replace = False
532
    if policy:
533
        try:
534
            request.backend.update_container_policy(
535
                request.user_uniq, v_account,
536
                v_container, policy, replace)
537
        except NotAllowedError:
538
            raise faults.Forbidden('Not allowed')
539
        except ItemNotExists:
540
            raise faults.ItemNotFound('Container does not exist')
541
        except ValueError:
542
            raise faults.BadRequest('Invalid policy header')
543
    if meta or replace:
544
        try:
545
            request.backend.update_container_meta(request.user_uniq, v_account,
546
                                                  v_container, 'pithos',
547
                                                  meta, replace)
548
        except NotAllowedError:
549
            raise faults.Forbidden('Not allowed')
550
        except ItemNotExists:
551
            raise faults.ItemNotFound('Container does not exist')
552

    
553
    content_length = -1
554
    if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
555
        content_length = get_int_parameter(
556
            request.META.get('CONTENT_LENGTH', 0))
557
    content_type = request.META.get('CONTENT_TYPE')
558
    hashmap = []
559
    if (content_type
560
            and content_type == 'application/octet-stream'
561
            and content_length != 0):
562
        for data in socket_read_iterator(request, content_length,
563
                                         request.backend.block_size):
564
            # TODO: Raise 408 (Request Timeout) if this takes too long.
565
            # TODO: Raise 499 (Client Disconnect) if a length is defined
566
            #       and we stop before getting this much data.
567
            hashmap.append(request.backend.put_block(data))
568

    
569
    response = HttpResponse(status=202)
570
    if hashmap:
571
        response.content = simple_list_response(request, hashmap)
572
    return response
573

    
574

    
575
@api_method('DELETE', user_required=True, logger=logger,
576
            lock_container_path=True)
577
def container_delete(request, v_account, v_container):
578
    # Normal Response Codes: 204
579
    # Error Response Codes: internalServerError (500),
580
    #                       conflict (409),
581
    #                       itemNotFound (404),
582
    #                       forbidden (403),
583
    #                       badRequest (400)
584
    #                       requestentitytoolarge (413)
585

    
586
    until = get_int_parameter(request.GET.get('until'))
587

    
588
    delimiter = request.GET.get('delimiter')
589

    
590
    try:
591
        request.backend.delete_container(
592
            request.user_uniq, v_account, v_container,
593
            until, delimiter=delimiter, listing_limit=settings.API_LIST_LIMIT)
594
    except NotAllowedError:
595
        raise faults.Forbidden('Not allowed')
596
    except ItemNotExists:
597
        raise faults.ItemNotFound('Container does not exist')
598
    except ContainerNotEmpty:
599
        raise faults.Conflict('Container is not empty')
600
    except QuotaError, e:
601
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
602
    return HttpResponse(status=204)
603

    
604

    
605
@api_method('GET', format_allowed=True, user_required=True, logger=logger,
606
            serializations=["text", "xml", "json"])
607
def object_list(request, v_account, v_container):
608
    # Normal Response Codes: 200, 204
609
    # Error Response Codes: internalServerError (500),
610
    #                       itemNotFound (404),
611
    #                       forbidden (403),
612
    #                       badRequest (400)
613

    
614
    until = get_int_parameter(request.GET.get('until'))
615
    try:
616
        meta = request.backend.get_container_meta(request.user_uniq, v_account,
617
                                                  v_container, 'pithos', until)
618
        meta['object_meta'] = \
619
            request.backend.list_container_meta(request.user_uniq,
620
                                                v_account, v_container,
621
                                                'pithos', until)
622
        policy = request.backend.get_container_policy(
623
            request.user_uniq, v_account,
624
            v_container)
625
    except NotAllowedError:
626
        raise faults.Forbidden('Not allowed')
627
    except ItemNotExists:
628
        raise faults.ItemNotFound('Container does not exist')
629

    
630
    validate_modification_preconditions(request, meta)
631

    
632
    response = HttpResponse()
633
    put_container_headers(request, response, meta, policy)
634

    
635
    path = request.GET.get('path')
636
    prefix = request.GET.get('prefix')
637
    delimiter = request.GET.get('delimiter')
638

    
639
    # Path overrides prefix and delimiter.
640
    virtual = True
641
    if path:
642
        prefix = path
643
        delimiter = '/'
644
        virtual = False
645

    
646
    # Naming policy.
647
    if prefix and delimiter and not prefix.endswith(delimiter):
648
        prefix = prefix + delimiter
649
    if not prefix:
650
        prefix = ''
651
    prefix = prefix.lstrip('/')
652

    
653
    marker = request.GET.get('marker')
654
    limit = get_int_parameter(request.GET.get('limit'))
655
    if not limit:
656
        limit = settings.API_LIST_LIMIT
657

    
658
    keys = request.GET.get('meta')
659
    if keys:
660
        keys = [smart_str(x.strip()) for x in keys.split(',')
661
                if x.strip() != '']
662
        included, excluded, opers = parse_filters(keys)
663
        keys = []
664
        keys += [format_header_key('X-Object-Meta-' + x) for x in included]
665
        keys += ['!' + format_header_key('X-Object-Meta-' + x)
666
                 for x in excluded]
667
        keys += ['%s%s%s' % (format_header_key(
668
            'X-Object-Meta-' + k), o, v) for k, o, v in opers]
669
    else:
670
        keys = []
671

    
672
    shared = False
673
    if 'shared' in request.GET:
674
        shared = True
675

    
676
    public_requested = 'public' in request.GET
677
    public_granted = public_requested and request.user_uniq == v_account
678

    
679
    if public_requested and not public_granted:
680
        raise faults.Forbidden(
681
            'PUblic object listing is not allowed to non path owners')
682

    
683
    if request.serialization == 'text':
684
        try:
685
            objects = request.backend.list_objects(
686
                request.user_uniq, v_account,
687
                v_container, prefix, delimiter, marker,
688
                limit, virtual, 'pithos', keys, shared,
689
                until, None, public_granted)
690
        except NotAllowedError:
691
            raise faults.Forbidden('Not allowed')
692
        except ItemNotExists:
693
            raise faults.ItemNotFound('Container does not exist')
694

    
695
        if len(objects) == 0:
696
            # The cloudfiles python bindings expect 200 if json/xml.
697
            response.status_code = 204
698
            return response
699
        response.status_code = 200
700
        response.content = '\n'.join([x[0] for x in objects]) + '\n'
701
        return response
702

    
703
    try:
704
        objects = request.backend.list_object_meta(
705
            request.user_uniq, v_account, v_container, prefix, delimiter,
706
            marker, limit, virtual, 'pithos', keys, shared, until, None,
707
            public_granted)
708
        object_permissions = {}
709
        object_public = {}
710
        if until is None:
711
            name = '/'.join((v_account, v_container, ''))
712
            name_idx = len(name)
713
            objects_bulk = []
714
            for x in request.backend.list_object_permissions(
715
                    request.user_uniq, v_account, v_container, prefix):
716

    
717
                # filter out objects which are not under the container
718
                if name != x[:name_idx]:
719
                    continue
720
                objects_bulk.append(x[name_idx:])
721

    
722
            if len(objects_bulk) > 0:
723
                object_permissions = \
724
                    request.backend.get_object_permissions_bulk(
725
                        request.user_uniq, v_account, v_container,
726
                        objects_bulk)
727

    
728
            if request.user_uniq == v_account:
729
                # Bring public information only if the request user
730
                # is the object owner
731
                for k, v in request.backend.list_object_public(
732
                        request.user_uniq, v_account,
733
                        v_container, prefix).iteritems():
734
                    object_public[k[name_idx:]] = v
735
    except NotAllowedError:
736
        raise faults.Forbidden('Not allowed')
737
    except ItemNotExists:
738
        raise faults.ItemNotFound('Container does not exist')
739

    
740
    object_meta = []
741
    for meta in objects:
742
        if TRANSLATE_UUIDS:
743
            modified_by = meta.get('modified_by')
744
            if modified_by:
745
                l = retrieve_displaynames(
746
                    getattr(request, 'token', None), [meta['modified_by']])
747
                if l is not None and len(l) == 1:
748
                    meta['modified_by'] = l[0]
749

    
750
        if len(meta) == 1:
751
            # Virtual objects/directories.
752
            object_meta.append(meta)
753
        else:
754
            rename_meta_key(
755
                meta, 'hash', 'x_object_hash')  # Will be replaced by checksum.
756
            rename_meta_key(meta, 'checksum', 'hash')
757
            rename_meta_key(meta, 'type', 'content_type')
758
            rename_meta_key(meta, 'uuid', 'x_object_uuid')
759
            if until is not None and 'modified' in meta:
760
                del(meta['modified'])
761
            else:
762
                rename_meta_key(meta, 'modified', 'last_modified')
763
            rename_meta_key(meta, 'modified_by', 'x_object_modified_by')
764
            rename_meta_key(meta, 'version', 'x_object_version')
765
            rename_meta_key(
766
                meta, 'version_timestamp', 'x_object_version_timestamp')
767
            permissions = object_permissions.get(meta['name'], None)
768
            if permissions:
769
                update_sharing_meta(request, permissions, v_account,
770
                                    v_container, meta['name'], meta)
771
            public_url = object_public.get(meta['name'], None)
772
            if request.user_uniq == v_account:
773
                # Return public information only if the request user
774
                # is the object owner
775
                update_public_meta(public_url, meta)
776
            object_meta.append(printable_header_dict(meta))
777

    
778
    if request.serialization == 'xml':
779
        data = render_to_string(
780
            'objects.xml', {'container': v_container, 'objects': object_meta})
781
    elif request.serialization == 'json':
782
        data = json.dumps(object_meta, default=json_encode_decimal)
783
    response.status_code = 200
784
    response.content = data
785
    return response
786

    
787

    
788
@api_method('HEAD', user_required=True, logger=logger)
789
def object_meta(request, v_account, v_container, v_object):
790
    # Normal Response Codes: 204
791
    # Error Response Codes: internalServerError (500),
792
    #                       itemNotFound (404),
793
    #                       forbidden (403),
794
    #                       badRequest (400)
795

    
796
    version = request.GET.get('version')
797
    try:
798
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
799
                                               v_container, v_object,
800
                                               'pithos', version)
801
        if version is None:
802
            permissions = request.backend.get_object_permissions(
803
                request.user_uniq,
804
                v_account, v_container, v_object)
805
            public = request.backend.get_object_public(
806
                request.user_uniq, v_account,
807
                v_container, v_object)
808
        else:
809
            permissions = None
810
            public = None
811
    except NotAllowedError:
812
        raise faults.Forbidden('Not allowed')
813
    except ItemNotExists:
814
        raise faults.ItemNotFound('Object does not exist')
815
    except VersionNotExists:
816
        raise faults.ItemNotFound('Version does not exist')
817

    
818
    update_manifest_meta(request, v_account, meta)
819
    update_sharing_meta(
820
        request, permissions, v_account, v_container, v_object, meta)
821
    if request.user_uniq == v_account:
822
        update_public_meta(public, meta)
823

    
824
    # Evaluate conditions.
825
    validate_modification_preconditions(request, meta)
826
    try:
827
        validate_matching_preconditions(request, meta)
828
    except faults.NotModified:
829
        response = HttpResponse(status=304)
830
        response['ETag'] = meta['hash'] if not UPDATE_MD5 else meta['checksum']
831
        return response
832

    
833
    response = HttpResponse(status=200)
834
    put_object_headers(response, meta, token=getattr(request, 'token', None))
835
    return response
836

    
837

    
838
@api_method('GET', format_allowed=True, user_required=True, logger=logger)
839
def object_read(request, v_account, v_container, v_object):
840
    return _object_read(request, v_account, v_container, v_object)
841

    
842

    
843
def _object_read(request, v_account, v_container, v_object):
844
    # Normal Response Codes: 200, 206
845
    # Error Response Codes: internalServerError (500),
846
    #                       rangeNotSatisfiable (416),
847
    #                       preconditionFailed (412),
848
    #                       itemNotFound (404),
849
    #                       forbidden (403),
850
    #                       badRequest (400),
851
    #                       notModified (304)
852

    
853
    version = request.GET.get('version')
854

    
855
    # Reply with the version list. Do this first, as the object may be deleted.
856
    if version == 'list':
857
        if request.serialization == 'text':
858
            raise faults.BadRequest('No format specified for version list.')
859

    
860
        try:
861
            v = request.backend.list_versions(request.user_uniq, v_account,
862
                                              v_container, v_object)
863
        except NotAllowedError:
864
            raise faults.Forbidden('Not allowed')
865
        except ItemNotExists:
866
            raise faults.ItemNotFound('Object does not exist')
867
        d = {'versions': v}
868
        if request.serialization == 'xml':
869
            d['object'] = v_object
870
            data = render_to_string('versions.xml', d)
871
        elif request.serialization == 'json':
872
            data = json.dumps(d, default=json_encode_decimal)
873

    
874
        response = HttpResponse(data, status=200)
875
        response['Content-Length'] = len(data)
876
        return response
877

    
878
    try:
879
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
880
                                               v_container, v_object,
881
                                               'pithos', version)
882
        if version is None:
883
            permissions = request.backend.get_object_permissions(
884
                request.user_uniq,
885
                v_account, v_container, v_object)
886
            public = request.backend.get_object_public(
887
                request.user_uniq, v_account,
888
                v_container, v_object)
889
        else:
890
            permissions = None
891
            public = None
892
    except NotAllowedError:
893
        raise faults.Forbidden('Not allowed')
894
    except ItemNotExists:
895
        raise faults.ItemNotFound('Object does not exist')
896
    except VersionNotExists:
897
        raise faults.ItemNotFound('Version does not exist')
898

    
899
    update_manifest_meta(request, v_account, meta)
900
    update_sharing_meta(
901
        request, permissions, v_account, v_container, v_object, meta)
902
    if request.user_uniq == v_account:
903
        update_public_meta(public, meta)
904

    
905
    # Evaluate conditions.
906
    validate_modification_preconditions(request, meta)
907
    try:
908
        validate_matching_preconditions(request, meta)
909
    except faults.NotModified:
910
        response = HttpResponse(status=304)
911
        response['ETag'] = meta['hash'] if not UPDATE_MD5 else meta['checksum']
912
        return response
913

    
914
    hashmap_reply = False
915
    if 'hashmap' in request.GET and request.serialization != 'text':
916
        hashmap_reply = True
917

    
918
    sizes = []
919
    hashmaps = []
920
    if 'X-Object-Manifest' in meta and not hashmap_reply:
921
        try:
922
            src_container, src_name = split_container_object_string(
923
                '/' + meta['X-Object-Manifest'])
924
            objects = request.backend.list_objects(
925
                request.user_uniq, v_account,
926
                src_container, prefix=src_name, virtual=False)
927
        except NotAllowedError:
928
            raise faults.Forbidden('Not allowed')
929
        except ValueError:
930
            raise faults.BadRequest('Invalid X-Object-Manifest header')
931
        except ItemNotExists:
932
            raise faults.ItemNotFound('Container does not exist')
933

    
934
        try:
935
            for x in objects:
936
                s, h = \
937
                    request.backend.get_object_hashmap(
938
                        request.user_uniq, v_account, src_container, x[0],
939
                        x[1])
940
                sizes.append(s)
941
                hashmaps.append(h)
942
        except NotAllowedError:
943
            raise faults.Forbidden('Not allowed')
944
        except ItemNotExists:
945
            raise faults.ItemNotFound('Object does not exist')
946
        except VersionNotExists:
947
            raise faults.ItemNotFound('Version does not exist')
948
    else:
949
        try:
950
            s, h = request.backend.get_object_hashmap(
951
                request.user_uniq, v_account,
952
                v_container, v_object, version)
953
            sizes.append(s)
954
            hashmaps.append(h)
955
        except NotAllowedError:
956
            raise faults.Forbidden('Not allowed')
957
        except ItemNotExists:
958
            raise faults.ItemNotFound('Object does not exist')
959
        except VersionNotExists:
960
            raise faults.ItemNotFound('Version does not exist')
961

    
962
    # Reply with the hashmap.
963
    if hashmap_reply:
964
        size = sum(sizes)
965
        hashmap = sum(hashmaps, [])
966
        d = {
967
            'block_size': request.backend.block_size,
968
            'block_hash': request.backend.hash_algorithm,
969
            'bytes': size,
970
            'hashes': hashmap}
971
        if request.serialization == 'xml':
972
            d['object'] = v_object
973
            data = render_to_string('hashes.xml', d)
974
        elif request.serialization == 'json':
975
            data = json.dumps(d)
976

    
977
        response = HttpResponse(data, status=200)
978
        put_object_headers(
979
            response, meta, token=getattr(request, 'token', None))
980
        response['Content-Length'] = len(data)
981
        return response
982

    
983
    request.serialization = 'text'  # Unset.
984
    return object_data_response(request, sizes, hashmaps, meta)
985

    
986

    
987
@api_method('PUT', format_allowed=True, user_required=True, logger=logger,
988
            lock_container_path=True)
989
def object_write(request, v_account, v_container, v_object):
990
    # Normal Response Codes: 201
991
    # Error Response Codes: internalServerError (500),
992
    #                       unprocessableEntity (422),
993
    #                       lengthRequired (411),
994
    #                       conflict (409),
995
    #                       itemNotFound (404),
996
    #                       forbidden (403),
997
    #                       badRequest (400)
998
    #                       requestentitytoolarge (413)
999

    
1000
    # Evaluate conditions.
1001
    if (request.META.get('HTTP_IF_MATCH')
1002
            or request.META.get('HTTP_IF_NONE_MATCH')):
1003
        try:
1004
            meta = request.backend.get_object_meta(
1005
                request.user_uniq, v_account,
1006
                v_container, v_object, 'pithos')
1007
        except NotAllowedError:
1008
            raise faults.Forbidden('Not allowed')
1009
        except NameError:
1010
            meta = {}
1011
        validate_matching_preconditions(request, meta)
1012

    
1013
    copy_from = request.META.get('HTTP_X_COPY_FROM')
1014
    move_from = request.META.get('HTTP_X_MOVE_FROM')
1015
    if copy_from or move_from:
1016
        delimiter = request.GET.get('delimiter')
1017
        content_length = get_content_length(request)  # Required by the API.
1018

    
1019
        src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
1020

    
1021
        if not src_account:
1022
            src_account = request.user_uniq
1023
        else:
1024
            if TRANSLATE_UUIDS:
1025
                try:
1026
                    src_account = retrieve_uuid(
1027
                        getattr(request, 'token', None), src_account)
1028
                except ItemNotExists:
1029
                    faults.ItemNotFound('Invalid source account')
1030

    
1031
        if move_from:
1032
            try:
1033
                src_container, src_name = split_container_object_string(
1034
                    move_from)
1035
            except ValueError:
1036
                raise faults.BadRequest('Invalid X-Move-From header')
1037
            version_id = copy_or_move_object(
1038
                request, src_account, src_container, src_name,
1039
                v_account, v_container, v_object,
1040
                move=True, delimiter=delimiter,
1041
                listing_limit=settings.API_LIST_LIMIT)
1042
        else:
1043
            try:
1044
                src_container, src_name = split_container_object_string(
1045
                    copy_from)
1046
            except ValueError:
1047
                raise faults.BadRequest('Invalid X-Copy-From header')
1048
            version_id = copy_or_move_object(
1049
                request, src_account, src_container, src_name,
1050
                v_account, v_container, v_object,
1051
                move=False, delimiter=delimiter,
1052
                listing_limit=settings.API_LIST_LIMIT)
1053
        response = HttpResponse(status=201)
1054
        response['X-Object-Version'] = version_id
1055
        return response
1056

    
1057
    content_type, meta, permissions, public = get_object_headers(request)
1058
    content_length = -1
1059
    if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
1060
        content_length = get_content_length(request)
1061
    # Should be BadRequest, but API says otherwise.
1062
    if content_type is None:
1063
        raise faults.LengthRequired('Missing Content-Type header')
1064

    
1065
    if 'hashmap' in request.GET:
1066
        if request.serialization not in ('json', 'xml'):
1067
            raise faults.BadRequest('Invalid hashmap format')
1068

    
1069
        data = ''
1070
        for block in socket_read_iterator(request, content_length,
1071
                                          request.backend.block_size):
1072
            data = '%s%s' % (data, block)
1073

    
1074
        if request.serialization == 'json':
1075
            d = json.loads(data)
1076
            if not hasattr(d, '__getitem__'):
1077
                raise faults.BadRequest('Invalid data formating')
1078
            try:
1079
                hashmap = d['hashes']
1080
                size = int(d['bytes'])
1081
            except:
1082
                raise faults.BadRequest('Invalid data formatting')
1083
        elif request.serialization == 'xml':
1084
            try:
1085
                xml = minidom.parseString(data)
1086
                obj = xml.getElementsByTagName('object')[0]
1087
                size = int(obj.attributes['bytes'].value)
1088

    
1089
                hashes = xml.getElementsByTagName('hash')
1090
                hashmap = []
1091
                for hash in hashes:
1092
                    hashmap.append(hash.firstChild.data)
1093
            except:
1094
                raise faults.BadRequest('Invalid data formatting')
1095

    
1096
        checksum = ''  # Do not set to None (will copy previous value).
1097
    else:
1098
        etag = request.META.get('HTTP_ETAG')
1099
        checksum_compute = Checksum() if etag or UPDATE_MD5 else NoChecksum()
1100
        size = 0
1101
        hashmap = []
1102
        for data in socket_read_iterator(request, content_length,
1103
                                         request.backend.block_size):
1104
            # TODO: Raise 408 (Request Timeout) if this takes too long.
1105
            # TODO: Raise 499 (Client Disconnect) if a length is defined
1106
            #       and we stop before getting this much data.
1107
            size += len(data)
1108
            hashmap.append(request.backend.put_block(data))
1109
            checksum_compute.update(data)
1110

    
1111
        checksum = checksum_compute.hexdigest()
1112
        if etag and parse_etags(etag)[0].lower() != checksum:
1113
            raise faults.UnprocessableEntity('Object ETag does not match')
1114

    
1115
    try:
1116
        version_id, merkle = request.backend.update_object_hashmap(
1117
            request.user_uniq, v_account, v_container, v_object, size,
1118
            content_type, hashmap, checksum, 'pithos', meta, True, permissions
1119
        )
1120
    except NotAllowedError:
1121
        raise faults.Forbidden('Not allowed')
1122
    except IndexError, e:
1123
        missing_blocks = e.data
1124
        response = HttpResponse(status=409)
1125
        response.content = simple_list_response(request, missing_blocks)
1126
        return response
1127
    except ItemNotExists:
1128
        raise faults.ItemNotFound('Container does not exist')
1129
    except ValueError:
1130
        raise faults.BadRequest('Invalid sharing header')
1131
    except QuotaError, e:
1132
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
1133
    except InvalidHash, e:
1134
        raise faults.BadRequest('Invalid hash: %s' % e)
1135
    if not checksum and UPDATE_MD5:
1136
        # Update the MD5 after the hashmap, as there may be missing hashes.
1137
        checksum = hashmap_md5(request.backend, hashmap, size)
1138
        try:
1139
            request.backend.update_object_checksum(request.user_uniq,
1140
                                                   v_account, v_container,
1141
                                                   v_object, version_id,
1142
                                                   checksum)
1143
        except NotAllowedError:
1144
            raise faults.Forbidden('Not allowed')
1145
    if public is not None:
1146
        try:
1147
            request.backend.update_object_public(request.user_uniq, v_account,
1148
                                                 v_container, v_object, public)
1149
        except NotAllowedError:
1150
            raise faults.Forbidden('Not allowed')
1151
        except ItemNotExists:
1152
            raise faults.ItemNotFound('Object does not exist')
1153

    
1154
    response = HttpResponse(status=201)
1155
    response['ETag'] = merkle if not UPDATE_MD5 else checksum
1156
    response['X-Object-Version'] = version_id
1157
    return response
1158

    
1159

    
1160
@api_method('POST', user_required=True, logger=logger,
1161
            lock_container_path=True)
1162
def object_write_form(request, v_account, v_container, v_object):
1163
    # Normal Response Codes: 201
1164
    # Error Response Codes: internalServerError (500),
1165
    #                       itemNotFound (404),
1166
    #                       forbidden (403),
1167
    #                       badRequest (400)
1168
    #                       requestentitytoolarge (413)
1169

    
1170
    request.upload_handlers = [SaveToBackendHandler(request)]
1171
    if 'X-Object-Data' not in request.FILES:
1172
        raise faults.BadRequest('Missing X-Object-Data field')
1173
    file = request.FILES['X-Object-Data']
1174

    
1175
    checksum = file.etag
1176
    try:
1177
        version_id, merkle = request.backend.update_object_hashmap(
1178
            request.user_uniq, v_account, v_container, v_object, file.size,
1179
            file.content_type, file.hashmap, checksum, 'pithos', {}, True
1180
        )
1181
    except NotAllowedError:
1182
        raise faults.Forbidden('Not allowed')
1183
    except ItemNotExists:
1184
        raise faults.ItemNotFound('Container does not exist')
1185
    except QuotaError, e:
1186
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
1187

    
1188
    response = HttpResponse(status=201)
1189
    response['ETag'] = merkle if not UPDATE_MD5 else checksum
1190
    response['X-Object-Version'] = version_id
1191
    response.content = checksum
1192
    return response
1193

    
1194

    
1195
@api_method('COPY', format_allowed=True, user_required=True, logger=logger,
1196
            lock_container_path=True)
1197
def object_copy(request, v_account, v_container, v_object):
1198
    # Normal Response Codes: 201
1199
    # Error Response Codes: internalServerError (500),
1200
    #                       itemNotFound (404),
1201
    #                       forbidden (403),
1202
    #                       badRequest (400)
1203
    #                       requestentitytoolarge (413)
1204

    
1205
    dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
1206
    if not dest_account:
1207
        dest_account = request.user_uniq
1208
    dest_path = request.META.get('HTTP_DESTINATION')
1209
    if not dest_path:
1210
        raise faults.BadRequest('Missing Destination header')
1211
    try:
1212
        dest_container, dest_name = split_container_object_string(dest_path)
1213
    except ValueError:
1214
        raise faults.BadRequest('Invalid Destination header')
1215

    
1216
    # Evaluate conditions.
1217
    if (request.META.get('HTTP_IF_MATCH')
1218
            or request.META.get('HTTP_IF_NONE_MATCH')):
1219
        src_version = request.META.get('HTTP_X_SOURCE_VERSION')
1220
        try:
1221
            meta = request.backend.get_object_meta(
1222
                request.user_uniq, v_account,
1223
                v_container, v_object, 'pithos', src_version)
1224
        except NotAllowedError:
1225
            raise faults.Forbidden('Not allowed')
1226
        except (ItemNotExists, VersionNotExists):
1227
            raise faults.ItemNotFound('Container or object does not exist')
1228
        validate_matching_preconditions(request, meta)
1229

    
1230
    delimiter = request.GET.get('delimiter')
1231

    
1232
    version_id = copy_or_move_object(request, v_account, v_container, v_object,
1233
                                     dest_account, dest_container, dest_name,
1234
                                     move=False, delimiter=delimiter,
1235
                                     listing_limit=settings.API_LIST_LIMIT)
1236
    response = HttpResponse(status=201)
1237
    response['X-Object-Version'] = version_id
1238
    return response
1239

    
1240

    
1241
@api_method('MOVE', format_allowed=True, user_required=True, logger=logger,
1242
            lock_container_path=True)
1243
def object_move(request, v_account, v_container, v_object):
1244
    # Normal Response Codes: 201
1245
    # Error Response Codes: internalServerError (500),
1246
    #                       itemNotFound (404),
1247
    #                       forbidden (403),
1248
    #                       badRequest (400)
1249
    #                       requestentitytoolarge (413)
1250

    
1251
    dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
1252
    if not dest_account:
1253
        dest_account = request.user_uniq
1254
    dest_path = request.META.get('HTTP_DESTINATION')
1255
    if not dest_path:
1256
        raise faults.BadRequest('Missing Destination header')
1257
    try:
1258
        dest_container, dest_name = split_container_object_string(dest_path)
1259
    except ValueError:
1260
        raise faults.BadRequest('Invalid Destination header')
1261

    
1262
    # Evaluate conditions.
1263
    if (request.META.get('HTTP_IF_MATCH')
1264
            or request.META.get('HTTP_IF_NONE_MATCH')):
1265
        try:
1266
            meta = request.backend.get_object_meta(
1267
                request.user_uniq, v_account,
1268
                v_container, v_object, 'pithos')
1269
        except NotAllowedError:
1270
            raise faults.Forbidden('Not allowed')
1271
        except ItemNotExists:
1272
            raise faults.ItemNotFound('Container or object does not exist')
1273
        validate_matching_preconditions(request, meta)
1274

    
1275
    delimiter = request.GET.get('delimiter')
1276

    
1277
    version_id = copy_or_move_object(request, v_account, v_container, v_object,
1278
                                     dest_account, dest_container, dest_name,
1279
                                     move=True, delimiter=delimiter)
1280
    response = HttpResponse(status=201)
1281
    response['X-Object-Version'] = version_id
1282
    return response
1283

    
1284

    
1285
@api_method('POST', format_allowed=True, user_required=True, logger=logger,
1286
            lock_container_path=True)
1287
def object_update(request, v_account, v_container, v_object):
1288
    # Normal Response Codes: 202, 204
1289
    # Error Response Codes: internalServerError (500),
1290
    #                       conflict (409),
1291
    #                       itemNotFound (404),
1292
    #                       forbidden (403),
1293
    #                       badRequest (400)
1294

    
1295
    content_type, meta, permissions, public = get_object_headers(request)
1296

    
1297
    try:
1298
        prev_meta = request.backend.get_object_meta(
1299
            request.user_uniq, v_account,
1300
            v_container, v_object, 'pithos')
1301
    except NotAllowedError:
1302
        raise faults.Forbidden('Not allowed')
1303
    except ItemNotExists:
1304
        raise faults.ItemNotFound('Object does not exist')
1305

    
1306
    # Evaluate conditions.
1307
    if (request.META.get('HTTP_IF_MATCH')
1308
            or request.META.get('HTTP_IF_NONE_MATCH')):
1309
        validate_matching_preconditions(request, prev_meta)
1310

    
1311
    replace = True
1312
    if 'update' in request.GET:
1313
        replace = False
1314

    
1315
    # A Content-Type or X-Source-Object header indicates data updates.
1316
    src_object = request.META.get('HTTP_X_SOURCE_OBJECT')
1317
    if ((not content_type or content_type != 'application/octet-stream')
1318
            and not src_object):
1319
        response = HttpResponse(status=202)
1320

    
1321
        # Do permissions first, as it may fail easier.
1322
        if permissions is not None:
1323
            try:
1324
                request.backend.update_object_permissions(
1325
                    request.user_uniq, v_account, v_container, v_object,
1326
                    permissions)
1327
            except NotAllowedError:
1328
                raise faults.Forbidden('Not allowed')
1329
            except ItemNotExists:
1330
                raise faults.ItemNotFound('Object does not exist')
1331
            except ValueError:
1332
                raise faults.BadRequest('Invalid sharing header')
1333
        if public is not None:
1334
            try:
1335
                request.backend.update_object_public(
1336
                    request.user_uniq, v_account,
1337
                    v_container, v_object, public)
1338
            except NotAllowedError:
1339
                raise faults.Forbidden('Not allowed')
1340
            except ItemNotExists:
1341
                raise faults.ItemNotFound('Object does not exist')
1342
        if meta or replace:
1343
            try:
1344
                version_id = request.backend.update_object_meta(
1345
                    request.user_uniq,
1346
                    v_account, v_container, v_object, 'pithos', meta, replace)
1347
            except NotAllowedError:
1348
                raise faults.Forbidden('Not allowed')
1349
            except ItemNotExists:
1350
                raise faults.ItemNotFound('Object does not exist')
1351
            response['X-Object-Version'] = version_id
1352

    
1353
        return response
1354

    
1355
    # Single range update. Range must be in Content-Range.
1356
    # Based on: http://code.google.com/p/gears/wiki/ContentRangePostProposal
1357
    # (with the addition that '*' is allowed for the range - will append).
1358
    content_range = request.META.get('HTTP_CONTENT_RANGE')
1359
    if not content_range:
1360
        raise faults.BadRequest('Missing Content-Range header')
1361
    ranges = get_content_range(request)
1362
    if not ranges:
1363
        raise faults.RangeNotSatisfiable('Invalid Content-Range header')
1364

    
1365
    try:
1366
        size, hashmap = \
1367
            request.backend.get_object_hashmap(
1368
                request.user_uniq, v_account, v_container, v_object)
1369
    except NotAllowedError:
1370
        raise faults.Forbidden('Not allowed')
1371
    except ItemNotExists:
1372
        raise faults.ItemNotFound('Object does not exist')
1373

    
1374
    offset, length, total = ranges
1375
    if offset is None:
1376
        offset = size
1377
    elif offset > size:
1378
        raise faults.RangeNotSatisfiable(
1379
            'Supplied offset is beyond object limits')
1380
    if src_object:
1381
        src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
1382
        if not src_account:
1383
            src_account = request.user_uniq
1384
        try:
1385
            src_container, src_name = split_container_object_string(src_object)
1386
        except ValueError:
1387
            raise faults.BadRequest('Invalid source object')
1388

    
1389
        try:
1390
            src_version = request.META.get('HTTP_X_SOURCE_VERSION')
1391
            src_size, src_hashmap = request.backend.get_object_hashmap(
1392
                request.user_uniq,
1393
                src_account, src_container, src_name, src_version)
1394
        except NotAllowedError:
1395
            raise faults.Forbidden('Not allowed')
1396
        except ItemNotExists:
1397
            raise faults.ItemNotFound('Source object does not exist')
1398

    
1399
        if length is None:
1400
            length = src_size
1401
        elif length > src_size:
1402
            raise faults.BadRequest(
1403
                'Object length is smaller than range length')
1404
    else:
1405
        # Require either a Content-Length, or 'chunked' Transfer-Encoding.
1406
        content_length = -1
1407
        if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
1408
            content_length = get_content_length(request)
1409

    
1410
        if length is None:
1411
            length = content_length
1412
        else:
1413
            if content_length == -1:
1414
                # TODO: Get up to length bytes in chunks.
1415
                length = content_length
1416
            elif length != content_length:
1417
                raise faults.BadRequest(
1418
                    'Content length does not match range length')
1419
    if (total is not None
1420
            and (total != size or offset >= size
1421
                 or (length > 0 and offset + length > size))):
1422
        raise faults.RangeNotSatisfiable(
1423
            'Supplied range will change provided object limits')
1424

    
1425
    dest_bytes = request.META.get('HTTP_X_OBJECT_BYTES')
1426
    if dest_bytes is not None:
1427
        dest_bytes = get_int_parameter(dest_bytes)
1428
        if dest_bytes is None:
1429
            raise faults.BadRequest('Invalid X-Object-Bytes header')
1430

    
1431
    if src_object:
1432
        if offset % request.backend.block_size == 0:
1433
            # Update the hashes only.
1434
            sbi = 0
1435
            while length > 0:
1436
                bi = int(offset / request.backend.block_size)
1437
                bl = min(length, request.backend.block_size)
1438
                if bi < len(hashmap):
1439
                    if bl == request.backend.block_size:
1440
                        hashmap[bi] = src_hashmap[sbi]
1441
                    else:
1442
                        data = request.backend.get_block(src_hashmap[sbi])
1443
                        hashmap[bi] = request.backend.update_block(
1444
                            hashmap[bi], data[:bl], 0)
1445
                else:
1446
                    hashmap.append(src_hashmap[sbi])
1447
                offset += bl
1448
                length -= bl
1449
                sbi += 1
1450
        else:
1451
            data = ''
1452
            sbi = 0
1453
            while length > 0:
1454
                if sbi < len(src_hashmap):
1455
                    data += request.backend.get_block(src_hashmap[sbi])
1456
                if length < request.backend.block_size:
1457
                    data = data[:length]
1458
                bytes = put_object_block(request, hashmap, data, offset)
1459
                offset += bytes
1460
                data = data[bytes:]
1461
                length -= bytes
1462
                sbi += 1
1463
    else:
1464
        data = ''
1465
        for d in socket_read_iterator(request, length,
1466
                                      request.backend.block_size):
1467
            # TODO: Raise 408 (Request Timeout) if this takes too long.
1468
            # TODO: Raise 499 (Client Disconnect) if a length is defined
1469
            #       and we stop before getting this much data.
1470
            data += d
1471
            bytes = put_object_block(request, hashmap, data, offset)
1472
            offset += bytes
1473
            data = data[bytes:]
1474
        if len(data) > 0:
1475
            bytes = put_object_block(request, hashmap, data, offset)
1476
            offset += bytes
1477

    
1478
    if offset > size:
1479
        size = offset
1480
    if dest_bytes is not None and dest_bytes < size:
1481
        size = dest_bytes
1482
        hashmap = hashmap[:(int((size - 1) / request.backend.block_size) + 1)]
1483
    checksum = hashmap_md5(
1484
        request.backend, hashmap, size) if UPDATE_MD5 else ''
1485
    try:
1486
        version_id, merkle = request.backend.update_object_hashmap(
1487
            request.user_uniq, v_account, v_container, v_object, size,
1488
            prev_meta['type'], hashmap, checksum, 'pithos', meta, replace,
1489
            permissions
1490
        )
1491
    except NotAllowedError:
1492
        raise faults.Forbidden('Not allowed')
1493
    except ItemNotExists:
1494
        raise faults.ItemNotFound('Container does not exist')
1495
    except ValueError:
1496
        raise faults.BadRequest('Invalid sharing header')
1497
    except QuotaError, e:
1498
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
1499
    if public is not None:
1500
        try:
1501
            request.backend.update_object_public(request.user_uniq, v_account,
1502
                                                 v_container, v_object, public)
1503
        except NotAllowedError:
1504
            raise faults.Forbidden('Not allowed')
1505
        except ItemNotExists:
1506
            raise faults.ItemNotFound('Object does not exist')
1507

    
1508
    response = HttpResponse(status=204)
1509
    response['ETag'] = merkle if not UPDATE_MD5 else checksum
1510
    response['X-Object-Version'] = version_id
1511
    return response
1512

    
1513

    
1514
@api_method('DELETE', user_required=True, logger=logger,
1515
            lock_container_path=True)
1516
def object_delete(request, v_account, v_container, v_object):
1517
    # Normal Response Codes: 204
1518
    # Error Response Codes: internalServerError (500),
1519
    #                       itemNotFound (404),
1520
    #                       forbidden (403),
1521
    #                       badRequest (400)
1522
    #                       requestentitytoolarge (413)
1523

    
1524
    until = get_int_parameter(request.GET.get('until'))
1525
    delimiter = request.GET.get('delimiter')
1526

    
1527
    try:
1528
        request.backend.delete_object(
1529
            request.user_uniq, v_account, v_container,
1530
            v_object, until, delimiter=delimiter,
1531
            listing_limit=settings.API_LIST_LIMIT)
1532
    except NotAllowedError:
1533
        raise faults.Forbidden('Not allowed')
1534
    except ItemNotExists:
1535
        raise faults.ItemNotFound('Object does not exist')
1536
    except QuotaError, e:
1537
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
1538
    return HttpResponse(status=204)