-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpsrv.py
99 lines (81 loc) · 2.65 KB
/
httpsrv.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
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import cs.hier
import json
import base64
import re
import sys
try:
from urllib.parse import unquote
except ImportError:
from urllib import unquote
class JSONRPC(HTTPServer):
def __init__(self,bindaddr,base=None):
''' Start a server bound to the supplied bindaddr, a tuple of (ip-addr, port).
base is the DocumentRoot for requests and defaults to "/".
'''
if base is None: base='/'
self.rpcBaseURL=base
HTTPServer.__init__(self,bindaddr,JSONRPCHandler)
basic_authorization_re=re.compile(r'^\s*basic\s+(\S+)',re.I)
def testAuthPWNam(user, password):
import pwd
try:
pw=pwd.getpwnam(user)
except KeyError:
return False
import crypt
pwcrypt = pw[1]
salt = pwcrypt[:2]
return crypt.crypt(password, salt) == pwcrypt
class RequestHandler(BaseHTTPRequestHandler):
def __init__(self,rq,cliaddr,srv):
BaseHTTPRequestHandler.__init__(self,rq,cliaddr,srv)
def getAuth(self, testAuth=None):
if testAuth is None:
global testAuthPWNam
testAuth=testAuthPWNam
authhdr=self.headers.get('AUTHORIZATION')
if authhdr is None:
return None
global basic_authorization_re
m=basic_authorization_re.match(authhdr)
if not m:
return None
try:
cred=base64.b64decode(m.group(1))
except TypeError:
return None
userid, password = cred.split(":",1)
if not testAuth(userid, password):
return None
return userid
def needAuth(self,realm):
self.wfile.write("HTTP/1.0 401 Auth needed.\r\nContent-Type: text/plain\r\nWWW-Authenticate: Basic realm=\"%s\"\r\nConnection: close\r\n\r\nTesting.\r\n" % realm)
class JSONRPCHandler(RequestHandler):
def __init__(self,rq,cliaddr,srv):
RequestHandler.__init__(self,rq,cliaddr,srv)
def reject(self,code,complaint):
self.wfile.write("%03d %s\r\nContent-Type: text/plain\r\n\r\n%03d %s\r\n"
% (code,complaint,code,complaint))
def do_GET(self):
path=self.path
root=self.server.rpcBaseURL
if not path.startswith(root):
self.reject(500, "path not inside root prefix: %s" % root)
return
path=path[len(root):]
slndx=path.find('/')
if slndx < 1 or not path[:slndx].isdigit():
self.reject(500, "missing sequence number")
return
seq=int(path[:slndx])
self.headers['Content-Type']="application/x-javascript\r\n"
jsontxt=path[slndx+1:]
jsontxt=unquote(jsontxt)
(args,unparsed)=cs.hier.tok(jsontxt)
rpcres=self.server.rpc(self,args)
if rpcres is None:
return
cb, result = rpcres
jscode="%s(%d,%s);\r\n" % (cb,seq,dumps(result,4));
self.wfile.write(jscode);