Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / functions.py @ 67100dd2

History | View | Annotate | Download (57.8 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

    
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 snf_django.lib.astakos import get_uuids as _get_uuids
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,
59
    retrieve_uuid, retrieve_uuids, retrieve_displaynames,
60
    get_pithos_usage
61
)
62

    
63
from pithos.api.settings import (UPDATE_MD5, TRANSLATE_UUIDS,
64
                                 SERVICE_TOKEN, ASTAKOS_URL)
65

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

    
70
from pithos.backends.filter import parse_filters
71

    
72
import hashlib
73

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

    
77

    
78
def get_uuids(names):
79
    try:
80
        url = ASTAKOS_URL + "/service/api/user_catalogs"
81
        uuids = _get_uuids(SERVICE_TOKEN, names, url=url)
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.method_not_allowed(request)
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.method_not_allowed(request)
121

    
122

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

    
132
    if request.method == 'HEAD':
133
        return container_meta(request, v_account, v_container)
134
    elif request.method == 'PUT':
135
        return container_create(request, v_account, v_container)
136
    elif request.method == 'POST':
137
        return container_update(request, v_account, v_container)
138
    elif request.method == 'DELETE':
139
        return container_delete(request, v_account, v_container)
140
    elif request.method == 'GET':
141
        return object_list(request, v_account, v_container)
142
    else:
143
        return api.method_not_allowed(request)
144

    
145

    
146
@csrf_exempt
147
def object_demux(request, v_account, v_container, v_object):
148
    # Helper to avoid placing the token in the URL
149
    # when loading objects from a browser.
150
    if TRANSLATE_UUIDS:
151
        if not is_uuid(v_account):
152
            uuids = get_uuids([v_account])
153
            if not uuids or not v_account in uuids:
154
                return HttpResponse(status=404)
155
            v_account = uuids[v_account]
156

    
157
    if request.method == 'HEAD':
158
        return object_meta(request, v_account, v_container, v_object)
159
    elif request.method == 'GET':
160
        return object_read(request, v_account, v_container, v_object)
161
    elif request.method == 'PUT':
162
        return object_write(request, v_account, v_container, v_object)
163
    elif request.method == 'COPY':
164
        return object_copy(request, v_account, v_container, v_object)
165
    elif request.method == 'MOVE':
166
        return object_move(request, v_account, v_container, v_object)
167
    elif request.method == 'POST':
168
        if request.META.get(
169
                'CONTENT_TYPE', '').startswith('multipart/form-data'):
170
            return object_write_form(request, v_account, v_container, v_object)
171
        return object_update(request, v_account, v_container, v_object)
172
    elif request.method == 'DELETE':
173
        return object_delete(request, v_account, v_container, v_object)
174
    else:
175
        return api.method_not_allowed(request)
176

    
177

    
178
@api_method('GET', user_required=False, logger=logger)
179
def authenticate(request):
180
    # Normal Response Codes: 204
181
    # Error Response Codes: internalServerError (500),
182
    #                       forbidden (403),
183
    #                       badRequest (400)
184

    
185
    x_auth_user = request.META.get('HTTP_X_AUTH_USER')
186
    x_auth_key = request.META.get('HTTP_X_AUTH_KEY')
187
    if not x_auth_user or not x_auth_key:
188
        raise faults.BadRequest('Missing X-Auth-User or X-Auth-Key header')
189
    response = HttpResponse(status=204)
190

    
191
    uri = request.build_absolute_uri()
192
    if '?' in uri:
193
        uri = uri[:uri.find('?')]
194

    
195
    response['X-Auth-Token'] = x_auth_key
196
    response['X-Storage-Url'] = uri + ('' if uri.endswith('/')
197
                                       else '/') + x_auth_user
198
    return response
199

    
200

    
201
@api_method('GET', format_allowed=True, user_required=True, logger=logger)
202
def account_list(request):
203
    # Normal Response Codes: 200, 204
204
    # Error Response Codes: internalServerError (500),
205
    #                       badRequest (400)
206
    response = HttpResponse()
207

    
208
    marker = request.GET.get('marker')
209
    limit = get_int_parameter(request.GET.get('limit'))
210
    if not limit:
211
        limit = 10000
212

    
213
    accounts = request.backend.list_accounts(request.user_uniq, marker, limit)
214

    
215
    if request.serialization == 'text':
216
        if TRANSLATE_UUIDS:
217
            accounts = retrieve_displaynames(
218
                getattr(request, 'token', None), accounts)
219
        if len(accounts) == 0:
220
            # The cloudfiles python bindings expect 200 if json/xml.
221
            response.status_code = 204
222
            return response
223
        response.status_code = 200
224
        response.content = '\n'.join(accounts) + '\n'
225
        return response
226

    
227
    account_meta = []
228
    for x in accounts:
229
        if x == request.user_uniq:
230
            continue
231
        usage = get_pithos_usage(request.x_auth_token)
232
        try:
233
            meta = request.backend.get_account_meta(
234
                request.user_uniq, x, 'pithos', include_user_defined=False,
235
                external_quota=usage)
236
            groups = request.backend.get_account_groups(request.user_uniq, x)
237
        except NotAllowedError:
238
            raise faults.Forbidden('Not allowed')
239
        else:
240
            rename_meta_key(meta, 'modified', 'last_modified')
241
            rename_meta_key(
242
                meta, 'until_timestamp', 'x_account_until_timestamp')
243
            if groups:
244
                meta['X-Account-Group'] = printable_header_dict(
245
                    dict([(k, ','.join(v)) for k, v in groups.iteritems()]))
246
            account_meta.append(printable_header_dict(meta))
247

    
248
    if TRANSLATE_UUIDS:
249
        uuids = list(d['name'] for d in account_meta)
250
        catalog = retrieve_displaynames(
251
            getattr(request, 'token', None), uuids, return_dict=True)
252
        for meta in account_meta:
253
            meta['name'] = catalog.get(meta.get('name'))
254

    
255
    if request.serialization == 'xml':
256
        data = render_to_string('accounts.xml', {'accounts': account_meta})
257
    elif request.serialization == 'json':
258
        data = json.dumps(account_meta)
259
    response.status_code = 200
260
    response.content = data
261
    return response
262

    
263

    
264
@api_method('HEAD', user_required=True, logger=logger)
265
def account_meta(request, v_account):
266
    # Normal Response Codes: 204
267
    # Error Response Codes: internalServerError (500),
268
    #                       forbidden (403),
269
    #                       badRequest (400)
270

    
271
    until = get_int_parameter(request.GET.get('until'))
272
    usage = get_pithos_usage(request.x_auth_token)
273
    try:
274
        meta = request.backend.get_account_meta(
275
            request.user_uniq, v_account, 'pithos', until,
276
            external_quota=usage)
277
        groups = request.backend.get_account_groups(
278
            request.user_uniq, v_account)
279

    
280
        if TRANSLATE_UUIDS:
281
            for k in groups:
282
                groups[k] = retrieve_displaynames(
283
                    getattr(request, 'token', None), groups[k])
284
        policy = request.backend.get_account_policy(
285
            request.user_uniq, v_account, external_quota=usage)
286
    except NotAllowedError:
287
        raise faults.Forbidden('Not allowed')
288

    
289
    validate_modification_preconditions(request, meta)
290

    
291
    response = HttpResponse(status=204)
292
    put_account_headers(response, meta, groups, policy)
293
    return response
294

    
295

    
296
@api_method('POST', user_required=True, logger=logger)
297
def account_update(request, v_account):
298
    # Normal Response Codes: 202
299
    # Error Response Codes: internalServerError (500),
300
    #                       forbidden (403),
301
    #                       badRequest (400)
302

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

    
342

    
343
@api_method('GET', format_allowed=True, user_required=True, logger=logger)
344
def container_list(request, v_account):
345
    # Normal Response Codes: 200, 204
346
    # Error Response Codes: internalServerError (500),
347
    #                       itemNotFound (404),
348
    #                       forbidden (403),
349
    #                       badRequest (400)
350

    
351
    until = get_int_parameter(request.GET.get('until'))
352
    usage = get_pithos_usage(request.x_auth_token)
353
    try:
354
        meta = request.backend.get_account_meta(
355
            request.user_uniq, v_account, 'pithos', until,
356
            external_quota=usage)
357
        groups = request.backend.get_account_groups(
358
            request.user_uniq, v_account)
359
        policy = request.backend.get_account_policy(
360
            request.user_uniq, v_account, external_quota=usage)
361
    except NotAllowedError:
362
        raise faults.Forbidden('Not allowed')
363

    
364
    validate_modification_preconditions(request, meta)
365

    
366
    response = HttpResponse()
367
    put_account_headers(response, meta, groups, policy)
368

    
369
    marker = request.GET.get('marker')
370
    limit = get_int_parameter(request.GET.get('limit'))
371
    if not limit:
372
        limit = 10000
373

    
374
    shared = False
375
    if 'shared' in request.GET:
376
        shared = True
377
    public = False
378
    if request.user_uniq == v_account and 'public' in request.GET:
379
        public = True
380

    
381
    try:
382
        containers = request.backend.list_containers(
383
            request.user_uniq, v_account,
384
            marker, limit, shared, until, public)
385
    except NotAllowedError:
386
        raise faults.Forbidden('Not allowed')
387
    except NameError:
388
        containers = []
389

    
390
    if request.serialization == 'text':
391
        if len(containers) == 0:
392
            # The cloudfiles python bindings expect 200 if json/xml.
393
            response.status_code = 204
394
            return response
395
        response.status_code = 200
396
        response.content = '\n'.join(containers) + '\n'
397
        return response
398

    
399
    container_meta = []
400
    for x in containers:
401
        try:
402
            meta = request.backend.get_container_meta(
403
                request.user_uniq, v_account,
404
                x, 'pithos', until, include_user_defined=False)
405
            policy = request.backend.get_container_policy(request.user_uniq,
406
                                                          v_account, x)
407
        except NotAllowedError:
408
            raise faults.Forbidden('Not allowed')
409
        except NameError:
410
            pass
411
        else:
412
            rename_meta_key(meta, 'modified', 'last_modified')
413
            rename_meta_key(
414
                meta, 'until_timestamp', 'x_container_until_timestamp')
415
            if policy:
416
                meta['X-Container-Policy'] = printable_header_dict(
417
                    dict([(k, v) for k, v in policy.iteritems()]))
418
            container_meta.append(printable_header_dict(meta))
419
    if request.serialization == 'xml':
420
        data = render_to_string('containers.xml', {'account':
421
                                v_account, 'containers': container_meta})
422
    elif request.serialization == 'json':
423
        data = json.dumps(container_meta)
424
    response.status_code = 200
425
    response.content = data
426
    return response
427

    
428

    
429
@api_method('HEAD', user_required=True, logger=logger)
430
def container_meta(request, v_account, v_container):
431
    # Normal Response Codes: 204
432
    # Error Response Codes: internalServerError (500),
433
    #                       itemNotFound (404),
434
    #                       forbidden (403),
435
    #                       badRequest (400)
436

    
437
    until = get_int_parameter(request.GET.get('until'))
438
    try:
439
        meta = request.backend.get_container_meta(request.user_uniq, v_account,
440
                                                  v_container, 'pithos', until)
441
        meta['object_meta'] = \
442
            request.backend.list_container_meta(request.user_uniq,
443
                                                v_account, v_container,
444
                                                'pithos', until)
445
        policy = request.backend.get_container_policy(
446
            request.user_uniq, v_account,
447
            v_container)
448
    except NotAllowedError:
449
        raise faults.Forbidden('Not allowed')
450
    except ItemNotExists:
451
        raise faults.ItemNotFound('Container does not exist')
452

    
453
    validate_modification_preconditions(request, meta)
454

    
455
    response = HttpResponse(status=204)
456
    put_container_headers(request, response, meta, policy)
457
    return response
458

    
459

    
460
@api_method('PUT', user_required=True, logger=logger)
461
def container_create(request, v_account, v_container):
462
    # Normal Response Codes: 201, 202
463
    # Error Response Codes: internalServerError (500),
464
    #                       itemNotFound (404),
465
    #                       forbidden (403),
466
    #                       badRequest (400)
467

    
468
    meta, policy = get_container_headers(request)
469

    
470
    try:
471
        request.backend.put_container(
472
            request.user_uniq, v_account, v_container, policy)
473
        ret = 201
474
    except NotAllowedError:
475
        raise faults.Forbidden('Not allowed')
476
    except ValueError:
477
        raise faults.BadRequest('Invalid policy header')
478
    except ContainerExists:
479
        ret = 202
480

    
481
    if ret == 202 and policy:
482
        try:
483
            request.backend.update_container_policy(
484
                request.user_uniq, v_account,
485
                v_container, policy, replace=False)
486
        except NotAllowedError:
487
            raise faults.Forbidden('Not allowed')
488
        except ItemNotExists:
489
            raise faults.ItemNotFound('Container does not exist')
490
        except ValueError:
491
            raise faults.BadRequest('Invalid policy header')
492
    if meta:
493
        try:
