Greetings. I have been implementing an app that uses caching and I am setting the if-modified-since header to test if the response code is 200 or 304.
For some reason when it returns 304 status code response, it never calls the on_success block. Instead it calls both the on_complete and on_failure blocks.
Is this the desired action for 304 responses?
Here is an example of the code I am using.
#!/usr/bin/env ruby
require 'rubygems'
require 'curb'
require 'uri'
# Define URL
url = "http://www.google.com/intl/en_ALL/images/logo.gif"
easy = Curl::Easy.new URI.escape(url) do |curl|
# Set header for cache
curl.headers['If-Modified-Since'] = "Wed, 07 Jun 2006 19:38:24 GMT"
curl.headers['Cache-Control'] = "max-age=0"
# On successful download (ie a response was returned from the web server)
curl.on_success do |response|
print "We have success. The response code is #{response.response_code}\n"
end
curl.on_complete do |response|
print "We have complete. The response code is #{response.response_code}\n"
end
curl.on_failure do |response, err|
print "We have failure. The response code is #{response.response_code}\n"
print "Error is: #{err.inspect}\n"
end
end
easy.verbose = true
easy.perform
The output:
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.157.104... * Connected to www.google.com (74.125.157.104) port 80 (#0)
> GET /intl/en_ALL/images/logo.gif HTTP/1.1
Host: www.google.com
Accept: */*
If-Modified-Since: Wed, 07 Jun 2006 19:38:24 GMT
Cache-Control: max-age=0
< HTTP/1.1 304 Not Modified
< Last-Modified: Wed, 07 Jun 2006 19:38:24 GMT
< X-Content-Type-Options: nosniff
< Date: Mon, 02 Nov 2009 15:34:55 GMT
< Server: gws
< X-XSS-Protection: 0
<
* Expire cleared
* Connection #0 to host www.google.com left intact
We have complete. The response code is 304
We have failure. The response code is 304
Error is: [Curl::Err::CurlError, "Unknown error result from libcurl"]
I looked in the C code to see where that error gets set and it looks like it is the default code if no other curl error codes match. Why are 304 responses returning an error code from Curl?
Just as an aside, if I don't set caching headers, here is the 200 response
* About to connect() to www.google.com port 80 (#0)
* Trying 74.125.157.103... * Connected to www.google.com (74.125.157.103) port 80 (#0)
> GET /intl/en_ALL/images/logo.gif HTTP/1.1
Host: www.google.com
Accept: */*
< HTTP/1.1 200 OK
< Content-Type: image/gif
< Last-Modified: Wed, 07 Jun 2006 19:38:24 GMT
< Date: Mon, 02 Nov 2009 15:33:53 GMT
< Expires: Tue, 02 Nov 2010 15:33:53 GMT
< X-Content-Type-Options: nosniff
< Server: gws
< Content-Length: 8558
< Cache-Control: public, max-age=31536000
< Age: 85
< X-XSS-Protection: 0
<
* Expire cleared
* Connection #0 to host www.google.com left intact
We have complete. The response code is 200
We have success. The response code is 200
Any ideas?
Thanks,
Wes