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

No content

Slide 19

Slide 19 text

10010111001010001011

Slide 20

Slide 20 text

1234 4321

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

foo moo Anuar Zhumaev, from The Noun Project

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Tim Boelaars, from The Noun Project

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

nc

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

data Dmitry Baranovskiy, from The Noun Project

Slide 30

Slide 30 text

xinetd

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

#!/bin/bash fortune | cowsay

Slide 35

Slide 35 text

$ nc 7777

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

$ echo 'moo' | nc 7778

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

#!/usr/bin/php

Slide 41

Slide 41 text

cgi

Slide 42

Slide 42 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 43

Slide 43 text

xinetd tcp http httpd

Slide 44

Slide 44 text

http httpd script cgi

Slide 45

Slide 45 text

script env vars data headers data

Slide 46

Slide 46 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 47

Slide 47 text

Content-Type: text/html ...

Slide 48

Slide 48 text

curl => inetd | web | cgi

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

function helloWorld(array $env) { return [ 200, ["Content-Type" => "text/plain"], ["Hello world!"], ]; }

Slide 57

Slide 57 text

sapi

Slide 58

Slide 58 text

$_SERVER header echo exit

Slide 59

Slide 59 text

HttpKernelInterface

Slide 60

Slide 60 text

sapi kernel

Slide 61

Slide 61 text

tcp websocket HTTP/1.1 {“JSON”}

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

myths:

Slide 64

Slide 64 text

myths: caching

Slide 65

Slide 65 text

myths: caching (use varnish)

Slide 66

Slide 66 text

myths: caching (use varnish) functional tests

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

kernel CGI

Slide 69

Slide 69 text

kernel CGI anything!

Slide 70

Slide 70 text

export SCRIPT_NAME=/app.php export SCRIPT_FILENAME=/var/www/app.php export PATH_INFO=/foo export QUERY_STRING="bar=baz&qux=quux" export REQUEST_URI=/foo export REQUEST_METHOD=GET php-cgi app.php

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

caution: it’s slow (but could be optimized)

Slide 74

Slide 74 text

Where is the value?

Slide 75

Slide 75 text

REQ REP

Slide 76

Slide 76 text

fcgi roles

Slide 77

Slide 77 text

authorizer httpd responder filter

Slide 78

Slide 78 text

rack middlewares

Slide 79

Slide 79 text

class EmptyDecorator def initialize(app) @app = app end def call(env) @app.call(env) end end

Slide 80

Slide 80 text

class CommonLogger def initialize(app, logger=nil) @app = app @logger = logger end def call(env) began_at = Time.now response = @app.call(env) status, header, _ = response log(env, status, header, began_at) response end private def log(env, status, header, began_at) ... end end

Slide 81

Slide 81 text

class CommonLogger def initialize(app, logger=nil) @app = app @logger = logger end def call(env) began_at = Time.now response = @app.call(env) status, header, _ = response log(env, status, header, began_at) response end private def log(env, status, header, began_at) ... end end

Slide 82

Slide 82 text

class CommonLogger def initialize(app, logger=nil) @app = app @logger = logger end def call(env) began_at = Time.now response = @app.call(env) status, header, _ = response log(env, status, header, began_at) response end private def log(env, status, header, began_at) ... end end

Slide 83

Slide 83 text

class CommonLogger def initialize(app, logger=nil) @app = app @logger = logger end def call(env) began_at = Time.now response = @app.call(env) status, header, _ = response log(env, status, header, began_at) response end private def log(env, status, header, began_at) ... end end

Slide 84

Slide 84 text

class CommonLogger def initialize(app, logger=nil) @app = app @logger = logger end def call(env) began_at = Time.now response = @app.call(env) status, header, _ = response log(env, status, header, began_at) response end private def log(env, status, header, began_at) ... end end

Slide 85

Slide 85 text

HttpKernel middlewares

Slide 86

Slide 86 text

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

Slide 87

Slide 87 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 88

Slide 88 text

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

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

App Logger Authentication Session

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

CHH simensen igorw

Slide 93

Slide 93 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 94

Slide 94 text

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

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

OAuth $app = new 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 97

Slide 97 text

OAuth $app = new CallableHttpKernel(function ($request) { $token = $request->attributes->get('oauth.token'); if (!$token) { return new RedirectResponse('/auth'); } $params = $token->getExtraParams(); return sprintf('Welcome @%s!', $params['screen_name']); });

Slide 98

Slide 98 text

Composition!

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

It doesn’t stop there

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

Choose wisely

Slide 103

Slide 103 text

Michael Rowe, from The Noun Project

Slide 104

Slide 104 text

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