Re: generate an etag header that apache can subsequently use, how?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



hey Jochem,
as far as I can see, this should work for you:
<?php
$stats = stat('/dev/shm/file');
$etag = sprintf('"%x-%x-%x"', $stats['ino'], $stats['size'], $stats['mtime']); // lowercase hexadecimal numbers separated by dashes
header('Etag: '.$etag);
?>

Assuming your apache is configured to use the inode, modification time and filesize in its etag.

The function you attached simply converts integers of type long to hexadecimal strings. It is not the actual function creating the etag itself.

- Tul

Jochem Maas wrote:
I have an image.php script that generates images on the fly,
way back when this didn't even cache it's result! over time it's got better
and better so that now it caches resampled images and outputs Last-Modified
headers so that it can send subsequent requests and send out a 304
if appropriate.

but ... I need more speed ... so I changed the way in which the cached
files were named, setup apache Rewrite rules to serve images directly
from the cache of resampled images (stored in /dev/shm) and offered
up urls in the form /cache/foo.jpg rather than /image.php?f=foo.jpg - this works fine
and apache automatically sends out Etag headers when it serves up the resampled
files from the cache, additionally handing out a 304 when it receives a
valid Etag in a request.

when a cached, resampled image does not exist an apache Rewrite rule
will call image.php which will generate the image and output it... BUT it
won't generate an Etag header which means that on the second request to an
image it will downloaded again because apache does not find an Etag header.

I'd like to be able to generate an Etag header in php that matches
what Apache generates, I ended up in the apache source code here:

http://svn.apache.org/viewvc/httpd/httpd/branches/2.0.x/modules/http/http_protocol.c?view=markup

specifically this function, which is used to create each of
various 'bits' of the Etag apache generates (by default: Inode, FileSize and
FileModificationTime are used but this can be controlled with
the FileEtag apache directive):

static char *etag_ulong_to_hex(char *next, unsigned long u)
{
    int printing = 0;
    int shift = sizeof(unsigned long) * 8 - 4;
    do {
        unsigned long next_digit = ((u >> shift) & (unsigned long)0xf);
        if (next_digit) {
            *next++ = HEX_DIGITS[next_digit];
            printing = 1;
        }
        else if (printing) {
            *next++ = HEX_DIGITS[next_digit];
        }
        shift -= 4;
    } while (shift);
    *next++ = HEX_DIGITS[u & (unsigned long)0xf];
    return next;
}

I have been trying to translate what this does to php code without
any luck, dechex() it is not! basically I don't really understand what this
function is doing. can anyone help?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux