Skip to content

Instantly share code, notes, and snippets.

@nuria
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuria/e0b0d4a702cdd45f6d37 to your computer and use it in GitHub Desktop.
Save nuria/e0b0d4a702cdd45f6d37 to your computer and use it in GitHub Desktop.
VCL cookie setting, time maniulation
C{
#include<stdio.h>
#include <time.h>
#include <string.h>
#define vcl_string char
char* get_expiration() {
struct tm str_time;
time_t time_of_day;
char expiration[100];
str_time.tm_year = 3000 -1900;
str_time.tm_mon = 0;
str_time.tm_mday = 1;
str_time.tm_hour = 1;
str_time.tm_min = 1;
str_time.tm_sec = 1;
time_of_day = mktime(&str_time);
printf("%s\n", ctime(&time_of_day));
/* The value for this option is a date in the format Wdy, DD-Mon-YYYY HH:MM:SS GMT */
/* Like: Sat, 02 May 2009 23:38:25 GMT */
strftime (expiration, 100, "%a, %b %d %Y %H:%M:%S GMT", localtime(&time_of_day));
return expiration;
}
void set_cache_control(const struct sess *sp) {
char *now = VRT_time_string(sp, VRT_r_now(sp));
char *expiration = get_expiration();
VRT_SetHdr(sp, HDR_BERESP, "\016Last-Modified:", now, vrt_magic_string_end);
/*VRT_SetHdr(sp, HDR_OBJ, "\016Cache-Control:", "private, max-age=86400, s-maxage=0", vrt_magic_string_end);
*/
}
/*
* Sets cookie in the form:
* Blahs=Wed, Mar 11 2015 GMT;Expires=Wed, Jan 01 3000 01:01:01 GMT;Path=/; HttpOnly
*/
void set_last_access_cookie(const struct sess *sp) {
char now[30];
time_t mytime;
mytime = time(NULL);
// note this would print two digits for the day, like Thu, Mar 01
strftime (now, 100, "%F GMT", gmtime(&mytime));
char lacookie[100];
strcpy(lacookie, "Blah=");
strcat(lacookie, now);
strcat(lacookie, ";Expires=Wed, Jan 01 3000 01:01:01 GMT");
strcat(lacookie, ";Path=/;HttpOnly;");
/* by default cookies are set on the top domain */
Vmod_Func_header.append(sp, HDR_RESP, "\013Set-Cookie:", lacookie, vrt_magic_string_end);
}
}C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment