Fix some tests.
[pithos] / pithos / api / public.py
1 # Copyright 2011-2012 GRNET S.A. All rights reserved.
2
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10
11 #   2. Redistributions in binary form must reproduce the above
12 #      copyright notice, this list of conditions and the following
13 #      disclaimer in the documentation and/or other materials
14 #      provided with the distribution.
15
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
33
34 import logging
35
36 from django.http import HttpResponse
37 from django.views.decorators.csrf import csrf_exempt
38
39 from pithos.api.faults import (Fault, BadRequest, ItemNotFound)
40 from pithos.api.util import (put_object_headers, update_manifest_meta,
41     validate_modification_preconditions, validate_matching_preconditions,
42     object_data_response, api_method)
43 from pithos.api.short_url import decode_url
44
45
46 logger = logging.getLogger(__name__)
47
48
49 @csrf_exempt
50 def public_demux(request, v_public):
51     if request.method == 'HEAD':
52         return public_meta(request, v_public)
53     elif request.method == 'GET':
54         return public_read(request, v_public)
55     else:
56         return method_not_allowed(request)
57
58 @api_method('HEAD', user_required=False)
59 def public_meta(request, v_public):
60     # Normal Response Codes: 204
61     # Error Response Codes: internalServerError (500),
62     #                       itemNotFound (404),
63     #                       badRequest (400)
64     
65     try:
66         v_account, v_container, v_object = request.backend.get_public(request.user_uniq,
67                                                     decode_url(v_public))
68         meta = request.backend.get_object_meta(request.user_uniq, v_account,
69                                                     v_container, v_object, 'pithos')
70         public = request.backend.get_object_public(request.user_uniq, v_account,
71                                                     v_container, v_object)
72     except:
73         raise ItemNotFound('Object does not exist')
74     
75     if not public:
76         raise ItemNotFound('Object does not exist')
77     update_manifest_meta(request, v_account, meta)
78     
79     response = HttpResponse(status=200)
80     put_object_headers(response, meta, True)
81     return response
82
83 @api_method('GET', user_required=False)
84 def public_read(request, v_public):
85     # Normal Response Codes: 200, 206
86     # Error Response Codes: internalServerError (500),
87     #                       rangeNotSatisfiable (416),
88     #                       preconditionFailed (412),
89     #                       itemNotFound (404),
90     #                       badRequest (400),
91     #                       notModified (304)
92     
93     try:
94         v_account, v_container, v_object = request.backend.get_public(request.user_uniq,
95                                                     decode_url(v_public))
96         meta = request.backend.get_object_meta(request.user_uniq, v_account,
97                                                     v_container, v_object, 'pithos')
98         public = request.backend.get_object_public(request.user_uniq, v_account,
99                                                     v_container, v_object)
100     except:
101         raise ItemNotFound('Object does not exist')
102     
103     if not public:
104         raise ItemNotFound('Object does not exist')
105     update_manifest_meta(request, v_account, meta)
106     
107     # Evaluate conditions.
108     validate_modification_preconditions(request, meta)
109     try:
110         validate_matching_preconditions(request, meta)
111     except NotModified:
112         response = HttpResponse(status=304)
113         response['ETag'] = meta['ETag']
114         return response
115     
116     sizes = []
117     hashmaps = []
118     if 'X-Object-Manifest' in meta:
119         try:
120             src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest'])
121             objects = request.backend.list_objects(request.user_uniq, v_account,
122                                 src_container, prefix=src_name, virtual=False)
123         except:
124             raise ItemNotFound('Object does not exist')
125         
126         try:
127             for x in objects:
128                 s, h = request.backend.get_object_hashmap(request.user_uniq,
129                                         v_account, src_container, x[0], x[1])
130                 sizes.append(s)
131                 hashmaps.append(h)
132         except:
133             raise ItemNotFound('Object does not exist')
134     else:
135         try:
136             s, h = request.backend.get_object_hashmap(request.user_uniq, v_account,
137                                                         v_container, v_object)
138             sizes.append(s)
139             hashmaps.append(h)
140         except:
141             raise ItemNotFound('Object does not exist')
142     
143     if 'Content-Disposition' not in meta:
144         name = v_object.rstrip('/').split('/')[-1]
145         if not name:
146             name = v_public
147         meta['Content-Disposition'] = 'attachment; filename=%s' % (name,)
148     
149     return object_data_response(request, sizes, hashmaps, meta, True)
150
151 @api_method(user_required=False)
152 def method_not_allowed(request, **v_args):
153     raise ItemNotFound('Object does not exist')