From 0a8e16a7cbeed7af67e060a0b31076f6fe6e194d Mon Sep 17 00:00:00 2001 From: Maayan Glikser Date: Mon, 11 Jun 2018 23:08:12 +0300 Subject: [PATCH] Fix an issue where the browser throws an exception on responseXML When trying to use the XHR api set with a content type of 'text' the browser throws an error it is unable to read responseXML as the response isn't valid xml and the response type is not an empty string or 'document' as according to spec. this change was implemented due to inconsistent implementation across browsers and 3rd party network libraries which do parse xml responses even when the response type is not set according to the spec. In order to support all clients we now try to assign the responseXML property when possible but fail silently when we cannot. This will keep both behaviors consistent as incorrectly implemented clients will be able to read the XML response while still supporting the correctly implemented clients to not fail the request when trying to access responseXML on an invalid request. please see issues #86 #89 for more information. --- dist/xhook.js | 10 +++++++--- dist/xhook.min.js | 2 +- src/xhook.coffee | 8 +++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/dist/xhook.js b/dist/xhook.js index 05a9475..1df1cc5 100644 --- a/dist/xhook.js +++ b/dist/xhook.js @@ -1,6 +1,6 @@ // XHook - v1.4.8 - https://github.com/jpillora/xhook // Jaime Pillora - MIT Copyright 2018 -(function(undefined) { +(function(window,undefined) { var AFTER, BEFORE, COMMON_EVENTS, EventEmitter, FETCH, FIRE, FormData, NativeFetch, NativeFormData, NativeXMLHttp, OFF, ON, READY_STATE, UPLOAD_EVENTS, WINDOW, XHookFetchRequest, XHookFormData, XHookHttpRequest, XMLHTTP, convertHeaders, depricatedProp, document, fakeEvent, mergeObjects, msie, nullify, proxyEvents, slice, useragent, xhook, _base, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; @@ -333,7 +333,11 @@ XHookHttpRequest = WINDOW[XMLHTTP] = function() { if (!xhr.responseType || xhr.responseType === "text") { response.text = xhr.responseText; response.data = xhr.responseText; - response.xml = xhr.responseXML; + try { + response.xml = xhr.responseXML; + } catch (_error) { + + } } else if (xhr.responseType === "document") { response.xml = xhr.responseXML; response.data = xhr.responseXML; @@ -693,4 +697,4 @@ if (typeof define === "function" && define.amd) { WINDOW.xhook = xhook; } -}.call(this)); \ No newline at end of file +}.call(this,window)); \ No newline at end of file diff --git a/dist/xhook.min.js b/dist/xhook.min.js index 0f0d7ec..ee9aefb 100644 --- a/dist/xhook.min.js +++ b/dist/xhook.min.js @@ -1,3 +1,3 @@ // XHook - v1.4.8 - https://github.com/jpillora/xhook // Jaime Pillora - MIT Copyright 2018 -(function(a,b){var c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H=[].indexOf||function(a){for(var b=0,c=this.length;b=0||(f=f===b?d[a].length:f,d[a].splice(f,0,c))},c[m]=function(a,c){var f;if(a===b)return void(d={});c===b&&(d[a]=[]),f=e(a).indexOf(c),f!==-1&&e(a).splice(f,1)},c[h]=function(){var b,d,f,g,h,i,j,k;for(b=D(arguments),d=b.shift(),a||(b[0]=z(b[0],y(d))),g=c["on"+d],g&&g.apply(c,b),k=e(d).concat(e("*")),f=i=0,j=k.length;i2)throw"invalid hook";return F[n](d,a,b)},F[c]=function(a,b){if(a.length<2||a.length>3)throw"invalid hook";return F[n](c,a,b)},F.enable=function(){q[u]=t,"function"==typeof r&&(q[g]=r),k&&(q[i]=s)},F.disable=function(){q[u]=F[u],q[g]=F[g],k&&(q[i]=k)},v=F.headers=function(a,b){var c,d,e,f,g,h,i,j,k;switch(null==b&&(b={}),typeof a){case"object":d=[];for(e in a)g=a[e],f=e.toLowerCase(),d.push(f+":\t"+g);return d.join("\n")+"\n";case"string":for(d=a.split("\n"),i=0,j=d.length;ib&&b<4;)k[o]=++b,1===b&&k[h]("loadstart",{}),2===b&&G(),4===b&&(G(),E()),k[h]("readystatechange",{}),4===b&&(t.async===!1?g():setTimeout(g,0))},g=function(){l||k[h]("load",{}),k[h]("loadend",{}),l&&(k[o]=0)},b=0,x=function(a){var b,d;if(4!==a)return void i(a);b=F.listeners(c),(d=function(){var a;return b.length?(a=b.shift(),2===a.length?(a(t,w),d()):3===a.length&&t.async?a(t,w,d):d()):i(4)})()},k=t.xhr=f(),I.onreadystatechange=function(a){try{2===I[o]&&r()}catch(a){}4===I[o]&&(D=!1,r(),q()),x(I[o])},m=function(){l=!0},k[n]("error",m),k[n]("timeout",m),k[n]("abort",m),k[n]("progress",function(){b<3?x(3):k[h]("readystatechange",{})}),("withCredentials"in I||F.addWithCredentials)&&(k.withCredentials=!1),k.status=0,L=e.concat(p);for(J=0,K=L.length;J=0||(f=f===b?d[a].length:f,d[a].splice(f,0,c))},c[m]=function(a,c){var f;if(a===b)return void(d={});c===b&&(d[a]=[]),f=e(a).indexOf(c),f!==-1&&e(a).splice(f,1)},c[h]=function(){var b,d,f,g,h,i,j,k;for(b=D(arguments),d=b.shift(),a||(b[0]=z(b[0],y(d))),g=c["on"+d],g&&g.apply(c,b),k=e(d).concat(e("*")),f=i=0,j=k.length;i2)throw"invalid hook";return F[n](d,a,b)},F[c]=function(a,b){if(a.length<2||a.length>3)throw"invalid hook";return F[n](c,a,b)},F.enable=function(){q[u]=t,"function"==typeof r&&(q[g]=r),k&&(q[i]=s)},F.disable=function(){q[u]=F[u],q[g]=F[g],k&&(q[i]=k)},v=F.headers=function(a,b){var c,d,e,f,g,h,i,j,k;switch(null==b&&(b={}),typeof a){case"object":d=[];for(e in a)g=a[e],f=e.toLowerCase(),d.push(f+":\t"+g);return d.join("\n")+"\n";case"string":for(d=a.split("\n"),i=0,j=d.length;ib&&b<4;)k[o]=++b,1===b&&k[h]("loadstart",{}),2===b&&G(),4===b&&(G(),E()),k[h]("readystatechange",{}),4===b&&(t.async===!1?g():setTimeout(g,0))},g=function(){l||k[h]("load",{}),k[h]("loadend",{}),l&&(k[o]=0)},b=0,x=function(a){var b,d;if(4!==a)return void i(a);b=F.listeners(c),(d=function(){var a;return b.length?(a=b.shift(),2===a.length?(a(t,w),d()):3===a.length&&t.async?a(t,w,d):d()):i(4)})()},k=t.xhr=f(),I.onreadystatechange=function(a){try{2===I[o]&&r()}catch(a){}4===I[o]&&(D=!1,r(),q()),x(I[o])},m=function(){l=!0},k[n]("error",m),k[n]("timeout",m),k[n]("abort",m),k[n]("progress",function(){b<3?x(3):k[h]("readystatechange",{})}),("withCredentials"in I||F.addWithCredentials)&&(k.withCredentials=!1),k.status=0,L=e.concat(p);for(J=0,K=L.length;J if !xhr.responseType or xhr.responseType is "text" response.text = xhr.responseText response.data = xhr.responseText - response.xml = xhr.responseXML + try + response.xml = xhr.responseXML + catch + # unable to set responseXML due to response type, we attempt to assign responseXML + # when the type is text even though it's against the spec due to several libraries + # and browser vendors who allow this behavior. causing these requests to fail when + # xhook is installed on a page. else if xhr.responseType is "document" response.xml = xhr.responseXML response.data = xhr.responseXML