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

HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox [email protected]

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

HTTP AdapersPolyglot Programming DC 2015

Brock [email protected]

Page 2: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Web Applications

Page 3: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Request → Response

Page 4: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Let's go BACK....

Page 5: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

to 1993

Page 6: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

CGI

Common Gateway Interface

Page 7: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Request → Response

Page 8: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

... Unix!

Page 9: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Request → Response

ENV + STDIN → STDOUT

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

Page 10: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

#!/bin/sh

# A lovely cgi application

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

Page 11: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Parse the query string if you want

Page 12: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

cgi programsrise from ash then burn againupon each request

Page 13: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Long running applications

Page 14: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Kinda long-running

mod_perlmod_phpmod_python...

Page 15: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Kinda long-running

FastCGI

Page 16: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Just run your own HTTPD

Page 17: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Also annoying

Page 18: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Same webserver language,but unlink from webserver.

Page 19: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Web App Frameworks

Page 20: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

func app(request) → response

response = [ status, headers, body ]

Page 21: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

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

Page 22: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

# Python WSGI

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

Page 23: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

# Ruby Rack

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

Page 24: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

# Perl PSGI

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

Page 25: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

// Javascript JSGI

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

Page 26: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

# 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: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

; Clojure Ring

(ns hello-world.core)

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

Page 28: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

# Perl6 P6SGI

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

Page 29: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

// 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: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

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: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

// 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: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

{-# 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: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Streaming

response = [status, headers, callback]

Keep invoking callback until done

Page 34: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Streaming

response = [status, headers, promise]

Wait for promise results, maybe chained

Page 35: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Streaming / Websockets ... not unified

Page 36: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Middleware

Page 37: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

app: request → response

middleware: app → (request → response)

Page 38: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

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

Page 39: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

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: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

Middleware Examples

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

Page 41: HTTP Adapers PPDC HTTP Adapters... · HTTP Adapers Polyglot Programming DC 2015 Brock Wilcox awwaiid@thelackthereof.org

THE END