Statistics
| Branch: | Tag: | Revision:

root / pithos / api / functions.py @ 804e8fe7

History | View | Annotate | Download (39.9 kB)

1
# Copyright 2011 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
import logging
35
import hashlib
36

    
37
from django.conf import settings
38
from django.http import HttpResponse
39
from django.template.loader import render_to_string
40
from django.utils import simplejson as json
41
from django.utils.http import parse_etags
42
from xml.dom import minidom
43

    
44
from pithos.api.faults import (Fault, NotModified, BadRequest, Unauthorized, ItemNotFound, Conflict,
45
    LengthRequired, PreconditionFailed, RangeNotSatisfiable, UnprocessableEntity)
46
from pithos.api.util import (rename_meta_key, format_header_key, printable_header_dict, get_account_headers,
47
    put_account_headers, get_container_headers, put_container_headers, get_object_headers, put_object_headers,
48
    update_manifest_meta, update_sharing_meta, update_public_meta, validate_modification_preconditions,
49
    validate_matching_preconditions, split_container_object_string, copy_or_move_object,
50
    get_int_parameter, get_content_length, get_content_range, raw_input_socket,
51
    socket_read_iterator, object_data_response, put_object_block, hashmap_hash, api_method)
52
from pithos.backends import backend
53
from pithos.backends.base import NotAllowedError
54

    
55

    
56
logger = logging.getLogger(__name__)
57

    
58

    
59
def top_demux(request):
60
    if request.method == 'GET':
61
        if request.user:
62
            return account_list(request)
63
        return authenticate(request)
64
    else:
65
        return method_not_allowed(request)
66

    
67
def account_demux(request, v_account):
68
    if request.method == 'HEAD':
69
        return account_meta(request, v_account)
70
    elif request.method == 'POST':
71
        return account_update(request, v_account)
72
    elif request.method == 'GET':
73
        return container_list(request, v_account)
74
    else:
75
        return method_not_allowed(request)
76

    
77
def container_demux(request, v_account, v_container):
78
    if request.method == 'HEAD':
79
        return container_meta(request, v_account, v_container)
80
    elif request.method == 'PUT':
81
        return container_create(request, v_account, v_container)
82
    elif request.method == 'POST':
83
        return container_update(request, v_account, v_container)
84
    elif request.method == 'DELETE':
85
        return container_delete(request, v_account, v_container)
86
    elif request.method == 'GET':
87
        return object_list(request, v_account, v_container)
88
    else:
89
        return method_not_allowed(request)
90

    
91
def object_demux(request, v_account, v_container, v_object):
92
    if request.method == 'HEAD':
93
        return object_meta(request, v_account, v_container, v_object)
94
    elif request.method == 'GET':
95
        return object_read(request, v_account, v_container, v_object)
96
    elif request.method == 'PUT':
97
        return object_write(request, v_account, v_container, v_object)
98
    elif request.method == 'COPY':
99
        return object_copy(request, v_account, v_container, v_object)
100
    elif request.method == 'MOVE':
101
        return object_move(request, v_account, v_container, v_object)
102
    elif request.method == 'POST':
103
        if request.META.get('CONTENT_TYPE', '').startswith('multipart/form-data'):
104
            return object_write_form(request, v_account, v_container, v_object)
105
        return object_update(request, v_account, v_container, v_object)
106
    elif request.method == 'DELETE':
107
        return object_delete(request, v_account, v_container, v_object)
108
    else:
109
        return method_not_allowed(request)
110

    
111
@api_method('GET')
112
def authenticate(request):
113
    # Normal Response Codes: 204
114
    # Error Response Codes: serviceUnavailable (503),
115
    #                       unauthorized (401),
116
    #                       badRequest (400)
117
    
118
    x_auth_user = request.META.get('HTTP_X_AUTH_USER')
119
    x_auth_key = request.META.get('HTTP_X_AUTH_KEY')
120
    if not x_auth_user or not x_auth_key:
121
        raise BadRequest('Missing X-Auth-User or X-Auth-Key header')
122
    response = HttpResponse(status=204)
123
    
124
    inv_auth_tokens = dict((v, k) for k, v in settings.AUTH_TOKENS.items())
125
    uri = request.build_absolute_uri()
126
    if '?' in uri:
127
        uri = uri[:uri.find('?')]
128
    
129
    response['X-Auth-Token'] = inv_auth_tokens.get(x_auth_user, '0000')
130
    response['X-Storage-Url'] = uri + (uri.endswith('/') and '' or '/') + x_auth_user
131
    return response
132

    
133
@api_method('GET', format_allowed=True)
134
def account_list(request):
135
    # Normal Response Codes: 200, 204
136
    # Error Response Codes: serviceUnavailable (503),
137
    #                       badRequest (400)
138
    
139
    response = HttpResponse()
140
    
141
    marker = request.GET.get('marker')
142
    limit = get_int_parameter(request.GET.get('limit'))
143
    if not limit:
