]>
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
33 print "Status: 403 Forbidden"
34 print "Pragma: no-cache"
35 print "Cache-control: no-cache"
36 print "Connection: close"
42 print "Status: 404 Not found"
43 print "Pragma: no-cache"
44 print "Cache-control: no-cache"
45 print "Connection: close"
51 print "Status: 302 Moved"
53 print "Pragma: no-cache"
54 print "Cache-control: no-cache"
55 print "Connection: close"
60 class NotFoundError(Exception):
64 def __init__(self
, file):
67 self
.db
= sqlite
.connect("hashes.db")
69 c
.execute("CREATE TABLE IF NOT EXISTS hashes(file, sha1)")
72 def getHash(self
, type="sha1"):
75 c
.execute("SELECT %s FROM hashes WHERE file = '%s'" % (type, self
.file,))
77 hash = c
.fetchone()[0]
83 hash = sha
.new(self
.filedata
).hexdigest()
85 c
.execute("INSERT INTO hashes(file, sha1) VALUES('%s', '%s')" % \
92 print "Status: 200 - OK"
93 print "Pragma: no-cache"
94 print "Cache-control: no-cache"
95 print "Connection: close"
96 print "Content-type: " + self
.getMimetype()
97 print "Content-length: %s" % len(self
.filedata
)
98 print "X-SHA1: " + self
.getHash("sha1")
99 print "X-Object: %s" % str(self
.__class
__).split(".")[1]
100 print # An empty line ends the header
104 class FileObject(SourceObject
):
105 def __init__(self
, path
, file):
106 SourceObject
.__init
__(self
, file)
108 self
.filepath
= "/%s/%s/%s" % (os
.getcwd(), path
, file,)
111 f
= open(self
.filepath
, "rb")
115 self
.filedata
= f
.read()
118 def getMimetype(self
):
119 default_mimetype
= "text/plain"
120 from mimetypes
import guess_type
121 return guess_type(self
.filepath
)[0] or default_mimetype
124 class PatchObject(SourceObject
):
125 def __init__(self
, file, url
="/srv/git/patches.git"):
126 SourceObject
.__init
__(self
, file)
129 self
.repo
= Repository(self
.url
)
130 tree
= self
.repo
.head
.tree
133 for directory
in tree
.keys():
134 if isinstance(tree
[directory
], Blob
):
137 blob
= tree
[directory
][self
.file]
147 self
.filedata
= blob
._contents
148 self
.filedata
+= '\n'
150 def getMimetype(self
):
155 os
.environ
["QUERY_STRING"] = \
156 os
.environ
["QUERY_STRING"].replace("+", "%2b")
158 file = cgi
.FieldStorage().getfirst("file")
159 path
= cgi
.FieldStorage().getfirst("path")
161 if not os
.environ
["HTTP_USER_AGENT"].startswith("IPFireSourceGrabber"):
170 # At first, we assume that the requested object is a plain file:
172 object = FileObject(path
=path
, file=file)
173 except NotFoundError
:
174 # Second, we assume that the requestet object is in the patch repo:
176 object = PatchObject(file=file)
177 except NotFoundError
: