This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edna.py
executable file
·1167 lines (991 loc) · 35.4 KB
/
edna.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#
# edna.py -- an MP3 server
#
# Copyright (C) 2002 Fredrik Steen <[email protected]>. All Rights Reserved.
# Copyright (C) 1999-2000 Greg Stein. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
# This software is maintained by Greg and is available at:
# http://edna.sourceforge.net/
#
# Here is the CVS ID for tracking purposes:
# $Id: edna.py,v 1.83 2006/03/11 19:36:29 syrk Exp $
#
__version__ = '0.6'
import SocketServer
import BaseHTTPServer
import ConfigParser
import sys
import string
import os
import cgi
import urllib
import socket
import re
import stat
import random
import time
import struct
import zipfile
import ezt
import MP3Info
import md5
try:
import signal
signalSupport = 'yes'
except ImportError:
signalSupport = 'no'
try:
import ogg.vorbis
oggSupport = 'yes'
except ImportError:
oggSupport = 'no'
try:
import cStringIO
StringIO = cStringIO
except ImportError:
import StringIO
try:
import sha
except ImportError:
pass
error = __name__ + '.error'
TITLE = 'Streaming MP3 Server'
# a pattern used to trim leading digits, spaces, and dashes from a song
### would be nice to get a bit fancier with the possible trimming
re_trim = re.compile('[-0-9 ]*-[ ]*(.*)')
# determine which mixin to use: prefer threading, fall back to forking.
try:
import thread
mixin = SocketServer.ThreadingMixIn
except ImportError:
if not hasattr(os, 'fork'):
print "ERROR: your platform does not support threading OR forking."
sys.exit(1)
mixin = SocketServer.ForkingMixIn
class Server(mixin, BaseHTTPServer.HTTPServer):
def __init__(self, fname):
self.userLog = [ ] # to track server usage
self.userIPs = { } # log unique IPs
config = self.config = ConfigParser.ConfigParser()
config.add_section('server')
config.add_section('sources')
config.add_section('acl')
config.add_section('extra')
# set up some defaults for the web server.
d = config.defaults()
d['port'] = '8080'
d['robots'] = '1'
d['binding-hostname'] = ''
d['name_prefix'] = ''
d['log'] = ''
d['template-dir'] = 'templates'
d['template'] = 'default.ezt'
d['resource-dir'] = 'resources';
d['auth_level'] = '1'
d['debug_level'] = '0'
d['fileinfo'] = '0'
d['encoding'] = 'UTF-8,iso8859-1'
d['hide_names'] = ""
d['hide_matching'] = ""
d['zip'] = '0'
config.read(fname)
# Setup a logging file
self.log = None
log = self.config.get('server', 'log')
if log:
if log == '-':
self.log = sys.stdout
else:
try:
self.log = open(log, 'a')
except IOError:
pass
template_path = config.get('server', 'template-dir')
template_file = config.get('server', 'template')
template_path = os.path.join(os.path.dirname(fname), template_path)
self.resource_dir = os.path.join(os.path.dirname(fname), config.get('server', 'resource-dir'))
self.fileinfo = config.getint('server', 'fileinfo')
self.zipmax = config.getint('server', 'zip') * 1024 * 1024
self.zipsize = 0
global debug_level
debug_level = config.getint('extra', 'debug_level')
global DAYS_NEW
DAYS_NEW = config.getint('extra', 'days_new')
global HIDE_EXACT
HIDE_EXACT = filter(None, [toHide.strip().lower() for toHide in config.get('extra', 'hide_names').split(',')])
global HIDE_MATCH
HIDE_MATCH = filter(None, [toHide.strip().lower() for toHide in config.get('extra', 'hide_matching').split(',')])
if debug_level == 1:
self.log_message('Running in debug mode')
encodings = string.split(config.get('server', 'encoding'), ',')
tfname = os.path.join(template_path, template_file)
self.default_template = ezt.Template(tfname, encodings)
tfname = os.path.join(template_path, 'style-xml.ezt')
self.xml_template = ezt.Template(tfname, encodings)
tfname = os.path.join(template_path, 'stats.ezt')
self.stats_template = ezt.Template(tfname, encodings)
self.dirs = [ ]
dirs = [ ]
for option in config.options('sources'):
if option[:3] == 'dir':
dirs.append((int(option[3:]), config.get('sources', option)))
if not dirs:
raise error, 'no sources'
dirs.sort()
for i in range(len(dirs)):
dir = map(string.strip, string.split(dirs[i][1], '='))
if len(dir) == 1:
name = dir[0]
else:
name = dir[1]
if not os.path.isdir(dir[0]):
self.log_message("WARNING: a source's directory must exist")
self.log_message(" skipping: dir%d = %s = %s" % (dirs[i][0], dir[0], name))
continue
if string.find(name, '/') != -1:
self.log_message("WARNING: a source's display name cannot contain '/'")
self.log_message(" skipping: dir%d = %s = %s" % (dirs[i][0], dir[0], name))
continue
self.dirs.append((dir[0], name))
self.acls = []
try:
allowed = re.split(r'[\s\n,]+', config.get('acl', 'allow'))
except ConfigParser.NoOptionError:
allowed = []
for addr in allowed:
if '/' in addr:
addr, masklen = string.split(addr, '/')
masklen = int(masklen)
else:
masklen = 32
if not re.match(r'^\d+\.\d+\.\d+\.\d+$', addr):
addr = socket.gethostbyname(addr)
mask = ~((1 << (32-masklen)) - 1)
entry = (dot2int(addr), mask)
if not entry in self.acls:
self.acls.append(entry)
try:
auth_pairs = re.split(r'[\s\n,]+', config.get('acl', 'auth'))
self.auth_table = {}
try:
self.password_hash = config.get('acl','password_hash')
if not globals().has_key(self.password_hash):
self.log_message("WARNING: there is no hash module '%s' for passwords" % \
self.password_hash)
self.password_hash = None
else:
self.debug_message("passwords authenticated using %s hexdigest" % \
self.password_hash)
except ConfigParser.NoOptionError:
self.password_hash = None
if self.password_hash is None:
self.debug_message("passwords authenticated in plain text")
for pair in auth_pairs:
user,passw = string.split(pair,':')
self.auth_table[user] = passw
except ConfigParser.NoOptionError:
self.auth_table = {}
self.auth_level = config.get('acl', 'auth_level')
self.name_prefix = config.get('server', 'name_prefix')
self.port = config.getint('server', 'port')
try:
SocketServer.TCPServer.__init__(self,
(config.get('server', 'binding-hostname'), self.port),
EdnaRequestHandler)
except socket.error, value:
self.log_message( "edna: bind(): %s" % str(value[1]) )
raise SystemExit
def server_bind(self):
# set SO_REUSEADDR (if available on this platform)
if hasattr(socket, 'SOL_SOCKET') and hasattr(socket, 'SO_REUSEADDR'):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
BaseHTTPServer.HTTPServer.server_bind(self)
def log_user(self, ip, tm, url):
if len(self.userLog) > 40:
# delete the oldest entry
self.userLog.pop(0)
# append it to the queue
self.userLog.append((ip, tm, url))
if ip not in self.userIPs.keys():
# add the entry for the first time
self.userIPs[ip] = (1, tm)
else:
# increment the count and add the most recent time
count, oldTime = self.userIPs[ip]
self.userIPs[ip] = (count + 1, tm)
def acl_ok(self, ipaddr):
if not self.acls:
return 1
ipaddr = dot2int(ipaddr)
for allowed, mask in self.acls:
if (ipaddr & mask) == (allowed & mask):
return 1
return 0
def log_message(self, msg):
if self.log:
try:
self.log.write(msg + '\n')
self.log.flush()
except IOError:
pass
def debug_message(self, msg):
if debug_level<1:
return
self.log_message ('DEBUG: ' + msg)
class EdnaRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
try:
self._perform_GET()
except ClientAbortedException:
self.server.debug_message('Exception caught in "do_GET" --- ClientAbortException')
except IOError:
pass
def check_authorization(self):
auth_table = self.server.auth_table
auth = self.headers.getheader('Authorization')
this_user, this_pass = None, None
if auth:
def transl(passwd):
hash = globals()[self.server.password_hash]
return hash.new(passwd).hexdigest()
if not self.server.password_hash:
transl = str # i.e. no translation
if string.lower(auth[:6]) == 'basic ':
import base64
[name,password] = string.split(
base64.decodestring(string.split(auth)[-1]), ':')
this_user, this_pass = name, password
this_pass = transl(this_pass)
if auth_table.has_key(this_user) and auth_table[this_user] == this_pass:
self.server.debug_message('--- Authenticated --- User: %s Password: %s' % \
(this_user, this_pass))
return 1
self.server.debug_message('--- Auth FAILED --- User: %s Password: %s' % \
(this_user, this_pass))
if not auth_table.has_key(this_user):
self.server.debug_message('--- User does not exist --- %s' % this_user)
realm='edna'
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm="%s"' % realm)
self.send_header('Content-Type', 'text/html;');
self.send_header('Connection', 'close');
self.end_headers()
try:
short, long = self.responses[401]
except KeyError:
short, long = '???', '???'
self.wfile.write(self.error_message_format %
{'code': 401, 'message': short, 'explain': long})
return 0
def _perform_GET(self):
## verify the IP
if not self.server.acl_ok(self.client_address[0]):
self.send_error(403, 'Forbidden')
return
## verify the Username/Password
if self.server.auth_table:
if self.server.auth_level == '2' or \
( self.server.auth_level == '1' and self.path[-1] == '/' ) or \
self.path == '/':
if not self.check_authorization():
return
path = self.translate_path()
if path is None:
self.send_error(400, 'Illegal URL construction')
return
if path == ["robots.txt"] and self.server.config.getint('server', 'robots') != 0:
self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.end_headers()
self.wfile.write("User-agent: *\nDisallow /\n")
return
self.output_style = 'html'
if len(path) >= 1:
if path[0] == 'xml':
path.pop(0)
self.output_style = 'xml'
if not path and len(self.server.dirs) > 1:
# home page
subdirs = [ ]
for d, name in self.server.dirs:
subdirs.append(_datablob(href=urllib.quote(name) + '/', is_new='',
text=name))
self.display_page(TITLE, subdirs, skiprec=1)
elif path and path[0] == 'stats':
# the site statistics were requested
self.display_stats()
elif path and path[0] == 'resources' and len(path) > 1:
# a resource file was requested
fullpath = os.path.join(self.server.resource_dir, path[1])
self.serve_file(path[1], fullpath, '/resources');
else:
# other requests fall under the user configured namespace
if path:
title = cgi.escape(path[-1])
else:
title = TITLE
if len(self.server.dirs) == 1:
url = '/'
curdir = self.server.dirs[0][0]
else:
url = '/' + urllib.quote(path[0])
for d, name in self.server.dirs:
if path[0] == name:
curdir = d
path.pop(0)
break
else:
self.send_error(404)
return
for p in path:
if p == 'all.m3u' or p == 'allrecursive.m3u' or \
p == 'shuffle.m3u' or p == 'shufflerecursive.m3u' or \
p == 'all.zip':
# serve up a pseudo-file
self.serve_file(p, curdir, url)
return
pathname = os.path.join(curdir, p)
base, ext = os.path.splitext(p)
if string.lower(ext) == '.m3u':
base, ext = os.path.splitext(base)
if extensions.has_key(string.lower(ext)):
# something.mp3.m3u -- one of our pseudo-files
pathname = os.path.join(curdir, base + ext)
if not os.path.exists(pathname):
self.send_error(404)
return
if os.path.isfile(pathname):
# requested a file.
self.serve_file(p, pathname, url, self.headers.getheader('range'))
return
curdir = pathname
if url == '/':
url = '/' + urllib.quote(p)
else:
url = url + '/' + urllib.quote(p)
# requested a directory.
# ensure there is a trailing slash so that the (relative) href
# values will work.
if self.path[-1] != '/':
redir = self.build_url(self.path)
self.redirect(redir)
return
pictures = []
subdirs = []
songs = []
playlists = []
plainfiles = []
if path:
thisdir = path[-1]
else:
# one of the top-level virtual directories
thisdir = ''
thisdirlen = len(thisdir)
for name in sort_dir(curdir):
href = urllib.quote(name)
try:
is_new = check_new(os.stat(os.path.join(curdir, name))[stat.ST_MTIME])
except:
# For example, in the case of disk I/O errors
print "Failed to stat %s"%(name)
continue
nameLower = name.lower()
if nameLower in HIDE_EXACT: continue
skip = False
for toHide in HIDE_MATCH:
if toHide in nameLower:
self.server.debug_message("Hiding %s"%(name))
# I can't find a way to "continue" up two levels with one call...
skip = True
continue
if skip:
continue
base, ext = os.path.splitext(name)
ext = string.lower(ext)
if picture_extensions.has_key(ext):
pictures.append(_datablob(href=href, is_new=is_new))
continue
if plainfiles_extensions.has_key(ext):
plainfiles.append(_datablob(href=href, is_new=is_new, text=base))
continue
if ext == '.m3u':
playlists.append(_datablob(href=href, is_new=is_new, text=base))
continue
fullpath = os.path.join(curdir, name)
if extensions.has_key(ext):
# if a song has a prefix that matches the directory, and something
# exists after that prefix, then strip it. don't strip if the
# directory is a single-letter.
if len(base) > thisdirlen > 1 and base[:thisdirlen] == thisdir:
base = base[thisdirlen:]
# trim a bit of stuff off of the file
match = re_trim.match(base)
if match:
base = match.group(1)
d = _datablob(href=href, is_new=is_new, text=base)
if self.server.fileinfo:
info = FileInfo(fullpath)
else:
info = _datablob()
d.info = empty_delegator(info)
songs.append(d)
else:
newdir = os.path.join(curdir, name)
if os.path.isdir(fullpath):
subdirs.append(_datablob(href=href + '/', is_new=is_new, text=name))
self.display_page(title, subdirs, pictures, plainfiles, songs, playlists)
def display_stats(self):
self.send_response(200)
self.send_header("Content-Type", 'text/html')
self.end_headers()
data = { 'users' : [ ],
'ips' : [ ],
}
user_log = self.server.userLog
for i in range(len(user_log) - 1, -1, -1):
d = _datablob()
d.ip, tm, d.url = user_log[i]
d.unquoted_url = urllib.unquote(d.url)
d.time = time.strftime("%B %d %I:%M:%S %p", time.localtime(tm))
data['users'].append(d)
ip_log = self.server.userIPs
ips = ip_log.keys()
ips.sort()
for ip in ips:
d = _datablob()
d.ip = ip
d.count, tm = ip_log[ip]
d.time = time.strftime("%B %d %I:%M:%S %p", time.localtime(tm))
data['ips'].append(d)
self.server.stats_template.generate(self.wfile, data)
def display_page(self, title, subdirs, pictures=[], plainfiles=[], songs=[], playlists=[],
skiprec=0):
### implement a URL-selectable style here with a cache of templates
if self.output_style == 'html':
template = self.server.default_template
content_type = 'text/html'
else: # == 'xml'
template = self.server.xml_template
content_type = 'text/xml'
self.send_response(200)
self.send_header("Content-Type", content_type)
self.end_headers()
data = { 'title' : title,
'links' : self.tree_position(),
'pictures' : pictures,
'plainfiles' : plainfiles,
'subdirs' : subdirs,
'songs' : songs,
'playlists' : playlists,
}
if not skiprec:
data['display-recursive'] = 'yes'
else:
data['display-recursive'] = ''
template.generate(self.wfile, data)
def tree_position(self):
mypath = self.translate_path()
if not mypath:
return ''
url = self.build_url('')[:-1] # lose the trailing slash
links = [ '<a href="%s/">HOME</a>\n' % url ]
last = len(mypath)
for count in range(last):
url = url + '/' + urllib.quote(mypath[count])
text = cgi.escape(mypath[count])
if count == last - 1:
links.append('<b> / %s</b>' % text)
else:
links.append('<b> / </b><a href="%s/">%s</a>' % (url, text))
return '<p>' + string.join(links, '\n') + '</p>'
def make_list(self, fullpath, url, recursive, shuffle, songs=None):
# This routine takes a string for 'fullpath' and 'url', a list for
# 'songs' and a boolean for 'recursive' and 'shuffle'. If recursive is
# false make_list will return a list of every file ending in '.mp3' in
# fullpath. If recursive is true make_list will return a list of every
# file ending in '.mp3' in fullpath and in every directory beneath
# fullpath.
#
# WARNING: There is no checking for the recursive directory structures
# which are possible in most Unixes using ln -s etc... If you have
# such a directory structure, make_list will continue to traverse it
# until it hits the inherent limit in Python for the number of functions.
# This number is quite large. I found this out the hard way :). Learn
# from my experience...
if songs is None:
songs = []
for name in sort_dir(fullpath):
if url:
base, ext = os.path.splitext(name)
if extensions.has_key(string.lower(ext)):
# add the song's URL to the list we're building
songs.append(self.build_url(url, name) + '\n')
else:
if os.path.isfile(fullpath + '/' + name):
songs.append(name)
# recurse down into subdirectories looking for more MP3s.
if recursive and os.path.isdir(fullpath + '/' + name):
songs = self.make_list(fullpath + '/' + name,
url + '/' + urllib.quote(name),
recursive, 0, # don't shuffle subdir results
songs)
# The user asked us to mix up the results.
if shuffle:
count = len(songs)
for i in xrange(count):
j = random.randrange(count)
songs[i], songs[j] = songs[j], songs[i]
return songs
def open_playlist(self, fullpath, url):
dirpath = os.path.dirname(fullpath)
f = open(fullpath)
output = [ ]
for line in f.readlines():
line = string.strip(line)
if line[:7] == '#EXTM3U' or line[:8] == '#EXTINF:':
output.append(line)
continue
if line[:5] == 'http:' or line[:4] == 'ftp:':
output.append(line)
continue
line = os.path.normpath(line)
if os.path.isabs(line):
self.log_message('bad line in "%s": %s', self.path, line)
continue
if not os.path.exists(os.path.join(dirpath, line)):
self.log_message('file not found (in "%s"): %s', self.path, line)
continue
line = string.replace(line, "\\", "/") # if we're on Windows
output.append(self.build_url(url, line))
f = StringIO.StringIO(string.join(output, '\n') + '\n')
return f
def serve_file(self, name, fullpath, url, range=None):
base, ext = os.path.splitext(name)
ext = string.lower(ext)
mtime = None
if any_extensions.has_key(ext):
if not picture_extensions.has_key(ext):
# log the request of this file
ip, port = self.client_address
self.server.log_user(ip, time.time(), url + '/' + urllib.quote(name))
# get the file and info for delivery
type = any_extensions[ext]
f = open(fullpath, 'rb')
st = os.fstat(f.fileno())
clen = st.st_size
mtime = st.st_mtime
elif url == '/resources':
# We don't want to serve pseudo files under /resources
self.send_error(404)
return
elif ext == '.m3u':
type = 'audio/x-mpegurl'
if name == 'all.m3u' or name == 'allrecursive.m3u' or \
name == 'shuffle.m3u' or name == 'shufflerecursive.m3u':
recursive = name == 'allrecursive.m3u' or name == 'shufflerecursive.m3u'
shuffle = name == 'shuffle.m3u' or name == 'shufflerecursive.m3u'
# generate the list of URLs to the songs
songs = self.make_list(fullpath, url, recursive, shuffle)
f = StringIO.StringIO(string.join(songs, ''))
clen = len(f.getvalue())
else:
base, ext = os.path.splitext(base)
if extensions.has_key(string.lower(ext)):
f = StringIO.StringIO(self.build_url(url, base) + ext + '\n')
clen = len(f.getvalue())
else:
f = self.open_playlist(fullpath, url)
clen = len(f.getvalue())
mtime = os.stat(fullpath)[stat.ST_MTIME]
elif name == 'all.zip':
if not self.server.zipmax > 0:
self.send_error(403, 'The ZIP service has been disabled by the server administrator.')
return
type = 'application/zip'
f = StringIO.StringIO()
z = zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED)
songs = self.make_list(fullpath, None, None, None)
for s in songs:
z.write(fullpath + '/' + s, os.path.basename(fullpath) + '/' + s)
if self.server.zipsize + len(f.getvalue()) > self.server.zipmax:
break
z.close()
f.seek(0)
clen = len(f.getvalue())
self.server.debug_message("ZUP thresholds: %d + %d vs %d" %
(self.server.zipsize, clen, self.server.zipmax))
if self.server.zipsize + clen > self.server.zipmax:
self.send_error(503, 'The <b>ZIP</b> service is currently under heavy load. Please try again later.')
return
self.server.zipsize += clen
else:
self.send_error(404)
return
self.send_response(200)
self.send_header("Content-Type", type)
self.send_header("Content-Length", clen)
if mtime:
self.send_header('Last-Modified', time.strftime("%a, %d %b %Y %T GMT"))
# Thanks to Stefan Alfredsson <[email protected]>
# for the suggestion, Now the filenames get displayed right.
self.send_header("icy-name", base)
self.end_headers()
#Seek if the client requests it (a HTTP/1.1 request)
if range:
type, seek = string.split(range,'=')
startSeek, endSeek = string.split(seek,'-')
f.seek(int(startSeek))
while 1:
data = f.read(8192)
if not data:
break
try:
self.wfile.write(data)
except ClientAbortedException:
self.log_message('client closed connection for "%s"', self.path)
break
except socket.error:
# it was probably closed on the other end
break
if type == 'application/zip':
self.server.zipsize -= clen
def build_url(self, url, file=''):
host = self.server.name_prefix or self.headers.getheader('host') or self.server.server_name
if string.find(host, ':'):
return 'http://%s%s/%s' % (host, url, urllib.quote(file))
return 'http://%s:%s%s/%s' % (host, self.server.server_port, url,
urllib.quote(file))
def translate_path(self):
parts = string.split(urllib.unquote(self.path), '/')
parts = filter(None, parts)
while 1:
try:
parts.remove('.')
except ValueError:
break
while 1:
try:
idx = parts.index('..')
except ValueError:
break
if idx == 0:
self.server.debug_message('Warning in translate_path --- Illegal path: the \'..\' attempted to go above the root')
return None
del parts[idx-1:idx+1]
return parts
def redirect(self, url):
"Send a redirect to the specified URL."
self.log_error("code 301 -- Moved")
self.send_response(301, 'Moved')
self.send_header('Location', url)
self.end_headers()
self.wfile.write(self.error_message_format %
{'code': 301,
'message': 'Moved',
'explain': 'Object moved permanently'})
def log_request(self, code='-', size='-'):
try:
self.log_message('"%s" %s', self.path, code)
except AttributeError:
# sometimes, we get an error before self.path exists
self.log_message('<unknown URL> %s', code)
def log_message(self, format, *args):
if not self.server.log:
return
msg = "%s [%s] %s" % (self.address_string(),
self.log_date_time_string(),
format % args)
self.server.log_message (msg)
def setup(self):
SocketServer.StreamRequestHandler.setup(self)
# wrap the wfile with a class that will eat up "Broken pipe" errors
self.wfile = _SocketWriter(self.wfile)
def finish(self):
# if the other end breaks the connection, these operations will fail
try:
self.wfile.close()
except socket.error:
pass
try:
self.rfile.close()
except socket.error:
pass
def version_string(self):
return BaseHTTPServer.BaseHTTPRequestHandler.version_string(self) \
+ ' edna/' + __version__
class _SocketWriter:
"This class ignores 'Broken pipe' errors."
def __init__(self, wfile):
self.wfile = wfile
def __getattr__(self, name):
return getattr(self.wfile, name)
def write(self, buf):
try:
s_buf = str(buf)
return self.wfile.write(s_buf)
except IOError, v:
if v.errno == 32 or v.errno == 104:
raise ClientAbortedException
else:
# not a 'Broken pipe' or Connection reset by peer
# re-raise the error
raise
class ClientAbortedException(Exception):
pass
class _datablob:
def __init__(self, **args):
self.__dict__.update(args)
class empty_delegator:
"Delegate attrs to another object; fill in empty string for unknown attrs."
def __init__(self, ob):
self.ob = ob
def __getattr__(self, name):
if hasattr(self.ob, name):
return getattr(self.ob, name)
else:
return ''
class FileInfo:
"""Grab as much info as you can from the file given"""
def __init__(self, fullpath):
base, ext = os.path.splitext(fullpath)
ext = string.lower(ext)
if ext == '.ogg':
info = OggInfo(fullpath)
self.__dict__.update(info.__dict__)
else:
info = MP3Info.MP3Info(open(fullpath, 'rb'))
self.__dict__.update(info.__dict__)
self.total_time = info.mpeg.total_time;
self.filesize = info.mpeg.filesize2
self.bitrate = int(info.mpeg.bitrate)
self.samplerate = info.mpeg.samplerate/1000
self.mode = info.mpeg.mode
self.mode_extension = info.mpeg.mode_extension
# if hasattr(info, 'length'):
if self.total_time > 3600:
self.duration = '%d:%02d:%02d' % (int(self.total_time / 3600),
int(self.total_time / 60) % 60,
int(self.total_time) % 60)
elif self.total_time > 60:
self.duration = '%d:%02d' % (int(self.total_time / 60),
int(self.total_time) % 60)
else:
self.duration ='%02d' % int(self.total_time)
class OggInfo:
"""Extra information about an Ogg Vorbis file.
Uses ogg-python and vorbis-python from http://www.duke.edu/~ahc4/pyogg/.
Patch from Justin Erenkrantz <[email protected]>
"""
def __init__(self, name):
global oggSupport
# Setup the defaults
self.valid = 0
self.total_time = 0
self.samplerate = 'unkown'
self.bitrate = 'unkown'
self.mode = ''
self.mode_extension = ''
if oggSupport == 'no':
return
#
# Generic File Info
#
vf = ogg.vorbis.VorbisFile(name)
vc = vf.comment()
vi = vf.info()
# According to the docs, -1 means the current bitstream
self.samplerate = vi.rate
self.total_time = vf.time_total(-1)
self.bitrate = vf.bitrate(-1) / 1000
self.filesize = vf.raw_total(-1)/1024/1024
# recognized_comments = ('Artist', 'Album', 'Title', 'Version',
# 'Organization', 'Genre', 'Description',
# 'Date', 'Location', 'Copyright', 'Vendor')
for key, val in vc.items():
if key == 'TITLE':
self.title = val
elif key == 'ARTIST':
self.artist = val
elif key == 'ALBUM':
self.album = val
elif key == 'DATE':
self.year = val
elif key == 'GENRE':
self.genre = val
elif key == 'VENDOR':
self.vendor = val
elif key == 'TRACKNUMBER':
self.track = val
elif key == 'COMMENT':
self.comment = val
elif key == 'TRANSCODED':
self.transcoded = val
self.valid = 1
def _usable_file(fname):
return fname[0] != '.'
def sort_dir(d):
l = filter(_usable_file, os.listdir(d))
l.sort()
return l
def dot2int(dotaddr):
a, b, c, d = map(int, string.split(dotaddr, '.'))