forked from psung/zeya
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzeya.py
executable file
·350 lines (319 loc) · 12.5 KB
/
zeya.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2009 Phil Sung, Samson Yeung, Romain Francoise
#
# This file is part of Zeya.
#
# Zeya is free software: you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Zeya is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Zeya. If not, see <http://www.gnu.org/licenses/>.
# Zeya - a web music server.
# Work with python2.5
from __future__ import with_statement
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import base64
import crypt
import getopt
import os
import re
import socket
import sys
import tempfile
import traceback
import urllib
import zlib
try:
from urlparse import parse_qs
except: # (ImportError, AttributeError):
from cgi import parse_qs
try:
import json
json.dumps
except (ImportError, AttributeError):
import simplejson as json
import decoders
import options
b64dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
auth = 'Authorization'
no_auth_rval = \
"""
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Error</TITLE>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</HEAD>
<BODY><H1>401 Unauthorized.</H1></BODY>
</HTML>
"""
# Auth types
NO_AUTH = None
BASIC_AUTH = 'basic'
class BadArgsError(Exception):
"""
Error due to incorrect command-line invocation of this program.
"""
def __init__(self, message):
self.error_message = message
def __str__(self):
return "Error: %s" % (self.error_message,)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""
HTTP Server that handles requests in separate threads.
"""
# Allow IPv6 connections if possible.
if socket.has_ipv6:
address_family = socket.AF_INET6
user_pass_regexp = re.compile('([^:]):(.*)$')
def split_user_pass(data):
""" Split the given data into user and password. """
return user_pass_regexp.search(data).groups()
def ZeyaHandler(backend, library_repr, resource_basedir, bitrate,
auth_type=None, auth_data=None):
"""
Wrapper around the actual HTTP request handler implementation class. We
need to create a closure so that the inner class can receive the following
data:
Backend to use.
Library data.
Base directory for resources.
Bitrate for encoding.
Authentication data.
"""
class ZeyaHandlerImpl(BaseHTTPRequestHandler, object):
"""
Web server request handler.
"""
def do_GET(self):
"""
Handle a GET request.
"""
# http://host/ yields the library main page.
if self.path == '/':
self.serve_static_content('/library.html')
# http://host/getlibrary returns a representation of the music
# collection.
elif self.path == '/getlibrary':
self.serve_library()
# http://host/getcontent?key=N yields an Ogg stream of the file
# associated with the specified key.
elif self.path.startswith('/getcontent?'):
self.serve_content(urllib.unquote(self.path[12:]))
# All other paths are assumed to be static content.
# http://host/foo is mapped to resources/foo.
else:
self.serve_static_content(self.path)
def get_content_type(self, path):
"""
Return the MIME type associated with the given path.
"""
path = path.lower()
if path.endswith('.html'):
return 'text/html'
elif path.endswith('.png'):
return 'image/png'
elif path.endswith('.gif'):
return 'image/gif'
elif path.endswith('.css'):
return 'text/css'
elif path.endswith('.js'):
return 'text/javascript'
elif path.endswith('.ogg'):
return 'audio/ogg'
else:
print ("Warning: couldn't identify content-type for %r, "
+ "serving as application/octet-stream") % (path,)
return 'application/octet-stream'
def serve_content(self, query):
"""
Serve an audio stream (audio/ogg).
"""
# The query is of the form key=N or key=N&buffered=true.
args = parse_qs(query)
key = args['key'][0] if args.has_key('key') else ''
# If buffering is activated, encode the entire file and serve the
# Content-Length header. This increases song load latency because
# we can't serve any of the file until we've finished encoding the
# whole thing. However, Chrome needs the Content-Length header to
# accompany audio data.
buffered = args['buffered'][0] if args.has_key('buffered') else ''
self.send_response(200)
self.send_header('Content-type', 'audio/ogg')
if buffered:
# Complete the transcode and write to a temporary file.
# Determine its length and serve the Content-Length header.
output_file = tempfile.TemporaryFile()
backend.get_content(key, output_file, bitrate, buffered=True)
output_file.seek(0)
data = output_file.read()
self.send_header('Content-Length', str(len(data)))
self.end_headers()
self.wfile.write(data)
else:
# Don't determine the Content-Length. Just stream to the client
# on the fly.
self.end_headers()
backend.get_content(key, self.wfile, bitrate)
self.wfile.close()
def send_data(self, ctype, data):
"""
Send data to the client.
Use deflate compression if client headers indicate that the
other end supports it and if it's appropriate for this
content-type.
"""
compress_data = \
(ctype.startswith('text/')
and 'Accept-Encoding' in self.headers
and 'deflate' in self.headers['Accept-Encoding'].split(','))
self.send_response(200)
if compress_data:
data = zlib.compress(data)
self.send_header('Content-Encoding', 'deflate')
self.send_header('Vary', 'Accept-Encoding')
self.send_header('Content-Length', str(len(data)))
self.send_header('Content-Type', ctype)
self.end_headers()
self.wfile.write(data)
self.wfile.close()
def serve_library(self):
"""
Serve a representation of the library.
"""
self.send_data('text/html', library_repr.encode('utf-8'))
def serve_static_content(self, path):
"""
Serve static content from the resources/ directory.
"""
try:
# path already has a leading '/' in front of it. Strip it.
full_path = os.path.join(resource_basedir, path[1:])
# Ensure that the basedir we use for security checks ends in '/'.
effective_basedir = os.path.join(resource_basedir, '')
# Prevent directory traversal attacks. Canonicalize the
# filename we're going to open and verify that it's inside the
# resource directory.
if not os.path.abspath(full_path).startswith(effective_basedir):
self.send_error(404, 'File not found: %s' % (path,))
return
with open(full_path) as f:
self.send_data(self.get_content_type(path), f.read())
except IOError:
traceback.print_exc()
self.send_error(404, 'File not found: %s' % (path,))
class ZeyaBasicAuthHandlerImpl(ZeyaHandlerImpl):
def __init__(self, *args, **kwargs):
self.auth_regexp = re.compile('Basic ([%s[]*)' % b64dict)
super(ZeyaBasicAuthHandlerImpl, self).__init__(*args, **kwargs)
def send_no_auth(self):
"""
Send an unauthorized required page.
"""
self.send_response(401)
self.send_header('Content-type', 'text/html')
self.send_header('Content-Length', str(len(no_auth_rval)))
self.send_header('WWW-Authenticate', 'Basic realm="Zeya Secure"')
self.end_headers()
self.wfile.write(no_auth_rval)
def authorized(self):
"""
Return true if self.headers has valid authentication information.
"""
if auth in self.headers and self.auth_regexp.match(self.headers[auth]):
encoded_auth = self.auth_regexp.sub('\\1', self.headers[auth])
decoded_auth = base64.b64decode(encoded_auth)
client_user, client_pass = split_user_pass(decoded_auth)
if client_user in auth_data:
client_crypt_pass = crypt.crypt(\
client_pass, auth_data[client_user][:2])
return client_crypt_pass == auth_data[client_user]
return False
def do_GET(self):
"""
Handle a GET request, sending an authentication required header if
not authenticated.
"""
if self.authorized():
ZeyaHandlerImpl.do_GET(self)
else:
self.send_no_auth()
if auth_type == BASIC_AUTH:
print 'Using Basic Auth Handler...'
return ZeyaBasicAuthHandlerImpl
return ZeyaHandlerImpl
def get_backend(backend_type):
"""
Return a backend object of the requested type.
backend_type: string giving the backend type to use.
"""
if backend_type == "rhythmbox":
# Import the backend modules conditionally, so users don't have to
# install dependencies unless they are actually used.
from rhythmbox import RhythmboxBackend
return RhythmboxBackend()
elif backend_type == 'dir':
from directory import DirectoryBackend
return DirectoryBackend(path)
else:
raise ValueError("Invalid backend %r" % (backend_type,))
def run_server(backend, port, bitrate, basic_auth_file=None):
# Read the library.
print "Loading library..."
library_contents = backend.get_library_contents()
# Filter out songs that we won't be able to decode.
library_contents = \
[ s for s in library_contents \
if decoders.has_decoder(backend.get_filename_from_key(s['key'])) ]
if not library_contents:
print "WARNING: no tracks were found. Check that you've specified " \
+ "the right backend/path."
library_repr = json.dumps(library_contents, ensure_ascii=False)
basedir = os.path.abspath(os.path.dirname(os.path.realpath(sys.argv[0])))
auth_data = None
if basic_auth_file is not None:
auth_data = {}
for line in basic_auth_file:
s_user, s_pass = split_user_pass(line.rstrip())
auth_data[s_user] = s_pass
server = ThreadedHTTPServer(
('', port),
ZeyaHandler(backend,
library_repr,
os.path.join(basedir, 'resources'),
bitrate,
auth_type=NO_AUTH if basic_auth_file is None else BASIC_AUTH,
auth_data=auth_data,
))
print "Listening on port %d" % (port,)
# Start up a web server.
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
server.server_close()
if __name__ == '__main__':
try:
(show_help, backend_type, bitrate, port, path, basic_auth_file) = \
options.get_options(sys.argv[1:])
except options.BadArgsError, e:
print e
options.print_usage()
sys.exit(1)
if show_help:
options.print_usage()
sys.exit(0)
print "Using %r backend." % (backend_type,)
backend = get_backend(backend_type)
run_server(backend, port, bitrate, basic_auth_file)