functions.py 58.8 KB
Newer Older
1
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2
#
Antony Chazapis's avatar
Antony Chazapis committed
3
4
5
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
6
#
Antony Chazapis's avatar
Antony Chazapis committed
7
8
9
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
10
#
Antony Chazapis's avatar
Antony Chazapis committed
11
12
13
14
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
15
#
Antony Chazapis's avatar
Antony Chazapis committed
16
17
18
19
20
21
22
23
24
25
26
27
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
28
#
Antony Chazapis's avatar
Antony Chazapis committed
29
30
31
32
33
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

Antony Chazapis's avatar
Antony Chazapis committed
34
from xml.dom import minidom
35
36
37
38
39

from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils import simplejson as json
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
47
from snf_django.lib.api import faults

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

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

65
66
from pithos.api import settings

Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
67
68
from pithos.backends.base import (
    NotAllowedError, QuotaError, ContainerNotEmpty, ItemNotExists,
69
    VersionNotExists, ContainerExists, InvalidHash)
70

Antony Chazapis's avatar
Antony Chazapis committed
71
72
from pithos.backends.filter import parse_filters

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

76

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

    return uuids

88

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

103

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

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

122

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

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

145

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

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

177

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

185
186
187
    x_auth_user = request.META.get('HTTP_X_AUTH_USER')
    x_auth_key = request.META.get('HTTP_X_AUTH_KEY')
    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
192
193
    uri = request.build_absolute_uri()
    if '?' in uri:
        uri = uri[:uri.find('?')]
194

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

200

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

209
210
211
    marker = request.GET.get('marker')
    limit = get_int_parameter(request.GET.get('limit'))
    if not limit:
212
        limit = settings.API_LIST_LIMIT
213

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

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

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

    if TRANSLATE_UUIDS:
        uuids = list(d['name'] for d in account_meta)
        catalog = retrieve_displaynames(
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
252
            getattr(request, 'token', None), uuids, return_dict=True)
253
254
255
        for meta in account_meta:
            meta['name'] = catalog.get(meta.get('name'))

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

264

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

Antony Chazapis's avatar
Antony Chazapis committed
272
    until = get_int_parameter(request.GET.get('until'))
273
    usage = get_pithos_usage(request.x_auth_token)
Antony Chazapis's avatar
Antony Chazapis committed
274
    try:
275
        meta = request.backend.get_account_meta(
276
            request.user_uniq, v_account, 'pithos', until,
277
            external_quota=usage)
278
279
        groups = request.backend.get_account_groups(
            request.user_uniq, v_account)
280
281
282
283

        if TRANSLATE_UUIDS:
            for k in groups:
                groups[k] = retrieve_displaynames(
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
284
                    getattr(request, 'token', None), groups[k])
285
        policy = request.backend.get_account_policy(
286
            request.user_uniq, v_account, external_quota=usage)
Antony Chazapis's avatar
Antony Chazapis committed
287
    except NotAllowedError:
288
        raise faults.Forbidden('Not allowed')
289

290
    validate_modification_preconditions(request, meta)
291

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

296

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

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

343

344
@api_method('GET', format_allowed=True, user_required=True, logger=logger,
345
            serializations=["text", "xml", "json"])
346
347
def container_list(request, v_account):
    # Normal Response Codes: 200, 204
348
    # Error Response Codes: internalServerError (500),
349
    #                       itemNotFound (404),
350
    #                       forbidden (403),
351
    #                       badRequest (400)
352

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

366
    validate_modification_preconditions(request, meta)
367

368
    response = HttpResponse()
369
    put_account_headers(response, meta, groups, policy)
370

371
    marker = request.GET.get('marker')
372
373
    limit = get_int_parameter(request.GET.get('limit'))
    if not limit:
374
        limit = settings.API_LIST_LIMIT
375

376
377
378
    shared = False
    if 'shared' in request.GET:
        shared = True
379
380
381
382
383
384
385

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

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

387
    try:
388
389
        containers = request.backend.list_containers(
            request.user_uniq, v_account,
390
            marker, limit, shared, until, public_granted)
Antony Chazapis's avatar
Antony Chazapis committed
391
    except NotAllowedError:
392
        raise faults.Forbidden('Not allowed')
393
394
    except NameError:
        containers = []
395