144
        limit = 10000
145
    
146
    accounts = backend.list_accounts(request.user, marker, limit)
147
    
148
    if request.serialization == 'text':
149
        if len(accounts) == 0:
150
            # The cloudfiles python bindings expect 200 if json/xml.
151
            response.status_code = 204
152
            return response
153
        response.status_code = 200
154
        response.content = '\n'.join(accounts) + '\n'
155
        return response
156
    
157
    account_meta = []
158
    for x in accounts:
159
        try:
160
            meta = backend.get_account_meta(request.user, x)
161
            groups = backend.get_account_groups(request.user, x)
162
        except NotAllowedError:
163
            raise Unauthorized('Access denied')
164
        else:
165
            rename_meta_key(meta, 'modified', 'last_modified')
166
            rename_meta_key(meta, 'until_timestamp', 'x_account_until_timestamp')
167
            for k, v in groups.iteritems():
168
                meta['X-Container-Group-' + k] = ','.join(v)
169
            account_meta.append(printable_header_dict(meta))
170
    if request.serialization == 'xml':
171
        data = render_to_string('accounts.xml', {'accounts': account_meta})
172
    elif request.serialization  == 'json':
173
        data = json.dumps(account_meta)
174
    response.status_code = 200
175
    response.content = data
176
    return response
177

    
178
@api_method('HEAD')
179
def account_meta(request, v_account):
180
    # Normal Response Codes: 204
181
    # Error Response Codes: serviceUnavailable (503),
182
    #                       unauthorized (401),
183
    #                       badRequest (400)
184
    
185
    until = get_int_parameter(request.GET.get('until'))
186
    try:
187
        meta = backend.get_account_meta(request.user, v_account, until)
188
        groups = backend.get_account_groups(request.user, v_account)
189
    except NotAllowedError:
190
        raise Unauthorized('Access denied')
191
    
192
    response = HttpResponse(status=204)
193
    put_account_headers(response, meta, groups)
194
    return response
195

    
196
@api_method('POST')
197
def account_update(request, v_account):
198
    # Normal Response Codes: 202
199
    # Error Response Codes: serviceUnavailable (503),
200
    #                       unauthorized (401),
201
    #                       badRequest (400)
202
    
203
    meta, groups = get_account_headers(request)
204
    replace = True
205
    if 'update' in request.GET:
206
        replace = False
207
    if groups:
208
        try:
209
            backend.update_account_groups(request.user, v_account, groups, replace)
210
        except NotAllowedError:
211
            raise Unauthorized('Access denied')
212
        except ValueError:
213
            raise BadRequest('Invalid groups header')
214
    try:
215
        backend.update_account_meta(request.user, v_account, meta, replace)
216
    except NotAllowedError:
217
        raise Unauthorized('Access denied')
218
    return HttpResponse(status=202)
219

    
220
@api_method('GET', format_allowed=True)
221
def container_list(request, v_account):
222
    # Normal Response Codes: 200, 204
223
    # Error Response Codes: serviceUnavailable (503),
224
    #                       itemNotFound (404),
225
    #                       unauthorized (401),
226
    #                       badRequest (400)
227
    
228
    until = get_int_parameter(request.GET.get('until'))
229
    try:
230
        meta = backend.get_account_meta(request.user, v_account, until)
231
        groups = backend.get_account_groups(request.user, v_account)
232
    except NotAllowedError:
233
        raise Unauthorized('Access denied')
234
    
235
    validate_modification_preconditions(request, meta)
236
    
237
    response = HttpResponse()
238
    put_account_headers(response, meta, groups)
239
    
240
    marker = request.GET.get('marker')
241
    limit = get_int_parameter(request.GET.get('limit'))
242
    if not limit:
243
        limit = 10000
244
    
245
    shared = False
246
    if 'shared' in request.GET:
247
        shared = True
248
    
249
    try:
250
        containers = backend.list_containers(request.user, v_account, marker, limit, shared, until)
251
    except NotAllowedError:
252
        raise Unauthorized('Access denied')
253
    except NameError:
254
        containers = []
255
    
256
    if request.serialization == 'text':
257
        if len(containers) == 0:
258
            # The cloudfiles python bindings expect 200 if json/xml.
259
            response.status_code = 204
260
            return response
261
        response.status_code = 200
262
        response.content = '\n'.join(containers) + '\n'
263
        return response
264
    
265
    container_meta = []
266
    for x in containers:
267
        try:
268
            meta = backend.get_container_meta(request.user, v_account, x, until)
269
            policy = backend.get_container_policy(request.user, v_account, x)
270
        except NotAllowedError:
271
            raise Unauthorized('Access denied')
272
        except NameError:
273
            pass
274
        else:
275
            rename_meta_key(meta, 'modified', 'last_modified')
276
            rename_meta_key(meta, 'until_timestamp', 'x_container_until_timestamp')
277
            for k, v in policy.iteritems():
278
                meta['X-Container-Policy-' + k] = v