494
            request.backend.update_container_meta(request.user_uniq, v_account,
495
                                                  v_container, 'pithos',
496
                                                  meta, replace=False)
497
        except NotAllowedError:
498
            raise faults.Forbidden('Not allowed')
499
        except ItemNotExists:
500
            raise faults.ItemNotFound('Container does not exist')
501

    
502
    return HttpResponse(status=ret)
503

    
504

    
505
@api_method('POST', format_allowed=True, user_required=True, logger=logger)
506
def container_update(request, v_account, v_container):
507
    # Normal Response Codes: 202
508
    # Error Response Codes: internalServerError (500),
509
    #                       itemNotFound (404),
510
    #                       forbidden (403),
511
    #                       badRequest (400)
512

    
513
    meta, policy = get_container_headers(request)
514
    replace = True
515
    if 'update' in request.GET:
516
        replace = False
517
    if policy:
518
        try:
519
            request.backend.update_container_policy(
520
                request.user_uniq, v_account,
521
                v_container, policy, replace)
522
        except NotAllowedError:
523
            raise faults.Forbidden('Not allowed')
524
        except ItemNotExists:
525
            raise faults.ItemNotFound('Container does not exist')
526
        except ValueError:
527
            raise faults.BadRequest('Invalid policy header')
528
    if meta or replace:
529
        try:
530
            request.backend.update_container_meta(request.user_uniq, v_account,
531
                                                  v_container, 'pithos',
532
                                                  meta, replace)
533
        except NotAllowedError:
534
            raise faults.Forbidden('Not allowed')
535
        except ItemNotExists:
536
            raise faults.ItemNotFound('Container does not exist')
537

    
538
    content_length = -1
539
    if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
540
        content_length = get_int_parameter(
541
            request.META.get('CONTENT_LENGTH', 0))
542
    content_type = request.META.get('CONTENT_TYPE')
543
    hashmap = []
544
    if (content_type
545
            and content_type == 'application/octet-stream'
546
            and content_length != 0):
547
        for data in socket_read_iterator(request, content_length,
548
                                         request.backend.block_size):
549
            # TODO: Raise 408 (Request Timeout) if this takes too long.
550
            # TODO: Raise 499 (Client Disconnect) if a length is defined
551
            #       and we stop before getting this much data.
552
            hashmap.append(request.backend.put_block(data))
553

    
554
    response = HttpResponse(status=202)
555
    if hashmap:
556
        response.content = simple_list_response(request, hashmap)
557
    return response
558

    
559

    
560
@api_method('DELETE', user_required=True, logger=logger)
561
def container_delete(request, v_account, v_container):
562
    # Normal Response Codes: 204
563
    # Error Response Codes: internalServerError (500),
564
    #                       conflict (409),
565
    #                       itemNotFound (404),
566
    #                       forbidden (403),
567
    #                       badRequest (400)
568
    #                       requestentitytoolarge (413)
569

    
570
    until = get_int_parameter(request.GET.get('until'))
571

    
572
    delimiter = request.GET.get('delimiter')
573

    
574
    try:
575
        request.backend.delete_container(
576
            request.user_uniq, v_account, v_container,
577
            until, delimiter=delimiter)
578
    except NotAllowedError:
579
        raise faults.Forbidden('Not allowed')
580
    except ItemNotExists:
581
        raise faults.ItemNotFound('Container does not exist')
582
    except ContainerNotEmpty:
583
        raise faults.Conflict('Container is not empty')
584
    except QuotaError, e:
585
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
586
    return HttpResponse(status=204)
587

    
588

    
589
@api_method('GET', format_allowed=True, user_required=True, logger=logger)
590
def object_list(request, v_account, v_container):
591
    # Normal Response Codes: 200, 204
592
    # Error Response Codes: internalServerError (500),
593
    #                       itemNotFound (404),
594
    #                       forbidden (403),
595
    #                       badRequest (400)
596

    
597
    until = get_int_parameter(request.GET.get('until'))
598
    try:
599
        meta = request.backend.get_container_meta(request.user_uniq, v_account,
600
                                                  v_container, 'pithos', until)
601
        meta['object_meta'] = \
602
            request.backend.list_container_meta(request.user_uniq,
603
                                                v_account, v_container,
604
                                                'pithos', until)
605
        policy = request.backend.get_container_policy(
606
            request.user_uniq, v_account,
607
            v_container)
608
    except NotAllowedError:
609
        raise faults.Forbidden('Not allowed')
610
    except ItemNotExists:
611
        raise faults.ItemNotFound('Container does not exist')
612

    
613
    validate_modification_preconditions(request, meta)
614

    
615
    response = HttpResponse()
616
    put_container_headers(request, response, meta, policy)
617

    
618
    path = request.GET.get('path')
619
    prefix = request.GET.get('prefix')
620
    delimiter = request.GET.get('delimiter')
621

    
622
    # Path overrides prefix and delimiter.
623
    virtual = True
624
    if path:
625
        prefix = path
626
        delimiter = '/'
627
        virtual = False
628

    
629
    # Naming policy.
630
    if prefix and delimiter and not prefix.endswith(delimiter):
631
        prefix = prefix + delimiter
632
    if not prefix:
633
        prefix = ''
634
    prefix = prefix.lstrip('/')
635

    
636
    marker = request.GET.get('marker')
637
    limit = get_int_parameter(request.GET.get('limit'))
638
    if not limit:
639
        limit = 10000
640

    
641
    keys = request.GET.get('meta')
642
    if keys:
643
        keys = [smart_str(x.strip()) for x in keys.split(',')
644
                if x.strip() != '']
645
        included, excluded, opers = parse_filters(keys)
646
        keys = []
647
        keys += [format_header_key('X-Object-Meta-' + x) for x in included]
648
        keys += ['!' + format_header_key('X-Object-Meta-' + x)
649
                 for x in excluded]
650
        keys += ['%s%s%s' % (format_header_key(
651
            'X-Object-Meta-' + k), o, v) for k, o, v in opers]
652
    else:
653
        keys = []
654

    
655
    shared = False
656
    if 'shared' in request.GET:
657
        shared = True
658

    
659
    public_requested = 'public' in request.GET
660
    public_granted = public_requested and request.user_uniq == v_account
661

    
662
    if request.serialization == 'text':
663
        try:
664
            objects = request.backend.list_objects(
665
                request.user_uniq, v_account,
666
                v_container, prefix, delimiter, marker,
667
                limit, virtual, 'pithos', keys, shared,
668
                until, None, public_granted)
669
        except NotAllowedError:
670
            raise faults.Forbidden('Not allowed')
671
        except ItemNotExists:
672
            raise faults.ItemNotFound('Container does not exist')
673

    
674
        if len(objects) == 0:
675
            # The cloudfiles python bindings expect 200 if json/xml.
676
            response.status_code = 204
677
            return response
678
        response.status_code = 200
679
        response.content = '\n'.join([x[0] for x in objects]) + '\n'
680
        return response
681

    
682
    try:
683
        objects = request.backend.list_object_meta(
684
            request.user_uniq, v_account,
685
            v_container, prefix, delimiter, marker,
686
            limit, virtual, 'pithos', keys, shared, until, None, public_granted)
687
        object_permissions = {}
688
        object_public = {}
689
        if until is None:
690
            name = '/'.join((v_account, v_container, ''))
691
            name_idx = len(name)
692
            for x in request.backend.list_object_permissions(
693
                    request.user_uniq, v_account, v_container, prefix):
694

    
695
                # filter out objects which are not under the container
696
                if name != x[:name_idx]:
697
                    continue
698

    
699
                object = x[name_idx:]
700
                object_permissions[object] = \
701
                    request.backend.get_object_permissions(
702
                        request.user_uniq, v_account, v_container, object)
703

    
704
            if public_granted:
705
                for k, v in request.backend.list_object_public(
706
                        request.user_uniq, v_account,
707
                        v_container, prefix).iteritems():
708
                    object_public[k[name_idx:]] = v
709
    except NotAllowedError:
710
        raise faults.Forbidden('Not allowed')
711
    except ItemNotExists:
712
        raise faults.ItemNotFound('Container does not exist')
713

    
714
    object_meta = []
715
    for meta in objects:
716
        if TRANSLATE_UUIDS:
717
            modified_by = meta.get('modified_by')
718
            if modified_by:
719
                l = retrieve_displaynames(
720
                    getattr(request, 'token', None), [meta['modified_by']])
721
                if l is not None and len(l) == 1:
722
                    meta['modified_by'] = l[0]
723

    
724
        if len(meta) == 1:
725
            # Virtual objects/directories.
726
            object_meta.append(meta)
727
        else:
728
            rename_meta_key(
729
                meta, 'hash', 'x_object_hash')  # Will be replaced by checksum.
730
            rename_meta_key(meta, 'checksum', 'hash')
731
            rename_meta_key(meta, 'type', 'content_type')
732
            rename_meta_key(meta, 'uuid', 'x_object_uuid')
733
            if until is not None and 'modified' in meta:
734
                del(meta['modified'])
735
            else:
736
                rename_meta_key(meta, 'modified', 'last_modified')
737
            rename_meta_key(meta, 'modified_by', 'x_object_modified_by')
738
            rename_meta_key(meta, 'version', 'x_object_version')
739
            rename_meta_key(
740
                meta, 'version_timestamp', 'x_object_version_timestamp')
741
            permissions = object_permissions.get(meta['name'], None)
742
            if permissions:
743
                update_sharing_meta(request, permissions, v_account,
744
                                    v_container, meta['name'], meta)
745
            public_url = object_public.get(meta['name'], None)
746
            if public_granted:
747
                update_public_meta(public_url, meta)
748
            object_meta.append(printable_header_dict(meta))
749

    
750
    if request.serialization == 'xml':
751
        data = render_to_string(
752
            'objects.xml', {'container': v_container, 'objects': object_meta})
753
    elif request.serialization == 'json':
754
        data = json.dumps(object_meta, default=json_encode_decimal)
755
    response.status_code = 200
756
    response.content = data
757
    return response
758

    
759

    
760
@api_method('HEAD', user_required=True, logger=logger)
761
def object_meta(request, v_account, v_container, v_object):
762
    # Normal Response Codes: 204
763
    # Error Response Codes: internalServerError (500),
764
    #                       itemNotFound (404),
765
    #                       forbidden (403),
766
    #                       badRequest (400)
767

    
768
    version = request.GET.get('version')
769
    try:
770
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
771
                                               v_container, v_object,
772
                                               'pithos', version)
773
        if version is None:
774
            permissions = request.backend.get_object_permissions(
775
                request.user_uniq,
776
                v_account, v_container, v_object)
777
            public = request.backend.get_object_public(
778
                request.user_uniq, v_account,
779
                v_container, v_object)
780
        else:
781
            permissions = None
782
            public = None
783
    except NotAllowedError:
784
        raise faults.Forbidden('Not allowed')
785
    except ItemNotExists:
786
        raise faults.ItemNotFound('Object does not exist')
787
    except VersionNotExists:
788
        raise faults.ItemNotFound('Version does not exist')
789

    
790
    update_manifest_meta(request, v_account, meta)
791
    update_sharing_meta(
792
        request, permissions, v_account, v_container, v_object, meta)
793
    if request.user_uniq == v_account:
794
        update_public_meta(public, meta)
795

    
796
    # Evaluate conditions.
797
    validate_modification_preconditions(request, meta)
798
    try:
799
        validate_matching_preconditions(request, meta)
800
    except faults.NotModified:
801
        response = HttpResponse(status=304)
802
        response['ETag'] = meta['checksum']
803
        return response
804

    
805
    response = HttpResponse(status=200)
806
    put_object_headers(response, meta, token=getattr(request, 'token', None))
807
    return response
808

    
809

    
810
@api_method('GET', format_allowed=True, user_required=True, logger=logger)
811
def object_read(request, v_account, v_container, v_object):
812
    # Normal Response Codes: 200, 206
813
    # Error Response Codes: internalServerError (500),
814
    #                       rangeNotSatisfiable (416),
815
    #                       preconditionFailed (412),
816
    #                       itemNotFound (404),
817
    #                       forbidden (403),
818
    #                       badRequest (400),
819
    #                       notModified (304)
820

    
821
    version = request.GET.get('version')
822

    
823
    # Reply with the version list. Do this first, as the object may be deleted.
824
    if version == 'list':
825
        if request.serialization == 'text':
826
            raise faults.BadRequest('No format specified for version list.')
827

    
828
        try:
829
            v = request.backend.list_versions(request.user_uniq, v_account,
830
                                              v_container, v_object)
831
        except NotAllowedError:
832
            raise faults.Forbidden('Not allowed')
833
        except ItemNotExists:
834
            raise faults.ItemNotFound('Object does not exist')
835
        d = {'versions': v}
836
        if request.serialization == 'xml':
837
            d['object'] = v_object
838
            data = render_to_string('versions.xml', d)
839
        elif request.serialization == 'json':
840
            data = json.dumps(d, default=json_encode_decimal)
841

    
842
        response = HttpResponse(data, status=200)
843
        response['Content-Length'] = len(data)
844
        return response
845

    
846
    try:
847
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
848
                                               v_container, v_object,
849
                                               'pithos', version)
850
        if version is None:
851
            permissions = request.backend.get_object_permissions(
852
                request.user_uniq,
853
                v_account, v_container, v_object)
854
            public = request.backend.get_object_public(
855
                request.user_uniq, v_account,
856
                v_container, v_object)
857
        else:
858
            permissions = None
859
            public = None
860
    except NotAllowedError:
861
        raise faults.Forbidden('Not allowed')
862
    except ItemNotExists:
863
        raise faults.ItemNotFound('Object does not exist')
864
    except VersionNotExists:
865
        raise faults.ItemNotFound('Version does not exist')
866

    
867
    update_manifest_meta(request, v_account, meta)
868
    update_sharing_meta(
869
        request, permissions, v_account, v_container, v_object, meta)
870
    if request.user_uniq == v_account:
871
        update_public_meta(public, meta)
872

    
873
    # Evaluate conditions.
874
    validate_modification_preconditions(request, meta)
875
    try:
876
        validate_matching_preconditions(request, meta)
877
    except faults.NotModified:
878
        response = HttpResponse(status=304)
879
        response['ETag'] = meta['checksum']
880
        return response
881

    
882
    hashmap_reply = False
883
    if 'hashmap' in request.GET and request.serialization != 'text':
884
        hashmap_reply = True
885

    
886
    sizes = []
887
    hashmaps = []
888
    if 'X-Object-Manifest' in meta and not hashmap_reply:
889
        try:
890
            src_container, src_name = split_container_object_string(
891
                '/' + meta['X-Object-Manifest'])
892
            objects = request.backend.list_objects(
893
                request.user_uniq, v_account,
894
                src_container, prefix=src_name, virtual=False)
895
        except NotAllowedError:
896
            raise faults.Forbidden('Not allowed')
897
        except ValueError:
898
            raise faults.BadRequest('Invalid X-Object-Manifest header')
899
        except ItemNotExists:
900
            raise faults.ItemNotFound('Container does not exist')
901

    
902
        try:
903
            for x in objects:
904
                s, h = \
905
                    request.backend.get_object_hashmap(request.user_uniq,
906
                                                       v_account, src_container,
907
                                                       x[0], x[1])
908
                sizes.append(s)
909
                hashmaps.append(h)
910
        except NotAllowedError:
911
            raise faults.Forbidden('Not allowed')
912
        except ItemNotExists:
913
            raise faults.ItemNotFound('Object does not exist')
914
        except VersionNotExists:
915
            raise faults.ItemNotFound('Version does not exist')
916
    else:
917
        try:
918
            s, h = request.backend.get_object_hashmap(
919
                request.user_uniq, v_account,
920
                v_container, v_object, version)
921
            sizes.append(s)
922
            hashmaps.append(h)
923
        except NotAllowedError:
924
            raise faults.Forbidden('Not allowed')
925
        except ItemNotExists:
926
            raise faults.ItemNotFound('Object does not exist')
927
        except VersionNotExists:
928
            raise faults.ItemNotFound('Version does not exist')
929

    
930
    # Reply with the hashmap.
931
    if hashmap_reply:
932
        size = sum(sizes)
933
        hashmap = sum(hashmaps, [])
934
        d = {
935
            'block_size': request.backend.block_size,
936
            'block_hash': request.backend.hash_algorithm,
937
            'bytes': size,
938
            'hashes': hashmap}
939
        if request.serialization == 'xml':
940
            d['object'] = v_object
941
            data = render_to_string('hashes.xml', d)
942
        elif request.serialization == 'json':
943
            data = json.dumps(d)
944

    
945
        response = HttpResponse(data, status=200)
946
        put_object_headers(
947
            response, meta, token=getattr(request, 'token', None))
948
        response['Content-Length'] = len(data)
949
        return response
950

    
951
    request.serialization = 'text'  # Unset.
952
    response.override_serialization = True
953
    return object_data_response(request, sizes, hashmaps, meta)
954

    
955

    
956
@api_method('PUT', format_allowed=True, user_required=True, logger=logger)
957
def object_write(request, v_account, v_container, v_object):
958
    # Normal Response Codes: 201
959
    # Error Response Codes: internalServerError (500),
960
    #                       unprocessableEntity (422),
961
    #                       lengthRequired (411),
962
    #                       conflict (409),
963
    #                       itemNotFound (404),
964
    #                       forbidden (403),
965
    #                       badRequest (400)
966
    #                       requestentitytoolarge (413)
967

    
968
    # Evaluate conditions.
969
    if (request.META.get('HTTP_IF_MATCH')
970
            or request.META.get('HTTP_IF_NONE_MATCH')):
971
        try:
972
            meta = request.backend.get_object_meta(
973
                request.user_uniq, v_account,
974
                v_container, v_object, 'pithos')
975
        except NotAllowedError:
976
            raise faults.Forbidden('Not allowed')
977
        except NameError:
978
            meta = {}
979
        validate_matching_preconditions(request, meta)
980

    
981
    copy_from = request.META.get('HTTP_X_COPY_FROM')
982
    move_from = request.META.get('HTTP_X_MOVE_FROM')
983
    if copy_from or move_from:
984
        delimiter = request.GET.get('delimiter')
985
        content_length = get_content_length(request)  # Required by the API.
986

    
987
        src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
988

    
989
        if not src_account:
990
            src_account = request.user_uniq
991
        else:
992
            if TRANSLATE_UUIDS:
993
                try:
994
                    src_account = retrieve_uuid(getattr(request, 'token', None),
995
                                                src_account)
996
                except ItemNotExists:
997
                    faults.ItemNotFound('Invalid source account')
998

    
999
        if move_from:
1000
            try:
1001
                src_container, src_name = split_container_object_string(
1002
                    move_from)
1003
            except ValueError:
1004
                raise faults.BadRequest('Invalid X-Move-From header')
1005
            version_id = copy_or_move_object(
1006
                request, src_account, src_container, src_name,
1007
                v_account, v_container, v_object,
1008
                move=True, delimiter=delimiter)
1009
        else:
1010
            try:
1011
                src_container, src_name = split_container_object_string(
1012
                    copy_from)
1013
            except ValueError:
1014
                raise faults.BadRequest('Invalid X-Copy-From header')
1015
            version_id = copy_or_move_object(
1016
                request, src_account, src_container, src_name,
1017
                v_account, v_container, v_object,
1018
                move=False, delimiter=delimiter)
1019
        response = HttpResponse(status=201)
1020
        response['X-Object-Version'] = version_id
1021
        return response
1022

    
1023
    content_type, meta, permissions, public = get_object_headers(request)
