{"id":573,"date":"2012-09-30T00:02:38","date_gmt":"2012-09-29T22:02:38","guid":{"rendered":"http:\/\/blog.jeremm.fr\/?p=573"},"modified":"2014-09-24T21:15:30","modified_gmt":"2014-09-24T19:15:30","slug":"varnish-transformer-le-cache-control-max-age-en-expires","status":"publish","type":"post","link":"https:\/\/blog.jeremm.fr\/?p=573","title":{"rendered":"Varnish : transformer le Cache-Control : max-age en Expires"},"content":{"rendered":"<p>Cr\u00e9ation de l&rsquo;Expires \u00e0 partir du cache-control max-age si il est pr\u00e9sent et diff\u00e9rent de 0, puis suppression du max-age.<\/p>\n<p>Ajoutez au d\u00e9but de la configuration :<\/p>\n<pre lang=\"cpp\">C{\r\n        #include <string.h>\r\n        #include <stdlib.h>\r\n\r\n        void TIM_format(double t, char *p);\r\n        double TIM_real(void);\r\n}C<\/pre>\n<p>Et dans le vcl_deliver :<\/p>\n<pre lang=\"cpp\">if ( resp.http.Cache-Control ~ \"max-age\" && ! resp.http.Cache-Control ~ \"max-age=0\" ) {\r\n        C{\r\n                 char *cache = VRT_GetHdr(sp, HDR_RESP, \"\\016cache-control:\");\r\n                 char date[40];\r\n                 int max_age = -1;\r\n                 int want_equals = 0;\r\n                 if(cache) {\r\n                         while(*cache != '\\0') {\r\n                                 if (want_equals && *cache == '=') {\r\n                                         cache++;\r\n                                         max_age = strtoul(cache, 0, 0);\r\n                                         break;\r\n                                  }\r\n                                  if (*cache == 'm' && !memcmp(cache, \"max-age\", 7)) {\r\n                                         cache += 7;\r\n                                         want_equals = 1;\r\n                                         continue;\r\n                                  }\r\n                         cache++;\r\n                         }\r\n                         if (max_age != -1) {\r\n                                 TIM_format(TIM_real() + max_age, date);\r\n                                 VRT_SetHdr(sp, HDR_RESP, \"\\010Expires:\", date, vrt_magic_string_end);\r\n                         }\r\n                 }\r\n        }C\r\n        set resp.http.Cache-Control = regsuball(resp.http.Cache-Control,\"max-age=[0-9]+(,)?\", \"\");\r\n        if ( resp.http.Cache-Control ~ \"^$\") {\r\n                 unset resp.http.Cache-Control;\r\n        }\r\n}<\/pre>\n<p>Source : <a title=\"https:\/\/www.varnish-cache.org\/trac\/wiki\/VCLExampleSetExpires\" href=\"https:\/\/www.varnish-cache.org\/trac\/wiki\/VCLExampleSetExpires\" target=\"_blank\">https:\/\/www.varnish-cache.org\/trac\/wiki\/VCLExampleSetExpires<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cr\u00e9ation de l&rsquo;Expires \u00e0 partir du cache-control max-age si il est pr\u00e9sent et diff\u00e9rent de 0, puis suppression du max-age. Ajoutez au d\u00e9but de la configuration : C{ #include #include void TIM_format(double t, char *p); double TIM_real(void); }C Et dans le vcl_deliver : if ( resp.http.Cache-Control ~ \u00ab\u00a0max-age\u00a0\u00bb &#038;&#038; ! resp.http.Cache-Control ~ \u00ab\u00a0max-age=0\u00a0\u00bb ) { <a href='https:\/\/blog.jeremm.fr\/?p=573' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[67,68,9],"_links":{"self":[{"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=\/wp\/v2\/posts\/573"}],"collection":[{"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=573"}],"version-history":[{"count":14,"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=\/wp\/v2\/posts\/573\/revisions"}],"predecessor-version":[{"id":890,"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=\/wp\/v2\/posts\/573\/revisions\/890"}],"wp:attachment":[{"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jeremm.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}