Page 1 of 2

etag??

Posted: Sat Feb 22, 2014 11:57 am
by Vegan
been looking at etags, for conditional http get

any suggestions?

Re: etag??

Posted: Sat Feb 22, 2014 5:41 pm
by requinix
Suggestions for what?

Re: etag??

Posted: Sat Feb 22, 2014 9:53 pm
by Vegan
dealing with the ability to tell a search bot, no change etc

Re: etag??

Posted: Sat Feb 22, 2014 11:34 pm
by requinix
Suggestions for how to use an ETag? Well, you should be familiar with the specifications for ETag, If-Match, and If-None-Match, and entity tags in general.
Generate the ETag for a resource, before executing the rest of the request, and decide what to do from there: a matching If-Match means to continue else 412 (and stop), while a matching If-None-Match means 412 else to continue. Make sure to send the ETag regardless.

Can you try writing, like, at least two or three sentences of what you want? Throwing a handful of words in a post doesn't do a whole lot.

Re: etag??

Posted: Tue Feb 25, 2014 3:49 pm
by Vegan
I was wondering, would a md5 hash do the job to notice if a given page is modified to deal with it

http://www.microsoft.com/search/Tools/default.aspx

Re: etag??

Posted: Tue Feb 25, 2014 5:13 pm
by requinix
Sure, hash of the filename + modification time should cover most the bases. It depends on your needs: is the modification time alone enough to identify a version of a page?

Re: etag??

Posted: Fri Feb 28, 2014 8:24 am
by Vegan
It would but I am not so sure how to massage that

I know how to display the modification date

Code: Select all

<?php echo "Last modified: " . date ("F d, Y (H:i:s)", getlastmod()); ?>
so how can i massage this into something the bot would like

Re: etag??

Posted: Fri Feb 28, 2014 1:06 pm
by requinix
Hash it.

Code: Select all

header("ETag: " . md5(date("YmdHis", getlastmod())));

Re: etag??

Posted: Sun Mar 02, 2014 12:29 am
by Vegan
thanks it now recognizes an etag as desired, but now how about the conditional GET, or is that more of a server issue?

Re: etag??

Posted: Sun Mar 02, 2014 12:39 am
by requinix
It's entirely server-side. If you're sending ETags then the client may send any of those If-*-Match headers I mentioned earlier (including a third one that I didn't list there). Check the links to see what you're supposed to do if you see them.

Re: etag??

Posted: Mon Mar 03, 2014 7:15 am
by Vegan
the bot check recognizes the etag generated fine, but its still balking at conditional get

so is the bot sending me an etag that i need to check?

Re: etag??

Posted: Mon Mar 03, 2014 12:59 pm
by requinix
Clients don't send ETags. Your server does. They send If-*-Match headers.

For example, if you reply with "ETag: abc123" then their next request may include "If-None-Match: abc123"; if it does match then you respond with a 412 and no content. Or maybe they send "If-Match: abc123", and you give a 412 if it doesn't match.

Re: etag??

Posted: Mon Mar 03, 2014 4:19 pm
by Vegan
maybe something like this?

Code: Select all

function isModified($mtime, $etag) {
    return !( (
        isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
        && 
        strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $mtime
    ) || (
        isset($_SERVER['HTTP_IF_NONE_MATCH'])
        && 
        $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
    ) ) ;
}

Re: etag??

Posted: Mon Mar 03, 2014 4:38 pm
by requinix
Yes, except you react differently for If-Modified-Since and If-None-Match: for the former you return a 304 Not Modified in case of failure and for the latter you return a 412. So you can't just bundle the two checks together like that.

Re: etag??

Posted: Tue Mar 04, 2014 12:53 pm
by Vegan
given I only want to tell a bot I am modified

isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])

so is the bot sending me an etag I need to handle?