Slide 1

Slide 1 text

the HttpKernelInterface is a lie

Slide 2

Slide 2 text

@igorwesome

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

“Interface” Protocol Communication Message passing

Slide 5

Slide 5 text

Catherine Please, from The Noun Project

Slide 6

Slide 6 text

Paulo Sá Ferreira - Purple Matter, from The Noun Project

Slide 7

Slide 7 text

hi!

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

10010101110100100

Slide 10

Slide 10 text

1001010111010010000110101 Massimiliano Mauro, from The Noun Project

Slide 11

Slide 11 text

Sergey Shmidt, from The Noun Project aloha!

Slide 12

Slide 12 text

yay!

Slide 13

Slide 13 text

00101000011101100102

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Universal Interface

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

10010111001010001011

Slide 21

Slide 21 text

1234 4321

Slide 22

Slide 22 text

foo bar baz foo baz Patrick n Hawaii, from The Noun Project

Slide 23

Slide 23 text

foo moo Anuar Zhumaev, from The Noun Project

Slide 24

Slide 24 text

find src -name '*.php' | grep -iv tests | cut -f2- -d/ | cut -f1 -d\. | awk '{ print length, $0 }' | sort -n | tr / \\ ;

Slide 25

Slide 25 text

Tim Boelaars, from The Noun Project

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

nc

Slide 28

Slide 28 text

/\_/\ / 0 0 \ ====v==== \ W / | | _ / ___ \ / / / \ \ | (((-----)))-' / ( ___ \__.=|___E /

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

data Dmitry Baranovskiy, from The Noun Project

Slide 31

Slide 31 text

xinetd

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

/etc/xinet.d/ service { disable = no socket_type = stream protocol = tcp user = wait = no server = /usr/local/bin/ }

Slide 34

Slide 34 text

/etc/xinet.d/fortune service fortune { disable = no socket_type = stream protocol = tcp user = xinetd wait = no server = /usr/local/bin/fortune }

Slide 35

Slide 35 text

#!/bin/bash fortune | cowsay

Slide 36

Slide 36 text

$ nc 7777

Slide 37

Slide 37 text

$ nc 7777 ______________________________________ < Eat right, stay fit, and die anyway. > -------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||

Slide 38

Slide 38 text

/etc/xinet.d/cowsay service cowsay { disable = no socket_type = stream protocol = tcp user = xinetd wait = no server = /usr/games/cowsay }

Slide 39

Slide 39 text

$ echo 'moo' | nc 7778

Slide 40

Slide 40 text

$ echo 'moo' | nc 7778 _____ < moo > ----- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||

Slide 41

Slide 41 text

#!/usr/bin/php

Slide 42

Slide 42 text

cgi

Slide 43

Slide 43 text

The Common Gateway Interface (CGI) [22] allows an HTTP [1], [4] server and a CGI script to share responsibility for responding to client requests. RFC 3875

Slide 44

Slide 44 text

xinetd tcp http httpd

Slide 45

Slide 45 text

http httpd script cgi

Slide 46

Slide 46 text

script env vars data headers data

Slide 47

Slide 47 text

GET / HTTP/1.1 Host: igor.io Accept: */* REQUEST_METHOD = GET PATH_INFO = / HTTP_HOST = igor.io HTTP_ACCEPT = */* SERVER_NAME = igor.io

Slide 48

Slide 48 text

Content-Type: text/html ...

Slide 49

Slide 49 text

fcgi

Slide 50

Slide 50 text

script

Slide 51

Slide 51 text

sockets are the new pipes

Slide 52

Slide 52 text

• 1991: HTTP • 1993: CGI • 1995: PHP • 1996: FastCGI

Slide 53

Slide 53 text

• 1997: Java Servlet • 2003: Python WSGI • 2007: Ruby Rack • 2009: Perl PSGI • 2011: Symfony2 HttpKernelInterface

Slide 54

Slide 54 text

rack

Slide 55

Slide 55 text

class HelloWorld def call(env) [ 200, {"Content-Type" => "text/plain"}, ["Hello world!"] ] end end

Slide 56

Slide 56 text

sapi

Slide 57

Slide 57 text

$_SERVER header echo exit

Slide 58

Slide 58 text

HttpKernelInterface

Slide 59

Slide 59 text

sapi kernel

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

“legacy” apps: it’s all great until exit;

Slide 62

Slide 62 text

myths:

Slide 63

Slide 63 text

myths: caching

Slide 64

Slide 64 text

myths: caching (use varnish)

Slide 65

Slide 65 text

myths: caching (use varnish) functional tests

Slide 66

Slide 66 text

CgiHttpKernel adapter from kernel to cgi functional test “legacy” apps

Slide 67

Slide 67 text

kernel CGI

Slide 68

Slide 68 text

kernel CGI anything!

Slide 69

Slide 69 text

$kernel = new CgiHttpKernel(__DIR__.'/app'); $request = Request::create('/index.php'); $response = $kernel->handle($request); var_dump($response->getContent());

Slide 70

Slide 70 text

use Symfony\Component\HttpKernel\Client; $client = new Client($kernel); $crawler = $client->request('GET', '/index.php');

Slide 71

Slide 71 text

caution: it’s slow (WIP: FcgiHttpKernel)

Slide 72

Slide 72 text

Where is the value?

Slide 73

Slide 73 text

REQ REP

Slide 74

Slide 74 text

fcgi roles

Slide 75

Slide 75 text

authorizer httpd responder filter

Slide 76

Slide 76 text

rack middlewares

Slide 77

Slide 77 text

HttpKernel middlewares

Slide 78

Slide 78 text

$app = new CallableHttpKernel(function ($request) { return new Response('Hello World!'); });

Slide 79

Slide 79 text

class Logger implements HttpKernelInterface { private $app; private $logger; public function __construct(HttpKernelInterface $app, LoggerInterface $logger) { $this->app = $app; $this->logger = $logger; } public function handle(Request $request, ...) { $response = $this->app->handle($request, $type, $catch); $this->log($request, $response); return $response; } private function log(Request $request, Response $response) { ... } }

Slide 80

Slide 80 text

$app = new Logger( $app, new Monolog\Logger() );

Slide 81

Slide 81 text

use Symfony\Component\HttpKernel\HttpCache\HttpCache; use Symfony\Component\HttpKernel\HttpCache\Store; $app = new HttpCache( $app, new Store(__DIR__.'/cache') );

Slide 82

Slide 82 text

use Symfony\Component\HttpKernel\HttpCache\HttpCache; use Symfony\Component\HttpKernel\HttpCache\Store; $app = new HttpCache( $app, new Store(__DIR__.'/cache') ); use varnish!

Slide 83

Slide 83 text

App Logger Authentication Session

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

do something before or after every request

Slide 86

Slide 86 text

different take on event listeners

Slide 87

Slide 87 text

class Foo implements HttpKernelInterface { private $app; public function __construct(HttpKernelInterface $app) { $this->app = $app; } public function handle(Request $request, ...) { $response = $this->app->handle($request, $type, $catch); return $response; } } 1 2 3

Slide 88

Slide 88 text

UrlMap $blog = new Silex\Application(); $blog->get('/', function () { return 'This is the blog!'; }); $app = new Stack\UrlMap($app, [ '/blog' => $blog, ]);

Slide 89

Slide 89 text

Session $app = new CallableHttpKernel(function ($request) { $session = $request->getSession(); ... }); $app = new Stack\Session($app);

Slide 90

Slide 90 text

OAuth $app = new Igorw\Stack\OAuth($app, [ 'key' => 'foo', 'secret' => 'bar', 'callback_url' => 'http://localhost:8080/auth/verify', 'success_url' => '/', 'failure_url' => '/auth', ]); $app = new Stack\Session($app);

Slide 91

Slide 91 text

OAuth $request->attributes->get('oauth.token');

Slide 92

Slide 92 text

Composition!

Slide 93

Slide 93 text

$stack = (new Stack\Builder()) ->push('Stack\Session') ->push('Igorw\Stack\OAuth', [...]) ->push('Foo'); $app = $stack->resolve($app);

Slide 94

Slide 94 text

• GeoIp • IpRestrict • Backstage • Basic Authentication • Hawk • CORS Community

Slide 95

Slide 95 text

It doesn’t stop there

Slide 96

Slide 96 text

• Authentication • Debug toolbar • Injected routes • Signed cookies • Asset management • Force SSL • Error handling

Slide 97

Slide 97 text

( )

Slide 98

Slide 98 text

Choose wisely

Slide 99

Slide 99 text

Michael Rowe, from The Noun Project

Slide 100

Slide 100 text

• joind.in/9327 • stackphp.com • @stackphp • @igorwesome • github.com/igorw/CgiHttpKernel