2 #include <sys/resource.h>
5 #include "apachetools.h"
6 #include <opensrf/osrf_app_session.h>
7 #include <opensrf/osrf_system.h>
8 #include <opensrf/osrfConfig.h>
9 #include <opensrf/osrf_json.h>
10 #include <opensrf/osrf_cache.h>
12 #define MODULE_NAME "osrf_http_translator_module"
13 #define TRANSLATOR_CONFIG_FILE "OSRFTranslatorConfig"
14 #define TRANSLATOR_CONFIG_CTX "OSRFTranslatorConfigContext"
15 #define TRANSLATOR_CACHE_SERVER "OSRFTranslatorCacheServer"
16 #define DEFAULT_TRANSLATOR_CONFIG_CTX "gateway"
17 #define DEFAULT_TRANSLATOR_CONFIG_FILE "/openils/conf/opensrf_core.xml"
18 #define DEFAULT_TRANSLATOR_TIMEOUT 1200
20 #define MULTIPART_CONTENT_TYPE "multipart/x-mixed-replace;boundary=\"%s\""
21 #define JSON_CONTENT_TYPE "text/plain"
22 #define CACHE_TIME 300
24 #define OSRF_HTTP_HEADER_TO "X-OpenSRF-to"
25 #define OSRF_HTTP_HEADER_XID "X-OpenSRF-xid"
26 #define OSRF_HTTP_HEADER_FROM "X-OpenSRF-from"
27 #define OSRF_HTTP_HEADER_THREAD "X-OpenSRF-thread"
28 #define OSRF_HTTP_HEADER_TIMEOUT "X-OpenSRF-timeout"
29 #define OSRF_HTTP_HEADER_SERVICE "X-OpenSRF-service"
30 #define OSRF_HTTP_HEADER_MULTIPART "X-OpenSRF-multipart"
31 #define MAX_MSGS_PER_PACKET 256
33 char* configFile = DEFAULT_TRANSLATOR_CONFIG_FILE;
34 char* configCtx = DEFAULT_TRANSLATOR_CONFIG_CTX;
35 char* routerName = NULL;
36 char* domainName = NULL;
37 int osrfConnected = 0;
38 char recipientBuf[128];
39 char contentTypeBuf[80];
41 // for development only, writes to apache error log
42 static void _dbg(char* s, ...) {
44 fprintf(stderr, "%s\n", VA_BUF);
51 transport_client* handle;
55 const char* recipient;
58 const char* remoteHost;
68 * Constructs a new translator object based on the current apache
69 * request_rec. Reads the request body and headers.
71 static osrfHttpTranslator* osrfNewHttpTranslator(request_rec* apreq) {
72 osrfHttpTranslator* trans;
73 OSRF_MALLOC(trans, sizeof(osrfHttpTranslator));
76 trans->connectOnly = 0;
77 trans->disconnectOnly = 0;
78 trans->remoteHost = apreq->connection->remote_ip;
79 trans->messages = NULL;
81 /* load the message body */
82 osrfStringArray* params = apacheParseParms(apreq);
83 trans->body = apacheGetFirstParamValue(params, "osrf-msg");
84 osrfStringArrayFree(params);
86 /* load the request headers */
87 if (apr_table_get(apreq->headers_in, OSRF_HTTP_HEADER_XID) ) {
93 trans->handle = osrfSystemGetTransportClient();
94 trans->recipient = apr_table_get(apreq->headers_in, OSRF_HTTP_HEADER_TO);
95 trans->service = apr_table_get(apreq->headers_in, OSRF_HTTP_HEADER_SERVICE);
96 trans->thread = apr_table_get(apreq->headers_in, OSRF_HTTP_HEADER_THREAD); /* XXX create thread if necessary */
98 const char* timeout = apr_table_get(apreq->headers_in, OSRF_HTTP_HEADER_TIMEOUT);
100 trans->timeout = atoi(timeout);
102 trans->timeout = DEFAULT_TRANSLATOR_TIMEOUT;
104 const char* multipart = apr_table_get(apreq->headers_in, OSRF_HTTP_HEADER_MULTIPART);
105 if(multipart && !strcasecmp(multipart, "true"))
106 trans->multipart = 1;
108 trans->multipart = 0;
111 snprintf(buf, sizeof(buf), "%d%ld", getpid(), time(NULL));
112 trans->delim = md5sum(buf);
117 static void osrfHttpTranslatorFree(osrfHttpTranslator* trans) {
123 osrfListFree(trans->messages);
127 static void osrfHttpTranslatorDebug(osrfHttpTranslator* trans) {
128 _dbg("-----------------------------------");
129 _dbg("body = %s", trans->body);
130 _dbg("service = %s", trans->service);
131 _dbg("thread = %s", trans->thread);
132 _dbg("multipart = %d", trans->multipart);
133 _dbg("recipient = %s", trans->recipient);
137 * Determines the correct recipient address based on the requested
138 * service or recipient address.
140 static int osrfHttpTranslatorSetTo(osrfHttpTranslator* trans) {
142 jsonObject* sessionCache = NULL;
145 if(trans->recipient) {
146 osrfLogError(OSRF_LOG_MARK, "Specifying both SERVICE and TO are not allowed");
149 // service is specified, build a recipient address
150 // from the router, domain, and service
151 int size = snprintf(recipientBuf, 128, "%s@%s/%s", routerName, domainName, trans->service);
152 recipientBuf[size] = '\0';
153 osrfLogDebug(OSRF_LOG_MARK, "Set recipient to %s", recipientBuf);
154 trans->recipient = recipientBuf;
160 if(trans->recipient) {
161 sessionCache = osrfCacheGetObject(trans->thread);
164 char* ipAddr = jsonObjectGetString(jsonObjectGetKey(sessionCache, "ip"));
165 char* recipient = jsonObjectGetString(jsonObjectGetKey(sessionCache, "jid"));
167 // choosing a specific recipient address requires that the recipient and
168 // thread be cached on the server (so drone processes cannot be hijacked)
169 if(!strcmp(ipAddr, trans->remoteHost) && !strcmp(recipient, trans->recipient)) {
170 osrfLogDebug(OSRF_LOG_MARK, "Found cached session from host %s and recipient %s",
171 trans->remoteHost, trans->recipient);
173 trans->service = jsonObjectGetString(jsonObjectGetKey(sessionCache, "service"));
176 osrfLogError(OSRF_LOG_MARK,
177 "Session cache for thread %s does not match request", trans->thread);
180 osrfLogError(OSRF_LOG_MARK,
181 "attempt to send directly to %s without a session", trans->recipient);
184 osrfLogError(OSRF_LOG_MARK, "No SERVICE or RECIPIENT defined");
188 jsonObjectFree(sessionCache);
193 * Parses the request body and logs any REQUEST messages to the activity log
195 static int osrfHttpTranslatorParseRequest(osrfHttpTranslator* trans) {
197 osrfMessage* msgList[MAX_MSGS_PER_PACKET];
198 int numMsgs = osrf_message_deserialize(trans->body, msgList, MAX_MSGS_PER_PACKET);
199 osrfLogDebug(OSRF_LOG_MARK, "parsed %d opensrf messages in this packet", numMsgs);
206 if(msg->m_type == CONNECT) {
207 trans->connectOnly = 1;
210 if(msg->m_type == DISCONNECT) {
211 trans->disconnectOnly = 1;
216 // log request messages to the activity log
218 for(i = 0; i < numMsgs; i++) {
220 if(msg->m_type == REQUEST) {
222 jsonObject* params = msg->_params;
223 growing_buffer* act = buffer_init(128);
224 buffer_fadd(act, "[%s] [%s] %s %s", trans->remoteHost, "", trans->service, msg->method_name);
228 while((str = jsonObjectGetString(jsonObjectGetIndex(params, i++)))) {
230 OSRF_BUFFER_ADD(act, " ");
232 OSRF_BUFFER_ADD(act, ", ");
233 OSRF_BUFFER_ADD(act, str);
235 osrfLogActivity(OSRF_LOG_MARK, act->buf);
243 static int osrfHttpTranslatorCheckStatus(osrfHttpTranslator* trans, transport_message* msg) {
244 osrfMessage* omsgList[MAX_MSGS_PER_PACKET];
245 int numMsgs = osrf_message_deserialize(msg->body, omsgList, MAX_MSGS_PER_PACKET);
246 osrfLogDebug(OSRF_LOG_MARK, "parsed %d response messages", numMsgs);
247 if(numMsgs == 0) return 0;
249 osrfMessage* last = omsgList[numMsgs-1];
250 if(last->m_type == STATUS) {
251 if(last->status_code == OSRF_STATUS_TIMEOUT) {
252 osrfLogDebug(OSRF_LOG_MARK, "removing cached session on request timeout");
253 osrfCacheRemove(trans->thread);
256 // XXX hm, check for explicit status=COMPLETE message instead??
257 if(last->status_code != OSRF_STATUS_CONTINUE)
264 static void osrfHttpTranslatorInitHeaders(osrfHttpTranslator* trans, transport_message* msg) {
265 apr_table_set(trans->apreq->headers_out, OSRF_HTTP_HEADER_FROM, msg->sender);
266 apr_table_set(trans->apreq->headers_out, OSRF_HTTP_HEADER_THREAD, trans->thread);
267 if(trans->multipart) {
268 sprintf(contentTypeBuf, MULTIPART_CONTENT_TYPE, trans->delim);
269 contentTypeBuf[79] = '\0';
270 osrfLogDebug(OSRF_LOG_MARK, "content type %s : %s : %s", MULTIPART_CONTENT_TYPE, trans->delim, contentTypeBuf);
271 ap_set_content_type(trans->apreq, contentTypeBuf);
272 ap_rprintf(trans->apreq, "--%s\n", trans->delim);
274 ap_set_content_type(trans->apreq, JSON_CONTENT_TYPE);
278 static void osrfHttpTranslatorCacheSession(osrfHttpTranslator* trans) {
279 jsonObject* cacheObj = jsonNewObject(NULL);
280 jsonObjectSetKey(cacheObj, "ip", jsonNewObject(trans->remoteHost));
281 jsonObjectSetKey(cacheObj, "jid", jsonNewObject(trans->recipient));
282 jsonObjectSetKey(cacheObj, "service", jsonNewObject(trans->service));
283 osrfCachePutObject((char*) trans->thread, cacheObj, CACHE_TIME);
288 * Writes a single chunk of multipart/x-mixed-replace content
290 static void osrfHttpTranslatorWriteChunk(osrfHttpTranslator* trans, transport_message* msg) {
291 ap_rprintf(trans->apreq,
292 "Content-type: %s\n\n%s\n\n", JSON_CONTENT_TYPE, msg->body);
294 ap_rprintf(trans->apreq, "--%s--\n", trans->delim);
296 ap_rprintf(trans->apreq, "--%s\n", trans->delim);
297 ap_rflush(trans->apreq);
300 static int osrfHttpTranslatorProcess(osrfHttpTranslator* trans) {
301 if(trans->body == NULL)
302 return HTTP_BAD_REQUEST;
304 if(!osrfHttpTranslatorSetTo(trans))
305 return HTTP_BAD_REQUEST;
307 if(!osrfHttpTranslatorParseRequest(trans))
308 return HTTP_BAD_REQUEST;
310 while(client_recv(trans->handle, 0))
311 continue; // discard any old status messages in the recv queue
313 // send the message to the recipient
314 transport_message* tmsg = message_init(
315 trans->body, NULL, trans->thread, trans->recipient, NULL);
316 client_send_message(trans->handle, tmsg);
319 if(trans->disconnectOnly) {
320 osrfLogDebug(OSRF_LOG_MARK, "exiting early on disconnect");
324 // process the response from the opensrf service
326 while(!trans->complete) {
327 transport_message* msg = client_recv(trans->handle, trans->timeout);
329 if(trans->handle->error) {
330 osrfLogError(OSRF_LOG_MARK, "Transport error");
331 return HTTP_INTERNAL_SERVER_ERROR;
335 return HTTP_GATEWAY_TIME_OUT;
338 osrfLogError(OSRF_LOG_MARK, "XMPP message resulted in error code %d", msg->error_code);
339 return HTTP_NOT_FOUND;
342 if(!osrfHttpTranslatorCheckStatus(trans, msg))
346 osrfHttpTranslatorInitHeaders(trans, msg);
347 osrfHttpTranslatorCacheSession(trans);
351 if(trans->multipart) {
352 osrfHttpTranslatorWriteChunk(trans, msg);
353 if(trans->connectOnly)
357 trans->messages = osrfNewList();
358 osrfListPush(trans->messages, msg->body);
360 if(trans->complete || trans->connectOnly) {
361 growing_buffer* buf = buffer_init(128);
363 OSRF_BUFFER_ADD(buf, osrfListGetIndex(trans->messages, 0));
364 for(i = 1; i < trans->messages->size; i++) {
365 buffer_chomp(buf); // chomp off the closing array bracket
366 char* body = osrfListGetIndex(trans->messages, i);
367 char newbuf[strlen(body)];
368 sprintf(newbuf, body+1); // chomp off the opening array bracket
369 OSRF_BUFFER_ADD_CHAR(buf, ',');
370 OSRF_BUFFER_ADD(buf, newbuf);
373 ap_rputs(buf->buf, trans->apreq);
382 static void testConnection(request_rec* r) {
383 if(!osrfConnected || !osrfSystemGetTransportClient()) {
384 osrfLogError(OSRF_LOG_MARK, "We're not connected to OpenSRF");
385 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "We're not connected to OpenSRF");
386 usleep(100000); // .1 second to prevent process die/start overload
392 static apr_status_t childExit(void* data) {
393 osrf_system_shutdown();
397 static void childInit(apr_pool_t *p, server_rec *s) {
398 if(!osrfSystemBootstrapClientResc(configFile, configCtx, "translator")) {
399 ap_log_error( APLOG_MARK, APLOG_ERR, 0, s,
400 "Unable to Bootstrap OpenSRF Client with config %s..", configFile);
404 routerName = osrfConfigGetValue(NULL, "/router_name");
405 domainName = osrfConfigGetValue(NULL, "/domain");
406 // ---------------------
407 // XXX initialize the cache from the Apache settings
408 const char* servers[] = {"127.0.0.1:11211"};
409 osrfCacheInit(servers, 1, 86400);
410 // ---------------------
413 // at pool destroy time (= child exit time), cleanup
414 apr_pool_cleanup_register(p, NULL, childExit, NULL);
417 static int handler(request_rec *r) {
419 if(strcmp(r->handler, MODULE_NAME)) return DECLINED;
420 if(r->header_only) return stat;
422 r->allowed |= (AP_METHOD_BIT << M_GET);
423 r->allowed |= (AP_METHOD_BIT << M_POST);
425 osrfLogSetAppname("osrf_http_translator");
428 osrfHttpTranslator* trans = osrfNewHttpTranslator(r);
430 stat = osrfHttpTranslatorProcess(trans);
431 //osrfHttpTranslatorDebug(trans);
432 osrfLogInfo(OSRF_LOG_MARK, "translator resulted in status %d", stat);
434 osrfLogWarning(OSRF_LOG_MARK, "no message body to process");
436 osrfHttpTranslatorFree(trans);
441 static void registerHooks (apr_pool_t *p) {
442 ap_hook_handler(handler, NULL, NULL, APR_HOOK_MIDDLE);
443 ap_hook_child_init(childInit, NULL, NULL, APR_HOOK_MIDDLE);
447 module AP_MODULE_DECLARE_DATA osrf_http_translator_module = {
448 STANDARD20_MODULE_STUFF,