Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTTP: The Good Parts

Sean Cribbs
September 26, 2014

HTTP: The Good Parts

REST demands we think about resources, representations, and rich interactions; however, it's easy to forget the substrate upon which our beautiful hypermedia APIs "rest". HTTP has lots of interesting and powerful ways to enhance interaction and efficiency that most applications leave untapped because they focus so much on the act of processing a request, rather than the properties of the resource being requested. Let's look more closely at those HTTP features and then see how you can tap into them more declaratively with Webmachine.

Sean Cribbs

September 26, 2014
Tweet

More Decks by Sean Cribbs

Other Decks in Programming

Transcript

  1. HTTP/0.9 "The protocol uses the normal internet-style telnet protocol style

    on a TCP-IP link. The following Describes how a client acquires a (hypertext) document from an HTTP server, given an HTTP document address."
  2. HTTP/0.9 "The protocol uses the normal internet-style telnet protocol style

    on a TCP-IP link. The following Describes how a client acquires a (hypertext) document from an HTTP server, given an HTTP document address." "The client sends a document request consisting of a line of ASCII characters terminated by a CR LF...The response to a simple GET request is a message in hypertext mark-up language ( HTML ). This is a byte stream of ASCII characters."
  3. “Basic HTTP” “It is a generic stateless object-oriented protocol, which

    may be used for many similar tasks such as name servers, and distributed object-oriented systems, by extending the commands, or "methods", used.”
  4. “Basic HTTP” “It is a generic stateless object-oriented protocol, which

    may be used for many similar tasks such as name servers, and distributed object-oriented systems, by extending the commands, or "methods", used.” Media types
  5. “Basic HTTP” “It is a generic stateless object-oriented protocol, which

    may be used for many similar tasks such as name servers, and distributed object-oriented systems, by extending the commands, or "methods", used.” Media types Content Negotiation
  6. “Basic HTTP” “It is a generic stateless object-oriented protocol, which

    may be used for many similar tasks such as name servers, and distributed object-oriented systems, by extending the commands, or "methods", used.” Media types Content Negotiation
  7. “Basic HTTP” “It is a generic stateless object-oriented protocol, which

    may be used for many similar tasks such as name servers, and distributed object-oriented systems, by extending the commands, or "methods", used.” Media types Content Negotiation Conditional requests
  8. “Basic HTTP” “It is a generic stateless object-oriented protocol, which

    may be used for many similar tasks such as name servers, and distributed object-oriented systems, by extending the commands, or "methods", used.” Media types Content Negotiation Conditional requests URI
  9. “Basic HTTP” “It is a generic stateless object-oriented protocol, which

    may be used for many similar tasks such as name servers, and distributed object-oriented systems, by extending the commands, or "methods", used.” Media types Content Negotiation Conditional requests URI Payment, versioning, locking
  10. “Basic HTTP” “It is a generic stateless object-oriented protocol, which

    may be used for many similar tasks such as name servers, and distributed object-oriented systems, by extending the commands, or "methods", used.” Media types Content Negotiation Conditional requests URI Payment, versioning, locking LINK / UNLINK
  11. HTTP/1.0 RFC 1945 GET / HEAD / POST 302 Moved

    Temporarily x-gzip, x-compress Definition of “Safe” methods
  12. Constraint Promotes At the expense of Client-Server Stateless Optional non-shared

    Caching Uniform interface Layered system Optional code-on- demand UI Portability Simplified Server Multiple organizational domains Simplified Server Scalability Reliability Efficiency Reduced latency Efficiency Scalability Reliability Visibility Independent evolution Decoupled implementation Efficiency Shared caching Legacy encapsulation Simplified clients Scalability Load balancing Higher latency Simplified clients Extensibility Visibility
  13. Constraint Promotes At the expense of Client-Server Stateless Optional non-shared

    Caching Uniform interface Layered system Optional code-on- demand UI Portability Simplified Server Multiple organizational domains Simplified Server Scalability Reliability Efficiency Reduced latency Efficiency Scalability Reliability Visibility Independent evolution Decoupled implementation Efficiency Shared caching Legacy encapsulation Simplified clients Scalability Load balancing Higher latency Simplified clients Extensibility Visibility •Identification of resources •Manipulation via representations •Self-descriptive messages •HATEOAS
  14. Constraint Promotes At the expense of Client-Server Stateless Optional non-shared

    Caching Uniform interface Layered system Optional code-on- demand UI Portability Simplified Server Multiple organizational domains Simplified Server Scalability Reliability Efficiency Reduced latency Efficiency Scalability Reliability Visibility Independent evolution Decoupled implementation Efficiency Shared caching Legacy encapsulation Simplified clients Scalability Load balancing Higher latency Simplified clients Extensibility Visibility •Identification of resources •Manipulation via representations •Self-descriptive messages •HATEOAS
  15. State Transfer Request ! S ! (Response, S’) Request !

    (S ! (Response, S’)) Request ! Response
  16. State Transfer Request ! S ! (Response, S’) Request !

    (S ! (Response, S’)) (Request ! S) ! (Response, S’) Request ! Response
  17. Request ! (Request ! S) ! (Response, S’) State Transfer

    Request ! S ! (Response, S’) Request ! (S ! (Response, S’)) Request ! Response
  18. Protocol, n. 1. The official procedure governing affairs of state

    or diplomatic occasions. 2. The established code of procedure or behavior in any group, organization, or situation.
  19. Function 㱺 FSM Req ! Res ! S ! (Res',

    S') Request ! S ! (Response, S’)
  20. Extract Req ! Res ! S ! (Res', S') Req

    ! Res ! S ! (V, Res', S')
  21. Extract Req ! Res ! S ! (Res', S') Req

    ! Res ! S ! (V, Res', S') Req ! Res ! V ! Res'
  22. Resource exists? 404 NOT FOUND REDIRECTION CREATION { 200 OK

    CONDITION VALIDATION DELETION UPDATE / REPLACE {
  23. Resource exists? 404 NOT FOUND REDIRECTION CREATION { 200 OK

    CONDITION VALIDATION DELETION UPDATE / REPLACE { Req ! Res ! S ! (Bool, Res, S')
  24. Resource exists? FETCH THE INTERNAL REPRESENTATION OF THE RESOURCE 404

    NOT FOUND REDIRECTION CREATION { 200 OK CONDITION VALIDATION DELETION UPDATE / REPLACE { Req ! Res ! S ! (Bool, Res, S')
  25. c. 2007 by Sheehy, Latt, Gross et al Erlang, Ruby,

    Node.js, Go, Clojure, Perl, Scala, Haskell*
  26. Restricted subset of HTTP/1.1 Declarative: derive facts about your resource

    Removes need to decide many things “Just leaky enough”
  27. Full-stack web framework An “API tool” For processing arbitrary requests

    For Websockets “Easy” (but it is “correct”) Webmachine is NOT
  28. Semantics Req ! Res ! S ! (V, Res', S')

    predicate(ReqData, State) -> %% True referential transparency, %% through immutability {SomeValue, ReqData, State}. def predicate # @request, @response are bound # State is instance variables some_value end
  29. Redirection/Obsoletion resource_exists(ReqData, State) -> {false, ReqData, State}. previously_existed(ReqData, State) ->

    {true, ReqData, State}. moved_permanently(ReqData, State) -> {{true, “http://google.com”}, ReqData, State}.