wplayer

Check-in [b1aec2fb65]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Bringup
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:b1aec2fb659f6ef849a4ab92a161cffde340c2cbc1a64aae269678d7f824e52b
User & Date: ajv-899-334-8894@vsta.org 2016-01-26 22:41:20
Context
2016-01-27
17:00
Bringup; files now play check-in: b9c87d983f user: ajv-899-334-8894@vsta.org tags: master, trunk
2016-01-26
22:41
Bringup check-in: b1aec2fb65 user: ajv-899-334-8894@vsta.org tags: master, trunk
17:24
First pass, coding and chore lib dev check-in: 6d4a2228cc user: ajv-899-334-8894@vsta.org tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to chore/config.py.

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	f = open(cfg_file, "r")
	res = {}
	for t in self.mults0:
	    res[t] = []
	curdict = None

	self.lnum = 0
	import pdb
	pdb.set_trace()
	for l in f:
	    self.lnum += 1

	    # Note if it's an indented line (and thus annotation
	    #  applying to the current element)
	    indented = l[0].isspace()








<
<







88
89
90
91
92
93
94


95
96
97
98
99
100
101
	f = open(cfg_file, "r")
	res = {}
	for t in self.mults0:
	    res[t] = []
	curdict = None

	self.lnum = 0


	for l in f:
	    self.lnum += 1

	    # Note if it's an indented line (and thus annotation
	    #  applying to the current element)
	    indented = l[0].isspace()

Changes to chore/handlers.py.

1
2
3
4
5
6
7
8
9
10
11
12
..
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
...
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
...
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
#
# www.py
#	Handling for actual web server bits
#
import base64, threading, socket, sys, json
from BaseHTTPServer import BaseHTTPRequestHandler

# Return value for a digit (hex)
def digval(c):
    if c.isdigit():
	return ord(c) - ord('0')
    c = c.lower()
................................................................................
#
# An actual app will inherit this and add app-specific dispatchers.
#
# self.server gets set to our app's root state
class Chore_Handler(BaseHTTPRequestHandler):

    # Default config
    def __init__(self):

	# /js/..., /imgs/..., and such just serve files
	self.lits = ("js", "imgs", "css", "html")

	# Code which, in turn, tries to dispatch ops
	# (Each is (op, fn), e.g., ("GET", self.do_get1))
	self.dispatchers = [ ("GET", self.base_get), ]





	# Default title
	self.title = "web server"












    # Log HTTP-level errors
    # (or pass, to quiet them)
    def send_error(self, code, msg):
	print "HTTP error", code, msg

    # Hook to set up SSL
    def setup(self):
	self.connection = self.request
	self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)

    # Find & dispatch handler
    def dispatch(self, op, *args):

	# Always decode <path>?<opts>
	self.options()

	# Find handler
	for tup in self.dispatchers:
	    if tup[0] == op:
		dispatched,res = tup[1](*args)
................................................................................

	# Make sure we know its MIME type
	tup = self.get_mtype(fn)
	if tup is None:
	    # Unknown file type
	    self.send_error(404, "File not found")
	    return None
	isbin,mtyp = self.get_mtype(fn)

	try:
	    f = open(fn, "r")
	except:
	    self.send_error(404, "File not found")
	    return None
	buf = f.read()
	f.close()

	buf = self.send_result(buf, mtyp, binary=isbin)
	return buf

    # Should be overridden
    def send_top(self):
	self.send_error(404, "File not found")
	return None

    # Common code to strip and decode options
    # Also bursts the path to self.paths[]
    def options(self):
	p = self.path

	# Options?
	if "?" in p:
................................................................................
    # Intercept after request is parsed; apply HTTP authentication
    def parse_request(self):

	# Make sure it's well-formed
	res = BaseHTTPRequestHandler.parse_request(self)
	if not res:
	    return False






	# Apply digest authentication?
	auth = self.headers.getheader('Authorization')
	if auth is None:
	    return self.send401("Authentication required\r\n")

	# Do we accept it?
	auth = base64.decodestring(auth[6:])
	tup = auth.split(":")
	if len(tup) != 2:
	    return self.send401("Malformed authentication response\r\n")
	webxmpp = self.server
	user = self.authenticate(tup[0], tup[1])
	if user is None:
	    return self.send401("Incorrect username or password\r\n")

	# Record User instance of this connection
	self.user = user





|







 







|







>
>
>
>




