~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/controllers/__init__.py

[bug=758618] Remove the HeadMiddleware in 'bzr serve --http',
until it can be fixed properly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# along with this program; if not, write to the Free Software
18
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
 
20
 
import re
 
20
import bzrlib.errors
21
21
import time
22
22
 
 
23
from paste.httpexceptions import HTTPNotFound
23
24
from paste.request import path_info_pop, parse_querystring
24
25
 
25
26
from loggerhead import util
34
35
        self.buf = []
35
36
        self.buflen = 0
36
37
        self.writefunc = writefunc
37
 
        self.bytes_saved = 0
38
38
        self.buf_limit = buf_limit
39
39
 
40
40
    def flush(self):
41
 
        chunk = ''.join(self.buf)
42
 
        chunk = re.sub(r'\s*\n\s*', '\n', chunk)
43
 
        chunk = re.sub(r'[ \t]+', ' ', chunk)
44
 
        self.bytes_saved += self.buflen - len(chunk)
45
 
        self.writefunc(chunk)
 
41
        self.writefunc(''.join(self.buf))
46
42
        self.buf = []
47
43
        self.buflen = 0
48
44
 
76
72
        kwargs = dict(parse_querystring(environ))
77
73
        util.set_context(kwargs)
78
74
        args = []
79
 
        while 1:
 
75
        while True:
80
76
            arg = path_info_pop(environ)
81
77
            if arg is None:
82
78
                break
84
80
 
85
81
        path = None
86
82
        if len(args) > 1:
87
 
            path = '/'.join(args[1:])
 
83
            path = unicode('/'.join(args[1:]), 'utf-8')
88
84
        self.args = args
89
85
 
90
86
        vals = {
98
94
 
99
95
        vals.update(self.get_values(path, kwargs, headers))
100
96
 
101
 
        self.log.info('Getting information for %s: %r secs' % (
 
97
        self.log.info('Getting information for %s: %.3f secs' % (
102
98
            self.__class__.__name__, time.time() - z))
103
99
        if 'Content-Type' not in headers:
104
100
            headers['Content-Type'] = 'text/html'
105
101
        writer = start_response("200 OK", headers.items())
 
102
        if environ.get('REQUEST_METHOD') == 'HEAD':
 
103
            # No content for a HEAD request
 
104
            return []
106
105
        template = load_template(self.template_path)
107
106
        z = time.time()
108
107
        w = BufferingWriter(writer, 8192)
109
108
        template.expand_into(w, **vals)
110
109
        w.flush()
111
110
        self.log.info(
112
 
            'Rendering %s: %r secs, %s bytes, %s (%2.1f%%) bytes saved' % (
113
 
                self.__class__.__name__,
114
 
                time.time() - z,
115
 
                w.bytes,
116
 
                w.bytes_saved,
117
 
                100.0*w.bytes_saved/w.bytes))
 
111
            'Rendering %s: %.3f secs, %s bytes' % (
 
112
                self.__class__.__name__, time.time() - z, w.bytes))
118
113
        return []
119
114
 
120
115
    def get_revid(self):
121
116
        h = self._history
122
117
        if h is None:
123
118
            return None
124
 
        if len(self.args) > 0:
125
 
            return h.fix_revid(self.args[0])
 
119
        if len(self.args) > 0 and self.args != ['']:
 
120
            try:
 
121
                revid = h.fix_revid(self.args[0])
 
122
            except bzrlib.errors.NoSuchRevision:
 
123
                raise HTTPNotFound;
126
124
        else:
127
 
            return h.last_revid
 
125
            revid = h.last_revid
 
126
        return revid