Statistics
| Branch: | Tag: | Revision:

root / pithos / api / functions.py @ 038f1ae9

History | View | Annotate | Download (33.4 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 os
35
import logging
36
import hashlib
37

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

    
44
from pithos.api.faults import (Fault, NotModified, BadRequest, Unauthorized, ItemNotFound, Conflict,
45
    LengthRequired, PreconditionFailed, RangeNotSatisfiable, UnprocessableEntity)
46
from pithos.api.util import (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
        return authenticate(request)
62
    else:
63
        return method_not_allowed(request)
64

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

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

    
89
def object_demux(request, v_account, v_container, v_object):
90
    if request.method == 'HEAD':
91
        return object_meta(request, v_account, v_container, v_object)
92
    elif request.method == 'GET':
93
        return object_read(request, v_account, v_container, v_object)
94
    elif request.method == 'PUT':
95
        return object_write(request, v_account, v_container, v_object)
96
    elif request.method == 'COPY':
97
        return object_copy(request, v_account, v_container, v_object)
98
    elif request.method == 'MOVE':
99
        return object_move(request, v_account, v_container, v_object)
100
    elif request.method == 'POST':
101
        return object_update(request, v_account, v_container, v_object)
102
    elif request.method == 'DELETE':
103
        return object_delete(request, v_account, v_container, v_object)
104
    else:
105
        return method_not_allowed(request)
106

    
107
@api_method('GET')
108
def authenticate(request):
109
    # Normal Response Codes: 204
110
    # Error Response Codes: serviceUnavailable (503),
111
    #                       unauthorized (401),
112
    #                       badRequest (400)
113
    
114
    x_auth_user = request.META.get('HTTP_X_AUTH_USER')
115
    x_auth_key = request.META.get('HTTP_X_AUTH_KEY')
116
    if not x_auth_user or not x_auth_key:
117
        raise BadRequest('Missing X-Auth-User or X-Auth-Key header')
118
    response = HttpResponse(status=204)
119
    inv_auth_tokens = dict((v, k) for k, v in settings.AUTH_TOKENS.items())
120
    response['X-Auth-Token'] = inv_auth_tokens.get(x_auth_user, '0000')
121
    response['X-Storage-Url'] = os.path.join(request.build_absolute_uri(), 'demo')
122
    return response
123

    
124
@api_method('HEAD')
125
def account_meta(request, v_account):
126
    # Normal Response Codes: 204
127
    # Error Response Codes: serviceUnavailable (503),
128
    #                       unauthorized (401),
129
    #                       badRequest (400)
130
    
131
    until = get_int_parameter(request, 'until')
132
    try:
133
        meta = backend.get_account_meta(request.user, v_account, until)
134
        groups = backend.get_account_groups(request.user, v_account)
135
    except NotAllowedError:
136
        raise Unauthorized('Access denied')
137
    
138
    response = HttpResponse(status=204)
139
    put_account_headers(response, meta, groups)
140
    return response
141

    
142
@api_method('POST')
143
def account_update(request, v_account):
144
    # Normal Response Codes: 202
145
    # Error Response Codes: serviceUnavailable (503),
146
    #                       unauthorized (401),
147
    #                       badRequest (400)
148
    
149
    meta, groups = get_account_headers(request)
150
    replace = True
151
    if 'update' in request.GET:
152
        replace = False    
153
    if groups:
154
        try:
155
            backend.update_account_groups(request.user, v_account, groups, replace)
156
        except NotAllowedError:
157
            raise Unauthorized('Access denied')
158
        except ValueError:
159
            raise BadRequest('Invalid groups header')
160
    try:
161
        backend.update_account_meta(request.user, v_account, meta, replace)
162
    except NotAllowedError:
163
        raise Unauthorized('Access denied')
164
    return HttpResponse(status=202)
165

    
166
@api_method('GET', format_allowed=True)
167
def container_list(request, v_account):
168
    # Normal Response Codes: 200, 204
169
    # Error Response Codes: serviceUnavailable (503),
170
    #                       itemNotFound (404),
171
    #                       unauthorized (401),
172
    #                       badRequest (400)
173
    
174
    until = get_int_parameter(request, 'until')
175
    try:
176
        meta = backend.get_account_meta(request.user, v_account, until)
177
        groups = backend.get_account_groups(request.user, v_account)
178
    except NotAllowedError:
179
        raise Unauthorized('Access denied')
180
    
181
    validate_modification_preconditions(request, meta)
182
    
183
    response = HttpResponse()
184
    put_account_headers(response, meta, groups)
185
    
186
    marker = request.GET.get('marker')
187
    limit = request.GET.get('limit')
188
    if limit:
189
        try:
190
            limit = int(limit)
191
            if limit <= 0:
192
                raise ValueError
193
        except ValueError:
194
            limit = 10000
195
    
196
    try:
197
        containers = backend.list_containers(request.user, v_account, marker, limit, until)
198
    except NotAllowedError:
199
        raise Unauthorized('Access denied')
200
    except NameError:
201
        containers = []
202
    
203
    if request.serialization == 'text':
204
        if len(containers) == 0:
205
            # The cloudfiles python bindings expect 200 if json/xml.
206
            response.status_code = 204
207
            return response
208
        response.status_code = 200
209
        response.content = '\n'.join([x[0] for x in containers]) + '\n'
210
        return response
211
    
212
    container_meta = []
213
    for x in containers:
214
        if x[1] is not None:
215
            try:
216
                meta = backend.get_container_meta(request.user, v_account, x[0], until)
217
                policy = backend.get_container_policy(request.user, v_account, x[0])
218
            except NotAllowedError:
219
                raise Unauthorized('Access denied')
220
            except NameError:
221
                pass
222
            else:
223
                for k, v in policy.iteritems():
224
                    meta['X-Container-Policy-' + k] = v
225
                container_meta.append(printable_header_dict(meta))
226
    if request.serialization == 'xml':
227
        data = render_to_string('containers.xml', {'account': v_account, 'containers': container_meta})
228
    elif request.serialization  == 'json':
229
        data = json.dumps(container_meta)
230
    response.status_code = 200
231
    response.content = data
232
    return response
233

    
234
@api_method('HEAD')
235
def container_meta(request, v_account, v_container):
236
    # Normal Response Codes: 204
237
    # Error Response Codes: serviceUnavailable (503),
238
    #                       itemNotFound (404),
239
    #                       unauthorized (401),
240
    #                       badRequest (400)
241
    
242
    until = get_int_parameter(request, 'until')
243
    try:
244
        meta = backend.get_container_meta(request.user, v_account, v_container, until)
245
        meta['object_meta'] = backend.list_object_meta(request.user, v_account, v_container, until)
246
        policy = backend.get_container_policy(request.user, v_account, v_container)
247
    except NotAllowedError:
248
        raise Unauthorized('Access denied')
249
    except NameError:
250
        raise ItemNotFound('Container does not exist')
251
    
252
    response = HttpResponse(status=204)
253
    put_container_headers(response, meta, policy)
254
    return response
255

    
256
@api_method('PUT')
257
def container_create(request, v_account, v_container):
258
    # Normal Response Codes: 201, 202
259
    # Error Response Codes: serviceUnavailable (503),
260
    #                       itemNotFound (404),
261
    #                       unauthorized (401),
262
    #                       badRequest (400)
263
    
264
    meta, policy = get_container_headers(request)
265
    
266
    try:
267
        backend.put_container(request.user, v_account, v_container, policy)
268
        ret = 201
269
    except NotAllowedError:
270
        raise Unauthorized('Access denied')
271
    except NameError:
272
        ret = 202
273
    
274
    if len(meta) > 0:
275
        try:
276
            backend.update_container_meta(request.user, v_account, v_container, meta, replace=True)
277
        except NotAllowedError:
278
            raise Unauthorized('Access denied')
279
        except NameError:
280
            raise ItemNotFound('Container does not exist')
281
    
282
    return HttpResponse(status=ret)
283

    
284
@api_method('POST')
285
def container_update(request, v_account, v_container):
286
    # Normal Response Codes: 202
287
    # Error Response Codes: serviceUnavailable (503),
288
    #                       itemNotFound (404),
289
    #                       unauthorized (401),
290
    #                       badRequest (400)
291
    
292
    meta, policy = get_container_headers(request)
293
    replace = True
294
    if 'update' in request.GET:
295
        replace = False
296
    if policy:
297
        try:
298
            backend.update_container_policy(request.user, v_account, v_container, policy, replace)
299
        except NotAllowedError:
300
            raise Unauthorized('Access denied')
301
        except NameError:
302
            raise ItemNotFound('Container does not exist')
303
        except ValueError:
304
            raise BadRequest('Invalid policy header')
305
    try:
306
        backend.update_container_meta(request.user, v_account, v_container, meta, replace)
307
    except NotAllowedError:
308
        raise Unauthorized('Access denied')
309
    except NameError:
310
        raise ItemNotFound('Container does not exist')
311
    return HttpResponse(status=202)
312

    
313
@api_method('DELETE')
314
def container_delete(request, v_account, v_container):
315
    # Normal Response Codes: 204
316
    # Error Response Codes: serviceUnavailable (503),
317
    #                       conflict (409),
318
    #                       itemNotFound (404),
319
    #                       unauthorized (401),
320
    #                       badRequest (400)
321
    
322
    try:
323
        backend.delete_container(request.user, v_account, v_container)
324
    except NotAllowedError:
325
        raise Unauthorized('Access denied')
326
    except NameError:
327
        raise ItemNotFound('Container does not exist')
328
    except IndexError:
329
        raise Conflict('Container is not empty')
330
    return HttpResponse(status=204)
331

    
332
@api_method('GET', format_allowed=True)
333
def object_list(request, v_account, v_container):
334
    # Normal Response Codes: 200, 204
335
    # Error Response Codes: serviceUnavailable (503),
336
    #                       itemNotFound (404),
337
    #                       unauthorized (401),
338
    #                       badRequest (400)
339
    
340
    until = get_int_parameter(request, 'until')
341
    try:
342
        meta = backend.get_container_meta(request.user, v_account, v_container, until)
343
        meta['object_meta'] = backend.list_object_meta(request.user, v_account, v_container, until)
344
        policy = backend.get_container_policy(request.user, v_account, v_container)
345
    except NotAllowedError:
346
        raise Unauthorized('Access denied')
347
    except NameError:
348
        raise ItemNotFound('Container does not exist')
349
    
350
    validate_modification_preconditions(request, meta)
351
    
352
    response = HttpResponse()
353
    put_container_headers(response, meta, policy)
354
    
355
    path = request.GET.get('path')
356
    prefix = request.GET.get('prefix')
357
    delimiter = request.GET.get('delimiter')
358
    
359
    # Path overrides prefix and delimiter.
360
    virtual = True
361
    if path:
362
        prefix = path
363
        delimiter = '/'
364
        virtual = False
365
    
366
    # Naming policy.
367
    if prefix and delimiter:
368
        prefix = prefix + delimiter
369
    if not prefix:
370
        prefix = ''
371
    prefix = prefix.lstrip('/')
372
    
373
    marker = request.GET.get('marker')
374
    limit = request.GET.get('limit')
375
    if limit:
376
        try:
377
            limit = int(limit)
378
            if limit <= 0:
379
                raise ValueError
380
        except ValueError:
381
            limit = 10000
382
    
383
    keys = request.GET.get('meta')
384
    if keys:
385
        keys = keys.split(',')
386
        keys = [format_header_key('X-Object-Meta-' + x.strip()) for x in keys if x.strip() != '']
387
    else:
388
        keys = []
389
    
390
    try:
391
        objects = backend.list_objects(request.user, v_account, v_container, prefix, delimiter, marker, limit, virtual, keys, until)
392
    except NotAllowedError:
393
        raise Unauthorized('Access denied')
394
    except NameError:
395
        raise ItemNotFound('Container does not exist')
396
    
397
    if request.serialization == 'text':
398
        if len(objects) == 0:
399
            # The cloudfiles python bindings expect 200 if json/xml.
400
            response.status_code = 204
401
            return response
402
        response.status_code = 200
403
        response.content = '\n'.join([x[0] for x in objects]) + '\n'
404
        return response
405
    
406
    object_meta = []
407
    for x in objects:
408
        if x[1] is None:
409
            # Virtual objects/directories.
410
            object_meta.append({'subdir': x[0]})
411
        else:
412
            try:
413
                meta = backend.get_object_meta(request.user, v_account, v_container, x[0], x[1])
414
                if until is None:
415
                    permissions = backend.get_object_permissions(request.user, v_account, v_container, x[0])
416
                    public = backend.get_object_public(request.user, v_account, v_container, x[0])
417
                else:
418
                    permissions = None
419
                    public = None
420
            except NotAllowedError:
421
                raise Unauthorized('Access denied')
422
            except NameError:
423
                pass
424
            else:
425
                update_sharing_meta(permissions, v_account, v_container, x[0], meta)
426
                update_public_meta(public, meta)
427
                object_meta.append(printable_header_dict(meta))
428
    if request.serialization == 'xml':
429
        data = render_to_string('objects.xml', {'container': v_container, 'objects': object_meta})
430
    elif request.serialization  == 'json':
431
        data = json.dumps(object_meta)
432
    response.status_code = 200
433
    response.content = data
434
    return response
435

    
436
@api_method('HEAD')
437
def object_meta(request, v_account, v_container, v_object):
438
    # Normal Response Codes: 204
439
    # Error Response Codes: serviceUnavailable (503),
440
    #                       itemNotFound (404),
441
    #                       unauthorized (401),
442
    #                       badRequest (400)
443
    
444
    version = request.GET.get('version')
445
    try:
446
        meta = backend.get_object_meta(request.user, v_account, v_container, v_object, version)
447
        if version is None:
448
            permissions = backend.get_object_permissions(request.user, v_account, v_container, v_object)
449
            public = backend.get_object_public(request.user, v_account, v_container, v_object)
450
        else:
451
            permissions = None
452
            public = None
453
    except NotAllowedError:
454
        raise Unauthorized('Access denied')
455
    except NameError:
456
        raise ItemNotFound('Object does not exist')
457
    except IndexError:
458
        raise ItemNotFound('Version does not exist')
459
    
460
    update_manifest_meta(request, v_account, meta)
461
    update_sharing_meta(permissions, v_account, v_container, v_object, meta)
462
    update_public_meta(public, meta)
463
    
464
    response = HttpResponse(status=200)
465
    put_object_headers(response, meta)
466
    return response
467

    
468
@api_method('GET', format_allowed=True)
469
def object_read(request, v_account, v_container, v_object):
470
    # Normal Response Codes: 200, 206
471
    # Error Response Codes: serviceUnavailable (503),
472
    #                       rangeNotSatisfiable (416),
473
    #                       preconditionFailed (412),
474
    #                       itemNotFound (404),
475
    #                       unauthorized (401),
476
    #                       badRequest (400),
477
    #                       notModified (304)
478
    
479
    version = request.GET.get('version')
480
    
481
    # Reply with the version list. Do this first, as the object may be deleted.
482
    if version == 'list':
483
        if request.serialization == 'text':
484
            raise BadRequest('No format specified for version list.')
485
        
486
        try:
487
            v = backend.list_versions(request.user, v_account, v_container, v_object)
488
        except NotAllowedError:
489
            raise Unauthorized('Access denied')
490
        d = {'versions': v}
491
        if request.serialization == 'xml':
492
            d['object'] = v_object
493
            data = render_to_string('versions.xml', d)
494
        elif request.serialization  == 'json':
495
            data = json.dumps(d)
496
        
497
        response = HttpResponse(data, status=200)
498
        response['Content-Length'] = len(data)
499
        return response
500
    
501
    try:
502
        meta = backend.get_object_meta(request.user, v_account, v_container, v_object, version)
503
        if version is None:
504
            permissions = backend.get_object_permissions(request.user, v_account, v_container, v_object)
505
            public = backend.get_object_public(request.user, v_account, v_container, v_object)
506
        else:
507
            permissions = None
508
            public = None
509
    except NotAllowedError:
510
        raise Unauthorized('Access denied')
511
    except NameError:
512
        raise ItemNotFound('Object does not exist')
513
    except IndexError:
514
        raise ItemNotFound('Version does not exist')
515
    
516
    update_manifest_meta(request, v_account, meta)
517
    update_sharing_meta(permissions, v_account, v_container, v_object, meta)
518
    update_public_meta(public, meta)
519
    
520
    # Evaluate conditions.
521
    validate_modification_preconditions(request, meta)
522
    try:
523
        validate_matching_preconditions(request, meta)
524
    except NotModified:
525
        response = HttpResponse(status=304)
526
        response['ETag'] = meta['hash']
527
        return response
528
    
529
    sizes = []
530
    hashmaps = []
531
    if 'X-Object-Manifest' in meta:
532
        try:
533
            src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest'])
534
            objects = backend.list_objects(request.user, v_account, src_container, prefix=src_name, virtual=False)
535
        except NotAllowedError:
536
            raise Unauthorized('Access denied')
537
        except ValueError:
538
            raise BadRequest('Invalid X-Object-Manifest header')
539
        except NameError:
540
            raise ItemNotFound('Container does not exist')
541
        
542
        try:
543
            for x in objects:
544
                s, h = backend.get_object_hashmap(request.user, v_account, src_container, x[0], x[1])
545
                sizes.append(s)
546
                hashmaps.append(h)
547
        except NotAllowedError:
548
            raise Unauthorized('Access denied')
549
        except NameError:
550
            raise ItemNotFound('Object does not exist')
551
        except IndexError:
552
            raise ItemNotFound('Version does not exist')
553
    else:
554
        try:
555
            s, h = backend.get_object_hashmap(request.user, v_account, v_container, v_object, version)
556
            sizes.append(s)
557
            hashmaps.append(h)
558
        except NotAllowedError:
559
            raise Unauthorized('Access denied')
560
        except NameError:
561
            raise ItemNotFound('Object does not exist')
562
        except IndexError:
563
            raise ItemNotFound('Version does not exist')
564
    
565
    # Reply with the hashmap.
566
    if request.serialization != 'text':
567
        size = sum(sizes)
568
        hashmap = sum(hashmaps, [])
569
        d = {'block_size': backend.block_size, 'block_hash': backend.hash_algorithm, 'bytes': size, 'hashes': hashmap}
570
        if request.serialization == 'xml':
571
            d['object'] = v_object
572
            data = render_to_string('hashes.xml', d)
573
        elif request.serialization  == 'json':
574
            data = json.dumps(d)
575
        
576
        response = HttpResponse(data, status=200)
577
        put_object_headers(response, meta)
578
        response['Content-Length'] = len(data)
579
        return response
580
    
581
    return object_data_response(request, sizes, hashmaps, meta)
582

    
583
@api_method('PUT', format_allowed=True)
584
def object_write(request, v_account, v_container, v_object):
585
    # Normal Response Codes: 201
586
    # Error Response Codes: serviceUnavailable (503),
587
    #                       unprocessableEntity (422),
588
    #                       lengthRequired (411),
589
    #                       conflict (409),
590
    #                       itemNotFound (404),
591
    #                       unauthorized (401),
592
    #                       badRequest (400)
593
    copy_from = request.META.get('HTTP_X_COPY_FROM')
594
    move_from = request.META.get('HTTP_X_MOVE_FROM')
595
    if copy_from or move_from:
596
        # TODO: Why is this required? Copy this ammount?
597
        content_length = get_content_length(request)
598
        
599
        if move_from:
600
            try:
601
                src_container, src_name = split_container_object_string(move_from)
602
            except ValueError:
603
                raise BadRequest('Invalid X-Move-From header')
604
            copy_or_move_object(request, v_account, src_container, src_name, v_container, v_object, move=True)
605
        else:
606
            try:
607
                src_container, src_name = split_container_object_string(copy_from)
608
            except ValueError:
609
                raise BadRequest('Invalid X-Copy-From header')
610
            copy_or_move_object(request, v_account, src_container, src_name, v_container, v_object, move=False)
611
        return HttpResponse(status=201)
612
    
613
    meta, permissions, public = get_object_headers(request)
614
    content_length = -1
615
    if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
616
        content_length = get_content_length(request)
617
    # Should be BadRequest, but API says otherwise.
618
    if 'Content-Type' not in meta:
619
        raise LengthRequired('Missing Content-Type header')
620
    
621
    if request.serialization == 'json':
622
        data = ''
623
        sock = raw_input_socket(request)
624
        for block in socket_read_iterator(sock, content_length, backend.block_size):
625
            data = '%s%s' % (data, block)
626
        d = json.loads(data)
627
        if not hasattr(d, '__getitem__'):
628
            raise BadRequest('Invalid data formating')
629
        try:
630
            hashmap = d['hashes']
631
            size = d['bytes']
632
        except KeyError:
633
            raise BadRequest('Invalid data formatting')
634
        meta.update({'hash': hashmap_hash(hashmap)}) # Update ETag.
635
    elif request.serialization == 'xml':
636
        #TODO support for xml
637
        raise BadRequest('Format xml is not supported')
638
    else:
639
        md5 = hashlib.md5()
640
        size = 0
641
        hashmap = []
642
        sock = raw_input_socket(request)
643
        for data in socket_read_iterator(sock, content_length, backend.block_size):
644
            # TODO: Raise 408 (Request Timeout) if this takes too long.
645
            # TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
646
            size += len(data)
647
            hashmap.append(backend.put_block(data))
648
            md5.update(data)
649
        
650
        meta['hash'] = md5.hexdigest().lower()
651
        etag = request.META.get('HTTP_ETAG')
652
        if etag and parse_etags(etag)[0].lower() != meta['hash']:
653
            raise UnprocessableEntity('Object ETag does not match')
654
    
655
    try:
656
        backend.update_object_hashmap(request.user, v_account, v_container, v_object, size, hashmap, meta, True, permissions)
657
    except NotAllowedError:
658
        raise Unauthorized('Access denied')
659
    except IndexError, e:
660
        raise Conflict(json.dumps(e.data))
661
    except NameError:
662
        raise ItemNotFound('Container does not exist')
663
    except ValueError:
664
        raise BadRequest('Invalid sharing header')
665
    except AttributeError, e:
666
        raise Conflict(json.dumps(e.data))
667
    if public is not None:
668
        try:
669
            backend.update_object_public(request.user, v_account, v_container, v_object, public)
670
        except NotAllowedError:
671
            raise Unauthorized('Access denied')
672
        except NameError:
673
            raise ItemNotFound('Object does not exist')
674
    
675
    response = HttpResponse(status=201)
676
    response['ETag'] = meta['hash']
677
    return response
678

    
679
@api_method('COPY')
680
def object_copy(request, v_account, v_container, v_object):
681
    # Normal Response Codes: 201
682
    # Error Response Codes: serviceUnavailable (503),
683
    #                       itemNotFound (404),
684
    #                       unauthorized (401),
685
    #                       badRequest (400)
686
    
687
    dest_path = request.META.get('HTTP_DESTINATION')
688
    if not dest_path:
689
        raise BadRequest('Missing Destination header')
690
    try:
691
        dest_container, dest_name = split_container_object_string(dest_path)
692
    except ValueError:
693
        raise BadRequest('Invalid Destination header')
694
    copy_or_move_object(request, v_account, v_container, v_object, dest_container, dest_name, move=False)
695
    return HttpResponse(status=201)
696

    
697
@api_method('MOVE')
698
def object_move(request, v_account, v_container, v_object):
699
    # Normal Response Codes: 201
700
    # Error Response Codes: serviceUnavailable (503),
701
    #                       itemNotFound (404),
702
    #                       unauthorized (401),
703
    #                       badRequest (400)
704
    
705
    dest_path = request.META.get('HTTP_DESTINATION')
706
    if not dest_path:
707
        raise BadRequest('Missing Destination header')
708
    try:
709
        dest_container, dest_name = split_container_object_string(dest_path)
710
    except ValueError:
711
        raise BadRequest('Invalid Destination header')
712
    copy_or_move_object(request, v_account, v_container, v_object, dest_container, dest_name, move=True)
713
    return HttpResponse(status=201)
714

    
715
@api_method('POST')
716
def object_update(request, v_account, v_container, v_object):
717
    # Normal Response Codes: 202, 204
718
    # Error Response Codes: serviceUnavailable (503),
719
    #                       conflict (409),
720
    #                       itemNotFound (404),
721
    #                       unauthorized (401),
722
    #                       badRequest (400)
723
    
724
    meta, permissions, public = get_object_headers(request)
725
    content_type = meta.get('Content-Type')
726
    if content_type:
727
        del(meta['Content-Type']) # Do not allow changing the Content-Type.
728
    
729
    try:
730
        prev_meta = backend.get_object_meta(request.user, v_account, v_container, v_object)
731
    except NotAllowedError:
732
        raise Unauthorized('Access denied')
733
    except NameError:
734
        raise ItemNotFound('Object does not exist')
735
    # If replacing, keep previous values of 'Content-Type' and 'hash'.
736
    replace = True
737
    if 'update' in request.GET:
738
        replace = False
739
    if replace:
740
        for k in ('Content-Type', 'hash'):
741
            if k in prev_meta:
742
                meta[k] = prev_meta[k]
743
    
744
    # A Content-Type header indicates data updates.
745
    if not content_type or content_type != 'application/octet-stream':
746
        # Do permissions first, as it may fail easier.
747
        if permissions is not None:
748
            try:
749
                backend.update_object_permissions(request.user, v_account, v_container, v_object, permissions)
750
            except NotAllowedError:
751
                raise Unauthorized('Access denied')
752
            except NameError:
753
                raise ItemNotFound('Object does not exist')
754
            except ValueError:
755
                raise BadRequest('Invalid sharing header')
756
            except AttributeError, e:
757
                raise Conflict(json.dumps(e.data))
758
        if public is not None:
759
            try:
760
                backend.update_object_public(request.user, v_account, v_container, v_object, public)
761
            except NotAllowedError:
762
                raise Unauthorized('Access denied')
763
            except NameError:
764
                raise ItemNotFound('Object does not exist')
765
        try:
766
            backend.update_object_meta(request.user, v_account, v_container, v_object, meta, replace)
767
        except NotAllowedError:
768
            raise Unauthorized('Access denied')
769
        except NameError:
770
            raise ItemNotFound('Object does not exist')
771
        return HttpResponse(status=202)
772
    
773
    # Single range update. Range must be in Content-Range.
774
    # Based on: http://code.google.com/p/gears/wiki/ContentRangePostProposal
775
    # (with the addition that '*' is allowed for the range - will append).
776
    content_range = request.META.get('HTTP_CONTENT_RANGE')
777
    if not content_range:
778
        raise BadRequest('Missing Content-Range header')
779
    ranges = get_content_range(request)
780
    if not ranges:
781
        raise RangeNotSatisfiable('Invalid Content-Range header')
782
    # Require either a Content-Length, or 'chunked' Transfer-Encoding.
783
    content_length = -1
784
    if request.META.get('HTTP_TRANSFER_ENCODING') != 'chunked':
785
        content_length = get_content_length(request)
786
    
787
    try:
788
        size, hashmap = backend.get_object_hashmap(request.user, v_account, v_container, v_object)
789
    except NotAllowedError:
790
        raise Unauthorized('Access denied')
791
    except NameError:
792
        raise ItemNotFound('Object does not exist')
793
    
794
    offset, length, total = ranges
795
    if offset is None:
796
        offset = size
797
    elif offset > size:
798
        raise RangeNotSatisfiable('Supplied offset is beyond object limits')
799
    if length is None or content_length == -1:
800
        length = content_length # Nevermind the error.
801
    elif length != content_length:
802
        raise BadRequest('Content length does not match range length')
803
    if total is not None and (total != size or offset >= size or (length > 0 and offset + length >= size)):
804
        raise RangeNotSatisfiable('Supplied range will change provided object limits')
805
    
806
    sock = raw_input_socket(request)
807
    data = ''
808
    for d in socket_read_iterator(sock, length, backend.block_size):
809
        # TODO: Raise 408 (Request Timeout) if this takes too long.
810
        # TODO: Raise 499 (Client Disconnect) if a length is defined and we stop before getting this much data.
811
        data += d
812
        bytes = put_object_block(hashmap, data, offset)
813
        offset += bytes
814
        data = data[bytes:]
815
    if len(data) > 0:
816
        put_object_block(hashmap, data, offset)
817
    
818
    if offset > size:
819
        size = offset
820
    meta.update({'hash': hashmap_hash(hashmap)}) # Update ETag.
821
    try:
822
        backend.update_object_hashmap(request.user, v_account, v_container, v_object, size, hashmap, meta, replace, permissions)
823
    except NotAllowedError:
824
        raise Unauthorized('Access denied')
825
    except NameError:
826
        raise ItemNotFound('Container does not exist')
827
    except ValueError:
828
        raise BadRequest('Invalid sharing header')
829
    except AttributeError, e:
830
        raise Conflict(json.dumps(e.data))
831
    if public is not None:
832
        try:
833
            backend.update_object_public(request.user, v_account, v_container, v_object, public)
834
        except NotAllowedError:
835
            raise Unauthorized('Access denied')
836
        except NameError:
837
            raise ItemNotFound('Object does not exist')
838
    
839
    response = HttpResponse(status=204)
840
    response['ETag'] = meta['hash']
841
    return response
842

    
843
@api_method('DELETE')
844
def object_delete(request, v_account, v_container, v_object):
845
    # Normal Response Codes: 204
846
    # Error Response Codes: serviceUnavailable (503),
847
    #                       itemNotFound (404),
848
    #                       unauthorized (401),
849
    #                       badRequest (400)
850
    
851
    try:
852
        backend.delete_object(request.user, v_account, v_container, v_object)
853
    except NotAllowedError:
854
        raise Unauthorized('Access denied')
855
    except NameError:
856
        raise ItemNotFound('Object does not exist')
857
    return HttpResponse(status=204)
858

    
859
@api_method()
860
def method_not_allowed(request):
861
    raise BadRequest('Method not allowed')