forked from wmcbrine/pytivo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver.py
373 lines (321 loc) · 13.3 KB
/
httpserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import BaseHTTPServer
import SocketServer
import cgi
import gzip
import logging
import mimetypes
import os
import shutil
import socket
import time
from cStringIO import StringIO
from email.utils import formatdate
from urllib import unquote_plus, quote
from xml.sax.saxutils import escape
from Cheetah.Template import Template
import config
from plugin import GetPlugin, EncodeUnicode
SCRIPTDIR = os.path.dirname(__file__)
SERVER_INFO = """<?xml version="1.0" encoding="utf-8"?>
<TiVoServer>
<Version>1.6</Version>
<InternalName>pyTivo</InternalName>
<InternalVersion>1.0</InternalVersion>
<Organization>pyTivo Developers</Organization>
<Comment>http://pytivo.sf.net/</Comment>
</TiVoServer>"""
VIDEO_FORMATS = """<?xml version="1.0" encoding="utf-8"?>
<TiVoFormats>
<Format><ContentType>video/x-tivo-mpeg</ContentType><Description/></Format>
</TiVoFormats>"""
VIDEO_FORMATS_TS = """<?xml version="1.0" encoding="utf-8"?>
<TiVoFormats>
<Format><ContentType>video/x-tivo-mpeg</ContentType><Description/></Format>
<Format><ContentType>video/x-tivo-mpeg-ts</ContentType><Description/></Format>
</TiVoFormats>"""
BASE_HTML = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html> <head><title>pyTivo</title>
<link rel="stylesheet" type="text/css" href="main.css">
</head> <body> %s </body> </html>"""
RELOAD = '<p>The <a href="%s">page</a> will reload in %d seconds.</p>'
UNSUP = '<h3>Unsupported Command</h3> <p>Query:</p> <ul>%s</ul>'
class TivoHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
def __init__(self, server_address, RequestHandlerClass):
self.containers = {}
self.stop = False
self.restart = False
self.logger = logging.getLogger('pyTivo')
BaseHTTPServer.HTTPServer.__init__(self, server_address,
RequestHandlerClass)
self.daemon_threads = True
def add_container(self, name, settings):
if name in self.containers or name == 'TiVoConnect':
raise "Container Name in use"
try:
self.containers[name] = settings
except KeyError:
self.logger.error('Unable to add container ' + name)
def reset(self):
self.containers.clear()
for section, settings in config.getShares():
self.add_container(section, settings)
def handle_error(self, request, client_address):
self.logger.exception('Exception during request from %s' %
(client_address,))
def set_beacon(self, beacon):
self.beacon = beacon
def set_service_status(self, status):
self.in_service = status
class TivoHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def __init__(self, request, client_address, server):
self.wbufsize = 0x10000
self.server_version = 'pyTivo/1.0'
self.protocol_version = 'HTTP/1.1'
self.sys_version = ''
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, request,
client_address, server)
def address_string(self):
host, port = self.client_address[:2]
return host
def version_string(self):
""" Override version_string() so it doesn't include the Python
version.
"""
return self.server_version
def do_GET(self):
tsn = self.headers.getheader('TiVo_TCD_ID',
self.headers.getheader('tsn', ''))
if not self.authorize(tsn):
return
if tsn and (not config.tivos_found or tsn in config.tivos):
attr = config.tivos.get(tsn, {})
if 'address' not in attr:
attr['address'] = self.address_string()
if 'name' not in attr:
attr['name'] = self.server.beacon.get_name(attr['address'])
config.tivos[tsn] = attr
if '?' in self.path:
path, opts = self.path.split('?', 1)
query = cgi.parse_qs(opts)
else:
path = self.path
query = {}
if path == '/TiVoConnect':
self.handle_query(query, tsn)
else:
## Get File
splitpath = [x for x in unquote_plus(path).split('/') if x]
if splitpath:
self.handle_file(query, splitpath)
else:
## Not a file not a TiVo command
self.infopage()
def do_POST(self):
tsn = self.headers.getheader('TiVo_TCD_ID',
self.headers.getheader('tsn', ''))
if not self.authorize(tsn):
return
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
query = cgi.parse_multipart(self.rfile, pdict)
else:
length = int(self.headers.getheader('content-length'))
qs = self.rfile.read(length)
query = cgi.parse_qs(qs, keep_blank_values=1)
self.handle_query(query, tsn)
def do_command(self, query, command, target, tsn):
for name, container in config.getShares(tsn):
if target == name:
plugin = GetPlugin(container['type'])
if hasattr(plugin, command):
self.cname = name
self.container = container
method = getattr(plugin, command)
method(self, query)
return True
else:
break
return False
def handle_query(self, query, tsn):
mname = False
if 'Command' in query and len(query['Command']) >= 1:
command = query['Command'][0]
# If we are looking at the root container
if (command == 'QueryContainer' and
(not 'Container' in query or query['Container'][0] == '/')):
self.root_container()
return
if 'Container' in query:
# Dispatch to the container plugin
basepath = query['Container'][0].split('/')[0]
if self.do_command(query, command, basepath, tsn):
return
elif command == 'QueryItem':
path = query.get('Url', [''])[0]
splitpath = [x for x in unquote_plus(path).split('/') if x]
if splitpath and not '..' in splitpath:
if self.do_command(query, command, splitpath[0], tsn):
return
elif (command == 'QueryFormats' and 'SourceFormat' in query and
query['SourceFormat'][0].startswith('video')):
if config.is_ts_capable(tsn):
self.send_xml(VIDEO_FORMATS_TS)
else:
self.send_xml(VIDEO_FORMATS)
return
elif command == 'QueryServer':
self.send_xml(SERVER_INFO)
return
elif command in ('FlushServer', 'ResetServer'):
# Does nothing -- included for completeness
self.send_response(200)
self.send_header('Content-Length', '0')
self.end_headers()
self.wfile.flush()
return
# If we made it here it means we couldn't match the request to
# anything.
self.unsupported(query)
def send_content_file(self, path):
lmdate = os.path.getmtime(path)
try:
handle = open(path, 'rb')
except:
self.send_error(404)
return
# Send the header
mime = mimetypes.guess_type(path)[0]
self.send_response(200)
if mime:
self.send_header('Content-Type', mime)
self.send_header('Content-Length', os.path.getsize(path))
self.send_header('Last-Modified', formatdate(lmdate))
self.end_headers()
# Send the body of the file
try:
shutil.copyfileobj(handle, self.wfile)
except:
pass
handle.close()
self.wfile.flush()
def handle_file(self, query, splitpath):
if '..' not in splitpath: # Protect against path exploits
## Pass it off to a plugin?
for name, container in self.server.containers.items():
if splitpath[0] == name:
self.cname = name
self.container = container
base = os.path.normpath(container['path'])
path = os.path.join(base, *splitpath[1:])
plugin = GetPlugin(container['type'])
plugin.send_file(self, path, query)
return
## Serve it from a "content" directory?
base = os.path.join(SCRIPTDIR, *splitpath[:-1])
path = os.path.join(base, 'content', splitpath[-1])
if os.path.isfile(path):
self.send_content_file(path)
return
## Give up
self.send_error(404)
def authorize(self, tsn=None):
# if allowed_clients is empty, we are completely open
allowed_clients = config.getAllowedClients()
if not allowed_clients or (tsn and config.isTsnInConfig(tsn)):
return True
client_ip = self.client_address[0]
for allowedip in allowed_clients:
if client_ip.startswith(allowedip):
return True
self.send_fixed('Unauthorized.', 'text/plain', 403)
return False
def log_message(self, format, *args):
self.server.logger.info("%s [%s] %s" % (self.address_string(),
self.log_date_time_string(), format%args))
def send_fixed(self, page, mime, code=200, refresh=''):
squeeze = (len(page) > 256 and mime.startswith('text') and
'gzip' in self.headers.getheader('Accept-Encoding', ''))
if squeeze:
out = StringIO()
gzip.GzipFile(mode='wb', fileobj=out).write(page)
page = out.getvalue()
out.close()
self.send_response(code)
self.send_header('Content-Type', mime)
self.send_header('Content-Length', len(page))
if squeeze:
self.send_header('Content-Encoding', 'gzip')
self.send_header('Expires', '0')
if refresh:
self.send_header('Refresh', refresh)
self.end_headers()
self.wfile.write(page)
self.wfile.flush()
def send_xml(self, page):
self.send_fixed(page, 'text/xml')
def send_html(self, page, code=200, refresh=''):
self.send_fixed(page, 'text/html; charset=utf-8', code, refresh)
def root_container(self):
tsn = self.headers.getheader('TiVo_TCD_ID', '')
tsnshares = config.getShares(tsn)
tsncontainers = []
for section, settings in tsnshares:
try:
mime = GetPlugin(settings['type']).CONTENT_TYPE
if mime.split('/')[1] in ('tivo-videos', 'tivo-music',
'tivo-photos'):
settings['content_type'] = mime
tsncontainers.append((section, settings))
except Exception, msg:
self.server.logger.error(section + ' - ' + str(msg))
t = Template(file=os.path.join(SCRIPTDIR, 'templates',
'root_container.tmpl'),
filter=EncodeUnicode)
if self.server.beacon.bd:
t.renamed = self.server.beacon.bd.renamed
else:
t.renamed = {}
t.containers = tsncontainers
t.hostname = socket.gethostname()
t.escape = escape
t.quote = quote
self.send_xml(str(t))
def infopage(self):
t = Template(file=os.path.join(SCRIPTDIR, 'templates',
'info_page.tmpl'),
filter=EncodeUnicode)
t.admin = ''
if config.get_server('tivo_mak') and config.get_server('togo_path'):
t.togo = '<br>Pull from TiVos:<br>'
else:
t.togo = ''
for section, settings in config.getShares():
plugin_type = settings.get('type')
if plugin_type == 'settings':
t.admin += ('<a href="TiVoConnect?Command=Settings&' +
'Container=' + quote(section) +
'">Settings</a><br>')
elif plugin_type == 'togo' and t.togo:
for tsn in config.tivos:
if tsn and 'address' in config.tivos[tsn]:
t.togo += ('<a href="TiVoConnect?' +
'Command=NPL&Container=' + quote(section) +
'&TiVo=' + config.tivos[tsn]['address'] +
'">' + config.tivos[tsn]['name'] +
'</a><br>')
self.send_html(str(t))
def unsupported(self, query):
message = UNSUP % '\n'.join(['<li>%s: %s</li>' % (key, repr(value))
for key, value in query.items()])
text = BASE_HTML % message
self.send_html(text, code=404)
def redir(self, message, seconds=2):
url = self.headers.getheader('Referer')
if url:
message += RELOAD % (url, seconds)
refresh = '%d; url=%s' % (seconds, url)
else:
refresh = ''
text = (BASE_HTML % message).encode('utf-8')
self.send_html(text, refresh=refresh)