Package elisa :: Package plugins :: Package good :: Package httpd_plugin :: Module http_server
[hide private]
[frames] | no frames]

Source Code for Module elisa.plugins.good.httpd_plugin.http_server

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  """ 
 18  Twisted based HTTP server 
 19  """ 
 20   
 21  __maintainer__ = "Philippe Normand <philippe@fluendo.com>" 
 22   
 23  from elisa.core import common 
 24  from elisa.base_components import service_provider 
 25  from elisa.core.bus.bus_message import HttpResource 
 26  from elisa.core.utils import network 
 27   
 28  from twisted.internet import reactor 
 29  from twisted.internet.error import CannotListenError 
 30  from twisted.web import resource, server 
 31   
32 -class RootResource(resource.Resource):
33 addSlash = True 34
35 - def render(self, request):
36 return """ 37 <html> 38 <head> 39 <title>Elisa HTTP Server</title> 40 </head> 41 <body> 42 <h1>Empty page</h1> 43 </body> 44 </html> 45 """
46
47 - def getChild(self, path, request):
48 if (path == ''): 49 return self 50 else: 51 return self.children.get(path)
52
53 -class SubResource(resource.Resource):
54 addSlash = True
55 - def __init__(self, name):
56 resource.Resource.__init__(self) 57 self._name = name
58
59 - def render(self, request):
60 return """ 61 <html> 62 <head> 63 <title>Elisa HTTP Server</title> 64 </head> 65 <body> 66 <h1>%s Resource</h1> 67 </body> 68 </html> 69 """ % self._name
70
71 - def getChild(self, path, request):
72 return self.children.get(path)
73
74 -class HttpServer(service_provider.ServiceProvider):
75 """ 76 DOCME 77 """ 78 79 default_config = {'port': '8000', 'interface': '' } 80 config_doc = {'port': 'TCP port the server should listen on', 81 'interface': 'the hostname to bind to, defaults to "" (all)'} 82
83 - def initialize(self):
89
90 - def start(self):
91 port = int(self.config.get('port')) 92 self._interface = self.config.get('interface','') 93 site = server.Site(self._root) 94 95 iface = self._interface or 'all' 96 self.info("Listening on %s:%s", iface, port) 97 98 try: 99 self._twisted_port = reactor.listenTCP(port, site, 100 interface=self._interface) 101 except CannotListenError, error: 102 self.warning(error) 103 else: 104 reactor.addSystemEventTrigger('before', 'shutdown', self.stop)
105
106 - def stop(self):
107 self._twisted_port.stopListening()
108
109 - def _get_ip_port(self):
110 if not self._ip_port: 111 thost = self._twisted_port.getHost() 112 ip = thost.host 113 port = thost.port 114 115 if ip == '0.0.0.0': 116 ip = network.get_host_address() 117 elif self._interface: 118 ip = network.get_ip_address(self._interface) 119 self._ip_port = (ip, port) 120 121 return self._ip_port
122
123 - def _bus_message_received(self, msg, sender):
124 path = msg.path 125 rsrc = msg.resource 126 parts = path.split('/') 127 128 self.info("Adding web resource handler for: %r", path) 129 130 # process resource path and lazily add SubResources where needed 131 subs = parts[:-1] 132 current = self._root 133 while subs: 134 sub = subs.pop(0) 135 rs = current.getChild(sub, None) 136 if not rs: 137 rs = SubResource(sub) 138 current.putChild(sub, rs) 139 current = rs 140 141 # give my ip/port to the resource so that it can build valid URLs 142 # pointing to me 143 rsrc.ip_port = self._get_ip_port() 144 current.putChild(parts[-1], rsrc)
145