279
            container_meta.append(printable_header_dict(meta))
280
    if request.serialization == 'xml':
281
        data = render_to_string('containers.xml', {'account': v_account, 'containers': container_meta})
282
    elif request.serialization  == 'json':
283
        data = json.dumps(container_meta)
284
    response.status_code = 200
285
    response.content = data
286
    return response
287

    
288
@api_method('HEAD')
289
def container_meta(request, v_account, v_container):
290
    # Normal Response Codes: 204
291
    # Error Response Codes: serviceUnavailable (503),
292
    #                       itemNotFound (404),
293
    #                       unauthorized (401),
294
    #                       badRequest (400)
295
    
296
    until = get_int_parameter(request.GET.get('until'))
297
    try:
298
        meta = backend.get_container_meta(request.user, v_account, v_container, until)
299
        meta['object_meta'] = backend.list_object_meta(request.user, v_account, v_container, until)
300
        policy = backend.get_container_policy(request.user, v_account, v_container)
301
    except NotAllowedError:
302
        raise Unauthorized('Access denied')
303
    except NameError:
304
        raise ItemNotFound('Container does not exist')
305
    
306
    response = HttpResponse(status=204)
307
    put_container_headers(response, meta, policy)
308
    return response
309

    
310
@api_method('PUT')
311
def container_create(request, v_account, v_container):
312
    # Normal Response Codes: 201, 202
313
    # Error Response Codes: serviceUnavailable (503),
314
    #                       itemNotFound (404),
315
    #                       unauthorized (401),
316
    #                       badRequest (400)
317
    
318
    meta, policy = get_container_headers(request)
319
    
320
    try:
321
        backend.put_container(request.user, v_account, v_container, policy)
322
        ret = 201
323
    except NotAllowedError:
324
        raise Unauthorized('Access denied')
325
    except NameError:
326
        ret = 202
327
    
328
    if len(meta) > 0:
329
        try:
330
            backend.update_container_meta(request.user, v_account, v_container, meta, replace=True)
331
        except NotAllowedError:
332
            raise Unauthorized('Access denied')
333
        except NameError:
334
            raise ItemNotFound('Container does not exist')
335
    
336
    return HttpResponse(status=ret)
337

    
338
@api_method('POST')
339
def container_update(request, v_account, v_container):
340
    # Normal Response Codes: 202
341
    # Error Response Codes: serviceUnavailable (503),
342
    #                       itemNotFound (404),
343
    #                       unauthorized (401),
344
    #                       badRequest (400)
345
    
346
    meta, policy = get_container_headers(request)
347
    replace = True
348
    if 'update' in request.GET:
349
        replace = False
350
    if policy:
351
        try:
352
            backend.update_container_policy(request.user, v_account, v_container, policy, replace)
353
        except NotAllowedError:
354
            raise Unauthorized('Access denied')
355
        except NameError:
356
            raise ItemNotFound('Container does not exist')
357
        except ValueError:
358
            raise BadRequest('Invalid policy header')
359
    try:
360
        backend.update_container_meta(request.user, v_account, v_container, meta, replace)
361
    except NotAllowedError:
362
        raise Unauthorized('Access denied')
363
    except NameError:
364
        raise ItemNotFound('Container does not exist')
365
    return HttpResponse(status=202)
366

    
367
@api_method('DELETE')
368
def container_delete(request, v_account, v_container):
369
    # Normal Response Codes: 204
370
    # Error Response Codes: serviceUnavailable (503),
371
    #                       conflict (409),
372
    #                       itemNotFound (404),
373
    #                       unauthorized (401),
374
    #                       badRequest (400)
375
    
376
    until = get_int_parameter(request.GET.get('until'))
377
    try:
378
        backend.delete_container(request.user, v_account, v_container, until)
379
    except NotAllowedError:
380
        raise Unauthorized('Access denied')
381
    except NameError:
382
        raise ItemNotFound('Container does not exist')
383
    except IndexError:
384
        raise Conflict('Container is not empty')
385
    return HttpResponse(status=204)
386

    
387
@api_method('GET', format_allowed=True)
388
def object_list(request, v_account, v_container):
389
    # Normal Response Codes: 200, 204
390
    # Error Response Codes: serviceUnavailable (503),
391
    #                       itemNotFound (404),
392
    #                       unauthorized (401),
393
    #                       badRequest (400)
394
    
395
    until = get_int_parameter(request.GET.get('until'))
396
    try:
397
        meta = backend.get_container_meta(request.user, v_account, v_container, until)
398
        meta['object_meta'] = backend.list_object_meta(request.user, v_account, v_container, until)
399
        policy = backend.get_container_policy(request.user, v_account, v_container)
400
    except NotAllowedError:
401
        raise Unauthorized('Access denied')
402
    except NameError:
403
        raise ItemNotFound('Container does not exist')
404
    
405
    validate_modification_preconditions(request, meta)
406
    
407
    response = HttpResponse()
408
    put_container_headers(response, meta, policy)
409
    
410
    path = request.GET.get('path')
411
    prefix = request.GET.get('prefix')
412
    delimiter = request.GET.get('delimiter')
413
    
414
    # Path overrides prefix and delimiter.
415
    virtual = True
416
    if path:
417
        prefix = path
418
        delimiter = '/'
419
        virtual = False
420
    
421
    # Naming policy.
422
    if prefix and delimiter:
423
        prefix = prefix + delimiter
424
    if not prefix:
425
        prefix = ''
426
    prefix = prefix.lstrip('/')
427
    
428
    marker = request.GET.get('marker')
429
    limit = get_int_parameter(request.GET.get('limit'))
430
    if not limit:
431
        limit = 10000
432
    
433
    keys = request.GET.get('meta')
434
    if keys:
435
        keys = keys.split(',')
436
        keys = [format_header_key('X-Object-Meta-' + x.strip()) for x in keys if x.strip() != '']
437
    else:
438
        keys = []
439
    
440
    shared = False
441
    if 'shared' in request.GET:
442
        shared = True
443
    
444
    try:
445
        objects = backend.list_objects(request.user, v_account, v_container, prefix, delimiter, marker, limit, virtual, keys, shared, until)
446
    except NotAllowedError:
447
        raise Unauthorized('Access denied')
448
    except NameError:
449
        raise ItemNotFound('Container does not exist')
450
    
451
    if request.serialization == 'text':
452
        if len(objects) == 0:
453
            # The cloudfiles python bindings expect 200 if json/xml.
454
            response.status_code = 204
455
            return response
456
        response.status_code = 200
457
        response.content = '\n'.join([x[0] for x in objects]) + '\n'
458
        return response
459
    
460
    object_meta = []
461
    for x in objects:
462
        if x[1] is None:
463
            # Virtual objects/directories.
464
            object_meta.append({'subdir': x[0]})
465
        else:
466
            try:
467
                meta = backend.get_object_meta(request.user, v_account, v_container, x[0], x[1])
468
                if until is None:
469
                    permissions = backend.get_object_permissions(request.user, v_account, v_container, x[0])
470
                    public = backend.get_object_public(request.user, v_account, v_container, x[0])
471
                else:
472
                    permissions = None
473
                    public = None
474
            except NotAllowedError:
475
                raise Unauthorized('Access denied')
476
            except NameError:
477
                pass
478
            else:
479
                rename_meta_key(meta, 'modified', 'last_modified')
480
                rename_meta_key(meta, 'modified_by', 'x_object_modified_by')
481
                rename_meta_key(meta, 'version', 'x_object_version')
482
                rename_meta_key(meta, 'version_timestamp', 'x_object_version_timestamp')
483
                update_sharing_meta(permissions, v_account, v_container, x[0], meta)
484
                update_public_meta(public, meta)
485
                object_meta.append(printable_header_dict(meta))
486
    if request.serialization == 'xml':
487
        data = render_to_string('objects.xml', {'container': v_container, 'objects': object_meta})
488
    elif request.serialization  == 'json':
489
        data = json.dumps(object_meta)
490
    response.status_code = 200
491
    response.content = data
492
    return response
493

    
494
@api_method('HEAD')
495
def object_meta(request, v_account, v_container, v_object):
496
    # Normal Response Codes: 204
497
    # Error Response Codes: serviceUnavailable (503),
498
    #                       itemNotFound (404),
499
    #                       unauthorized (401),
500
    #                       badRequest (400)
501
    
502
    version = request.GET.get('version')
503
    try:
504
        meta = backend.get_object_meta(request.user, v_account, v_container, v_object, version)
505
        if version is None:
506
            permissions = backend.get_object_permissions(request.user, v_account, v_container, v_object)
507
            public = backend.get_object_public(request.user, v_account, v_container, v_object)
508
        else:
509
            permissions = None
510
            public = None
511
    except NotAllowedError:
512
        raise Unauthorized('Access denied')
513
    except NameError:
514
        raise ItemNotFound('Object does not exist')
515
    except IndexError:
516
        raise ItemNotFound('Version does not exist')
517
    
518
    update_manifest_meta(request, v_account, meta)
519
    update_sharing_meta(permissions, v_account, v_container, v_object, meta)
520
    update_public_meta(public, meta)
521
    
522
    response = HttpResponse(status=200)
523
    put_object_headers(response, meta)
524
    return response
525

    
526
@api_method('GET', format_allowed=True)
527
def object_read(request, v_account, v_container, v_object):
528
    # Normal Response Codes: 200, 206
529
    # Error Response Codes: serviceUnavailable (503),
530
    #                       rangeNotSatisfiable (416),
531
    #                       preconditionFailed (412),
532
    #                       itemNotFound (404),
533
    #                       unauthorized (401),
534
    #                       badRequest (400),
535
    #                       notModified (304)
536
    
537
    version = request.GET.get('version')
538
    
539
    # Reply with the version list. Do this first, as the object may be deleted.
540
    if version == 'list':
541
        if request.serialization == 'text':
542
            raise BadRequest('No format specified for version list.')
543
        
544
        try:
545
            v = backend.list_versions(request.user, v_account, v_container, v_object)
546
        except NotAllowedError:
547
            raise Unauthorized('Access denied')
548
        d = {'versions': v}
549
        if request.serialization == 'xml':
550
            d['object'] = v_object
551
            data = render_to_string('versions.xml', d)
552
        elif request.serialization  == 'json':
553
            data = json.dumps(d)
554
        
555
        response = HttpResponse(data, status=200)
556
        response['Content-Length'] = len(data)
557
        return response
558
    
559
    try:
560
        meta = backend.get_object_meta(request.user, v_account, v_container, v_object, version)
561
        if version is None:
562
            permissions = backend.get_object_permissions(request.user, v_account, v_container, v_object)
563
            public = backend.get_object_public(request.user, v_account, v_container, v_object)
564
        else:
565
            permissions = None
566
            public = None
567
    except NotAllowedError:
568
        raise Unauthorized('Access denied')
569
    except NameError:
570
        raise ItemNotFound('Object does not exist')
571
    except IndexError:
572
        raise ItemNotFound('Version does not exist')
573
    
574
    update_manifest_meta(request, v_account, meta)
575
    update_sharing_meta(permissions, v_account, v_container, v_object, meta)
576
    update_public_meta(public, meta)
577
    
578
    # Evaluate conditions.
579
    validate_modification_preconditions(request, meta)
580
    try:
581
        validate_matching_preconditions(request, meta)
582
    except NotModified:
583
        response = HttpResponse(status=304)
584
        response['ETag'] = meta['hash']
585
        return response
586
    
587
    sizes = []
588
    hashmaps = []
589
    if 'X-Object-Manifest' in meta:
590
        try:
591
            src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest'])
592
            objects = backend.list_objects(request.user, v_account, src_container, prefix=src_name, virtual=False)
593
        except NotAllowedError:
594
            raise Unauthorized('Access denied')
595
        except ValueError:
596
            raise BadRequest('Invalid X-Object-Manifest header')
597
        except NameError:
598
            raise ItemNotFound('Container does not exist')
599
        
600
        try:
601
            for x in objects:
602
                s, h = backend.get_object_hashmap(request.user, v_account, src_container, x[0], x[1])
603
                sizes.append(s)
604
                hashmaps.append(h)
605
        except NotAllowedError:
606
            raise Unauthorized('Access denied')
607
        except NameError:
608
            raise ItemNotFound('Object does not exist')
609
        except IndexError:
610
            raise ItemNotFound('Version does not exist')
611
    else:
612
        try:
613
            s, h = backend.get_object_hashmap(request.user, v_account, v_container, v_object, version)
614
            sizes.append(s)
615
            hashmaps.append(h)
616
        except NotAllowedError:
617
            raise Unauthorized('Access denied')
618
        except NameError:
619
            raise ItemNotFound('Object does not exist')
620
        except IndexError:
621
            raise ItemNotFound('Version does not exist')
622
    
623
    # Reply with the hashmap.
624
    if request.serialization != 'text':
625
        size = sum(sizes)
626
        hashmap = sum(hashmaps, [])
627
        d = {'block_size': backend.block_size, 'block_hash': backend.hash_algorithm, 'bytes': size, 'hashes': hashmap}
628
        if request.serialization == 'xml':
629
            d['object'] = v_object
630
            data = render_to_string('hashes.xml', d)
631
        elif request.serialization  == 'json':
632
            data = json.dumps(d)
633
        
634
        response = HttpResponse(data, status=200)
635
        put_object_headers(response, meta)
636
        response['Content-Length'] = len(data)
637
        return response
638
    
639
    return object_data_response(request, sizes, hashmaps, meta)
640

    
641
@api_method('PUT', format_allowed=True)
642
def object_write(request, v_account, v_container, v_object):
643
    # Normal Response Codes: 201
644
    # Error Response Codes: serviceUnavailable (503),
645
    #                       unprocessableEntity (422),
646
    #                       lengthRequired (411),
647
    #                       conflict (409),
648
    #                       itemNotFound (404),
649
    #                       unauthorized (401),
650
    #                       badRequest (400)
651
    
652
    if not request.GET.get('format'):
653
        request.serialization = 'text'
654
    
655
    copy_from = request.META.get('HTTP_X_COPY_FROM')
656
    move_from = request.META.get('HTTP_X_MOVE_FROM')
657
    if copy_from or move_from:
658
        content_length = get_content_length(request) # Required by the API.
659
        
660
        if move_from:
661
            try:
662
                src_container, src_name = split_container_object_string(move_from)
663
            except ValueError:
664
                raise BadRequest('Invalid X-Move-From header')
