[StreamServerSeek] Reverse-Proxy + Player
[enigma2-plugins.git] / streamserverseek / src / WebChilds / Proxy.py
1 from twisted.web import resource, server, proxy
2 from twisted.internet import reactor
3
4 from Plugins.Extensions.StreamServerSeek.StreamServerSeek import StreamServerSeek
5
6 import urlparse
7
8 class MyProxyClient(proxy.ProxyClient):
9         def __init__(self, command, rest, version, headers, data, father, responseEndCallback):
10                 proxy.ProxyClient.__init__(self, command, rest, version, headers, data, father)
11                 self.version = version
12                 self.responseEndCallback = responseEndCallback
13
14         def sendCommand(self, command, path):
15                 self.transport.writeSequence([command, b' ', path, b' ' + self.version + b'\r\n'])
16
17         def handleResponseEnd(self):
18                 if not self._finished:
19                         if self.responseEndCallback:
20                                 self.responseEndCallback(self.father)
21                         proxy.ProxyClient.handleResponseEnd(self)
22
23 class MyProxyClientFactory(proxy.ProxyClientFactory):
24         protocol = MyProxyClient
25
26         def __init__(self, command, rest, version, headers, data, father, responseEndCallback):
27                 proxy.ProxyClientFactory.__init__(self, command, rest, version, headers, data, father)
28                 self.responseEndCallback = responseEndCallback
29
30         def buildProtocol(self, addr):
31                 return self.protocol(self.command, self.rest, self.version,
32                         self.headers, self.data, self.father, self.responseEndCallback)
33
34 class ProxyResource(resource.Resource):
35         isLeaf = True
36         session = None
37         
38         proxyClientFactoryClass = MyProxyClientFactory
39
40         def __init__(self, session):
41                 resource.Resource.__init__(self)
42                 self.session = session
43                 self.streamServerSeek = StreamServerSeek()
44
45         def render(self, request):
46                 request.requestHeaders.setRawHeaders(b"host", [request.getRequestHostname().encode('ascii') + b":8080"])
47                 request.content.seek(0, 0)
48                 
49                 path = "/" + "/".join(request.postpath)
50
51                 qs = urlparse.urlparse(request.uri)[4]
52                 if qs:
53                         path = path + '?' + qs
54                 
55                 print "[StreamserverSeek] Reverse-Proxy %s" % path
56
57                 clientFactory = self.proxyClientFactoryClass(
58                         request.method, path, request.clientproto,
59                         request.getAllHeaders(), request.content.read(), request, self.responseEndCallback)
60                 reactor.connectTCP(request.getRequestHostname(), 8080, clientFactory)
61
62                 return server.NOT_DONE_YET
63
64         def responseEndCallback(self, request):
65                 print "[StreamserverSeek] ResponseEndCallback"