1 import os, time, md5, random
2 from mod_python import apache, util
10 from osrf.const import *
11 from osrf.net import get_network_handle
16 Proof of concept OpenSRF-HTTP multipart streaming gateway.
18 Example Apache mod_python config:
20 <Location /osrf-http-translator>
22 PythonPath "['/path/to/osrf-python'] + sys.path"
23 PythonHandler osrf.http_translator
24 PythonOption OSRF_CONFIG /path/to/opensrf_core.xml
25 PythonOption OSRF_CONFIG_CONTEXT gateway
32 OSRF_HTTP_HEADER_TO = 'X-OpenSRF-to'
33 OSRF_HTTP_HEADER_XID = 'X-OpenSRF-thread'
34 OSRF_HTTP_HEADER_FROM = 'X-OpenSRF-from'
35 OSRF_HTTP_HEADER_THREAD = 'X-OpenSRF-thread'
36 OSRF_HTTP_HEADER_TIMEOUT = 'X-OpenSRF-timeout'
37 OSRF_HTTP_HEADER_SERVICE = 'X-OpenSRF-service'
38 OSRF_HTTP_HEADER_MULTIPART = 'X-OpenSRF-multipart'
40 MULTIPART_CONTENT_TYPE = 'multipart/x-mixed-replace;boundary="%s"'
41 JSON_CONTENT_TYPE = 'text/plain'
47 # If DEBUG_WRITE = True, all data sent to the client is also written
48 # to stderr (apache error log)
53 sys.stderr.write("%s\n\n" % str(msg))
59 ''' At time of writing, mod_python doesn't support a child_init handler,
60 so this function is called once per process to initialize
61 the opensrf connection '''
63 global INIT_COMPLETE, ROUTER_NAME, OSRF_DOMAIN
67 ops = req.get_options()
68 conf = ops['OSRF_CONFIG']
69 ctxt = ops.get('OSRF_CONFIG_CONTEXT') or 'opensrf'
70 osrf.system.connect(conf, ctxt)
72 ROUTER_NAME = osrf.conf.get('router_name')
73 OSRF_DOMAIN = osrf.conf.get('domains.domain')
76 servers = osrf.set.get('cache.global.servers.server')
77 if not isinstance(servers, list):
79 osrf.cache.CacheClient.connect(servers)
83 ''' Create the translator and tell it to process the request. '''
85 return HTTPTranslator(req).process()
87 class HTTPTranslator(object):
88 def __init__(self, apreq):
95 post = util.parse_qsl(apreq.read(int(apreq.headers_in['Content-length'])))
96 self.body = [d for d in post if d[0] == 'osrf-msg'][0][1]
102 self.complete = False
103 self.handle = osrf.net.get_network_handle()
104 self.handle.set_receive_callback(None)
106 self.recipient = apreq.headers_in.get(OSRF_HTTP_HEADER_TO)
107 self.service = apreq.headers_in.get(OSRF_HTTP_HEADER_SERVICE)
108 self.thread = apreq.headers_in.get(OSRF_HTTP_HEADER_THREAD) or \
109 "%s%s" % (os.getpid(), time.time())
110 self.timeout = apreq.headers_in.get(OSRF_HTTP_HEADER_TIMEOUT) or 1200
111 self.multipart = str( \
112 apreq.headers_in.get(OSRF_HTTP_HEADER_MULTIPART)).lower() == 'true'
113 self.disconnect_only = False
115 # generate a random multipart delimiter
117 mpart.update("%f%d%d" % (time.time(), os.getpid(), \
118 random.randint(100, 10000000)))
119 self.delim = mpart.hexdigest()
120 self.remote_host = self.apreq.get_remote_host(apache.REMOTE_NOLOOKUP)
121 self.cache = osrf.cache.CacheClient()
126 if self.apreq.header_only:
129 return apache.HTTP_BAD_REQUEST
130 if not self.set_to_addr():
131 return apache.HTTP_BAD_REQUEST
132 if not self.parse_request():
133 return apache.HTTP_BAD_REQUEST
135 while self.handle.recv(0):
136 pass # drop stale messages
139 net_msg = NetworkMessage(recipient=self.recipient, thread=self.thread, \
141 self.handle.send(net_msg)
143 if self.disconnect_only:
144 osrf.log.logDebug("exiting early on DISCONNECT")
148 while not self.complete:
150 net_msg = self.handle.recv(self.timeout)
152 return apache.GATEWAY_TIME_OUT
154 if not self.check_status(net_msg):
158 self.init_headers(net_msg)
162 self.respond_chunk(net_msg)
164 self.messages.append(net_msg.body)
166 # condense the sets of arrays into a single array of messages
168 json = self.messages.pop(0)
169 while len(self.messages) > 0:
170 msg = self.messages.pop(0)
171 json = "%s,%s" % (json[0:len(json)-1], msg[1:])
173 self.write("%s" % json)
178 def parse_request(self):
180 If this is solely a DISCONNECT message, we set self.disconnect_only
182 @return True if the body parses correctly, False otherwise
184 osrf_msgs = osrf.json.to_object(self.body)
188 if len(osrf_msgs) == 1 and \
189 osrf_msgs[0].type() == OSRF_MESSAGE_TYPE_DISCONNECT:
190 self.disconnect_only = True
195 def set_to_addr(self):
196 ''' Determines the TO address. Returns false if
197 the address is missing or ambiguous.
198 Also returns false if an explicit TO is specified and the
199 thread/IP/TO combination is not found in the session cache
203 osrf.log.log_warn("specifying both SERVICE and TO is not allowed")
205 self.recipient = "%s@%s/%s" % \
206 (ROUTER_NAME, OSRF_DOMAIN, self.service)
210 # If the client specifies a specific TO address, verify it's
211 # the same address that was cached with the previous request.
212 obj = self.cache.get(self.thread)
213 if obj and obj['ip'] == self.remote_host and \
214 obj['jid'] == self.recipient:
216 osrf.log.log_warn("client [%s] attempted to send directly "
217 "[%s] without a session" % (self.remote_host, self.recipient))
221 def init_headers(self, net_msg):
222 self.apreq.headers_out[OSRF_HTTP_HEADER_FROM] = net_msg.sender
224 self.apreq.content_type = MULTIPART_CONTENT_TYPE % self.delim
225 self.write("--%s\n" % self.delim)
227 self.apreq.content_type = JSON_CONTENT_TYPE
228 self.cache.put(self.thread, \
229 {'ip':self.remote_host, 'jid': net_msg.sender}, CACHE_TIME)
231 osrf.log.logDebug("caching session [%s] for host [%s] and server "
232 " drone [%s]" % (self.thread, self.remote_host, net_msg.sender))
236 def check_status(self, net_msg):
237 ''' Checks the status of the server response.
238 If we received a timeout message, we drop it.
239 if it's any other non-continue status, we mark this session as
240 complete and carry on.
241 @return False if there is no data to return to the caller
242 (dropped message, eg. timeout), True otherwise '''
244 osrf_msgs = osrf.json.to_object(net_msg.body)
245 last_msg = osrf_msgs.pop()
247 if last_msg.type() == OSRF_MESSAGE_TYPE_STATUS:
248 code = int(last_msg.payload().statusCode())
250 if code == OSRF_STATUS_TIMEOUT:
251 osrf.log.logDebug("removing cached session [%s] and "
252 "dropping TIMEOUT message" % net_msg.thread)
253 self.cache.delete(net_msg.thread)
256 if code != OSRF_STATUS_CONTINUE:
262 def respond_chunk(self, resp):
263 ''' Writes a single multipart-delimited chunk of data '''
265 self.write("Content-type: %s\n\n" % JSON_CONTENT_TYPE)
266 self.write("%s\n\n" % resp.body)
268 self.write("--%s--\n" % self.delim)
270 self.write("--%s\n" % self.delim)
273 def write(self, msg):
274 ''' Writes data to the client stream. '''
277 sys.stderr.write(msg)
279 self.apreq.write(msg)