Advertisement
Guest User

fuf-extended_cache_control.vcl

a guest
Jul 25th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. // Adds custom Cache-Control directive to control how long Varnish
  2. // keeps the item in cache. Copied from varnish examples site.
  3. // Sets the response ttl to the value specified as v-max-age in the
  4. // Cache-Control header. Fall back to max-age value.
  5.  
  6. C{
  7. #include <errno.h>
  8. #include <limits.h>
  9. #include <stdlib.h>
  10. }C
  11.  
  12. sub extended_cache_control
  13. {
  14. if (beresp.http.Cache-Control ~ "v-max-age=[0-9]+") {
  15. /* Copy the ttl part from original header */
  16. set beresp.http.X-Cache-Control-TTL = regsub(beresp.http.Cache-Control, ".*v-max-age=([0-9]+).*", "\1");
  17. C{
  18. {
  19. char *x_end = 0;
  20. const char *x_hdr_val;
  21. const struct gethdr_s hdr = { HDR_BERESP, "\024X-Cache-Control-TTL:" };
  22. x_hdr_val = VRT_GetHdr(ctx, &hdr); // number of chars
  23.  
  24. if (x_hdr_val) {
  25. long x_cache_ttl = strtol(x_hdr_val, &x_end, 0);
  26. if (ERANGE != errno && x_end != x_hdr_val && x_cache_ttl >= 0 && x_cache_ttl < INT_MAX) {
  27. VRT_l_beresp_ttl(ctx, (x_cache_ttl * 1));
  28. }
  29. }
  30. }
  31. }C
  32. unset beresp.http.X-Cache-Control-TTL;
  33. } else if (beresp.http.Cache-Control ~ "max-age=[0-9]+") {
  34. /* Copy the ttl part from original header */
  35. set beresp.http.X-Cache-Control-TTL = regsub(beresp.http.Cache-Control, ".*max-age=([0-9]+).*", "\1");
  36. C{
  37. {
  38. char *x_end = 0;
  39. const char *x_hdr_val;
  40. const struct gethdr_s hdr = { HDR_BERESP, "\024X-Cache-Control-TTL:" };
  41. x_hdr_val = VRT_GetHdr(ctx, &hdr); // number of chars
  42. if (x_hdr_val) {
  43. long x_cache_ttl = strtol(x_hdr_val, &x_end, 0);
  44. if (ERANGE != errno && x_end != x_hdr_val && x_cache_ttl >= 0 && x_cache_ttl < INT_MAX) {
  45. VRT_l_beresp_ttl(ctx, (x_cache_ttl * 1));
  46. }
  47. }
  48. }
  49. }C
  50. unset beresp.http.X-Cache-Control-TTL;
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement