1
6
from LTSPAgent.plugin import Plugin
2
7
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
3
8
from SocketServer import ThreadingMixIn
4
import threading, json, urlparse, socket
6
11
class jsonHandler(BaseHTTPRequestHandler):
7
12
def log_message(self, format, *args):
11
request=self.path.split('/')
16
request = self.path.split('/')
13
18
if len(request) != 3 or request[2] != "json":
14
19
self.send_response(404)
19
24
if not plugin in self.server.plugins:
20
25
self.send_response(404)
24
if not hasattr(self.server.plugins[plugin],'json_handler'):
29
if not hasattr(self.server.plugins[plugin], 'json_handler'):
25
30
self.send_response(404)
30
length=self.headers.getheader('content-length','0')
31
content=self.rfile.read(int(length))
32
post=urlparse.parse_qs(content)
35
length = self.headers.getheader('content-length', '0')
36
content = self.rfile.read(int(length))
37
post = urlparse.parse_qs(content)
33
38
if "query" in post:
34
query=json.loads(post['query'][0])
39
query = json.loads(post['query'][0])
36
41
self.send_response(404)
49
client=self.headers.getheader('X-Forwarded-For',self.client_address[0])
54
client = self.headers.getheader('X-Forwarded-For',
55
self.client_address[0])
51
57
self.send_response(200)
53
self.server.LOGGER.debug("Remote call on %s with params %s", self.server.plugins[plugin], str(query))
59
self.server.LOGGER.debug("Remote call on %s with params %s",
60
self.server.plugins[plugin], str(query))
55
self.wfile.write(json.dumps(getattr(self.server.plugins[plugin],'json_handler')(query,client)))
62
self.wfile.write(json.dumps(getattr(self.server.plugins[plugin],
63
'json_handler')(query, client)))
56
64
self.wfile.write('\n')
58
66
self.server.LOGGER.info("Lost connection with %s" % client)
62
71
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
63
72
"""Handle requests in a separate thread."""
64
73
def __init__(self, server_address, SecureXMLRPCRequestHandler):
71
80
HTTPServer.__init__(self, server_address, SecureXMLRPCRequestHandler)
73
83
class jsonserverThread(threading.Thread):
75
85
server = ThreadedHTTPServer(self.connection, jsonHandler)
76
server.plugins=self.plugins
77
server.LOGGER=self.LOGGER
78
self.LOGGER.info("Serving HTTP/JSON on %s port %s", self.connection[0],self.connection[1])
86
server.plugins = self.plugins
87
server.LOGGER = self.LOGGER
88
self.LOGGER.info("Serving HTTP/JSON on %s port %s",
89
self.connection[0], self.connection[1])
79
90
server.serve_forever()
81
93
class jsonlink(Plugin):
82
94
"""Export another plugin's functions over JSON"""
84
96
def init_plugin(self):
85
jsonserver=jsonserverThread()
86
jsonserver.plugins=self.plugins
87
jsonserver.connection=(self.get_config_path(self.config,'general','bindaddr'), int(self.get_config_path(self.config,'general','bindport')))
88
jsonserver.LOGGER=self.LOGGER
97
jsonserver = jsonserverThread()
98
jsonserver.plugins = self.plugins
99
jsonserver.connection = (self.get_config_path(
100
self.config, 'general', 'bindaddr'),
101
int(self.get_config_path(
102
self.config, 'general', 'bindport')))
103
jsonserver.LOGGER = self.LOGGER
89
104
jsonserver.start()
91
106
Plugin.init_plugin(self)