2015-10-07 PPDC HTTP Adapters

41
HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox [email protected]

Transcript of 2015-10-07 PPDC HTTP Adapters

Page 1: 2015-10-07 PPDC HTTP Adapters

HTTP AdapersPolyglot Programming DC 2015

Brock [email protected]

Page 2: 2015-10-07 PPDC HTTP Adapters

Web Applications

Page 3: 2015-10-07 PPDC HTTP Adapters

Request → Response

Page 4: 2015-10-07 PPDC HTTP Adapters

Let's go BACK....

Page 5: 2015-10-07 PPDC HTTP Adapters

to 1993

Page 6: 2015-10-07 PPDC HTTP Adapters

CGI

Common Gateway Interface

Page 7: 2015-10-07 PPDC HTTP Adapters

Request → Response

Page 8: 2015-10-07 PPDC HTTP Adapters

... Unix!

Page 9: 2015-10-07 PPDC HTTP Adapters

Request → Response

ENV + STDIN → STDOUT

ENV == Headers (params)STDIN == POSTSTDOUT == Headers + Body

Page 10: 2015-10-07 PPDC HTTP Adapters

#!/bin/sh

# A lovely cgi application

echo "Content-type: text/html"echoecho "Hello, world! Query:"echo $QUERY_STRING

Page 11: 2015-10-07 PPDC HTTP Adapters

Parse the query string if you want

Page 12: 2015-10-07 PPDC HTTP Adapters

cgi programsrise from ash then burn againupon each request

Page 13: 2015-10-07 PPDC HTTP Adapters

Long running applications

Page 14: 2015-10-07 PPDC HTTP Adapters

Kinda long-running

mod_perlmod_phpmod_python...

Page 15: 2015-10-07 PPDC HTTP Adapters

Kinda long-running

FastCGI

Page 16: 2015-10-07 PPDC HTTP Adapters

Just run your own HTTPD

Page 17: 2015-10-07 PPDC HTTP Adapters

Also annoying

Page 18: 2015-10-07 PPDC HTTP Adapters

Same webserver language,but unlink from webserver.

Page 19: 2015-10-07 PPDC HTTP Adapters

Web App Frameworks

Page 20: 2015-10-07 PPDC HTTP Adapters

func app(request) → response

response = [ status, headers, body ]

Page 21: 2015-10-07 PPDC HTTP Adapters

Common Lisp - ClackClojure - RingElixir - PlugHaskell - Hack2Java - Servlets?Javascript - JSGILua - WSAPIPerl - PSGIPerl6 - P6SGIPython - WSGIRuby - RackScala - SSGI* - uWSGI

Page 22: 2015-10-07 PPDC HTTP Adapters

# Python WSGI

def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/plain')]) yield 'Hello, world!'

Page 23: 2015-10-07 PPDC HTTP Adapters

# Ruby Rack

app = lambda do |env| [200, {"Content-Type" => "text/plain"}, ["Hello, world!"]]end

Page 24: 2015-10-07 PPDC HTTP Adapters

# Perl PSGI

my $app = sub { my ($env) = @_; return [200, ['Content-Type' => 'text/plain'], ["Hello, world!"]];}

Page 25: 2015-10-07 PPDC HTTP Adapters

// Javascript JSGI

require("jsgi-node").start(function(request){ return { status:200, headers:{}, body:["Hello, world!"] };});

Page 26: 2015-10-07 PPDC HTTP Adapters

# Elixir Plug

defmodule MyPlug do import Plug.Conn

def init(options) do # initialize options options end

def call(conn, _opts) do conn |> put_resp_content_type("text/plain") |> send_resp(200, "Hello, world!") endend

Page 27: 2015-10-07 PPDC HTTP Adapters

; Clojure Ring

(ns hello-world.core)

(defn handler [request] {:status 200 :headers {"Content-Type" "text/html"} :body "Hello World"})

Page 28: 2015-10-07 PPDC HTTP Adapters

# Perl6 P6SGI

sub app(%env) { start { 200, [ Content-Type => 'text/plain' ], [ 'Hello World!' ] }}

Page 29: 2015-10-07 PPDC HTTP Adapters

// Scala SSGI

package org.scalatra.ssgipackage examples.servlet

import scala.xml.NodeSeq

class HelloWorldApp extends Application { def apply(v1: Request) = Response(body = <h1>Hello, world!</h1>)}

Page 30: 2015-10-07 PPDC HTTP Adapters

Lua WSAPI

function hello(wsapi_env) local headers = { ["Content-type"] = "text/html" }

local function hello_text() coroutine.yield("Hello, world!") end

return 200, headers, coroutine.wrap(hello_text)end

Page 31: 2015-10-07 PPDC HTTP Adapters

// Java Servlet

// Import required java librariesimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;

// Extend HttpServlet classpublic class HelloWorld extends HttpServlet {

private String message;

public void init() throws ServletException { // Do required initialization message = "Hello, world!"; }

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html");

// Actual logic goes here. PrintWriter out = response.getWriter(); out.println("<h1>" + message + "</h1>"); }

public void destroy() { // do nothing. }}

Page 32: 2015-10-07 PPDC HTTP Adapters

{-# Haskell #-}

{-# LANGUAGE OverloadedStrings #-}

import Hack2import Hack2.Contrib.Response (set_body_bytestring)import Hack2.Handler.SnapServer

app :: Applicationapp = env -> return $ Response 200 [ ("Content-Type", "text/plain") ] "Hello, world!"

main = run app

Page 33: 2015-10-07 PPDC HTTP Adapters

Streaming

response = [status, headers, callback]

Keep invoking callback until done

Page 34: 2015-10-07 PPDC HTTP Adapters

Streaming

response = [status, headers, promise]

Wait for promise results, maybe chained

Page 35: 2015-10-07 PPDC HTTP Adapters

Streaming / Websockets ... not unified

Page 36: 2015-10-07 PPDC HTTP Adapters

Middleware

Page 37: 2015-10-07 PPDC HTTP Adapters

app: request → response

middleware: app → (request → response)

Page 38: 2015-10-07 PPDC HTTP Adapters

def middlin(app) lambda do |request| # ... mess with request app_response = app(request) # ... mess with app_response app_response endend

Page 39: 2015-10-07 PPDC HTTP Adapters

def log_middleware(app) lambda do |request| File.open("middleware.log", "w+") do |f| f.puts request end app_response = app(request) app_response endend

app = lambda do |env| [200, {"Content-Type" => "text/plain"}, ["Hello, world!"]]end

new_app = log_middleware(app)

Page 40: 2015-10-07 PPDC HTTP Adapters

Middleware Examples

LoggingAuthenticatingProxyMulti-app routingSession trackingDebuggingContent filteringContent translation**Content injectionStatic serving

Page 41: 2015-10-07 PPDC HTTP Adapters

THE END