665
            copy_or_move_object(request, v_account, src_container, src_name, v_container, v_object, move=True)
666
        else:
667
            try:
668
                src_container, src_name = split_container_object_string(copy_from)
669
            except ValueError:
670
                raise BadRequest('Invalid X-Copy-From header')
671
            copy_or_move_object(request, v_account, src_container, src_name, v_container, v_object, move=False)
672
        return HttpResponse(status=201)
673
    
674
    meta, permissions, public = get_object_headers(request)
675
    content_length = -1
676
    if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
677
        content_length = get_content_length(request)
678
    # Should be BadRequest, but API says otherwise.
679
    if 'Content-Type' not in meta:
680
        raise LengthRequired('Missing Content-Type header')
681
    
682
    if request.serialization != 'text':
683
        data = ''
684
        sock = raw_input_socket(request)
685
        for block in socket_read_iterator(sock, content_length, backend.block_size):
686
            data = '%s%s' % (data, block)
687
        
688
        if request.serialization == 'json':
689
            d = json.loads(data)
690
            if not hasattr(d, '__getitem__'):
691
                raise BadRequest('Invalid data formating')
692
            try:
693
                hashmap = d['hashes']
694
                size = d['bytes']
695
            except KeyError:
696
                raise BadRequest('Invalid data formatting')
697
        elif request.serialization == 'xml':
698
            try:
699
                xml = minidom.parseString(data)
700
                obj = xml.getElementsByTagName('object')[0]
701
                size = obj.attributes['bytes'].value
702
                
703
                hashes = xml.getElementsByTagName('hash')
704
                hashmap = []
705
                for hash in hashes:
706
                    hashmap.append(hash.firstChild.data)
707
            except Exception:
708
                raise BadRequest('Invalid data formatting')
709
        
710
        meta.update({'hash': hashmap_hash(hashmap)}) # Update ETag.
711
    else:
712
        md5 = hashlib.md5()
713
        size = 0
714
        hashmap = []
715
        sock = raw_input_socket(request)
716
        for data in socket_read_iterator(sock, content_length, backend.block_size):
717
            # TODO: Raise 408 (Request Timeout) if this takes too long.
718
            # TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
719
            size += len(data)
720
            hashmap.append(backend.put_block(data))
721
            md5.update(data)
722
        
723
        meta['hash'] = md5.hexdigest().lower()
724
        etag = request.META.get('HTTP_ETAG')
725
        if etag and parse_etags(etag)[0].lower() != meta['hash']:
726
            raise UnprocessableEntity('Object ETag does not match')
727
    
728
    try:
729
        backend.update_object_hashmap(request.user, v_account, v_container, v_object, size, hashmap, meta, True, permissions)
730
    except NotAllowedError:
731
        raise Unauthorized('Access denied')
732
    except IndexError, e:
733
        raise Conflict(json.dumps(e.data))
734
    except NameError:
735
        raise ItemNotFound('Container does not exist')
736
    except ValueError:
737
        raise BadRequest('Invalid sharing header')
738
    except AttributeError, e:
739
        raise Conflict(json.dumps(e.data))
740
    if public is not None:
741
        try:
742
            backend.update_object_public(request.user, v_account, v_container, v_object, public)
743
        except NotAllowedError:
744
            raise Unauthorized('Access denied')
745
        except NameError:
746
            raise ItemNotFound('Object does not exist')
747
    
748
    response = HttpResponse(status=201)
749
    response['ETag'] = meta['hash']
750
    return response
751

    
752
@api_method('POST')
753
def object_write_form(request, v_account, v_container, v_object):
754
    # Normal Response Codes: 201
755
    # Error Response Codes: serviceUnavailable (503),
756
    #                       itemNotFound (404),
757
    #                       unauthorized (401),
758
    #                       badRequest (400)
759
    
760
    if not request.FILES.has_key('X-Object-Data'):
761
        raise BadRequest('Missing X-Object-Data field')
762
    file = request.FILES['X-Object-Data']
763
    
764
    meta = {}
765
    meta['Content-Type'] = file.content_type
766
    
767
    md5 = hashlib.md5()
768
    size = 0
769
    hashmap = []
770
    for data in file.chunks(backend.block_size):
771
        size += len(data)
772
        hashmap.append(backend.put_block(data))
773
        md5.update(data)
774
    
775
    meta['hash'] = md5.hexdigest().lower()
776
    
777
    try:
778
        backend.update_object_hashmap(request.user, v_account, v_container, v_object, size, hashmap, meta, True)
779
    except NotAllowedError:
780
        raise Unauthorized('Access denied')
781
    except NameError:
782
        raise ItemNotFound('Container does not exist')
783
    
784
    response = HttpResponse(status=201)
785
    response['ETag'] = meta['hash']
786
    return response
787

    
788
@api_method('COPY')
789
def object_copy(request, v_account, v_container, v_object):
790
    # Normal Response Codes: 201
791
    # Error Response Codes: serviceUnavailable (503),
