summaryrefslogtreecommitdiffstats
path: root/custodia/httpd/consumer.py
blob: 700c4f88e698df6fcb92c4d8fdc0bbbc44fd23cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Copyright (C) 2015  Custodia Project Contributors - see LICENSE file

from custodia.httpd.server import HTTPError


DEFAULT_CTYPE = 'text/html; charset=utf-8'


class HTTPConsumer(object):

    def __init__(self, config=None):
        self.config = config
        self.store_name = None
        if config and 'store' in config:
            self.store_name = config['store']

    def handle(self, request):
        command = request.get('command', 'GET')
        if not hasattr(self, command):
            raise HTTPError(400)

        handler = getattr(self, command)
        response = {'headers': dict()}

        # Handle request
        output = handler(request, response)

        if 'Content-type' not in response['headers']:
            response['headers']['Content-type'] = DEFAULT_CTYPE

        if output is not None:
            response['output'] = output

            if 'Content-Length' not in response['headers']:
                if hasattr(output, 'read'):
                    # LOG: warning file-type objects should set Content-Length
                    pass
                else:
                    response['headers']['Content-Length'] = str(len(output))

        return response