fix static responses
[enigma2.git] / lib / python / Plugins / Extensions / WebInterface / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2
3 sessions = [ ]
4
5 def startWebserver():
6         from twisted.internet import reactor
7         from twisted.web2 import server, http, static, resource, stream, http_headers, responsecode
8         import webif
9
10         class ScreenPage(resource.Resource):
11                 def render(self, req):
12                         global sessions
13                         if sessions == [ ]:
14                                 return http.Response(200, stream="please wait until enigma has booted")
15                         
16                         s = stream.ProducerStream()
17                         webif.renderPage(s, req, sessions[0])  # login?
18                         return http.Response(stream=s)
19
20         class Toplevel(resource.Resource):
21                 addSlash = True
22                 
23                 def render(self, req):
24                         return http.Response(responsecode.OK, {'Content-type': http_headers.MimeType('text', 'html')},
25                                 stream='Hello! you want probably go to <a href="/test">the test</a> instead.')
26
27                 child_test = ScreenPage() # "/test"
28                 child_hdd = static.File("/hdd")
29
30         site = server.Site(Toplevel())
31         
32         reactor.listenTCP(80, http.HTTPFactory(site))
33
34 def autostart(reason, **kwargs):
35         if "session" in kwargs:
36                 global sessions
37                 sessions.append(kwargs["session"])
38                 return
39
40         if reason == 0:
41                 try:
42                         startWebserver()
43                 except ImportError:
44                         print "twisted not available, not starting web services"
45
46 def Plugins(**kwargs):
47         return PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart)