1 from functools import wraps
3 from traceback import format_exc
4 from wsgiref.handlers import format_date_time
6 from django.conf import settings
7 from django.http import HttpResponse
8 from django.utils.http import http_date, parse_etags
10 from pithos.api.compat import parse_http_date_safe
11 from pithos.api.faults import (Fault, NotModified, BadRequest, ItemNotFound, LengthRequired,
12 PreconditionFailed, ServiceUnavailable)
13 from pithos.backends import backend
20 logger = logging.getLogger(__name__)
23 def printable_meta_dict(d):
24 """Format a meta dictionary for printing out json/xml.
26 Convert all keys to lower case and replace dashes to underscores.
27 Change 'modified' key from backend to 'last_modified' and format date.
30 d['last_modified'] = datetime.datetime.fromtimestamp(int(d['modified'])).isoformat()
32 return dict([(k.lower().replace('-', '_'), v) for k, v in d.iteritems()])
34 def format_meta_key(k):
35 """Convert underscores to dashes and capitalize intra-dash strings"""
36 return '-'.join([x.capitalize() for x in k.replace('_', '-').split('-')])
38 def get_meta_prefix(request, prefix):
39 """Get all prefix-* request headers in a dict. Reformat keys with format_meta_key()"""
40 prefix = 'HTTP_' + prefix.upper().replace('-', '_')
41 return dict([(format_meta_key(k[5:]), v) for k, v in request.META.iteritems() if k.startswith(prefix)])
43 def get_account_meta(request):
44 """Get metadata from an account request"""
45 meta = get_meta_prefix(request, 'X-Account-Meta-')
48 def put_account_meta(response, meta):
49 """Put metadata in an account response"""
50 response['X-Account-Container-Count'] = meta['count']
51 response['X-Account-Bytes-Used'] = meta['bytes']
52 if 'modified' in meta:
53 response['Last-Modified'] = http_date(int(meta['modified']))
54 for k in [x for x in meta.keys() if x.startswith('X-Account-Meta-')]:
55 response[k.encode('utf-8')] = meta[k].encode('utf-8')
57 def get_container_meta(request):
58 """Get metadata from a container request"""
59 meta = get_meta_prefix(request, 'X-Container-Meta-')
62 def put_container_meta(response, meta):
63 """Put metadata in a container response"""
64 response['X-Container-Object-Count'] = meta['count']
65 response['X-Container-Bytes-Used'] = meta['bytes']
66 if 'modified' in meta:
67 response['Last-Modified'] = http_date(int(meta['modified']))
68 for k in [x for x in meta.keys() if x.startswith('X-Container-Meta-')]:
69 response[k.encode('utf-8')] = meta[k].encode('utf-8')
70 response['X-Container-Object-Meta'] = [x[14:] for x in meta['object_meta'] if x.startswith('X-Object-Meta-')]
72 def get_object_meta(request):
73 """Get metadata from an object request"""
74 meta = get_meta_prefix(request, 'X-Object-Meta-')
75 if request.META.get('CONTENT_TYPE'):
76 meta['Content-Type'] = request.META['CONTENT_TYPE']
77 if request.META.get('HTTP_CONTENT_ENCODING'):
78 meta['Content-Encoding'] = request.META['HTTP_CONTENT_ENCODING']
79 if request.META.get('HTTP_CONTENT_DISPOSITION'):
80 meta['Content-Disposition'] = request.META['HTTP_CONTENT_DISPOSITION']
81 if request.META.get('HTTP_X_OBJECT_MANIFEST'):
82 meta['X-Object-Manifest'] = request.META['HTTP_X_OBJECT_MANIFEST']
85 def put_object_meta(response, meta):
86 """Put metadata in an object response"""
87 response['ETag'] = meta['hash']
88 response['Content-Length'] = meta['bytes']
89 response['Content-Type'] = meta.get('Content-Type', 'application/octet-stream')
90 response['Last-Modified'] = http_date(int(meta['modified']))
91 for k in [x for x in meta.keys() if x.startswith('X-Object-Meta-')]:
92 response[k.encode('utf-8')] = meta[k].encode('utf-8')
93 for k in ('Content-Encoding', 'Content-Disposition', 'X-Object-Manifest'):
97 def validate_modification_preconditions(request, meta):
98 """Check that the modified timestamp conforms with the preconditions set"""
99 if 'modified' not in meta:
100 return # TODO: Always return?
102 if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE')
103 if if_modified_since is not None:
104 if_modified_since = parse_http_date_safe(if_modified_since)
105 if if_modified_since is not None and int(meta['modified']) <= if_modified_since:
106 raise NotModified('Object has not been modified')
108 if_unmodified_since = request.META.get('HTTP_IF_UNMODIFIED_SINCE')
109 if if_unmodified_since is not None:
110 if_unmodified_since = parse_http_date_safe(if_unmodified_since)
111 if if_unmodified_since is not None and int(meta['modified']) > if_unmodified_since:
112 raise PreconditionFailed('Object has been modified')
114 def validate_matching_preconditions(request, meta):
115 """Check that the ETag conforms with the preconditions set"""
116 if 'hash' not in meta:
117 return # TODO: Always return?
119 if_match = request.META.get('HTTP_IF_MATCH')
120 if if_match is not None and if_match != '*':
121 if meta['hash'] not in [x.lower() for x in parse_etags(if_match)]:
122 raise PreconditionFailed('Object Etag does not match')
124 if_none_match = request.META.get('HTTP_IF_NONE_MATCH')
125 if if_none_match is not None:
126 if if_none_match == '*' or meta['hash'] in [x.lower() for x in parse_etags(if_none_match)]:
127 raise NotModified('Object Etag matches')
129 def copy_or_move_object(request, src_path, dest_path, move=False):
130 """Copy or move an object"""
131 if type(src_path) == str:
132 parts = src_path.split('/')
133 if len(parts) < 3 or parts[0] != '':
134 raise BadRequest('Invalid X-Copy-From or X-Move-From header')
135 src_container = parts[1]
136 src_name = '/'.join(parts[2:])
137 elif type(src_path) == tuple and len(src_path) == 2:
138 src_container, src_name = src_path
139 if type(dest_path) == str:
140 parts = dest_path.split('/')
141 if len(parts) < 3 or parts[0] != '':
142 raise BadRequest('Invalid Destination header')
143 dest_container = parts[1]
144 dest_name = '/'.join(parts[2:])
145 elif type(dest_path) == tuple and len(dest_path) == 2:
146 dest_container, dest_name = dest_path
148 meta = get_object_meta(request)
149 # Keep previous values of 'Content-Type' (if a new one is absent) and 'hash'.
151 src_meta = backend.get_object_meta(request.user, src_container, src_name)
153 raise ItemNotFound('Container or object does not exist')
154 if 'Content-Type' in meta and 'Content-Type' in src_meta:
155 del(src_meta['Content-Type'])
156 for k in ('Content-Type', 'hash'):
158 meta[k] = src_meta[k]
162 backend.move_object(request.user, src_container, src_name, dest_container, dest_name, meta, replace_meta=True)
164 backend.copy_object(request.user, src_container, src_name, dest_container, dest_name, meta, replace_meta=True)
166 raise ItemNotFound('Container or object does not exist')
168 def get_content_length(request):
169 content_length = request.META.get('CONTENT_LENGTH')
170 if not content_length:
171 raise LengthRequired('Missing Content-Length header')
173 content_length = int(content_length)
174 if content_length < 0:
177 raise BadRequest('Invalid Content-Length header')
178 return content_length
180 def get_range(request, size):
181 """Parse a Range header from the request
183 Either returns None, when the header is not existent or should be ignored,
184 or a list of (offset, length) tuples - should be further checked.
186 ranges = request.META.get('HTTP_RANGE', '').replace(' ', '')
187 if not ranges.startswith('bytes='):
191 for r in (x.strip() for x in ranges[6:].split(',')):
192 p = re.compile('^(?P<offset>\d*)-(?P<upto>\d*)$')
196 offset = m.group('offset')
197 upto = m.group('upto')
198 if offset == '' and upto == '':
207 ret.append((offset, upto - offset + 1))
209 ret.append((offset, size - offset))
212 ret.append((size - length, length))
216 def get_content_range(request):
217 """Parse a Content-Range header from the request
219 Either returns None, when the header is not existent or should be ignored,
220 or an (offset, length, total) tuple - check as length, total may be None.
221 Returns (None, None, None) if the provided range is '*/*'.
224 ranges = request.META.get('HTTP_CONTENT_RANGE', '')
228 p = re.compile('^bytes (?P<offset>\d+)-(?P<upto>\d*)/(?P<total>(\d+|\*))$')
231 if ranges == 'bytes */*':
232 return (None, None, None)
234 offset = int(m.group('offset'))
235 upto = m.group('upto')
236 total = m.group('total')
245 if (upto and offset > upto) or \
246 (total and offset >= total) or \
247 (total and upto and upto >= total):
253 length = upto - offset + 1
254 return (offset, length, total)
256 def raw_input_socket(request):
257 """Return the socket for reading the rest of the request"""
258 server_software = request.META.get('SERVER_SOFTWARE')
259 if not server_software:
260 if 'wsgi.input' in request.environ:
261 return request.environ['wsgi.input']
262 raise ServiceUnavailable('Unknown server software')
263 if server_software.startswith('WSGIServer'):
264 return request.environ['wsgi.input']
265 elif server_software.startswith('mod_python'):
267 raise ServiceUnavailable('Unknown server software')
269 MAX_UPLOAD_SIZE = 10 * (1024 * 1024) # 10MB
271 def socket_read_iterator(sock, length=0, blocksize=4096):
272 """Return a maximum of blocksize data read from the socket in each iteration
274 Read up to 'length'. If 'length' is negative, will attempt a chunked read.
275 The maximum ammount of data read is controlled by MAX_UPLOAD_SIZE.
277 if length < 0: # Chunked transfers
279 while length < MAX_UPLOAD_SIZE:
281 if hasattr(sock, 'readline'):
282 chunk_length = sock.readline()
285 while chunk_length[-1:] != '\n':
286 chunk_length += sock.read(1)
288 pos = chunk_length.find(';')
290 chunk_length = chunk_length[:pos]
292 chunk_length = int(chunk_length, 16)
294 raise BadRequest('Bad chunk size') # TODO: Change to something more appropriate.
296 if chunk_length == 0:
300 # Get the actual data.
301 while chunk_length > 0:
302 chunk = sock.read(min(chunk_length, blocksize))
303 chunk_length -= len(chunk)
306 if len(data) >= blocksize:
307 ret = data[:blocksize]
308 data = data[blocksize:]
311 # TODO: Raise something to note that maximum size is reached.
313 if length > MAX_UPLOAD_SIZE:
314 # TODO: Raise something to note that maximum size is reached.
317 data = sock.read(min(length, blocksize))
321 class ObjectWrapper(object):
322 """Return the object's data block-per-block in each iteration
324 Read from the object using the offset and length provided in each entry of the range list.
327 def __init__(self, v_account, v_container, v_object, ranges, size, hashmap, boundary):
328 self.v_account = v_account
329 self.v_container = v_container
330 self.v_object = v_object
333 self.hashmap = hashmap
334 self.boundary = boundary
336 self.block_index = -1
339 self.range_index = -1
340 self.offset, self.length = self.ranges[0]
345 def part_iterator(self):
347 # Get the block for the current offset.
348 bi = int(self.offset / backend.block_size)
349 if self.block_index != bi:
351 self.block = backend.get_block(self.hashmap[bi])
353 raise ItemNotFound('Block does not exist')
354 self.block_index = bi
355 # Get the data from the block.
356 bo = self.offset % backend.block_size
357 bl = min(self.length, backend.block_size - bo)
358 data = self.block[bo:bo + bl]
366 if len(self.ranges) == 1:
367 return self.part_iterator()
368 if self.range_index == len(self.ranges):
371 if self.range_index == -1:
373 return self.part_iterator()
374 except StopIteration:
375 self.range_index += 1
377 if self.range_index < len(self.ranges):
379 self.offset, self.length = self.ranges[self.range_index]
380 if self.range_index > 0:
382 out.append('--' + self.boundary)
383 out.append('Content-Range: bytes %d-%d/%d' % (self.offset, self.offset + self.length - 1, self.size))
384 out.append('Content-Transfer-Encoding: binary')
387 return '\r\n'.join(out)
391 out.append('--' + self.boundary + '--')
393 return '\r\n'.join(out)
395 def update_response_headers(request, response):
396 if request.serialization == 'xml':
397 response['Content-Type'] = 'application/xml; charset=UTF-8'
398 elif request.serialization == 'json':
399 response['Content-Type'] = 'application/json; charset=UTF-8'
400 elif not response['Content-Type']:
401 response['Content-Type'] = 'text/plain; charset=UTF-8'
404 response['Date'] = format_date_time(time())
406 def render_fault(request, fault):
407 if settings.DEBUG or settings.TEST:
408 fault.details = format_exc(fault)
410 request.serialization = 'text'
411 data = '\n'.join((fault.message, fault.details)) + '\n'
412 response = HttpResponse(data, status=fault.code)
413 update_response_headers(request, response)
416 def request_serialization(request, format_allowed=False):
417 """Return the serialization format requested
419 Valid formats are 'text' and 'json', 'xml' if 'format_allowed' is True.
421 if not format_allowed:
424 format = request.GET.get('format')
427 elif format == 'xml':
430 for item in request.META.get('HTTP_ACCEPT', '').split(','):
431 accept, sep, rest = item.strip().partition(';')
432 if accept == 'application/json':
434 elif accept == 'application/xml' or accept == 'text/xml':
439 def api_method(http_method=None, format_allowed=False):
440 """Decorator function for views that implement an API method"""
443 def wrapper(request, *args, **kwargs):
445 if http_method and request.method != http_method:
446 raise BadRequest('Method not allowed.')
448 # The args variable may contain up to (account, container, object).
449 if len(args) > 1 and len(args[1]) > 256:
450 raise BadRequest('Container name too large.')
451 if len(args) > 2 and len(args[2]) > 1024:
452 raise BadRequest('Object name too large.')
454 # Fill in custom request variables.
455 request.serialization = request_serialization(request, format_allowed)
456 # TODO: Authenticate.
457 request.user = "test"
459 response = func(request, *args, **kwargs)
460 update_response_headers(request, response)
463 return render_fault(request, fault)
464 except BaseException, e:
465 logger.exception('Unexpected error: %s' % e)
466 fault = ServiceUnavailable('Unexpected error')
467 return render_fault(request, fault)