]> git.ipfire.org Git - ipfire.org.git/blob - source/dlhandler.py
614a64c0c4c27feee842f1e790a20d7d18bd2bde
[ipfire.org.git] / source / dlhandler.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 import os
23 import cgi
24 from git import *
25
26 def give_403():
27 print "Status: 403 Forbidden"
28 print "Pragma: no-cache"
29 print "Cache-control: no-cache"
30 print "Connection: close"
31 print
32 print
33 os._exit(0)
34
35 def give_404():
36 print "Status: 404 Not found"
37 print "Pragma: no-cache"
38 print "Cache-control: no-cache"
39 print "Connection: close"
40 print
41 print
42 os._exit(0)
43
44 def give_302():
45 print "Status: 302 Moved"
46 print "Location: /"
47 print "Pragma: no-cache"
48 print "Cache-control: no-cache"
49 print "Connection: close"
50 print
51 print
52 os._exit(0)
53
54 class NotFoundError(Exception):
55 pass
56
57 class SourceObject:
58 def __init__(self, path):
59 self.path = path
60
61 self.object_type = None
62
63 # Init hashes
64 self.hash_md5 = None
65 self.hash_sha1 = None
66
67 def hash(self):
68 ## This is for python 2.4.
69 #import md5
70 #self.hash_md5 = md5.new(self.data()).hexdigest()
71
72 import sha
73 self.hash_sha1 = sha.new(self.data()).hexdigest()
74
75 def data(self):
76 return self.filedata
77
78 def run(self):
79 self.hash()
80 self.showhttpheaders()
81 print self.data()
82
83 def showhttpheaders(self):
84 print "Pragma: no-cache"
85 print "Cache-control: no-cache"
86 print "Connection: close"
87 print "Content-type:" + self.mimetype()
88 print "Content-length: %s" % len(self.data())
89 if self.object_type:
90 print "X-Object:" + self.object_type
91 if self.hash_md5:
92 print "X-MD5:" + self.hash_md5
93 if self.hash_sha1:
94 print "X-SHA1:" + self.hash_sha1
95 print
96 # An empty line ends the header
97
98 class FileObject(SourceObject):
99 def __init__(self, path, version, url="/srv/www/ipfire.org/source/"):
100 SourceObject.__init__(self, path)
101 self.url= os.path.join(url, "source-%s" % version)
102 self.filepath = os.path.join(self.url, path)
103 self.init_file()
104
105 self.object_type = "FileObject"
106
107 def init_file(self):
108 try:
109 f = open(self.filepath, "rb")
110 except:
111 raise NotFoundError
112
113 self.filedata = f.read()
114 f.close()
115
116 def mimetype(self):
117 default_mimetype = "text/plain"
118 from mimetypes import guess_type
119 return guess_type(self.filepath)[0] or default_mimetype
120
121 class PatchObject(SourceObject):
122 def __init__(self, path, url="/srv/git/patches.git"):
123 SourceObject.__init__(self, path)
124
125 self.object_type = "PatchObject"
126
127 self.url = url
128 self.init_repo()
129
130 def init_repo(self):
131 # init the repo
132 self.repo = Repository(self.url)
133
134 # head, tree & blob
135 #self.head = self.repo.head()
136 self.tree = self.repo.head.tree
137
138 self.blob = self.set_blob()
139
140 def set_blob(self):
141 blob = None
142
143 for directory in self.tree.keys():
144 if isinstance(self.tree[directory], Blob):
145 continue
146 try:
147 blob = self.tree[directory][path]
148 if blob:
149 break
150 except KeyError:
151 pass
152
153 if not blob:
154 raise NotFoundError
155
156 blob._load()
157 self.filedata = blob._contents
158 self.filedata += '\n'
159 return blob
160
161 def mimetype(self):
162 return "text/plain" #self.blob.mime_type
163
164 # main()
165 os.environ["QUERY_STRING"] = \
166 os.environ["QUERY_STRING"].replace("+", "%2b")
167
168 path = cgi.FieldStorage().getfirst('path')
169 ver = cgi.FieldStorage().getfirst('ver')
170
171 if not os.environ["HTTP_USER_AGENT"].startswith("IPFireSourceGrabber"):
172 give_403()
173
174 if not path:
175 give_302()
176
177 if not ver:
178 ver = "3.x"
179
180 # At first, we assume that the requested object is a plain file:
181 try:
182 object = FileObject(path=path, version=ver)
183 except NotFoundError:
184 # Second, we assume that the requestet object is in the patch repo:
185 try:
186 object = PatchObject(path=path)
187 except NotFoundError:
188 give_404()
189 else:
190 object.run()
191 else:
192 object.run()