HTTP: Generating ETag Header

后端 未结 7 1639
無奈伤痛
無奈伤痛 2020-12-05 14:16

How do I generate an ETag HTTP header for a resource file?

7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 14:29

    The code example of Mark Harrison is similar to what used in Apache 2.2. But such algorithm causes problems for load balancing when you have two servers with the same file but the file's inode is different. That's why in Apache 2.4 developers simplified ETag schema and removed the inode part. Also to make ETag shorter usually they encoded in hex:

        
    
        
        
    char *mketag(char *s, struct stat *sb)
    {
        sprintf(s, "\"%" PRIx64 "-%" PRIx64 "\"", sb->st_mtime, sb->st_size);
        return s;
    }
        
    

    or for Java

     etag = '"' + Long.toHexString(lastModified) + '-' +
                                    Long.toHexString(contentLength) + '"';
    

    for C#

    // Generate ETag from file's size and last modification time as unix timestamp in seconds from 1970
    public static string MakeEtag(long lastMod, long size)
    {
        string etag = '"' + lastMod.ToString("x") + '-' + size.ToString("x") + '"';
        return etag;
    }
    
    public static void Main(string[] args)
    {
        long lastMod = 1578315296;
        long size = 1047;
        string etag = MakeEtag(lastMod, size);
        Console.WriteLine("ETag: " + etag);
        //=> ETag: "5e132e20-417"
    }
    

    The function returns ETag compatible with Nginx. See comparison of ETags form different servers

提交回复
热议问题