Slide 1

Slide 1 text

Ben Ramsey Grokking HTTP

Slide 2

Slide 2 text

Why HTTP?

Slide 3

Slide 3 text

Because you are a web developer.

Slide 4

Slide 4 text

HTTP is the Web.

Slide 5

Slide 5 text

• A client-server architecture • Atomic operations • Cacheable responses • A uniform and constrained interface • Layered system • Allows for code on demand

Slide 6

Slide 6 text

HTTP is RESTful.

Slide 7

Slide 7 text

• My favorite HTTP tools • Overview of the protocol • Content negotiation • Conditional requests • Caching • The future of HTTP Grokking HTTP

Slide 8

Slide 8 text

grok • /ˈɡrɒk/ To grok is to intimately and completely share the same reality or line of thinking with another physical or conceptual entity. Author Robert A. Heinlein coined the term in his best-selling 1961 book Stranger in a Strange Land. In Heinlein's view, grokking is the intermingling of intelligence that necessarily a!ects both the observer and the observed. —from Wikipedia, http://en.wikipedia.org/wiki/Grok

Slide 9

Slide 9 text

My Favorite Tools

Slide 10

Slide 10 text

HTTPie • Ditch cURL. Use HTTPie. • httpie.org • Perfect for testing and debugging APIs • Free; requires Python

Slide 11

Slide 11 text

Charles • I cannot recommend this enough! • charlesproxy.com • Perfect for debugging Ajax and Flash remoting (AMF) requests • Well worth the $50 license fee

Slide 12

Slide 12 text

Setting Headers array( 'self' => array( 'href' => 'http://example.com/books/decd0562', ), ), 'author' => 'Stoyan Stefanov', 'id' => 'decd0562', 'isbn10' => '1449320198', 'isbn13' => '9781449320195', 'publisher' => 'O\'Reilly Media', 'title' => 'JavaScript for PHP Developers', 'year' => 2012, ));

Slide 13

Slide 13 text

Other Tools • PHP cURL functions • fopen wrappers for http/https • "le_get_contents() • "le_put_contents() • stream_context_create() • PHP pecl_http extension

Slide 14

Slide 14 text

Protocol Overview

Slide 15

Slide 15 text

RFC 2616

Slide 16

Slide 16 text

• GET • POST • PUT • DELETE • HEAD • OPTIONS • TRACE • CONNECT Methods

Slide 17

Slide 17 text

Safe Methods • GET & HEAD should not take action other than retrieval • These are considered safe • This allows user agents to represent POST, PUT, & DELETE in a special way

Slide 18

Slide 18 text

Idempotence • Side e!ects of N > 0 identical requests is the same as for a single request • GET, HEAD, PUT, and DELETE share this property • OPTIONS and TRACE are inherently idempotent

Slide 19

Slide 19 text

GET

Slide 20

Slide 20 text

GET /books/9790482c HTTP/1.1 Host: example.com Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0

Slide 21

Slide 21 text

GET /books/9790482c HTTP/1.1 Host: example.com Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0

Slide 22

Slide 22 text

GET /books/9790482c HTTP/1.1 Host: example.com Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0

Slide 23

Slide 23 text

HTTP/1.1 200 OK Date: Sun, 29 Jul 2012 22:46:43 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 ETag: "9790482c-1" Last-Modified: Sun, 15 Jul 2012 16:34:23 GMT Content-Length: 254 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/9790482c" } }, "author": "Luke Welling, Laura Thomson", "id": "9790482c", "isbn10": "0672329166", "isbn13": "9780672329166", "publisher": "Pearson Education", "title": "PHP and MySQL Web Development", "year": 2008 }

Slide 24

Slide 24 text

HTTP/1.1 200 OK Date: Sun, 29 Jul 2012 22:46:43 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 ETag: "9790482c-1" Last-Modified: Sun, 15 Jul 2012 16:34:23 GMT Content-Length: 254 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/9790482c" } }, "author": "Luke Welling, Laura Thomson", "id": "9790482c", "isbn10": "0672329166", "isbn13": "9780672329166", "publisher": "Pearson Education", "title": "PHP and MySQL Web Development", "year": 2008 }

Slide 25

Slide 25 text

HTTP/1.1 200 OK Date: Sun, 29 Jul 2012 22:46:43 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 ETag: "9790482c-1" Last-Modified: Sun, 15 Jul 2012 16:34:23 GMT Content-Length: 254 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/9790482c" } }, "author": "Luke Welling, Laura Thomson", "id": "9790482c", "isbn10": "0672329166", "isbn13": "9780672329166", "publisher": "Pearson Education", "title": "PHP and MySQL Web Development", "year": 2008 }

Slide 26

Slide 26 text

HTTP/1.1 200 OK Date: Sun, 29 Jul 2012 22:46:43 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 ETag: "9790482c-1" Last-Modified: Sun, 15 Jul 2012 16:34:23 GMT Content-Length: 254 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/9790482c" } }, "author": "Luke Welling, Laura Thomson", "id": "9790482c", "isbn10": "0672329166", "isbn13": "9780672329166", "publisher": "Pearson Education", "title": "PHP and MySQL Web Development", "year": 2008 }

Slide 27

Slide 27 text

POST

Slide 28

Slide 28 text

POST /books HTTP/1.1 Host: example.com Content-Type: application/hal+json Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0 { "author": "Stoyan Stefanov", "isbn10": "1449320198", "isbn13": "9781449320195", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 29

Slide 29 text

HTTP/1.1 201 Created Date: Sun, 29 Jul 2012 23:26:49 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Location: http://example.com/books/decd0562 ETag: "decd0562-1" Last-Modified: Sun, 29 Jul 2012 23:26:49 GMT Content-Length: 239 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 30

Slide 30 text

HTTP/1.1 201 Created Date: Sun, 29 Jul 2012 23:26:49 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Location: http://example.com/books/decd0562 ETag: "decd0562-1" Last-Modified: Sun, 29 Jul 2012 23:26:49 GMT Content-Length: 239 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 31

Slide 31 text

HTTP/1.1 201 Created Date: Sun, 29 Jul 2012 23:26:49 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Location: http://example.com/books/decd0562 ETag: "decd0562-1" Last-Modified: Sun, 29 Jul 2012 23:26:49 GMT Content-Length: 239 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 32

Slide 32 text

HTTP/1.1 201 Created Date: Sun, 29 Jul 2012 23:26:49 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Location: http://example.com/books/decd0562 ETag: "decd0562-1" Last-Modified: Sun, 29 Jul 2012 23:26:49 GMT Content-Length: 239 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 33

Slide 33 text

PUT

Slide 34

Slide 34 text

PUT /books/decd0562 HTTP/1.1 Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0 Host: example.com If-Match: "decd0562-1" Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "pubDate": "September 22, 2012", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 35

Slide 35 text

PUT /books/decd0562 HTTP/1.1 Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0 Host: example.com If-Match: "decd0562-1" Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "pubDate": "September 22, 2012", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 36

Slide 36 text

PUT /books/decd0562 HTTP/1.1 Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0 Host: example.com If-Match: "decd0562-1" Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "pubDate": "September 22, 2012", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 37

Slide 37 text

HTTP/1.1 200 OK Date: Sun, 29 Jul 2012 23:47:59 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 ETag: "decd0562-2" Last-Modified: Sun, 29 Jul 2012 23:47:59 GMT Content-Length: 270 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "pubDate": "September 22, 2012", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 38

Slide 38 text

HTTP/1.1 200 OK Date: Sun, 29 Jul 2012 23:47:59 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 ETag: "decd0562-2" Last-Modified: Sun, 29 Jul 2012 23:47:59 GMT Content-Length: 270 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "pubDate": "September 22, 2012", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 39

Slide 39 text

HTTP/1.1 200 OK Date: Sun, 29 Jul 2012 23:47:59 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 ETag: "decd0562-2" Last-Modified: Sun, 29 Jul 2012 23:47:59 GMT Content-Length: 270 Content-Type: application/hal+json { "_links": { "self": { "href": "http://example.com/books/decd0562" } }, "author": "Stoyan Stefanov", "id": "decd0562", "isbn10": "1449320198", "isbn13": "9781449320195", "pubDate": "September 22, 2012", "publisher": "O'Reilly Media", "title": "JavaScript for PHP Developers", "year": 2012 }

Slide 40

Slide 40 text

DELETE

Slide 41

Slide 41 text

DELETE /books/decd0562 HTTP/1.1 Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0 Host: example.com If-Match: "decd0562-2"

Slide 42

Slide 42 text

HTTP/1.1 204 No Content Date: Mon, 30 Jul 2012 00:01:44 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Content-Length: 0 Content-Type: application/hal+json

Slide 43

Slide 43 text

HTTP/1.1 204 No Content Date: Mon, 30 Jul 2012 00:01:44 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Content-Length: 0 Content-Type: application/hal+json

Slide 44

Slide 44 text

Status Codes

Slide 45

Slide 45 text

• Informational (1xx) • Successful (2xx) • Redirection (3xx) • Client error (4xx) • Server error (5xx)

Slide 46

Slide 46 text

Content Negotiation

Slide 47

Slide 47 text

• Also called “conneg” • “the process of selecting the best representation for a given response when there are multiple representations available” • Two kinds: • server-driven • agent-driven

Slide 48

Slide 48 text

Server-driven Negotiation

Slide 49

Slide 49 text

GET /books/9790482c HTTP/1.1 Accept-Charset: utf-8 Host: example.com Accept-Language: en-us, en-gb;q=0.8, en;q=0.7 Accept-Encoding: gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0

Slide 50

Slide 50 text

GET /books/9790482c HTTP/1.1 Accept-Charset: utf-8 Host: example.com Accept-Language: en-us, en-gb;q=0.8, en;q=0.7 Accept-Encoding: gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0

Slide 51

Slide 51 text

GET /books/9790482c HTTP/1.1 Accept-Charset: utf-8 Host: example.com Accept-Language: en-us, en-gb;q=0.8, en;q=0.7 Accept-Encoding: gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0

Slide 52

Slide 52 text

GET /books/9790482c HTTP/1.1 Accept-Charset: utf-8 Host: example.com Accept-Language: en-us, en-gb;q=0.8, en;q=0.7 Accept-Encoding: gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0

Slide 53

Slide 53 text

GET /books/9790482c HTTP/1.1 Accept-Charset: utf-8 Host: example.com Accept-Language: en-us, en-gb;q=0.8, en;q=0.7 Accept-Encoding: gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0

Slide 54

Slide 54 text

HTTP/1.1 200 OK Date: Mon, 30 Jul 2012 02:42:26 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Content-Language: en-us ETag: "9790482c-1" Vary: Accept,Accept-Charset,Accept-Language,Accept-Encoding Content-Encoding: gzip Content-Length: 213 Content-Type: application/hal+json; charset=utf-8 { ... }

Slide 55

Slide 55 text

HTTP/1.1 200 OK Date: Mon, 30 Jul 2012 02:42:26 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Content-Language: en-us ETag: "9790482c-1" Vary: Accept,Accept-Charset,Accept-Language,Accept-Encoding Content-Encoding: gzip Content-Length: 213 Content-Type: application/hal+json; charset=utf-8 { ... }

Slide 56

Slide 56 text

HTTP/1.1 200 OK Date: Mon, 30 Jul 2012 02:42:26 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Content-Language: en-us ETag: "9790482c-1" Vary: Accept,Accept-Charset,Accept-Language,Accept-Encoding Content-Encoding: gzip Content-Length: 213 Content-Type: application/hal+json; charset=utf-8 { ... }

Slide 57

Slide 57 text

HTTP/1.1 200 OK Date: Mon, 30 Jul 2012 02:42:26 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Content-Language: en-us ETag: "9790482c-1" Vary: Accept,Accept-Charset,Accept-Language,Accept-Encoding Content-Encoding: gzip Content-Length: 213 Content-Type: application/hal+json; charset=utf-8 { ... }

Slide 58

Slide 58 text

HTTP/1.1 200 OK Date: Mon, 30 Jul 2012 02:42:26 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Content-Language: en-us ETag: "9790482c-1" Vary: Accept,Accept-Charset,Accept-Language,Accept-Encoding Content-Encoding: gzip Content-Length: 213 Content-Type: application/hal+json; charset=utf-8 { ... }

Slide 59

Slide 59 text

Agent-driven Negotiation

Slide 60

Slide 60 text

GET /books/9790482c HTTP/1.1 Host: example.com User-Agent: HTTPie/0.2.0

Slide 61

Slide 61 text

HTTP/1.1 300 Multiple Choices Date: Mon, 30 Jul 2012 02:57:42 GMT Server: Apache/2.2.22 (Ubuntu) X-Powered-By: PHP/5.3.10-1ubuntu3.2 Content-Length: 444 Content-Type: application/hal+json

Slide 62

Slide 62 text

{ "_links": { "alternate": [ { "href": "http://example.com/books/9790482c.en-us.html", "hreflang": "en-us", "type": "text/html; charset=utf-8" }, { "href": "http://example.com/books/9790482c.en-us.json", "hreflang": "en-us", "type": "application/hal+json; charset=utf-8" }, { "href": "http://example.com/books/9790482c.en-us.xml", "hreflang": "en-us", "type": "application/hal+xml; charset=utf-8" } ], "self": { "href": "http://example.com/books/9790482c" } } }

Slide 63

Slide 63 text

Conditional Requests

Slide 64

Slide 64 text

Request Headers • If-Modi"ed-Since • If-Unmodi"ed-Since • If-Match • If-None-Match • If-Range

Slide 65

Slide 65 text

GET /books/9790482c HTTP/1.1 Host: example.com Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0 If-Modified-Since: Sun, 15 Jul 2012 16:34:23 GMT HTTP/1.1 304 Not Modified Date: Mon, 30 Jul 2012 03:39:51 GMT Server: Apache/2.2.22 (Ubuntu) Vary: Accept-Encoding

Slide 66

Slide 66 text

GET /books/9790482c HTTP/1.1 Host: example.com Accept-Encoding: identity, deflate, compress, gzip Accept: application/hal+json User-Agent: HTTPie/0.2.0 If-Modified-Since: Sun, 15 Jul 2012 16:34:23 GMT HTTP/1.1 304 Not Modified Date: Mon, 30 Jul 2012 03:39:51 GMT Server: Apache/2.2.22 (Ubuntu) Vary: Accept-Encoding

Slide 67

Slide 67 text

Caching

Slide 68

Slide 68 text

Response Headers • Expires • Cache-Control • max-age • s-maxage • public • private • no-cache • no-store • must-revalidate • proxy-revalidate

Slide 69

Slide 69 text

Cache-Control: max-age=3600, must-revalidate

Slide 70

Slide 70 text

The Future of HTTP

Slide 71

Slide 71 text

• HTTP Bis IETF Working Group http://tools.ietf.org/wg/httpbis/ • RFCs and speci"cations that extend HTTP: • WebDAV and related extensions • RFC 5789 (PATCH Method) • RFC 6266 (Use of Content-Disposition) • RFC 6585 (Additional Status Codes) • Method Registrations • Authentication Scheme Registrations • Permanent Message Header Fields

Slide 72

Slide 72 text

There’s too much in HTTP to fully grok it in one presentation.

Slide 73

Slide 73 text

• RFC 2616 (HTTP) • Mark Nottingham: mnot.net • Mark’s Caching Tutorial • RFC 2295 (Transparent Content Negotiation in HTTP) • RFC 2296 (HTTP Remote Variant Selection Algorithm) More Resources

Slide 74

Slide 74 text

Thank you. • Rate this talk: joind.in/6717 • Read my blog: benramsey.com • Follow me on Twitter: @ramsey • Slides: bram.se/cf12-http

Slide 75

Slide 75 text

Grokking HTTP Copyright © Ben Ramsey. Some rights reserved. This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported. For uses not covered under this license, please contact the author.