functions.py 59.7 KB
Newer Older
Vangelis Koukis's avatar
Vangelis Koukis committed
1
# Copyright (C) 2010-2014 GRNET S.A.
2
#
Vangelis Koukis's avatar
Vangelis Koukis committed
3
4
5
6
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
7
#
Vangelis Koukis's avatar
Vangelis Koukis committed
8
9
10
11
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
12
#
Vangelis Koukis's avatar
Vangelis Koukis committed
13
14
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Antony Chazapis's avatar
Antony Chazapis committed
15

16
17
18
19
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
20
from django.utils.encoding import smart_str
21
from django.views.decorators.csrf import csrf_exempt
22

23
from astakosclient import AstakosClient
24

25
from snf_django.lib import api
26
27
from snf_django.lib.api import faults

28
from pithos.api.util import (
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
29
30
31
32
33
34
35
36
37
    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,
38
    api_method, is_uuid, retrieve_uuid, retrieve_uuids,
39
    retrieve_displaynames, Checksum, NoChecksum
Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
40
)
41

42
from pithos.api.settings import (UPDATE_MD5, TRANSLATE_UUIDS,
43
                                 SERVICE_TOKEN, ASTAKOS_AUTH_URL)
44

45
46
from pithos.api import settings

Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
47
48
from pithos.backends.base import (
    NotAllowedError, QuotaError, ContainerNotEmpty, ItemNotExists,
49
    VersionNotExists, ContainerExists, InvalidHash, IllegalOperationError)
50

Antony Chazapis's avatar
Antony Chazapis committed
51
52
from pithos.backends.filter import parse_filters

53
import logging
54
55
logger = logging.getLogger(__name__)

56

57
58
def get_uuids(names):
    try:
59
60
61
        astakos = AstakosClient(SERVICE_TOKEN, ASTAKOS_AUTH_URL,
                                retry=2, use_pool=True, logger=logger)
        uuids = astakos.service_get_uuids(names)
62
63
64
65
66
67
    except Exception, e:
        logger.exception(e)
        return {}

    return uuids

68

69
@csrf_exempt
70
71
def top_demux(request):
    if request.method == 'GET':
72
73
74
75
76
77
78
79
        try:
            request.GET['X-Auth-Token']
        except KeyError:
            try:
                request.META['HTTP_X_AUTH_TOKEN']
            except KeyError:
                return authenticate(request)
        return account_list(request)
80
    else:
81
        return api.api_method_not_allowed(request, allowed_methods=['GET'])
82

83

84
@csrf_exempt
85
def account_demux(request, v_account):
86
87
88
89
90
91
92
    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]

93
94
95
96
    if request.method == 'HEAD':
        return account_meta(request, v_account)
    elif request.method == 'POST':
        return account_update(request, v_account)
97
98
    elif request.method == 'GET':
        return container_list(request, v_account)
99
    else:
100
101
102
103
        return api.api_method_not_allowed(request,
                                          allowed_methods=['HEAD',
                                                           'POST',
                                                           'GET'])
104

105

106
@csrf_exempt
107
def container_demux(request, v_account, v_container):
108
109
110
111
112
113
114
    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]

115
116
117
118
119
120
121
122
    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)
123
124
    elif request.method == 'GET':
        return object_list(request, v_account, v_container)
125
    else:
126
127
128
129
130
131
        return api.api_method_not_allowed(request,
                                          allowed_methods=['HEAD',
                                                           'PUT',
                                                           'POST',
                                                           'DELETE',
                                                           'GET'])
132

133

134
@csrf_exempt
135
def object_demux(request, v_account, v_container, v_object):
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
136
137
    # Helper to avoid placing the token in the URL
    # when loading objects from a browser.
138
139
140
141
142
143
144
    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]

145
146
147
148
149
150
151
152
153
154
155
    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
156
157
        if request.META.get(
                'CONTENT_TYPE', '').startswith('multipart/form-data'):
158
            return object_write_form(request, v_account, v_container, v_object)
159
160
161
162
        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:
163
164
165
166
167
168
169
        return api.api_method_not_allowed(request, allowed_methods=['HEAD',
                                                                    'GET',
                                                                    'PUT',
                                                                    'COPY',
                                                                    'MOVE',
                                                                    'POST',
                                                                    'DELETE'])
170

171

172
@api_method('GET', token_required=False, user_required=False, logger=logger)
173
174
def authenticate(request):
    # Normal Response Codes: 204
175
    # Error Response Codes: internalServerError (500),
176
    #                       forbidden (403),
177
    #                       badRequest (400)
178

179
180
181
    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:
182
        raise faults.BadRequest('Missing X-Auth-User or X-Auth-Key header')
183
    response = HttpResponse(status=204)
184

185
186
187
    uri = request.build_absolute_uri()
    if '?' in uri:
        uri = uri[:uri.find('?')]
188

189
    response['X-Auth-Token'] = x_auth_key
190
191
    response['X-Storage-Url'] = uri + ('' if uri.endswith('/')
                                       else '/') + x_auth_user
192
193
    return response

194

195
196
@api_method('GET', format_allowed=True, user_required=True, logger=logger,
            serializations=["text", "xml", "json"])
197
198
def account_list(request):
    # Normal Response Codes: 200, 204
199
    # Error Response Codes: internalServerError (500),
200
201
    #                       badRequest (400)
    response = HttpResponse()
202

203
204
205
    marker = request.GET.get('marker')
    limit = get_int_parameter(request.GET.get('limit'))
    if not limit:
206
        limit = settings.API_LIST_LIMIT
207

208
    accounts = request.backend.list_accounts(request.user_uniq, marker, limit)
209

210
    if request.serialization == 'text':
211
212
        if TRANSLATE_UUIDS:
            accounts = retrieve_displaynames(
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
213
                getattr(request, 'token', None), accounts)
214
215
216
217
218
219
220
        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
221

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

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

248
249
    if request.serialization == 'xml':
        data = render_to_string('accounts.xml', {'accounts': account_meta})
250
    elif request.serialization == 'json':
251
252
253
254
255
        data = json.dumps(account_meta)
    response.status_code = 200
    response.content = data
    return response

256

257
@api_method('HEAD', user_required=True, logger=logger)
258
259
def account_meta(request, v_account):
    # Normal Response Codes: 204
260
    # Error Response Codes: internalServerError (500),
261
    #                       forbidden (403),
262
    #                       badRequest (400)
263

Antony Chazapis's avatar
Antony Chazapis committed
264
    until = get_int_parameter(request.GET.get('until'))
Antony Chazapis's avatar
Antony Chazapis committed
265
    try:
266
        meta = request.backend.get_account_meta(
267
            request.user_uniq, v_account, 'pithos', until)
268
269
        groups = request.backend.get_account_groups(
            request.user_uniq, v_account)
270
271
272
273

        if TRANSLATE_UUIDS:
            for k in groups:
                groups[k] = retrieve_displaynames(
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
274
                    getattr(request, 'token', None), groups[k])
275
        policy = request.backend.get_account_policy(
276
            request.user_uniq, v_account)
277
        logger.debug(policy)
Antony Chazapis's avatar
Antony Chazapis committed
278
    except NotAllowedError:
279
        raise faults.Forbidden('Not allowed')
280

281
    validate_modification_preconditions(request, meta)
282

283
    response = HttpResponse(status=204)
284
    put_account_headers(response, meta, groups, policy)
285
286
    return response

287

288
@api_method('POST', user_required=True, logger=logger)
289
290
def account_update(request, v_account):
    # Normal Response Codes: 202
291
    # Error Response Codes: internalServerError (500),
292
    #                       forbidden (403),
293
    #                       badRequest (400)
294

295
    meta, groups = get_account_headers(request)
296
297
298
299
    for k in groups:
        if TRANSLATE_UUIDS:
            try:
                groups[k] = retrieve_uuids(
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
300
301
302
                    getattr(request, 'token', None),
                    groups[k],
                    fail_silently=False)
303
            except ItemNotExists, e:
304
                raise faults.BadRequest(
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
305
                    'Bad X-Account-Group header value: %s' % e)
306
307
308
309
310
311
312
        else:
            try:
                retrieve_displaynames(
                    getattr(request, 'token', None),
                    groups[k],
                    fail_silently=False)
            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
    replace = True
    if 'update' in request.GET:
317
        replace = False
318
319
    if groups:
        try:
320
            request.backend.update_account_groups(request.user_uniq, v_account,
321
                                                  groups, replace)
322
        except NotAllowedError:
323
            raise faults.Forbidden('Not allowed')
324
        except ValueError:
325
            raise faults.BadRequest('Invalid groups header')
326
327
    if meta or replace:
        try:
328
329
            request.backend.update_account_meta(request.user_uniq, v_account,
                                                'pithos', meta, replace)
330
        except NotAllowedError:
331
            raise faults.Forbidden('Not allowed')
332
333
    return HttpResponse(status=202)

334

335
@api_method('GET', format_allowed=True, user_required=True, logger=logger,
336
            serializations=["text", "xml", "json"])
337
338
def container_list(request, v_account):
    # Normal Response Codes: 200, 204
339
    # Error Response Codes: internalServerError (500),
340
    #                       itemNotFound (404),
341
    #                       forbidden (403),
342
    #                       badRequest (400)
343

Antony Chazapis's avatar
Antony Chazapis committed
344
    until = get_int_parameter(request.GET.get('until'))
Antony Chazapis's avatar
Antony Chazapis committed
345
    try:
346
        meta = request.backend.get_account_meta(
347
            request.user_uniq, v_account, 'pithos', until)
348
349
350
        groups = request.backend.get_account_groups(
            request.user_uniq, v_account)
        policy = request.backend.get_account_policy(
351
            request.user_uniq, v_account)
Antony Chazapis's avatar
Antony Chazapis committed
352
    except NotAllowedError:
353
        raise faults.Forbidden('Not allowed')
354

355
    validate_modification_preconditions(request, meta)
356

357
    response = HttpResponse()
358
    put_account_headers(response, meta, groups, policy)
359

360
    marker = request.GET.get('marker')
361
362
    limit = get_int_parameter(request.GET.get('limit'))
    if not limit:
363
        limit = settings.API_LIST_LIMIT
364

365
366
367
    shared = False
    if 'shared' in request.GET:
        shared = True
368
369
370
371
372
373
374

    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')
375

376
    try:
377
378
        containers = request.backend.list_containers(
            request.user_uniq, v_account,
379
            marker, limit, shared, until, public_granted)
Antony Chazapis's avatar
Antony Chazapis committed
380
    except NotAllowedError:
381
        raise faults.Forbidden('Not allowed')
382
383
    except NameError:
        containers = []
384

385
386
387
388
389
390
    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
391
        response.content = '\n'.join(containers) + '\n'
392
        return response
393

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

423

424
@api_method('HEAD', user_required=True, logger=logger)
425
426
def container_meta(request, v_account, v_container):
    # Normal Response Codes: 204
427
    # Error Response Codes: internalServerError (500),
428
    #                       itemNotFound (404),
429
    #                       forbidden (403),
430
    #                       badRequest (400)
431

Antony Chazapis's avatar
Antony Chazapis committed
432
    until = get_int_parameter(request.GET.get('until'))
433
    try:
434
        meta = request.backend.get_container_meta(request.user_uniq, v_account,
435
                                                  v_container, 'pithos', until)
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
436
437
438
439
        meta['object_meta'] = \
            request.backend.list_container_meta(request.user_uniq,
                                                v_account, v_container,
                                                'pithos', until)
