1 /* -----------------------------------------------------------------------
2 * Copyright (C) 2008 Georgia Public Library Service
3 * Bill Erickson <erickson@esilibrary.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * ----------------------------------------------------------------------- */
16 var OSRF_HTTP_HEADER_TO = 'X-OpenSRF-to';
17 var OSRF_HTTP_HEADER_XID = 'X-OpenSRF-xid';
18 var OSRF_HTTP_HEADER_FROM = 'X-OpenSRF-from';
19 var OSRF_HTTP_HEADER_THREAD = 'X-OpenSRF-thread';
20 var OSRF_HTTP_HEADER_TIMEOUT = 'X-OpenSRF-timeout';
21 var OSRF_HTTP_HEADER_SERVICE = 'X-OpenSRF-service';
22 var OSRF_HTTP_HEADER_MULTIPART = 'X-OpenSRF-multipart';
23 var OSRF_HTTP_TRANSLATOR = '/osrf-http-translator'; /* XXX config */
24 var OSRF_POST_CONTENT_TYPE = 'application/x-www-form-urlencoded';
27 OpenSRF.XHRequest = function(osrf_msg, args) {
28 this.message = osrf_msg;
31 this.xreq = new XMLHttpRequest();
34 this.xreq = new ActiveXObject("Msxml2.XMLHTTP");
36 this.xreq = new ActiveXObject("Microsoft.XMLHTTP");
42 OpenSRF.XHRequest.prototype.send = function() {
46 if(this.args.timeout) {
47 /* this is a standard blocking (non-multipart) call */
48 xreq.open('POST', OSRF_HTTP_TRANSLATOR, false);
52 if(!navigator.userAgent.match(/mozilla/i)) {
54 /* standard asynchronous call */
55 xreq.onreadystatechange = function() {
56 if(xreq.readyState == 4)
57 xhr_req.core_handler();
59 xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
63 /* asynchronous multipart call */
64 xreq.multipart = true;
65 xreq.onload = function(evt) {xhr_req.core_handler();}
66 xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
67 xreq.setRequestHeader(OSRF_HTTP_HEADER_MULTIPART, 'true');
69 /* multipart requests do not pass the status info to the onload if there
70 is no new data to load. Capture the status on the readystate handler */
71 xreq.onreadystatechange = function() {
72 if(xreq.readyState == 4 && xreq.status >= 400)
73 xhr_req.transport_error_handler();
78 xreq.setRequestHeader('Content-Type', OSRF_POST_CONTENT_TYPE);
79 xreq.setRequestHeader(OSRF_HTTP_HEADER_THREAD, this.args.thread);
81 xreq.setRequestHeader(OSRF_HTTP_HEADER_TO, this.args.rcpt);
83 xreq.setRequestHeader(OSRF_HTTP_HEADER_SERVICE, this.args.rcpt_service);
85 var post = 'osrf-msg=' + encodeURIComponent(js2JSON([this.message.serialize()]));
88 if(this.args.timeout) /* this was a blocking call, manually run the handler */
94 OpenSRF.XHRequest.prototype.core_handler = function() {
95 sender = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_FROM);
96 thread = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_THREAD);
97 json = this.xreq.responseText;
98 stat = this.xreq.status;
101 return this.transport_error_handler();
104 new OpenSRF.NetMessage(null, sender, thread, json),
106 onresponse : this.args.onresponse,
107 oncomplete : this.args.oncomplete,
108 onerror : this.args.onerror,
109 onmethoderror : this.method_error_handler()
115 OpenSRF.XHRequest.prototype.method_error_handler = function() {
117 return function(req, status, status_text) {
118 if(xhr.args.onmethoderror)
119 xhr.args.onmethoderror(req, status, status_text);
121 xhr.args.onerror(xhr.message, xhr.args.rcpt || xhr.args.rcpt_service, xhr.args.thread);
125 OpenSRF.XHRequest.prototype.transport_error_handler = function() {
126 if(this.args.ontransporterror)
127 this.args.ontransporterror(this.xreq);
128 if(this.args.onerror)
129 this.args.onerror(this.message, this.args.rcpt || this.args.rcpt_service, this.args.thread);