792
    #                       itemNotFound (404),
793
    #                       unauthorized (401),
794
    #                       badRequest (400)
795
    
796
    dest_path = request.META.get('HTTP_DESTINATION')
797
    if not dest_path:
798
        raise BadRequest('Missing Destination header')
799
    try:
800
        dest_container, dest_name = split_container_object_string(dest_path)
801
    except ValueError:
802
        raise BadRequest('Invalid Destination header')
803
    copy_or_move_object(request, v_account, v_container, v_object, dest_container, dest_name, move=False)
804
    return HttpResponse(status=201)
805

    
806
@api_method('MOVE')
807
def object_move(request, v_account, v_container, v_object):
808
    # Normal Response Codes: 201
809
    # Error Response Codes: serviceUnavailable (503),
810
    #                       itemNotFound (404),
811
    #                       unauthorized (401),
812
    #                       badRequest (400)
813
    
814
    dest_path = request.META.get('HTTP_DESTINATION')
815
    if not dest_path:
816
        raise BadRequest('Missing Destination header')
817
    try:
818
        dest_container, dest_name = split_container_object_string(dest_path)
819
    except ValueError:
820
        raise BadRequest('Invalid Destination header')
821
    copy_or_move_object(request, v_account, v_container, v_object, dest_container, dest_name, move=True)
822
    return HttpResponse(status=201)
823

    
824
@api_method('POST')
825
def object_update(request, v_account, v_container, v_object):
826
    # Normal Response Codes: 202, 204
827
    # Error Response Codes: serviceUnavailable (503),
828
    #                       conflict (409),
829
    #                       itemNotFound (404),
830
    #                       unauthorized (401),
831
    #                       badRequest (400)
832
    meta, permissions, public = get_object_headers(request)
833
    content_type = meta.get('Content-Type')
834
    if content_type:
835
        del(meta['Content-Type']) # Do not allow changing the Content-Type.
836
    
837
    try:
838
        prev_meta = backend.get_object_meta(request.user, v_account, v_container, v_object)
839
    except NotAllowedError:
840
        raise Unauthorized('Access denied')
841
    except NameError:
842
        raise ItemNotFound('Object does not exist')
843
    # If replacing, keep previous values of 'Content-Type' and 'hash'.
844
    replace = True
845
    if 'update' in request.GET:
846
        replace = False
847
    if replace:
848
        for k in ('Content-Type', 'hash'):
849
            if k in prev_meta:
850
                meta[k] = prev_meta[k]
851
    
852
    # A Content-Type or X-Source-Object header indicates data updates.
853
    src_object = request.META.get('HTTP_X_SOURCE_OBJECT')
854
    if (not content_type or content_type != 'application/octet-stream') and not src_object:
855
        # Do permissions first, as it may fail easier.
856
        if permissions is not None:
857
            try:
858
                backend.update_object_permissions(request.user, v_account, v_container, v_object, permissions)
859
            except NotAllowedError:
860
                raise Unauthorized('Access denied')
861
            except NameError:
862
                raise ItemNotFound('Object does not exist')
863
            except ValueError:
864
                raise BadRequest('Invalid sharing header')
865
            except AttributeError, e:
866
                raise Conflict(json.dumps(e.data))
867
        if public is not None:
868
            try:
869
                backend.update_object_public(request.user, v_account, v_container, v_object, public)
870
            except NotAllowedError:
871
                raise Unauthorized('Access denied')
872
            except NameError:
873
                raise ItemNotFound('Object does not exist')
874
        try:
875
            backend.update_object_meta(request.user, v_account, v_container, v_object, meta, replace)
876
        except NotAllowedError:
877
            raise Unauthorized('Access denied')
878
        except NameError:
879
            raise ItemNotFound('Object does not exist')
880
        return HttpResponse(status=202)
881
    
882
    # Single range update. Range must be in Content-Range.
883
    # Based on: http://code.google.com/p/gears/wiki/ContentRangePostProposal
884
    # (with the addition that '*' is allowed for the range - will append).
885
    content_range = request.META.get('HTTP_CONTENT_RANGE')
886
    if not content_range:
887
        raise BadRequest('Missing Content-Range header')
888
    ranges = get_content_range(request)
889
    if not ranges:
890
        raise RangeNotSatisfiable('Invalid Content-Range header')
891
    
892
    try:
893
        size, hashmap = backend.get_object_hashmap(request.user, v_account, v_container, v_object)
894
    except NotAllowedError:
895
        raise Unauthorized('Access denied')
896
    except NameError:
897
        raise ItemNotFound('Object does not exist')
898
    
899
    offset, length, total = ranges
900
    if offset is None:
901
        offset = size
902
    elif offset > size:
903
        raise RangeNotSatisfiable('Supplied offset is beyond object limits')
904
    if src_object:
905
        src_container, src_name = split_container_object_string(src_object)
906
        src_version = request.META.get('HTTP_X_SOURCE_VERSION')
907
        try:
908
            src_size, src_hashmap = backend.get_object_hashmap(request.user, v_account, src_container, src_name, src_version)
909
        except NotAllowedError:
910
            raise Unauthorized('Access denied')
911
        except NameError:
912
            raise ItemNotFound('Source object does not exist')
913
        
914
        if length is None:
915
            length = src_size
916
        elif length > src_size:
917
            raise BadRequest('Object length is smaller than range length')
918
    else:
919
        # Require either a Content-Length, or 'chunked' Transfer-Encoding.
920
        content_length = -1
921
        if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
922
            content_length = get_content_length(request)
923
        
924
        if length is None:
925
            length = content_length
926
        else:
927
            if content_length == -1:
928
                # TODO: Get up to length bytes in chunks.
929
                length = content_length
930
            elif length != content_length:
931
                raise BadRequest('Content length does not match range length')
932
    if total is not None and (total != size or offset >= size or (length > 0 and offset + length >= size)):
933
        raise RangeNotSatisfiable('Supplied range will change provided object limits')
934
    
935
    dest_bytes = request.META.get('HTTP_X_OBJECT_BYTES')
936
    if dest_bytes is not None:
937
        dest_bytes = get_int_parameter(dest_bytes)
938
        if dest_bytes is None:
939
            raise BadRequest('Invalid X-Object-Bytes header')
940
    
941
    if src_object:
942
        if offset % backend.block_size == 0:
943
            # Update the hashes only.
944
            sbi = 0
945
            while length > 0:
946
                bi = int(offset / backend.block_size)
947
                bl = min(length, backend.block_size)
948
                if bi < len(hashmap):
949
                    if bl == backend.block_size:
950
                        hashmap[bi] = src_hashmap[sbi]
951
                    else:
952
                        data = backend.get_block(src_hashmap[sbi])
953
                        hashmap[bi] = backend.update_block(hashmap[bi], data[:bl], 0)
954
                else:
955
                    hashmap.append(src_hashmap[sbi])
956
                offset += bl
957
                length -= bl
958
                sbi += 1
959
        else:
960
            data = ''
961
            sbi = 0
962
            while length > 0:
963
                data += backend.get_block(src_hashmap[sbi])
964
                if length < backend.block_size:
965
                    data = data[:length]
966
                bytes = put_object_block(hashmap, data, offset)
967
                offset += bytes
968
                data = data[bytes:]
969
                length -= bytes
970
                sbi += 1
971
    else:
972
        sock = raw_input_socket(request)
973
        data = ''
974
        for d in socket_read_iterator(sock, length, backend.block_size):
975
            # TODO: Raise 408 (Request Timeout) if this takes too long.
976
            # TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
977
            data += d
978
            bytes = put_object_block(hashmap, data, offset)
979
            offset += bytes
980
            data = data[bytes:]
981
        if len(data) > 0:
982
            put_object_block(hashmap, data, offset)
983
    
984
    if offset > size:
985
        size = offset
986
    if dest_bytes is not None and dest_bytes < size:
987
        size = dest_bytes
988
        hashmap = hashmap[:(int((size - 1) / backend.block_size) + 1)]
989
    meta.update({'hash': hashmap_hash(hashmap)}) # Update ETag.
990
    try:
991
        backend.update_object_hashmap(request.user, v_account, v_container, v_object, size, hashmap, meta, replace, permissions)
992
    except NotAllowedError:
993
        raise Unauthorized('Access denied')
994
    except NameError:
995
        raise ItemNotFound('Container does not exist')
996
    except ValueError:
997
        raise BadRequest('Invalid sharing header')
998
    except AttributeError, e:
999
        raise Conflict(json.dumps(e.data))
1000
    if public is not None:
1001
        try:
1002
            backend.update_object_public(request.user, v_account, v_container, v_object, public)
1003
        except NotAllowedError:
1004
            raise Unauthorized('Access denied')
1005
        except NameError:
1006
            raise ItemNotFound('Object does not exist')
1007
    
1008
    response = HttpResponse(status=204)
1009
    response['ETag'] = meta['hash']
1010
    return response
1011

    
1012
@api_method('DELETE')
1013
def object_delete(request, v_account, v_container, v_object):
1014
    # Normal Response Codes: 204
1015
    # Error Response Codes: serviceUnavailable (503),
1016
    #                       itemNotFound (404),
1017
    #                       unauthorized (401),
1018
    #                       badRequest (400)
1019
    
1020
    until = get_int_parameter(request.GET.get('until'))
1021
    try:
1022
        backend.delete_object(request.user, v_account, v_container, v_object, until)
1023
    except NotAllowedError:
1024
        raise Unauthorized('Access denied')
1025
    except NameError:
1026
        raise ItemNotFound('Object does not exist')
1027
    return HttpResponse(status=204)
1028

    
1029
@api_method()
1030
def method_not_allowed(request):
1031
    raise BadRequest('Method not allowed')