440
441
442
        policy = request.backend.get_container_policy(
            request.user_uniq, v_account,
            v_container)
Antony Chazapis's avatar
Antony Chazapis committed
443
    except NotAllowedError:
444
        raise faults.Forbidden('Not allowed')
445
    except ItemNotExists:
446
        raise faults.ItemNotFound('Container does not exist')
447

448
    validate_modification_preconditions(request, meta)
449

450
    response = HttpResponse(status=204)
451
    put_container_headers(request, response, meta, policy)
452
453
    return response

454

455
@api_method('PUT', user_required=True, logger=logger)
456
457
def container_create(request, v_account, v_container):
    # Normal Response Codes: 201, 202
458
    # Error Response Codes: internalServerError (500),
459
    #                       itemNotFound (404),
460
    #                       forbidden (403),
461
    #                       badRequest (400)
462

Antony Chazapis's avatar
Antony Chazapis committed
463
    meta, policy = get_container_headers(request)
464

465
    try:
466
467
        request.backend.put_container(
            request.user_uniq, v_account, v_container, policy)
468
        ret = 201
Antony Chazapis's avatar
Antony Chazapis committed
469
    except NotAllowedError:
470
        raise faults.Forbidden('Not allowed')
471
    except ValueError:
472
        raise faults.BadRequest('Invalid policy header')
473
    except ContainerExists:
474
        ret = 202
475

476
    if ret == 202 and policy:
Antony Chazapis's avatar
Antony Chazapis committed
477
        try:
478
479
480
            request.backend.update_container_policy(
                request.user_uniq, v_account,
                v_container, policy, replace=False)
481
        except NotAllowedError:
482
            raise faults.Forbidden('Not allowed')
483
        except ItemNotExists:
484
            raise faults.ItemNotFound('Container does not exist')
485
        except ValueError:
486
            raise faults.BadRequest('Invalid policy header')
487
488
        except QuotaError, e:
            raise faults.RequestEntityTooLarge('Quota error: %s' % e)
489
490
    if meta:
        try:
491
            request.backend.update_container_meta(request.user_uniq, v_account,
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
492
493
                                                  v_container, 'pithos',
                                                  meta, replace=False)
Antony Chazapis's avatar
Antony Chazapis committed
494
        except NotAllowedError:
495
            raise faults.Forbidden('Not allowed')
496
        except ItemNotExists:
497
            raise faults.ItemNotFound('Container does not exist')
498

499
500
    return HttpResponse(status=ret)

501

Sofia Papagiannaki's avatar
Sofia Papagiannaki committed
502
503
@api_method('POST', format_allowed=True, user_required=True, logger=logger,
            lock_container_path=True)
504
505
def container_update(request, v_account, v_container):
    # Normal Response Codes: 202
506
    # Error Response Codes: internalServerError (500),
507
    #                       itemNotFound (404),
508
    #                       forbidden (403),
509
    #                       badRequest (400)
510

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

538
539
    content_length = -1
    if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
540
541
        content_length = get_int_parameter(
            request.META.get('CONTENT_LENGTH', 0))
542
    content_type = request.META.get('CONTENT_TYPE')
543
    hashmap = []
Ilias Tsitsimpis's avatar
Ilias Tsitsimpis committed
544
545
546
    if (content_type
            and content_type == 'application/octet-stream'
            and content_length != 0):
547
548
549
550
551
552
553

        try:
            request.backend.can_write_container(request.user_uniq, v_account,
                                                v_container)
        except NotAllowedError:
            raise faults.Forbidden('Not allowed')

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
        request.backend.delete_container(
            request.user_uniq, v_account, v_container,
585
            until, delimiter=delimiter, listing_limit=settings.API_LIST_LIMIT)
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')
Antony Chazapis's avatar