[PATCH 4/4] httpd: Support caching via ETag header

Denys Vlasenko vda.linux at googlemail.com
Sat Aug 15 21:05:54 UTC 2020


On Sun, Aug 9, 2020 at 12:24 AM Sergey Ponomarev <stokito at gmail.com> wrote:
>
> If server respond with ETag then next time client (browser) resend it via If-None-Match header.
> Then httpd will check if file wasn't modified and if not return 304 Not Modified status code.
> The ETag value is constructed from file's last modification date in unix epoch and it's size:
> "hex(last_mod)-hex(file_size)" e.g. "5e132e20-417" (with quotes).
> That means that it's not completely reliable as hash functions but fair enough.
> The same form of ETag is used by Nginx so load balancing of static content is safe.
>
> Signed-off-by: Sergey Ponomarev <stokito at gmail.com>
> ---
>  networking/httpd.c | 73 ++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 70 insertions(+), 3 deletions(-)
>
> diff --git a/networking/httpd.c b/networking/httpd.c
> index 1cea33ddd..cc3f757aa 100644
> --- a/networking/httpd.c
> +++ b/networking/httpd.c
> @@ -215,6 +215,17 @@
>  //config:      Makes httpd send files using GZIP content encoding if the
>  //config:      client supports it and a pre-compressed <file>.gz exists.
>  //config:
> +//config:config FEATURE_HTTPD_ETAG
> +//config:      bool "Support caching via ETag header"
> +//config:      default y
> +//config:      depends on HTTPD
> +//config:      help
> +//config:      If server respond with ETag then next time client (browser) resend it via If-None-Match header.
> +//config:      Then httpd will check if file wasn't modified and if not return 304 Not Modified status code.
> +//config:      The ETag value is constructed from file's last modification date in unix epoch and it's size:
> +//config:      "hex(last_mod)-hex(file_size)" e.g. "5e132e20-417" (with quotes).
> +//config:      That means that it's not completely reliable as hash functions but fair enough.

$ make
  GEN     networking/Config.in
scripts/kconfig/conf -s Config.in
networking/Config.in:294 error: Overlong line
networking/Config.in:295 error: Overlong line
networking/Config.in:296 error: Overlong line
networking/Config.in:298 error: Overlong line
networking/Config.in:306 error: Overlong line
networking/Config.in:307 error: Overlong line
#
# using defaults found in .config


> +//config:
>  //config:config FEATURE_HTTPD_LAST_MODIFIED
>  //config:      bool "Add Last-Modified header to response"
>  //config:      default y
> @@ -266,6 +277,7 @@
>
>  #include "libbb.h"
>  #include "common_bufsiz.h"
> +#include <inttypes.h>

libbb.h includes inttypes.h

> +/*
> + * ETag is "hex(last_mod)-hex(file_size)" e.g. "5e132e20-417"
> + */
> +static char *make_etag(void)
> +{
> +       return xasprintf("\"%" PRIx64 "-%" PRIx64 "\"", last_mod, file_size);
> +}
> +

networking/httpd.c: In function 'make_etag':
networking/httpd.c:1082:19: error: format '%llx' expects argument of
type 'long long unsigned int', but argument 2 has type 'time_t {aka
long int}' [-Werror=format=]
  return xasprintf("\"%" PRIx64 "-%" PRIx64 "\"", last_mod, file_size);
                   ^~~~~


More information about the busybox mailing list