Webmachine a practical executable model for HTTP Justin Sheehy - - PowerPoint PPT Presentation

webmachine
SMART_READER_LITE
LIVE PREVIEW

Webmachine a practical executable model for HTTP Justin Sheehy - - PowerPoint PPT Presentation

Webmachine a practical executable model for HTTP Justin Sheehy justin@basho.com Webmachine a practical executable model for HTTP a toolkit for HTTP-based systems Webmachine a practical executable model for HTTP a toolkit for easily


slide-1
SLIDE 1

Webmachine

a practical executable model for HTTP

Justin Sheehy

justin@basho.com

slide-2
SLIDE 2

Webmachine

a practical executable model for HTTP

HTTP-based systems a toolkit for

slide-3
SLIDE 3

Webmachine

HTTP-based systems well-behaved easily creating a toolkit for

a practical executable model for HTTP

slide-4
SLIDE 4

Webmachine

HTTP-based systems well-behaved easily creating? a toolkit for

a practical executable model for HTTP

slide-5
SLIDE 5

Webmachine

HTTP-based systems well-behaved? easily creating a toolkit for

a practical executable model for HTTP

slide-6
SLIDE 6

HTTP is complicated.

slide-7
SLIDE 7

Webmachine makes HTTP easier.

slide-8
SLIDE 8
  • module(twohundred_resource).
  • export([init/1, to_html/2]).
  • include_lib("webmachine/include/webmachine.hrl").

init([]) -> {ok, undefined}. to_html(ReqData, State) -> {"Hello, Webmachine world", ReqData, State}.

(that’s it!)

slide-9
SLIDE 9

Want to get more interesting?

Just add generate_etag or last_modified...

slide-10
SLIDE 10

...and now you have conditional requests.

generate_etag(RD, State) -> {mochihex:to_hex(erlang:phash2(State)), RD, State}. last_modified(RD, State) -> {filelib:last_modified(State#s.fpath), RD, State}.

Just add generate_etag or last_modified...

slide-11
SLIDE 11

A resource family is just a set of functions.

to_html(ReqData,State) -> {Body,ReqData,State}. generate_etag(ReqData,State) -> {ETag,ReqData,State}. last_modified(ReqData,State) -> {Time,ReqData,State}. resource_exists(ReqData,State) -> {bool,ReqData,State}. is_authorized(ReqData,State) -> {bool,ReqData,State}. ... f(ReqData,State) -> {RetV,ReqData,State}.

slide-12
SLIDE 12

A resource family is just a set of functions.

f(ReqData,State) -> {RetV,ReqData,State}.

Resource functions are referentially transparent and have a uniform interface.

function behavior request/ response data process state + +

slide-13
SLIDE 13

Manipulating Request/Response Data

f(ReqData,State) -> {RetV,ReqData,State}.

The wrq module accesses and (nondestructively) modifies ReqData.

wrq:get_req_header(HdrName,ReqData) -> 'undefined' | HdrVal wrq:get_qs_value(Key,Default,ReqData) -> Value wrq:set_resp_header(HdrName,HdrVal,ReqData) -> NewReqData

slide-14
SLIDE 14

URL Dispatching = Pattern Matching

{["a"],some_resource,[]} pattern resource family args

slide-15
SLIDE 15

URL Dispatching = Pattern Matching

{["a"],some_resource,[]} http://myhost/a match! any other URL no match If no patterns match, then 404 Not Found. /a

slide-16
SLIDE 16

URL Dispatching = Pattern Matching

{["a"],some_resource,[]} /a wrq:disp_path wrq:path wrq:path_info wrq:path_tokens [] "/a" [] [] {["a" ,some_resource,[]}

slide-17
SLIDE 17

URL Dispatching = Pattern Matching

{["a", '*'],some_resource,[]} /a wrq:disp_path wrq:path wrq:path_info wrq:path_tokens [] "/a" [] [] (binds the remaining path) ,some_resource,[]} {["a"

slide-18
SLIDE 18

URL Dispatching = Pattern Matching

{["a", '*'],some_resource,[]} /a wrq:disp_path wrq:path wrq:path_info wrq:path_tokens “b/c” "/a/b/c" [] [“b”, “c”] /a/b/c {["a", ],some_resource,[]}

slide-19
SLIDE 19

URL Dispatching = Pattern Matching

{["a", foo],some_resource,[]} /a wrq:disp_path wrq:path wrq:path_info wrq:path_tokens “b/c” "/a/b/c" [] [“b”, “c”] /a/b/c (name-binds a path segment) 404 {["a", ],some_resource,[]}

slide-20
SLIDE 20

URL Dispatching = Pattern Matching

{["a", foo],some_resource,[]} /a wrq:disp_path wrq:path wrq:path_info wrq:path_tokens [] "/a/b" [{foo, “b”}] [] /a/b {["a", foo some_resource,[]}

slide-21
SLIDE 21

URL Dispatching = Pattern Matching

{["a", foo, '*'],some_resource,[]} wrq:disp_path wrq:path wrq:path_info wrq:path_tokens [] "/a/b" [{foo, “b”}] [] /a/b {["a", foo some_resource,[]}

slide-22
SLIDE 22

URL Dispatching = Pattern Matching

{["a", foo, '*'],some_resource,[]} wrq:disp_path wrq:path wrq:path_info wrq:path_tokens “c/d” "/a/b/c/d" [{foo, “b”}] [“c”,”d”] /a/b/c/d /a/b

slide-23
SLIDE 23

URL Dispatching = Pattern Matching

{["a", foo, '*'],some_resource,[]} wrq:disp_path wrq:path wrq:path_info wrq:path_tokens “c/d” "/a/b/c/d" [{foo, “b”}] [“c”,”d”] /a/b/c/d query strings are easy too /a/b/c/d?fee=ah&fie=ha wrq:get_qs_value("fie",ReqData) -> “ha”

slide-24
SLIDE 24

The Webmachine Visual Debugger

slide-25
SLIDE 25

Hooray!

slide-26
SLIDE 26

But sometimes things don’t go as well.

slide-27
SLIDE 27

It’s nice to know where your errors are.

slide-28
SLIDE 28

wrq:path(RD) -> "/d/test?q=1.5" {{error,{error,badarg, [{erlang,list_to_integer,["1.5"]}, {some_resource,resource_exists,2}...

slide-29
SLIDE 29

malformed_request(ReqData, State) -> {case catch list_to_integer(wrq:get_qs_value("q","0",ReqData)) of {'EXIT', _} -> true; _ -> false end, ReqData, State}.

  • export([malformed_request/2]).
slide-30
SLIDE 30

Visual debugging helps you put the fixes in the right place.

400 Bad Request

slide-31
SLIDE 31

Webmachine is a higher-level abstraction for HTTP .

slide-32
SLIDE 32

Webmachine is not a “framework.”

No built-in templating, no ORM or built-in storage. Webmachine is a good component in a framework.

slide-33
SLIDE 33

Webmachine is not an all-purpose network server.

No support for arbitrary sockets, etc. Webmachine is shaped like HTTP .

slide-34
SLIDE 34

Webmachine is

A toolkit for easily creating well-behaved HTTP systems.

a resource server for the Web.

slide-35
SLIDE 35

Webmachine is sincerely flattered.

dj-webmachine: clothesline: lemmachine: Django/Python Clojure Agda

slide-36
SLIDE 36

Webmachine

a practical executable model for HTTP http://webmachine.basho.com/

Justin Sheehy

justin@basho.com