MozillaZine

XMLHttpRequest and cache...

Talk about add-ons and extension development.
heavensdoor
 
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post Posted March 26th, 2006, 2:56 am

Hi all.
I need to download a document in background.
I use XMLHttpRequest of course... it works ok but if the doc is in the cache it loads from there and I don't wish this. I need always a fresh copy every time. How can I do this ?

Extra info:
- I don't wish to put in the URL extra arguments ( like '&' + d.getTime() ... )
- The pragma & cache-control seem to be useless...

Here's my code:

Code: Select all
XHRequest = new XMLHttpRequest();
XHRequest.onload  = OnLoad;
XHRequest.onerror = OnError;
XHRequest.open( 'GET', sURL, true );
// XHRequest.setRequestHeader( 'Expires', 'Sat, 1 Jan 2005 01:00:00 GMT' );
// XHRequest.setRequestHeader( 'Last-Modified', '-1' );
XHRequest.setRequestHeader( 'Pragma', 'no-cache' );
XHRequest.setRequestHeader( 'Cache-Control', 'no-cache, must-revalidate' );
XHRequest.send( null );


I always got the cached version of the document...
Any comments is appreciated!
Bye bye,
-Mat-

asqueella
 
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post Posted March 26th, 2006, 3:59 am

You could always use necko interfaces directly, there's a flag on nsIRequest allowing you specify that the request must bypass cache.

heavensdoor
 
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post Posted March 26th, 2006, 4:06 am

asqueella wrote:You could always use necko interfaces directly, there's a flag on nsIRequest allowing you specify that the request must bypass cache.

Can you provide me some lines?
It would help a lot...

I saw what the Reload button does to bypass the cache... ( nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE ) ... but I don't have a browser object nor an hidden frame to load the content and I don't wish this...

doron
 
Posts: 935
Joined: November 4th, 2002, 4:50 pm

Post Posted March 26th, 2006, 12:48 pm

THe reason I think is that the server needs to send that in the response - setting no-cache on a request is probably silly.

Changing the server to send the no-cache header is what you need.
If you see a marquee, clap your hands!

heavensdoor
 
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post Posted March 26th, 2006, 1:26 pm

doron wrote:THe reason I think is that the server needs to send that in the response - setting no-cache on a request is probably silly.
Changing the server to send the no-cache header is what you need.

Nice point of view :)
But... in this way, how could the browser obtain a refresh of the current document bypassing the cache? It doesn't change anything on the server-side...

asqueella
 
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post Posted March 27th, 2006, 6:34 am

Doron reacted to your confused example in the first post. There certainly is a way to bypass cache from browser code, but it's not done through setting request headers. I'm not willing to write a complete example of using necko interfaces, but you should be able to find the code you need by looking at references and examples for nsIChannel/nsIRequest. (Also lxr.mozilla.org is your friend.)

richwklein
 
Posts: 331
Joined: November 24th, 2002, 8:20 pm
Location: Iowa

Post Posted March 27th, 2006, 7:25 am

you can use the xmlhttprequest to bypass the cache. After calling the open method on the request, you need to do an instanceOf(nsIchannel) then set the load flags.
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a>
<a href="http://tipbar.mozdev.org">Tip of the Day</a>
<a href="http://urlnav.mozdev.org">Location Navigator</a>
<a href="http://finder.mozdev.org">Finder</a>
<a href="http://rsszilla.mozdev.org">RSSzilla</a>

heavensdoor
 
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post Posted March 27th, 2006, 12:21 pm

asqueella wrote:Doron reacted to your confused example in the first post.

Confused example... :)
BTW nothing wrong with Doron's post.
I explained my point of view.

asqueella wrote:I'm not willing to write a complete example of using necko interfaces, but you should be able to find the code you need by looking at references and examples for nsIChannel/nsIRequest. (Also lxr.mozilla.org is your friend.)

I didn't asked a complete example but some lines of code because my experience in this area is low.
And my experiments actually don't work.

BTW Don't worry, you don't need to find the solution for me.
I'll find my way. I only asked.

heavensdoor
 
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post Posted March 27th, 2006, 12:24 pm

richwklein wrote:you can use the xmlhttprequest to bypass the cache. After calling the open method on the request, you need to do an instanceOf(nsIchannel) then set the load flags.

I tried to access to the load flags throught XMLHttpRequest.channel with no good results...
But perhaps... you show me something useful... mmm...

heavensdoor
 
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post Posted March 27th, 2006, 12:54 pm

asqueella wrote:but you should be able to find the code you need by looking at references and examples for nsIChannel/nsIRequest

Got it.
From Venkman ext, in the mozilla source:

Code: Select all
var serv = Components.classes["@mozilla.org/network/io-service;1"].getService( Components.interfaces.nsIIOService );
var chan = serv.newChannel( sURL, null, null );
chan.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
var instream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
instream.init( chan.open() );
var result = "";
var avail;
while( ( avail = instream.available() ) > 0 )
    result += instream.read( avail );

( sync version )

richwklein
 
Posts: 331
Joined: November 24th, 2002, 8:20 pm
Location: Iowa

Post Posted March 27th, 2006, 12:59 pm

You have to make sure and access the channel after open is called.
Here is basically what we do in Forecastfox

Code: Select all
var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Components.interfaces.nsIXMLHttpRequest);     
request.open("GET", query, true);   
request.channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
if (request.channel instanceof Components.interfaces.nsISupportsPriority)
  request.channel.priority = Components.interfaces.nsISupportsPriority.PRIORITY_LOWEST;
request.send(null);
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a>
<a href="http://tipbar.mozdev.org">Tip of the Day</a>
<a href="http://urlnav.mozdev.org">Location Navigator</a>
<a href="http://finder.mozdev.org">Finder</a>
<a href="http://rsszilla.mozdev.org">RSSzilla</a>

heavensdoor
 
Posts: 16
Joined: February 22nd, 2006, 7:46 am
Location: Italy

Post Posted March 27th, 2006, 1:25 pm

richwklein wrote:You have to make sure and access the channel after open is called.
Here is basically what we do in Forecastfox

Great. It works like a charm :)
( my eng is not very good but this expression should be right in this context :) )

However, is there a special reason to low the priority level of the request... ?
I was looking <a href="http://developer.mozilla.org/en/docs/Changing_the_Priority_of_HTTP_Requests">here</a>... but... mmm.

Thank you very much Rich!

richwklein
 
Posts: 331
Joined: November 24th, 2002, 8:20 pm
Location: Iowa

Post Posted March 28th, 2006, 7:44 am

We set a lower priority to try and not interfer with page loads, but really no other reason.
My Extensions:
<a href="http://forecastfox.mozdev.org">Forecastfox</a>
<a href="http://tipbar.mozdev.org">Tip of the Day</a>
<a href="http://urlnav.mozdev.org">Location Navigator</a>
<a href="http://finder.mozdev.org">Finder</a>
<a href="http://rsszilla.mozdev.org">RSSzilla</a>

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 0 guests