1024
    content_length = -1
1025
    if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
1026
        content_length = get_content_length(request)
1027
    # Should be BadRequest, but API says otherwise.
1028
    if content_type is None:
1029
        raise faults.LengthRequired('Missing Content-Type header')
1030

    
1031
    if 'hashmap' in request.GET:
1032
        if request.serialization not in ('json', 'xml'):
1033
            raise faults.BadRequest('Invalid hashmap format')
1034

    
1035
        data = ''
1036
        for block in socket_read_iterator(request, content_length,
1037
                                          request.backend.block_size):
1038
            data = '%s%s' % (data, block)
1039

    
1040
        if request.serialization == 'json':
1041
            d = json.loads(data)
1042
            if not hasattr(d, '__getitem__'):
1043
                raise faults.BadRequest('Invalid data formating')
1044
            try:
1045
                hashmap = d['hashes']
1046
                size = int(d['bytes'])
1047
            except:
1048
                raise faults.BadRequest('Invalid data formatting')
1049
        elif request.serialization == 'xml':
1050
            try:
1051
                xml = minidom.parseString(data)
1052
                obj = xml.getElementsByTagName('object')[0]
1053
                size = int(obj.attributes['bytes'].value)
1054

    
1055
                hashes = xml.getElementsByTagName('hash')
1056
                hashmap = []
1057
                for hash in hashes:
1058
                    hashmap.append(hash.firstChild.data)
1059
            except:
1060
                raise faults.BadRequest('Invalid data formatting')
1061

    
1062
        checksum = ''  # Do not set to None (will copy previous value).
1063
    else:
1064
        md5 = hashlib.md5()
1065
        size = 0
1066
        hashmap = []
1067
        for data in socket_read_iterator(request, content_length,
1068
                                         request.backend.block_size):
1069
            # TODO: Raise 408 (Request Timeout) if this takes too long.
1070
            # TODO: Raise 499 (Client Disconnect) if a length is defined
1071
            #       and we stop before getting this much data.
1072
            size += len(data)
1073
            hashmap.append(request.backend.put_block(data))
1074
            md5.update(data)
1075

    
1076
        checksum = md5.hexdigest().lower()
1077
        etag = request.META.get('HTTP_ETAG')
1078
        if etag and parse_etags(etag)[0].lower() != checksum:
1079
            raise faults.UnprocessableEntity('Object ETag does not match')
1080

    
1081
    try:
1082
        version_id = \
1083
            request.backend.update_object_hashmap(request.user_uniq,
1084
                                                  v_account, v_container,
1085
                                                  v_object, size, content_type,
1086
                                                  hashmap, checksum,
1087
                                                  'pithos', meta, True,
1088
                                                  permissions)
1089
    except NotAllowedError:
1090
        raise faults.Forbidden('Not allowed')
1091
    except IndexError, e:
1092
        missing_blocks = e.data
1093
        response = HttpResponse(status=409)
1094
        response.content = simple_list_response(request, missing_blocks)
1095
        return response
1096
    except ItemNotExists:
1097
        raise faults.ItemNotFound('Container does not exist')
1098
    except ValueError:
1099
        raise faults.BadRequest('Invalid sharing header')
1100
    except QuotaError, e:
1101
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
1102
    if not checksum and UPDATE_MD5:
1103
        # Update the MD5 after the hashmap, as there may be missing hashes.
1104
        checksum = hashmap_md5(request.backend, hashmap, size)
1105
        try:
1106
            request.backend.update_object_checksum(request.user_uniq,
1107
                                                   v_account, v_container,
1108
                                                   v_object, version_id,
1109
                                                   checksum)
1110
        except NotAllowedError:
1111
            raise faults.Forbidden('Not allowed')
1112
    if public is not None:
1113
        try:
1114
            request.backend.update_object_public(request.user_uniq, v_account,
1115
                                                 v_container, v_object, public)
1116
        except NotAllowedError:
1117
            raise faults.Forbidden('Not allowed')
1118
        except ItemNotExists:
1119
            raise faults.ItemNotFound('Object does not exist')
1120

    
1121
    response = HttpResponse(status=201)
1122
    if checksum:
1123
        response['ETag'] = checksum
1124
    response['X-Object-Version'] = version_id
1125
    return response
1126

    
1127

    
1128
@api_method('POST', user_required=True, logger=logger)
1129
def object_write_form(request, v_account, v_container, v_object):
1130
    # Normal Response Codes: 201
1131
    # Error Response Codes: internalServerError (500),
1132
    #                       itemNotFound (404),
1133
    #                       forbidden (403),
1134
    #                       badRequest (400)
1135
    #                       requestentitytoolarge (413)
1136

    
1137
    request.upload_handlers = [SaveToBackendHandler(request)]
1138
    if 'X-Object-Data' not in request.FILES:
1139
        raise faults.BadRequest('Missing X-Object-Data field')
1140
    file = request.FILES['X-Object-Data']
1141

    
1142
    checksum = file.etag
1143
    try:
1144
        version_id = \
1145
            request.backend.update_object_hashmap(request.user_uniq,
1146
                                                  v_account, v_container,
1147
                                                  v_object, file.size,
1148
                                                  file.content_type,
1149
                                                  file.hashmap, checksum,
1150
                                                  'pithos', {}, True)
1151
    except NotAllowedError:
1152
        raise faults.Forbidden('Not allowed')
1153
    except ItemNotExists:
1154
        raise faults.ItemNotFound('Container does not exist')
1155
    except QuotaError, e:
1156
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
1157

    
1158
    response = HttpResponse(status=201)
1159
    response['ETag'] = checksum
1160
    response['X-Object-Version'] = version_id
1161
    response.content = checksum
1162
    return response
1163

    
1164

    
1165
@api_method('COPY', format_allowed=True, user_required=True, logger=logger)
1166
def object_copy(request, v_account, v_container, v_object):
1167
    # Normal Response Codes: 201
1168
    # Error Response Codes: internalServerError (500),
1169
    #                       itemNotFound (404),
1170
    #                       forbidden (403),
1171
    #                       badRequest (400)
1172
    #                       requestentitytoolarge (413)
1173

    
1174
    dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
1175
    if not dest_account:
1176
        dest_account = request.user_uniq
1177
    dest_path = request.META.get('HTTP_DESTINATION')
1178
    if not dest_path:
1179
        raise faults.BadRequest('Missing Destination header')
1180
    try:
1181
        dest_container, dest_name = split_container_object_string(dest_path)
1182
    except ValueError:
1183
        raise faults.BadRequest('Invalid Destination header')
1184

    
1185
    # Evaluate conditions.
1186
    if (request.META.get('HTTP_IF_MATCH')
1187
            or request.META.get('HTTP_IF_NONE_MATCH')):
1188
        src_version = request.META.get('HTTP_X_SOURCE_VERSION')
1189
        try:
1190
            meta = request.backend.get_object_meta(
1191
                request.user_uniq, v_account,
1192
                v_container, v_object, 'pithos', src_version)
1193
        except NotAllowedError:
1194
            raise faults.Forbidden('Not allowed')
1195
        except (ItemNotExists, VersionNotExists):
1196
            raise faults.ItemNotFound('Container or object does not exist')
1197
        validate_matching_preconditions(request, meta)
1198

    
1199
    delimiter = request.GET.get('delimiter')
1200

    
1201
    version_id = copy_or_move_object(request, v_account, v_container, v_object,
1202
                                     dest_account, dest_container, dest_name,
1203
                                     move=False, delimiter=delimiter)
1204
    response = HttpResponse(status=201)
1205
    response['X-Object-Version'] = version_id
1206
    return response
1207

    
1208

    
1209
@api_method('MOVE', format_allowed=True, user_required=True, logger=logger)
1210
def object_move(request, v_account, v_container, v_object):
1211
    # Normal Response Codes: 201
1212
    # Error Response Codes: internalServerError (500),
1213
    #                       itemNotFound (404),
1214
    #                       forbidden (403),
1215
    #                       badRequest (400)
1216
    #                       requestentitytoolarge (413)
1217

    
1218
    dest_account = request.META.get('HTTP_DESTINATION_ACCOUNT')
1219
    if not dest_account:
1220
        dest_account = request.user_uniq
1221
    dest_path = request.META.get('HTTP_DESTINATION')
1222
    if not dest_path:
1223
        raise faults.BadRequest('Missing Destination header')
1224
    try:
1225
        dest_container, dest_name = split_container_object_string(dest_path)
1226
    except ValueError:
1227
        raise faults.BadRequest('Invalid Destination header')
1228

    
1229
    # Evaluate conditions.
1230
    if (request.META.get('HTTP_IF_MATCH')
1231
            or request.META.get('HTTP_IF_NONE_MATCH')):
1232
        try:
1233
            meta = request.backend.get_object_meta(
1234
                request.user_uniq, v_account,
1235
                v_container, v_object, 'pithos')
1236
        except NotAllowedError:
1237
            raise faults.Forbidden('Not allowed')
1238
        except ItemNotExists:
1239
            raise faults.ItemNotFound('Container or object does not exist')
1240
        validate_matching_preconditions(request, meta)
1241

    
1242
    delimiter = request.GET.get('delimiter')
1243

    
1244
    version_id = copy_or_move_object(request, v_account, v_container, v_object,
1245
                                     dest_account, dest_container, dest_name,
1246
                                     move=True, delimiter=delimiter)
1247
    response = HttpResponse(status=201)
1248
    response['X-Object-Version'] = version_id
1249
    return response
1250

    
1251

    
1252
@api_method('POST', format_allowed=True, user_required=True, logger=logger)
1253
def object_update(request, v_account, v_container, v_object):
1254
    # Normal Response Codes: 202, 204
1255
    # Error Response Codes: internalServerError (500),
1256
    #                       conflict (409),
1257
    #                       itemNotFound (404),
1258
    #                       forbidden (403),
1259
    #                       badRequest (400)
1260

    
1261
    content_type, meta, permissions, public = get_object_headers(request)
1262

    
1263
    try:
1264
        prev_meta = request.backend.get_object_meta(
1265
            request.user_uniq, v_account,
1266
            v_container, v_object, 'pithos')
1267
    except NotAllowedError:
1268
        raise faults.Forbidden('Not allowed')
1269
    except ItemNotExists:
1270
        raise faults.ItemNotFound('Object does not exist')
1271

    
1272
    # Evaluate conditions.
1273
    if (request.META.get('HTTP_IF_MATCH')
1274
            or request.META.get('HTTP_IF_NONE_MATCH')):
1275
        validate_matching_preconditions(request, prev_meta)
1276

    
1277
    replace = True
1278
    if 'update' in request.GET:
1279
        replace = False
1280

    
1281
    # A Content-Type or X-Source-Object header indicates data updates.
1282
    src_object = request.META.get('HTTP_X_SOURCE_OBJECT')
1283
    if ((not content_type or content_type != 'application/octet-stream')
1284
            and not src_object):
1285
        response = HttpResponse(status=202)
1286

    
1287
        # Do permissions first, as it may fail easier.
1288
        if permissions is not None:
1289
            try:
1290
                request.backend.update_object_permissions(request.user_uniq,
1291
                                                          v_account,
1292
                                                          v_container, v_object,
1293
                                                          permissions)
1294
            except NotAllowedError:
1295
                raise faults.Forbidden('Not allowed')
1296
            except ItemNotExists:
1297
                raise faults.ItemNotFound('Object does not exist')
1298
            except ValueError:
1299
                raise faults.BadRequest('Invalid sharing header')
1300
        if public is not None:
1301
            try:
1302
                request.backend.update_object_public(
1303
                    request.user_uniq, v_account,
1304
                    v_container, v_object, public)
1305
            except NotAllowedError:
1306
                raise faults.Forbidden('Not allowed')
1307
            except ItemNotExists:
1308
                raise faults.ItemNotFound('Object does not exist')
1309
        if meta or replace:
1310
            try:
1311
                version_id = request.backend.update_object_meta(
1312
                    request.user_uniq,
1313
                    v_account, v_container, v_object, 'pithos', meta, replace)
1314
            except NotAllowedError:
1315
                raise faults.Forbidden('Not allowed')
1316
            except ItemNotExists:
1317
                raise faults.ItemNotFound('Object does not exist')
1318
            response['X-Object-Version'] = version_id
1319

    
1320
        return response
1321

    
1322
    # Single range update. Range must be in Content-Range.
1323
    # Based on: http://code.google.com/p/gears/wiki/ContentRangePostProposal
1324
    # (with the addition that '*' is allowed for the range - will append).
1325
    content_range = request.META.get('HTTP_CONTENT_RANGE')
1326
    if not content_range:
1327
        raise faults.BadRequest('Missing Content-Range header')
1328
    ranges = get_content_range(request)
1329
    if not ranges:
1330
        raise faults.RangeNotSatisfiable('Invalid Content-Range header')
1331

    
1332
    try:
1333
        size, hashmap = \
1334
            request.backend.get_object_hashmap(request.user_uniq,
1335
                                               v_account, v_container, v_object)
1336
    except NotAllowedError:
