Downloading files from web if updated

Hi,

Might be that I’m missing something here, but I seem to be hitting walls on implementing a basic “check if web resource has updated and download if needed”-function. Basically I want to use basic http features and submit “If-Modified-Since”-header and then check if I got 200 or 304 as response.

Now network.download kind of makes this in one call. Except that it seems to clobber the file in case of 304 with an empty one. I’d really like the file to update only if 200 is returned.

Next network.request is fine in returning the status code. But the event listener only gets the response data without knowing the file name / request. So I’d need to make sure I only have one request open, which kind of defeats the purpose.

Last LuaSocket allows you to go all the way and do everything you want, except it’s synchronous by default :slight_smile: I know you there are ways to go around this, but in general I’d like to keep it simpler.

Ideas, comments, thoughts?

Matias [import]uid: 46570 topic_id: 24516 reply_id: 324516[/import]

What I do is:

network.request( "http://www.website.com/myfile.txt", "GET", getNumListener )  

(myfile is a text file with a number in it)

and the getNumLIstener function goes:

local function getNumListener( event )  
 if ( event.isError ) then  
 --debugprint( "Network error!")  
 else  
 local theResponse = event.response  
 local z = 0  
 --debugprint ("the response from the server is: ", theResponse)  
  
 if theResponse \> "9" then  
 -- first character isn't a number, so its a 404 message  
  
 else  
 --woohoo.. now use theResponse for whatever  
 end  
 end  
end  

[import]uid: 108660 topic_id: 24516 reply_id: 99237[/import]

Thanks for the idea. Have you noticed that Ansca included the HTTP status code in event.status for network.request starting from build 744?

After further investigation, there is definitely some bug involved. I tried to do a solution where I would use network.download to get the file to Temporary directory and then copy it to Documents if the response is OK. I couldn’t make sense why it behaved so strangely so I started to use a network monitor to check what the request & response is. And depending whether I use request/download and whether I set headers or not, the request might be never fired at all. I.e. there is the response event with isError=true, but the http-request is never made.

[blockcode]
function net_response(event)
if (event.isError) then
print("response error: "…event.response)
else
print("response OK: "…event.response)
end
end

url = “http://frostygate.com/hello.txt
last_modified = os.date("!%a, %d %b %Y %H:%M:%S GMT")
params = {
headers = {
[“If-Modified-Since”] = last_modified,
}
}
network.request(url, “GET”, net_response, nil) – works OK
–network.request(url, “GET”, net_response, params) – no request in traffic monitor, returns with isError
–network.download(url, “GET”, net_response, nil, “hello.txt”, system.TemporaryDirectory) – no request in traffic monitor, no response event triggered
–network.download(url, “GET”, net_response, params, “hello.txt”, system.TemporaryDirectory)
[/blockcode] [import]uid: 46570 topic_id: 24516 reply_id: 99469[/import]