Strange double-download with Safari

I’m trying to log downloads of CarrierWave resources hosted at S3, so I
have a controller method that performs a redirect to the remote_url, and
creates a new database record to store the request details. But when I
access this endpoint with Safari, I am seeing a strange double log for
the request. Here’s what the request looks like in the logs:

I, [2015-08-09T18:22:01.463646 #44996] INFO – : Started GET
“/sources/28803-facsimile-pdf-cobbett-selections-from-cobbetts-political-works-vol-2/download”
for 173.161.197.5 at 2015-08-09 18:22:01 +0000
I, [2015-08-09T18:22:01.465244 #44996] INFO – : Processing by
SourcesController#download as HTML
I, [2015-08-09T18:22:01.465311 #44996] INFO – : Parameters:
{“id”=>“28803-facsimile-pdf-cobbett-selections-from-cobbetts-political-works-vol-2”}
I, [2015-08-09T18:22:01.468033 #44996] INFO – : Redirected to
http://lf-oll.s3.amazonaws.com/titles/2689/Cobbett_1627-02_Bk.pdf
I, [2015-08-09T18:22:01.471834 #44996] INFO – : Completed 403
Forbidden in 6ms (ActiveRecord: 1.1ms)
I, [2015-08-09T18:22:01.690815 #44996] INFO – : Started GET
“/sources/28803-facsimile-pdf-cobbett-selections-from-cobbetts-political-works-vol-2/download”
for 173.161.197.5 at 2015-08-09 18:22:01 +0000
I, [2015-08-09T18:22:01.692262 #44996] INFO – : Processing by
SourcesController#download as HTML
I, [2015-08-09T18:22:01.692324 #44996] INFO – : Parameters:
{“id”=>“28803-facsimile-pdf-cobbett-selections-from-cobbetts-political-works-vol-2”}
I, [2015-08-09T18:22:01.694572 #44996] INFO – : Redirected to
http://lf-oll.s3.amazonaws.com/titles/2689/Cobbett_1627-02_Bk.pdf
I, [2015-08-09T18:22:01.697804 #44996] INFO – : Completed 302 Found in
5ms (ActiveRecord: 1.2ms)

And the result is two rows in my database, one for the 403, and the
other for the 302.

My suspicion is that Safari is doing some introspection on the file to
determine its file type, since I know that it doesn’t trust headers.
Does anyone have any other ideas?

Thanks,

Walter

On 9 August 2015 at 19:39, Walter Lee D. [email protected] wrote:

I, [2015-08-09T18:22:01.694572 #44996] INFO – : Redirected to
http://lf-oll.s3.amazonaws.com/titles/2689/Cobbett_1627-02_Bk.pdf
I, [2015-08-09T18:22:01.697804 #44996] INFO – : Completed 302 Found in 5ms
(ActiveRecord: 1.2ms)

And the result is two rows in my database, one for the 403, and the other for
the 302.

My suspicion is that Safari is doing some introspection on the file to determine
its file type, since I know that it doesn’t trust headers. Does anyone have any
other ideas?

I don’t know exactly why it is happening, but the golden rule is never
change the database from a GET. Otherwise you leave yourself open to
exactly the sort of problem you are seeing. Change it to POST and the
problem will disappear.

Colin