]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/javascript/opensrf_xhr.js
LP1198983 disable multipart/mixed for Firefox
[OpenSRF.git] / src / javascript / opensrf_xhr.js
1 /* -----------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Bill Erickson <erickson@esilibrary.com>
4  *  
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.
9  * 
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  * ----------------------------------------------------------------------- */
15
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';
25
26
27 OpenSRF.XHRequest = function(osrf_msg, args) {
28     this.message = osrf_msg;
29     this.args = args;
30     try {
31         this.xreq =  new XMLHttpRequest();
32     } catch(e) {
33         try { 
34             this.xreq = new ActiveXObject("Msxml2.XMLHTTP"); 
35         } catch (e2) {
36             this.xreq = new ActiveXObject("Microsoft.XMLHTTP"); 
37         }
38     }
39 };
40
41 OpenSRF.XHRequest.prototype.send = function() {
42     var xhr_req = this;
43     var xreq = this.xreq;
44     
45     if(this.args.timeout) {
46         /* this is a standard blocking (non-multipart) call */
47         xreq.open('POST', OSRF_HTTP_TRANSLATOR, false);
48
49     } else {
50
51         /**
52          * multi-part messages are deprecated.  Eventually this will go away.
53          * For now, continue allowing the Evergreen staff client to use
54          * multi-part messages. */
55         if (!navigator.userAgent.match(/open_ils_staff_client/)) {
56             /* standard asynchronous call */
57             xreq.onreadystatechange = function() {
58                 if(xreq.readyState == 4)
59                     xhr_req.core_handler();
60             };
61             xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
62
63         } else {
64
65             /* asynchronous multipart call */
66             xreq.multipart = true;
67             xreq.onload = function(evt) {xhr_req.core_handler();};
68             xreq.open('POST', OSRF_HTTP_TRANSLATOR, true);
69             xreq.setRequestHeader(OSRF_HTTP_HEADER_MULTIPART, 'true');
70
71             /* multipart requests do not pass the status info to the onload if there 
72                is no new data to load.  Capture the status on the readystate handler */
73             xreq.onreadystatechange = function() {
74                 if(xreq.readyState == 4 && xreq.status >= 400)
75                     xhr_req.transport_error_handler();
76             };
77         }
78     }
79
80     xreq.setRequestHeader('Content-Type', OSRF_POST_CONTENT_TYPE);
81     xreq.setRequestHeader(OSRF_HTTP_HEADER_THREAD, this.args.thread);
82     if(this.args.rcpt) {
83         xreq.setRequestHeader(OSRF_HTTP_HEADER_TO, this.args.rcpt);
84     } else {
85         xreq.setRequestHeader(OSRF_HTTP_HEADER_SERVICE, this.args.rcpt_service);
86     }
87
88     var post = 'osrf-msg=' + encodeURIComponent(js2JSON([this.message.serialize()]));
89     xreq.send(post);
90
91     if(this.args.timeout) /* this was a blocking call, manually run the handler */
92         this.core_handler();
93
94     return this;
95 };
96
97 OpenSRF.XHRequest.prototype.core_handler = function() {
98     sender = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_FROM);
99     thread = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_THREAD);
100     json = this.xreq.responseText;
101     stat = this.xreq.status;
102
103     if(stat >= 400) 
104         return this.transport_error_handler();
105
106     OpenSRF.Stack.push(
107         new OpenSRF.NetMessage(null, sender, thread, json),
108         {
109             onresponse : this.args.onresponse,
110             oncomplete : this.args.oncomplete,
111             onerror : this.args.onerror,
112             onmethoderror : this.method_error_handler()
113         }
114     );
115 };
116
117
118 OpenSRF.XHRequest.prototype.method_error_handler = function() {
119     var xhr = this;
120     return function(req, status, status_text) {
121         if(xhr.args.onmethoderror) 
122             xhr.args.onmethoderror(req, status, status_text);
123         if(xhr.args.onerror)  
124             xhr.args.onerror(xhr.message, xhr.args.rcpt || xhr.args.rcpt_service, xhr.args.thread);
125     };
126 };
127
128 OpenSRF.XHRequest.prototype.transport_error_handler = function() {
129     if(this.args.ontransporterror) {
130         this.args.ontransporterror(this.xreq);
131     }
132     if(this.args.onerror) {
133         this.args.onerror(this.message, this.args.rcpt || this.args.rcpt_service, this.args.thread);
134     }
135 };
136
137