Skip to content

Instantly share code, notes, and snippets.

View ThijsFeryn's full-sized avatar

Thijs Feryn ThijsFeryn

View GitHub Profile
@ThijsFeryn
ThijsFeryn / composer.json
Created November 12, 2015 14:14
Simple ESI rendering example using Silex and Varnish v4. The render_esi function in Twig renders ESI tags when the right Surrogate-Capability header is thrown, otherwise it uses internal subrequests.
{
"require": {
"silex/silex": "1.3.*",
"symfony/twig-bridge": "2.7",
"twig/twig": "1.23.*"
}
}
@ThijsFeryn
ThijsFeryn / jwt.vcl
Created February 13, 2017 09:25
A custom VCL procedure that validates a JSON Web Token. This procedure can be called from within your other VCL logic. You will need to install vmod_digest to have the digest functions.
sub jwt {
if(req.http.cookie ~ "^([^;]+;[ ]*)*token=[^\.]+\.[^\.]+\.[^\.]+([ ]*;[^;]+)*$") {
set req.http.x-token = ";" + req.http.Cookie;
set req.http.x-token = regsuball(req.http.x-token, "; +", ";");
set req.http.x-token = regsuball(req.http.x-token, ";(token)=","; \1=");
set req.http.x-token = regsuball(req.http.x-token, ";[^ ][^;]*", "");
set req.http.x-token = regsuball(req.http.x-token, "^[; ]+|[; ]+$", "");
set req.http.tmpHeader = regsub(req.http.x-token,"token=([^\.]+)\.[^\.]+\.[^\.]+","\1");
set req.http.tmpTyp = regsub(digest.base64url_decode(req.http.tmpHeader),{"^.*?"typ"\s*:\s*"(\w+)".*?$"},"\1");
@ThijsFeryn
ThijsFeryn / call_jwt.vcl
Last active February 13, 2017 09:58
Call the custom-defined JWT VCL procedure that checks the validity of a JSON Web Token. This VCL snippet requires vmod_digest and https://gist.github.com/ThijsFeryn/b745c42dbfa328d702b8dbba8777aee2
vcl 4.0;
import digest;
backend default {
.host = "localhost";
.port = "8080";
}
include "jwt.vcl";
@ThijsFeryn
ThijsFeryn / jwt.vcl
Created February 13, 2017 09:24
A custom VCL procedure that validates a JSON Web Token. This procedure can be called from within your other VCL logic. Wath
sub jwt {
if(req.http.cookie ~ "^([^;]+;[ ]*)*token=[^\.]+\.[^\.]+\.[^\.]+([ ]*;[^;]+)*$") {
set req.http.x-token = ";" + req.http.Cookie;
set req.http.x-token = regsuball(req.http.x-token, "; +", ";");
set req.http.x-token = regsuball(req.http.x-token, ";(token)=","; \1=");
set req.http.x-token = regsuball(req.http.x-token, ";[^ ][^;]*", "");
set req.http.x-token = regsuball(req.http.x-token, "^[; ]+|[; ]+$", "");
set req.http.tmpHeader = regsub(req.http.x-token,"token=([^\.]+)\.[^\.]+\.[^\.]+","\1");
set req.http.tmpTyp = regsub(digest.base64url_decode(req.http.tmpHeader),{"^.*?"typ"\s*:\s*"(\w+)".*?$"},"\1");
@ThijsFeryn
ThijsFeryn / return_vcl.vcl
Created September 20, 2016 15:34
Returning custom VCLs on-the-fly
sub vcl_recv {
if (req.http.host == "blog.feryn.eu") {
return(vcl(blog_vcl));
}
if (req.http.host == "talks.feryn.eu") {
return(vcl(talks_vcl));
}
}
@ThijsFeryn
ThijsFeryn / load_and_label
Created September 20, 2016 15:31
Load and label VCL files
vcl.load blog /etc/varnish/blog.vcl
vcl.load talks /etc/varnish/talks.vcl
vcl.label blog_vcl blog
vcl.label talks_vcl talks
@ThijsFeryn
ThijsFeryn / varnish5_proxy_backend.vcl
Created September 20, 2016 15:22
Varnish 5 offers backend PROXY protocol support. If you add the ".proxy_header" attribute, the PROXY protocol will be used.
backend default {
.host = "127.0.0.1";
.port = "8080";
.proxy_header = 1;
}
@ThijsFeryn
ThijsFeryn / output_type_error.php
Created July 14, 2016 14:37
The returned array cannot be converted into the desired string. A type error is thrown.
<?php
function double(int $argument):string {
return [$argument*2];
}
var_dump(double(3));
@ThijsFeryn
ThijsFeryn / input_type_error.php
Created July 14, 2016 14:35
A type error is thrown because an array cannot be turned into an integer
<?php
function double(int $argument):int {
return $argument*2;
}
var_dump(double([]));
@ThijsFeryn
ThijsFeryn / output_coerce.php
Created July 14, 2016 14:32
Coerce returned integer into a string. No type error thrown despite the return type declaration.
<?php
function double(int $argument):string {
return $argument*2;
}
var_dump(double(3));