Question

I'm downloading a JAR file, and would like to utilize If-Modified-Since so I don't get the whole file if I don't need it, but for some reason my vanilla Apache (afaik) isn't returning the 304 correctly.

This is from wireshark:

GET /whatever.jar HTTP/1.1
If-Modified-Since: Sat, 04 Jan 2014 21:46:26 GMT
User-Agent: Jakarta Commons-HttpClient/3.1
Host: example.com

HTTP/1.1 200 OK
Date: Sat, 04 Jan 2014 20:32:31 GMT
Server: Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.8e DAV/2 mod_jk/1.2.26 PHP/5.3.6 SVN/1.4.4
Last-Modified: Sat, 04 Jan 2014 19:13:14 GMT
ETag: "b6c037-1ddad9f-d17a6680"
Accept-Ranges: bytes
Content-Length: 31305119
Vary: User-Agent
Content-Type: text/plain

... [bunch of bytes] ...

There aren't other headers I need to specify, is there? Am I missing a module that Apache needs in order to read this header correctly?

Any other thoughts or suggestions?

Here is my Java code, for reference:

    File jarFile = new File(filePath);
    GetMethod get = new GetMethod(downloadUrl);

    Date lastModified = new Date(jarFile.lastModified());
    get.setRequestHeader("If-Modified-Since", DateUtil.formatDate(lastModified));

    HttpClient client = new HttpClient();
    int code = client.executeMethod(get);

UPDATE: Solution

The If-Modified-Date needed to exactly match the server, and I achieved this by explicitly setting the lastModifiedDate on the downloaded file:

String serverModified = get.getResponseHeader("Last-Modified").getValue();
jarFile.setLastModified(DateUtil.parseDate(serverModified).getTime());

After doing this, subsequent calls would not download the file.

Was it helpful?

Solution

In order to use the "If-Modified-Since" header, you must send an identical header value as the "Last-Modified" header, that is Sat, 04 Jan 2014 19:13:14 GMT != Sat, 04 Jan 2014 21:46:26 GMT. Apache cannot guarantee the file wasn't modified and given a past time on purpose (perhaps through a version control roll-back).

If you want, you may check the "Last-Modified" header on the client side, by using a HeadMethod first to avoid "getting" the resource if it hasn't been modified. Then you would use a "GetMethod" if it has been modified.

See RFC2616 - Section 9, "HTTP/1.1: Method Definitions" for more.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top