396
397
398
399
400
401
    if request.serialization == 'text':
        if len(containers) == 0:
            # The cloudfiles python bindings expect 200 if json/xml.
            response.status_code = 204
            return response
        response.status_code = 200
402
        response.content = '\n'.join(containers) + '\n'
403
        return response
404

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

434

435
@api_method('HEAD', user_required=True, logger=logger)
436
437
def container_meta(request, v_account, v_container):
    # Normal Response Codes: 204
438
    # Error Response Codes: internalServerError (500),
439
    #                       itemNotFound (404),
440
    #                       forbidden (403),
441
    #                       badRequest (400)
442

Antony Chazapis's avatar
Antony Chazapis committed
443
    until = get_int_parameter(request.GET.get('until'))
444
    try:
445
        meta = request.backend.get_container_meta(request.user_uniq, v_account,
446
                                                  v_container, 'pithos', until)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
447
448
449
450
        meta['object_meta'] = \
            request.backend.list_container_meta(request.user_uniq,
                                                v_account, v_container,
                                                'pithos', until)
451
452
453
        policy = request.backend.get_container_policy(
            request.user_uniq, v_account,
            v_container)
Antony Chazapis's avatar
Antony Chazapis committed
454
    except NotAllowedError:
455
        raise faults.Forbidden('Not allowed')
456
    except ItemNotExists:
457
        raise faults.ItemNotFound('Container does not exist')
458

459
    validate_modification_preconditions(request, meta)
460

461
    response = HttpResponse(status=204)
462
    put_container_headers(request, response, meta, policy)
463
464
    return response

465

466
@api_method('PUT', user_required=True, logger=logger)
467
468
def container_create(request, v_account, v_container):
    # Normal Response Codes: 201, 202
469
    # Error Response Codes: internalServerError (500),
470
    #                       itemNotFound (404),
471
    #                       forbidden (403),
472
    #                       badRequest (400)
473

Antony Chazapis's avatar
Antony Chazapis committed
474
    meta, policy = get_container_headers(request)
475

476
    try:
477
478
        request.backend.put_container(
            request.user_uniq, v_account, v_container, policy)
479
        ret = 201
Antony Chazapis's avatar
Antony Chazapis committed
480
    except NotAllowedError:
481
        raise faults.Forbidden('Not allowed')
482
    except ValueError:
483
        raise faults.BadRequest('Invalid policy header')
484
    except ContainerExists:
485
        ret = 202
486

487
    if ret == 202 and policy:
Antony Chazapis's avatar
Antony Chazapis committed
488
        try:
489
490
491
            request.backend.update_container_policy(
                request.user_uniq, v_account,
                v_container, policy, replace=False)
492
        except NotAllowedError:
493
            raise faults.Forbidden('Not allowed')
494
        except ItemNotExists:
495
            raise faults.ItemNotFound('Container does not exist')
496
        except ValueError:
497
            raise faults.BadRequest('Invalid policy header')
498
499
    if meta:
        try:
500
            request.backend.update_container_meta(request.user_uniq, v_account,
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
501
502
                                                  v_container, 'pithos',
                                                  meta, replace=False)
Antony Chazapis's avatar
Antony Chazapis committed
503
        except NotAllowedError:
504
            raise faults.Forbidden('Not allowed')
505
        except ItemNotExists:
506
            raise faults.ItemNotFound('Container does not exist')
507

508
509
    return HttpResponse(status=ret)

510

Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
511
512
@api_method('POST', format_allowed=True, user_required=True, logger=logger,
            lock_container_path=True)
513
514
def container_update(request, v_account, v_container):
    # Normal Response Codes: 202
515
    # Error Response Codes: internalServerError (500),
516
    #                       itemNotFound (404),
517
    #                       forbidden (403),
518
    #                       badRequest (400)
519

Antony Chazapis's avatar
Antony Chazapis committed
520
    meta, policy = get_container_headers(request)
521
522
523
    replace = True
    if 'update' in request.GET:
        replace = False
Antony Chazapis's avatar
Antony Chazapis committed
524
525
    if policy:
        try:
526
527
528
            request.backend.update_container_policy(
                request.user_uniq, v_account,
                v_container, policy, replace)
Antony Chazapis's avatar
Antony Chazapis committed
529
        except NotAllowedError:
530
            raise faults.Forbidden('Not allowed')
531
        except ItemNotExists:
532
            raise faults.ItemNotFound('Container does not exist')
Antony Chazapis's avatar
Antony Chazapis committed
533
        except ValueError:
534
            raise faults.BadRequest('Invalid policy header')
535
536
    if meta or replace:
        try:
537
            request.backend.update_container_meta(request.user_uniq, v_account,
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
538
539
                                                  v_container, 'pithos',
                                                  meta, replace)
540
        except NotAllowedError:
541
            raise faults.Forbidden('Not allowed')
542
        except ItemNotExists:
543
            raise faults.ItemNotFound('Container does not exist')
544

545
546
    content_length = -1
    if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
547
548
        content_length = get_int_parameter(
            request.META.get('CONTENT_LENGTH', 0))
549
    content_type = request.META.get('CONTENT_TYPE')
550
    hashmap = []
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
551
552
553
    if (content_type
            and content_type == 'application/octet-stream'
            and content_length != 0):
554
        for data in socket_read_iterator(request, content_length,
555
                                         request.backend.block_size):
556
            # TODO: Raise 408 (Request Timeout) if this takes too long.
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
557
558
            # TODO: Raise 499 (Client Disconnect) if a length is defined
            #       and we stop before getting this much data.
559
            hashmap.append(request.backend.put_block(data))
560

561
562
    response = HttpResponse(status=202)
    if hashmap:
563
        response.content = simple_list_response(request, hashmap)
564
    return response
565

566

Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
567
568
@api_method('DELETE', user_required=True, logger=logger,
            lock_container_path=True)
569
570
def container_delete(request, v_account, v_container):
    # Normal Response Codes: 204
571
    # Error Response Codes: internalServerError (500),
572
573
    #                       conflict (409),
    #                       itemNotFound (404),
574
    #                       forbidden (403),
575
    #                       badRequest (400)
576
    #                       requestentitytoolarge (413)
577

578
    until = get_int_parameter(request.GET.get('until'))
579

580
    delimiter = request.GET.get('delimiter')
581

582
    try:
583
584
585
        request.backend.delete_container(
            request.user_uniq, v_account, v_container,
            until, delimiter=delimiter)
Antony Chazapis's avatar
Antony Chazapis committed
586
    except NotAllowedError:
587
        raise faults.Forbidden('Not allowed')
588
    except ItemNotExists:
589
        raise faults.ItemNotFound('Container does not exist')
590
    except ContainerNotEmpty:
591
        raise faults.Conflict('Container is not empty')
592
    except QuotaError, e:
593
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
594
595
    return HttpResponse(status=204)

596

597
@api_method('GET', format_allowed=True, user_required=True, logger=logger,
598
            serializations=["text", "xml", "json"])
599
600
def object_list(request, v_account, v_container):
    # Normal Response Codes: 200, 204
601
    # Error Response Codes: internalServerError (500),
602
    #                       itemNotFound (404),
603
    #                       forbidden (403),
604
    #                       badRequest (400)
605

Antony Chazapis's avatar
Antony Chazapis committed
606
    until = get_int_parameter(request.GET.get('until'))
607
    try:
608
        meta = request.backend.get_container_meta(request.user_uniq, v_account,
609
                                                  v_container, 'pithos', until)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
610
611
612
613
        meta['object_meta'] = \
            request.backend.list_container_meta(request.user_uniq,
                                                v_account, v_container,
                                                'pithos', until)
614
615
616
        policy = request.backend.get_container_policy(
            request.user_uniq, v_account,
            v_container)
Antony Chazapis's avatar
Antony Chazapis committed
617
    except NotAllowedError:
618
        raise faults.Forbidden('Not allowed')
619
    except ItemNotExists:
620
        raise faults.ItemNotFound('Container does not exist')
621

622
    validate_modification_preconditions(request, meta)
623

624
    response = HttpResponse()
625
    put_container_headers(request, response, meta, policy)
626

627
628
629
    path = request.GET.get('path')
    prefix = request.GET.get('prefix')
    delimiter = request.GET.get('delimiter')
630

631
632
633
634
635
636
    # Path overrides prefix and delimiter.
    virtual = True
    if path:
        prefix = path
        delimiter = '/'
        virtual = False
637

638
    # Naming policy.
639
    if prefix and delimiter and not prefix.endswith(delimiter):
640
641
642
643
        prefix = prefix + delimiter
    if not prefix:
        prefix = ''
    prefix = prefix.lstrip('/')
644

645
    marker = request.GET.get('marker')
646
647
    limit = get_int_parameter(request.GET.get('limit'))
    if not limit:
648
        limit = settings.API_LIST_LIMIT
649

650
651
    keys = request.GET.get('meta')
    if keys:
652
653
        keys = [smart_str(x.strip()) for x in keys.split(',')
                if x.strip() != '']
654
655
656
        included, excluded, opers = parse_filters(keys)
        keys = []
        keys += [format_header_key('X-Object-Meta-' + x) for x in included]
657
658
659
660
        keys += ['!' + format_header_key('X-Object-Meta-' + x)
                 for x in excluded]
        keys += ['%s%s%s' % (format_header_key(
            'X-Object-Meta-' + k), o, v) for k, o, v in opers]
661
662
    else:
        keys = []
663

664
665
666
    shared = False
    if 'shared' in request.GET:
        shared = True
667
668
669

    public_requested = 'public' in request.GET
    public_granted = public_requested and request.user_uniq == v_account
670

671
672
673
674
    if public_requested and not public_granted:
        raise faults.Forbidden(
            'PUblic object listing is not allowed to non path owners')

675
    if request.serialization == 'text':
676
        try:
677
678
679
680
            objects = request.backend.list_objects(
                request.user_uniq, v_account,
                v_container, prefix, delimiter, marker,
                limit, virtual, 'pithos', keys, shared,
681
                until, None, public_granted)
682
        except NotAllowedError:
683
            raise faults.Forbidden('Not allowed')
684
        except ItemNotExists:
685
            raise faults.ItemNotFound('Container does not exist')
686

687
688
689
690
691
        if len(objects) == 0:
            # The cloudfiles python bindings expect 200 if json/xml.
            response.status_code = 204
            return response
        response.status_code = 200
692
        response.content = '\n'.join([x[0] for x in objects]) + '\n'
693
        return response
694

695
    try:
696
        objects = request.backend.list_object_meta(
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
697
698
699
            request.user_uniq, v_account, v_container, prefix, delimiter,
            marker, limit, virtual, 'pithos', keys, shared, until, None,
            public_granted)
700
701
702
        object_permissions = {}
        object_public = {}
        if until is None:
703
704
            name = '/'.join((v_account, v_container, ''))
            name_idx = len(name)
705
            objects_bulk = []
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
706
707
            for x in request.backend.list_object_permissions(
                    request.user_uniq, v_account, v_container, prefix):
708
709
710
711

                # filter out objects which are not under the container
                if name != x[:name_idx]:
                    continue
712
                objects_bulk.append(x[name_idx:])
713

714
715
716
717
718
            if len(objects_bulk) > 0:
                object_permissions = \
                    request.backend.get_object_permissions_bulk(
                        request.user_uniq, v_account, v_container,
                        objects_bulk)
719

720
721
722
            if request.user_uniq == v_account:
                # Bring public information only if the request user
                # is the object owner
723
724
725
726
                for k, v in request.backend.list_object_public(
                        request.user_uniq, v_account,
                        v_container, prefix).iteritems():
                    object_public[k[name_idx:]] = v
727
    except NotAllowedError:
728
        raise faults.Forbidden('Not allowed')
729
    except ItemNotExists:
730
        raise faults.ItemNotFound('Container does not exist')
731

732
    object_meta = []
733
    for meta in objects:
734
735
736
737
        if TRANSLATE_UUIDS:
            modified_by = meta.get('modified_by')
            if modified_by:
                l = retrieve_displaynames(
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
738
                    getattr(request, 'token', None), [meta['modified_by']])
739
740
741
                if l is not None and len(l) == 1:
                    meta['modified_by'] = l[0]

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

770
    if request.serialization == 'xml':
771
772
773
        data = render_to_string(
            'objects.xml', {'container': v_container, 'objects': object_meta})
    elif request.serialization == 'json':
774
        data = json.dumps(object_meta, default=json_encode_decimal)
775
776
777
778
    response.status_code = 200
    response.content = data
    return response

779