>
>
>
>
>
>
>
>
>
>
>













>







 







|












<
<
<
<
<







 







>
>
>
>
>











<







1
2
3
4
5
6
7
8
9
10
11
12
..
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
...
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
...
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
#
# www.py
#	Handling for actual web server bits
#
import base64, socket, sys, json, os
from BaseHTTPServer import BaseHTTPRequestHandler

# Return value for a digit (hex)
def digval(c):
    if c.isdigit():
	return ord(c) - ord('0')
    c = c.lower()
................................................................................
#
# An actual app will inherit this and add app-specific dispatchers.
#
# self.server gets set to our app's root state
class Chore_Handler(BaseHTTPRequestHandler):

    # Default config
    def __init__(self, conn, addr, approot, inits):

	# /js/..., /imgs/..., and such just serve files
	self.lits = ("js", "imgs", "css", "html")

	# Code which, in turn, tries to dispatch ops
	# (Each is (op, fn), e.g., ("GET", self.do_get1))
	self.dispatchers = [ ("GET", self.base_get), ]

	# Add on others (mixins)
	for i in inits:
	    i(self)

	# Default title
	self.title = "web server"

	# Hook here if you want HTTP authen
	self.authentication = None

	# These get dicts if there are options
	self.vals = self.rvals = None

	# This both init's, and runs the web service
	# (BTW, this sucks.  Break out instance creation and
	#  service start--always.)
	BaseHTTPRequestHandler.__init__(self, conn, addr, approot)

    # Log HTTP-level errors
    # (or pass, to quiet them)
    def send_error(self, code, msg):
	print "HTTP error", code, msg

    # Hook to set up SSL
    def setup(self):
	self.connection = self.request
	self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)

    # Find & dispatch handler
    def dispatch(self, op, *args):

	# Always decode <path>?<opts>
	self.options()

	# Find handler
	for tup in self.dispatchers:
	    if tup[0] == op:
		dispatched,res = tup[1](*args)
................................................................................

	# Make sure we know its MIME type
	tup = self.get_mtype(fn)
	if tup is None:
	    # Unknown file type
	    self.send_error(404, "File not found")
	    return None
	isbin,mtyp = tup

	try:
	    f = open(fn, "r")
	except:
	    self.send_error(404, "File not found")
	    return None
	buf = f.read()
	f.close()

	buf = self.send_result(buf, mtyp, binary=isbin)
	return buf






    # Common code to strip and decode options
    # Also bursts the path to self.paths[]
    def options(self):
	p = self.path

	# Options?
	if "?" in p:
................................................................................
    # Intercept after request is parsed; apply HTTP authentication
    def parse_request(self):

	# Make sure it's well-formed
	res = BaseHTTPRequestHandler.parse_request(self)
	if not res:
	    return False

	# TBD, actual authentication
	if self.authentication is None:
	    return True
	approot = self.server

	# Apply digest authentication?
	auth = self.headers.getheader('Authorization')
	if auth is None:
	    return self.send401("Authentication required\r\n")

	# Do we accept it?
	auth = base64.decodestring(auth[6:])
	tup = auth.split(":")
	if len(tup) != 2:
	    return self.send401("Malformed authentication response\r\n")

	user = self.authenticate(tup[0], tup[1])
	if user is None:
	    return self.send401("Incorrect username or password\r\n")

	# Record User instance of this connection
	self.user = user

Changes to chore/server.py.

1
2
3
4

5
6
7
8
9
10
11
..
52
53
54
55
56
57
58
59
60
61
62
63
#
# server.py
#	Code for firing up a server
#

import www

class Server(object):
    def __init__(self, config, apphandler):
	# Our HTTP server(s)
	self.servers = []
	self.apphandler = apphandler
................................................................................

	# Run HTTP.  All activity is rooted from HTTP transactions
	#  coming in.
	for (proto, cfg) in self.config["serve"]:
	    port = cfg.get("port")
	    if not port:
		port = 443 if (proto == "https") else 80
	    web = www.HTTP(proto, port, cfg.get("ifname"),
		self.apphandler, self)
	    self.servers.append(web)
	    web.run()





>







 







<
|



1
2
3
4
5
6
7
8
9
10
11
12
..
53
54
55
56
57
58
59

60
61
62
63
#
# server.py
#	Code for firing up a server
#
import threading
import www

class Server(object):
    def __init__(self, config, apphandler):
	# Our HTTP server(s)
	self.servers = []
	self.apphandler = apphandler
................................................................................

	# Run HTTP.  All activity is rooted from HTTP transactions
	#  coming in.
	for (proto, cfg) in self.config["serve"]:
	    port = cfg.get("port")
	    if not port:
		port = 443 if (proto == "https") else 80

	    web = www.HTTP(proto, cfg, self.apphandler, self)
	    self.servers.append(web)
	    web.run()

Changes to chore/www.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#
# www.py
#	Code to run a web server on a specific port and protocol
#
import socket
import fcntl
import struct

# Get listen IP address given interface
def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
	res = socket.inet_ntoa(fcntl.ioctl(
	    s.fileno(),




|
<
<







1
2
3
4
5


6
7
8
9
10
11
12
#
# www.py
#	Code to run a web server on a specific port and protocol
#
import sys, socket, fcntl, struct, threading



# Get listen IP address given interface
def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
	res = socket.inet_ntoa(fcntl.ioctl(
	    s.fileno(),

Changes to get.py.

38
39
40
41
42
43
44

45














46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

61
	# Back it goes, as a JSON-encoded buffer
	return self.send_json(res)

    # GET dispatcher
    # See if it's a configured path, and serve file or dir
    def send_path(self):
	app = self.server

	pp = self.paths














	for tup in app.config["files"]:
	    if tup[0] == pp[0]:
		break
	else:
	    # Nope, we don't dispatch this path
	    return False,None

	# Their requested path, starting from here
	prefix = tup[1]["path"]
	path = os.path.join(prefix, os.path.join(pp[1:]))

	# File?  Or dir?
	st = os.stat(path)
	if stat.S_ISDIR(st):
	    return self._send_dir(path)

	return self.send_files(path)







>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
|













|
>
|
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
	# Back it goes, as a JSON-encoded buffer
	return self.send_json(res)

    # GET dispatcher
    # See if it's a configured path, and serve file or dir
    def send_path(self):
	app = self.server
	cpaths = app.config["files"]
	pp = self.paths

	# We serve /media/prefix/blah/blah...
	if pp[0] != "media":
	    return False,None
	del pp[0]

	# Root; list configured prefixes themselves
	if not pp:
	    res = {"files": (),
		"dirs": sorted(tup[0] for tup in cpaths)}
	    buf = self.send_json(res)
	    return True,buf

	# Find the next path element
	for tup in cpaths:
	    if tup[0] == pp[0]:
		break
	else:
	    # Nope, we don't dispatch this path
	    return False,None

	# Their requested path, starting from here
	prefix = tup[1]["path"]
	path = os.path.join(prefix, os.path.join(pp[1:]))

	# File?  Or dir?
	st = os.stat(path)
	if stat.S_ISDIR(st):
	    buf = self._send_dir(path)
	    return True,buf
	return True,self.send_files(path)

Changes to js/player.js.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
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
...
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
// The HTML5 player
var player = document.getElementById("player");

// File/dir browsing
var browser = document.getElementById("browser");

// Show the queue of tracks
var pqueue = document.getelementById("pqueue");

// The playlist queue
var playlist = [];

// Current browsing path, and how "deep" we are down into it
var path = "";
var pdepth = 0;
................................................................................

// Do we have something on the turntable right now?
var playing = false;

// Answer back on dir contents
function gotDir(req) {
    // Connection progress
    if ((req == null) || (req.readyState != 4)) {
        return;
    }

    // Decode JSON
    pcontents = JSON.parse(req.responseText);

    // Paint file browsing window
    var res = '<button onclick="clickup();">Go Up</button><br>'

    // Directories (if any)
    if (pcontents.dirs.length > 0) {
	res += '<b>Directories</b>:<br>';
	var didx = 0;

	for (d in pcontents.dirs) {
	    res += '<button onclick="dir(" + didx.toString() + '");>"
	    res += d;
	    res += '</button>';
	    didx += 1;
	}
    }

    // Files (if any)
    if (pcontents.files.length > 0) {
	res += '<b>Files</b>:<br>';
	var fidx = 0;

	for (f in pcontents.dirs) {
	    res += '<button onclick="file(" + fidx.toString() + '");>"
	    res += f;
	    res += '</button>';
	    vidx += 1;
	}
    }

    // Replace the HTML
................................................................................
	player.removeEventListener("ended", playNext);
	playing = false;
	nowplaying.textContent = '';
    }
}

// Add file to playlist (or play if playlist empty)
function file(fidx) {
    // Assemble full filename
    var fname = path + "/" + pcontents.files[fidx]
    if (!playing) {
	nowplaying.textContent = player.src = fname;
	player.addEventListener("ended", playNext);
	player.play();
	playing = true;
................................................................................
    } else {
	playlist.push(fname);
	paintQueue();
    }
}

// Go up a level
function clickup() {
    if (depth == 0) {
	return;
    }
    var idx = path.lastIndexOf("/");
    path = path.slice(0, idx);
    depth -= 1;
    curdir();
}

// Push to dir
function dir(didx) {
    path = path + "/" + pcontents.dirs[didx];
    depth += 1;
    curdir();
}

// Initial browser window update
curdir();







|







 







|







|




|
>
|
|









|
>
|
|







 







|







 







|
|




|




|

|





7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
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
...
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
// The HTML5 player
var player = document.getElementById("player");

// File/dir browsing
var browser = document.getElementById("browser");

// Show the queue of tracks
var pqueue = document.getElementById("pqueue");

// The playlist queue
var playlist = [];

// Current browsing path, and how "deep" we are down into it
var path = "";
var pdepth = 0;
................................................................................

// Do we have something on the turntable right now?
var playing = false;

// Answer back on dir contents
function gotDir(req) {
    // Connection progress
    if ((req.readyState != 4) || (req.status != 200)) {
        return;
    }

    // Decode JSON
    pcontents = JSON.parse(req.responseText);

    // Paint file browsing window
    var res = '<hr><button onclick="go_up();">Go Up</button><br>'

    // Directories (if any)
    if (pcontents.dirs.length > 0) {
	res += '<b>Directories</b>:<br>';
	var d, didx = 0;
	while (didx < pcontents.dirs.length) {
	    d = pcontents.dirs[didx];
	    res += '<button onclick="go_dir(' + didx.toString() + ');">'
	    res += d;
	    res += '</button>';
	    didx += 1;
	}
    }

    // Files (if any)
    if (pcontents.files.length > 0) {
	res += '<b>Files</b>:<br>';
	var f, fidx = 0;
	while (fidx < pcontents.files.length) {
	    f = pcontents.files[fidx];
	    res += '<button onclick="go_file(' + fidx.toString() + ');">'
	    res += f;
	    res += '</button>';
	    vidx += 1;
	}
    }

    // Replace the HTML
................................................................................
	player.removeEventListener("ended", playNext);
	playing = false;
	nowplaying.textContent = '';
    }
}

// Add file to playlist (or play if playlist empty)
function go_file(fidx) {
    // Assemble full filename
    var fname = path + "/" + pcontents.files[fidx]
    if (!playing) {
	nowplaying.textContent = player.src = fname;
	player.addEventListener("ended", playNext);
	player.play();
	playing = true;
................................................................................
    } else {
	playlist.push(fname);
	paintQueue();
    }
}

// Go up a level
function go_up() {
    if (pdepth == 0) {
	return;
    }
    var idx = path.lastIndexOf("/");
    path = path.slice(0, idx);
    pdepth -= 1;
    curdir();
}

// Push to dir
function go_dir(didx) {
    path = path + "/" + pcontents.dirs[didx];
    pdepth += 1;
    curdir();
}

// Initial browser window update
curdir();

Changes to main.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# main.py
#	Main driver for WWW-Usenet interface
#
import sys, threading, time
import chore
from get import GET_Mixin

# Tie our various handlers together
class App_Handler(chore.handlers.Chore_Handler, GET_Mixin):
    def __init__(self):
	core.handlers.Chore_Handler.__init__(self)
	GET_Mixin.__init__(self)

# Load our configuration file
#
# This includes configuring our config file elements,
#  then processing the supplied file.
def load_cfg(fn):





|





|
|
|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# main.py
#	Main driver for WWW-Usenet interface
#
import sys, time
import chore
from get import GET_Mixin

# Tie our various handlers together
class App_Handler(chore.handlers.Chore_Handler, GET_Mixin):
    def __init__(self, conn, tup, approot):
	chore.handlers.Chore_Handler.__init__(self, conn, tup, approot,
	    (GET_Mixin.__init__,))

# Load our configuration file
#
# This includes configuring our config file elements,
#  then processing the supplied file.
def load_cfg(fn):