1337
        raise faults.Forbidden('Not allowed')
1338
    except ItemNotExists:
1339
        raise faults.ItemNotFound('Object does not exist')
1340

    
1341
    offset, length, total = ranges
1342
    if offset is None:
1343
        offset = size
1344
    elif offset > size:
1345
        raise faults.RangeNotSatisfiable(
1346
            'Supplied offset is beyond object limits')
1347
    if src_object:
1348
        src_account = request.META.get('HTTP_X_SOURCE_ACCOUNT')
1349
        if not src_account:
1350
            src_account = request.user_uniq
1351
        src_container, src_name = split_container_object_string(src_object)
1352
        src_version = request.META.get('HTTP_X_SOURCE_VERSION')
1353
        try:
1354
            src_size, src_hashmap = request.backend.get_object_hashmap(
1355
                request.user_uniq,
1356
                src_account, src_container, src_name, src_version)
1357
        except NotAllowedError:
1358
            raise faults.Forbidden('Not allowed')
1359
        except ItemNotExists:
1360
            raise faults.ItemNotFound('Source object does not exist')
1361

    
1362
        if length is None:
1363
            length = src_size
1364
        elif length > src_size:
1365
            raise faults.BadRequest(
1366
                'Object length is smaller than range length')
1367
    else:
1368
        # Require either a Content-Length, or 'chunked' Transfer-Encoding.
1369
        content_length = -1
1370
        if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
1371
            content_length = get_content_length(request)
1372

    
1373
        if length is None:
1374
            length = content_length
1375
        else:
1376
            if content_length == -1:
1377
                # TODO: Get up to length bytes in chunks.
1378
                length = content_length
1379
            elif length != content_length:
1380
                raise faults.BadRequest(
1381
                    'Content length does not match range length')
1382
    if (total is not None
1383
            and (total != size or offset >= size
1384
                 or (length > 0 and offset + length >= size))):
1385
        raise faults.RangeNotSatisfiable(
1386
            'Supplied range will change provided object limits')
1387

    
1388
    dest_bytes = request.META.get('HTTP_X_OBJECT_BYTES')
1389
    if dest_bytes is not None:
1390
        dest_bytes = get_int_parameter(dest_bytes)
1391
        if dest_bytes is None:
1392
            raise faults.BadRequest('Invalid X-Object-Bytes header')
1393

    
1394
    if src_object:
1395
        if offset % request.backend.block_size == 0:
1396
            # Update the hashes only.
1397
            sbi = 0
1398
            while length > 0:
1399
                bi = int(offset / request.backend.block_size)
1400
                bl = min(length, request.backend.block_size)
1401
                if bi < len(hashmap):
1402
                    if bl == request.backend.block_size:
1403
                        hashmap[bi] = src_hashmap[sbi]
1404
                    else:
1405
                        data = request.backend.get_block(src_hashmap[sbi])
1406
                        hashmap[bi] = request.backend.update_block(hashmap[bi],
1407
                                                                   data[:bl], 0)
1408
                else:
1409
                    hashmap.append(src_hashmap[sbi])
1410
                offset += bl
1411
                length -= bl
1412
                sbi += 1
1413
        else:
1414
            data = ''
1415
            sbi = 0
1416
            while length > 0:
1417
                data += request.backend.get_block(src_hashmap[sbi])
1418
                if length < request.backend.block_size:
1419
                    data = data[:length]
1420
                bytes = put_object_block(request, hashmap, data, offset)
1421
                offset += bytes
1422
                data = data[bytes:]
1423
                length -= bytes
1424
                sbi += 1
1425
    else:
1426
        data = ''
1427
        for d in socket_read_iterator(request, length,
1428
                                      request.backend.block_size):
1429
            # TODO: Raise 408 (Request Timeout) if this takes too long.
1430
            # TODO: Raise 499 (Client Disconnect) if a length is defined
1431
            #       and we stop before getting this much data.
1432
            data += d
1433
            bytes = put_object_block(request, hashmap, data, offset)
1434
            offset += bytes
1435
            data = data[bytes:]
1436
        if len(data) > 0:
1437
            put_object_block(request, hashmap, data, offset)
1438

    
1439
    if offset > size:
1440
        size = offset
1441
    if dest_bytes is not None and dest_bytes < size:
1442
        size = dest_bytes
1443
        hashmap = hashmap[:(int((size - 1) / request.backend.block_size) + 1)]
1444
    checksum = hashmap_md5(
1445
        request.backend, hashmap, size) if UPDATE_MD5 else ''
1446
    try:
1447
        version_id = \
1448
            request.backend.update_object_hashmap(request.user_uniq,
1449
                                                  v_account, v_container,
1450
                                                  v_object, size,
1451
                                                  prev_meta['type'],
1452
                                                  hashmap, checksum, 'pithos',
1453
                                                  meta, replace, permissions)
1454
    except NotAllowedError:
1455
        raise faults.Forbidden('Not allowed')
1456
    except ItemNotExists:
1457
        raise faults.ItemNotFound('Container does not exist')
1458
    except ValueError:
1459
        raise faults.BadRequest('Invalid sharing header')
1460
    except QuotaError, e:
1461
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
1462
    if public is not None:
1463
        try:
1464
            request.backend.update_object_public(request.user_uniq, v_account,
1465
                                                 v_container, v_object, public)
1466
        except NotAllowedError:
1467
            raise faults.Forbidden('Not allowed')
1468
        except ItemNotExists:
1469
            raise faults.ItemNotFound('Object does not exist')
1470

    
1471
    response = HttpResponse(status=204)
1472
    response['ETag'] = checksum
1473
    response['X-Object-Version'] = version_id
1474
    return response
1475

    
1476

    
1477
@api_method('DELETE', user_required=True, logger=logger)
1478
def object_delete(request, v_account, v_container, v_object):
1479
    # Normal Response Codes: 204
1480
    # Error Response Codes: internalServerError (500),
1481
    #                       itemNotFound (404),
1482
    #                       forbidden (403),
1483
    #                       badRequest (400)
1484
    #                       requestentitytoolarge (413)
1485

    
1486
    until = get_int_parameter(request.GET.get('until'))
1487
    delimiter = request.GET.get('delimiter')
1488

    
1489
    try:
1490
        request.backend.delete_object(
1491
            request.user_uniq, v_account, v_container,
1492
            v_object, until, delimiter=delimiter)
1493
    except NotAllowedError:
1494
        raise faults.Forbidden('Not allowed')
1495
    except ItemNotExists:
1496
        raise faults.ItemNotFound('Object does not exist')
1497
    except QuotaError, e:
1498
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
1499
    return HttpResponse(status=204)