Skip to content

Instantly share code, notes, and snippets.

View ThijsFeryn's full-sized avatar

Thijs Feryn ThijsFeryn

View GitHub Profile
@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));
@ThijsFeryn
ThijsFeryn / input_coerce.php
Created July 14, 2016 14:25
Coerce string into an integer. No type error thrown despite the type hint.
<?php
function double(int $argument):int {
return $argument*2;
}
var_dump(double("2"));
@ThijsFeryn
ThijsFeryn / catch_as_type_error_error_or_throwable.php
Created July 14, 2016 13:10
Catch a type error either as a type error, an error, or a throwable
<?php
function double(int $argument):int {
return $argument*2;
}
try {
var_dump(double([3]));
} catch (TypeError $e) {
echo "Caught as type error: ".$e->getMessage().PHP_EOL;
}
@ThijsFeryn
ThijsFeryn / catch_type_error.php
Created July 14, 2016 12:50
Catch type errors in PHP 7
<?php
declare(strict_types=1);
function double(int $argument):int {
return $argument*2;
}
try {
var_dump(double("3"));
} catch (TypeError $e) {