]>
git.ipfire.org Git - ipfire.org.git/blob - source/dlhandler.py
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
26 from pysqlite2
import dbapi2
as sqlite
32 SOURCE_BASE
= "/srv/sources"
35 print "Status: 403 Forbidden"
36 print "Pragma: no-cache"
37 print "Cache-control: no-cache"
38 print "Connection: close"
44 print "Status: 404 Not found"
45 print "Pragma: no-cache"
46 print "Cache-control: no-cache"
47 print "Connection: close"
53 print "Status: 302 Moved"
55 print "Pragma: no-cache"
56 print "Cache-control: no-cache"
57 print "Connection: close"
62 class NotFoundError(Exception):
66 def __init__(self
, file):
69 self
.db
= sqlite
.connect("hashes.db")
71 c
.execute("CREATE TABLE IF NOT EXISTS hashes(file, sha1)")
74 def getHash(self
, type="sha1"):
77 c
.execute("SELECT %s FROM hashes WHERE file = '%s'" % (type, self
.file,))
79 hash = c
.fetchone()[0]
85 hash = sha
.new(self
.filedata
).hexdigest()
87 c
.execute("INSERT INTO hashes(file, sha1) VALUES('%s', '%s')" % \
94 print "Status: 200 - OK"
95 print "Pragma: no-cache"
96 print "Cache-control: no-cache"
97 print "Connection: close"
98 print "Content-type: " + self
.getMimetype()
99 print "Content-length: %s" % len(self
.filedata
)
100 print "X-SHA1: " + self
.getHash("sha1")
101 print "X-Object: %s" % str(self
.__class
__).split(".")[1]
102 print # An empty line ends the header
106 class FileObject(SourceObject
):
107 def __init__(self
, path
, file):
108 SourceObject
.__init
__(self
, file)
110 self
.filepath
= "/%s/%s/%s" % (SOURCE_BASE
, path
, file,)
113 f
= open(self
.filepath
, "rb")
117 self
.filedata
= f
.read()
120 def getMimetype(self
):
121 default_mimetype
= "text/plain"
122 from mimetypes
import guess_type
123 return guess_type(self
.filepath
)[0] or default_mimetype
126 class PatchObject(SourceObject
):
127 def __init__(self
, file, url
="/srv/git/patches.git"):
128 SourceObject
.__init
__(self
, file)
131 self
.repo
= Repository(self
.url
)
132 tree
= self
.repo
.head
.tree
135 for directory
in tree
.keys():
136 if isinstance(tree
[directory
], Blob
):
139 blob
= tree
[directory
][self
.file]
149 self
.filedata
= blob
._contents
150 self
.filedata
+= '\n'
152 def getMimetype(self
):
157 os
.environ
["QUERY_STRING"] = \
158 os
.environ
["QUERY_STRING"].replace("+", "%2b")
160 file = cgi
.FieldStorage().getfirst("file")
161 path
= cgi
.FieldStorage().getfirst("path")
166 if not path
or path
== "download":
169 # At first, we assume that the requested object is a plain file:
171 object = FileObject(path
=path
, file=file)
172 except NotFoundError
:
173 # Second, we assume that the requestet object is in the patch repo:
175 object = PatchObject(file=file)
176 except NotFoundError
: