W3C home > Mailing lists > Public > public-webapps@w3.org > July to September 2009

Re: HTTP status code equivalents for file:// operations - compat with xhr

From: Michael A. Puls II <shadow2531@gmail.com>
Date: Wed, 19 Aug 2009 23:50:18 -0400
To: "Jonas Sicking" <jonas@sicking.cc>
Cc: arun@mozilla.com, public-webapps@w3.org
Message-ID: <op.uyxzl3qg1ejg13@sandra-svwliu01>
On Wed, 19 Aug 2009 15:18:03 -0400, Michael A. Puls II  
<shadow2531@gmail.com> wrote:

> However note that I'm not sure that failing to parse should fire an
> error event. For someone only caring about responseText things loaded
> just fine. (I think I actually changed firefox from what you describe
> to what I describe sometime after Firefox 2, for this very reason).
> The "XML" part of the name "XMLHttpRequest" was never very true.
>  Ah, understood. I think I could work around that by checking  
> responseXML to see if it's right. If it's not, perhaps I could manually  
> dispatch an 'error', if there's a way to manually, create and init a  
> progress event so that you can dispatch it. Will play with that.

I can do this in FF:

function loadXMLFile(fileURI, load_callback, error_callback) {
     var req = new XMLHttpRequest();
     req.addEventListener("load", function(e) {
         var doc = e.target.responseXML;
         if (doc instanceof XMLDocument && doc.documentElement instanceof  
Element &&  
doc.getElementsByTagNameNS("http://www.mozilla.org/newlayout/xml/parsererror.xml",  
"parsererror").length === 0) {
             load_callback(e);
         } else {
             var evt = document.createEvent("ProgressEvent");
             evt.initProgressEvent("error", false, false,  
e.lengthComputable, e.loaded, e.total);
             evt.message = "XML Parse Errror";
             req.dispatchEvent(evt);
         }
     }, false);
     req.addEventListener("error", function(e) {
         if (typeof e.message !== "string") {
             e.message = "Unknown error - Perhaps cross-origin violation";
         }
         error_callback(e);
     }, false);
     try {
         req.open("GET", fileURI);
         req.send();
     } catch (err) {
         var evt = document.createEvent("ProgressEvent");
         evt.initProgressEvent("error", false, false, 0, 0, 0);
         evt.message = "Some kind of Access error - Could be File Not  
Found, Access Denied or something else";
         req.dispatchEvent(evt);
     }
}

// Usage:
loadXMLFile("test.xml", function(e) {
     alert(e.target.responseXML);
}, function(e) {
     alert(e.message);
});

Then, XHR2 would be wrapped again for a loadTextFile().

Anyway, that at least makes the load and error callbacks fire only/exactly  
when they should and still passes in a ProgressEvent, but with a .message  
getter with some error info. Still not possible to get the error info  
specific enough for each case though.

With _lookupGetter__ and __defineGetter__, I might be able to make .status  
return 200, 404 and 403 for some of the conditions if I wanted.

In the try{} part, I could create an anchor element, set its href to  
fileURI so that it's resolved and compare it to the document's location to  
make sure the requested file is in the document's directory or one of its  
subdirectories. That way, I could at least detect that particular security  
violation and fire 'error' for it.

But, all of this is just trying to force what XHR and ProgressEvents were  
not even closely designed to do. Wrapped like that, it is at least a  
little bit closer to DOM3 L&S.

-- 
Michael
Received on Thursday, 20 August 2009 03:51:01 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 18:49:33 GMT