~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/exporter.py

Improve escaping of filenames in revision views. Fixes a couple of XSS holes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2011 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
"""Exports an archive from a bazaar branch"""
14
 
 
15
 
from bzrlib.export import get_export_generator
16
 
 
17
 
 
18
 
class ExporterFileObject(object):
19
 
    """Shim that accumulates temporarily written out data.
20
 
 
21
 
    There are python tarfile classes that want to write to a file like object.
22
 
    We want to stream data.  But wsgi assumes it can pull data from the
23
 
    handler, rather than having bytes pushed.
24
 
 
25
 
    So this class holds the data temporarily, until it is pulled.  It 
26
 
    should never buffer everything because as soon as a chunk is produced, 
27
 
    wsgi will be given the chance to take it.
28
 
    """
29
 
 
30
 
    def __init__(self):
31
 
        self._buffer = []
32
 
 
33
 
    def write(self, s):
34
 
        self._buffer.append(s)
35
 
 
36
 
    def get_buffer(self):
37
 
        try:
38
 
            return ''.join(self._buffer)
39
 
        finally:
40
 
            self._buffer = []
41
 
 
42
 
    def close(self):
43
 
        pass
44
 
 
45
 
 
46
 
def export_archive(history, root, revid, archive_format):
47
 
    """Export tree contents to an archive
48
 
 
49
 
    :param history: Instance of history to export
50
 
    :param root: Root location inside the archive.
51
 
    :param revid: Revision to export
52
 
    :param archive_format: Format of the archive, eg 'tar.gz'.
53
 
    """
54
 
    fileobj = ExporterFileObject()
55
 
    tree = history._branch.repository.revision_tree(revid)
56
 
    for _ in get_export_generator(tree=tree, root=root, fileobj=fileobj,
57
 
        format=archive_format):
58
 
        yield fileobj.get_buffer()
59
 
    # Might have additonal contents written
60
 
    yield fileobj.get_buffer()