Slide 1

Slide 1 text

Hidden Gems in HTTP Ben Ramsey ■ Atlanta PHP ■ 5 Nov 2009

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

That’s all I have to say about that.

Slide 6

Slide 6 text

Some properties of HTTP…

Slide 7

Slide 7 text

■ A client-server architecture ■ Atomic ■ Cacheable ■ A uniform interface ■ Layered ■ Code on demand

Slide 8

Slide 8 text

Now, what does that sound like?

Slide 9

Slide 9 text

REST!

Slide 10

Slide 10 text

And, that’s all I have to say about that, too.

Slide 11

Slide 11 text

Our focus today…

Slide 12

Slide 12 text

■ Semantic HTTP ■ Methods you’ve never used ■ Status codes you didn’t know existed ■ Working with HTTP in PHP

Slide 13

Slide 13 text

Semantics are important.

Slide 14

Slide 14 text

1 User requests page above their authorization level.

Slide 15

Slide 15 text

2 User is redirected to a login page where they are prompted to increase their authorization level.

Slide 16

Slide 16 text

GET /protected/content/1234 HTTP/1.1 Host: example.org HTTP/1.1 302 Found Date: Tue, 05 Nov 2009 17:34:24 GMT Server: Apache/2.2.14 (Unix) PHP/5.3.0 X-Powered-By: PHP/5.3.0 Location: /login Content-Length: 0 Content-Type: text/html; charset=utf-8

Slide 17

Slide 17 text

The resource requested is found at another location?

Slide 18

Slide 18 text

No, no, no. That’s not what we

Slide 19

Slide 19 text

The semantics are all wrong.

Slide 20

Slide 20 text

Methods you’ve never used…

Slide 21

Slide 21 text

Well, not really never.

Slide 22

Slide 22 text

■ You know GET ■ Retrieval of information ■ Transfers a representation of a resource from the server to the client ■ Safe & idempotent GET

Slide 23

Slide 23 text

GET /user/ramsey HTTP/1.1 Host: atom.example.org HTTP/1.1 200 OK Date: Tue, 22 Sep 2009 17:28:14 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 594 Content-Type: application/atom+xml;type=entry ramsey ...

Slide 24

Slide 24 text

He just thinks he’s funny.

Slide 25

Slide 25 text

Stop laughing. You’re just encouraging him.

Slide 26

Slide 26 text

POST ■ You know POST ■ The body content should be accepted as a new subordinate of the resource ■ Append, annotate, paste after ■ Not safe or idempotent

Slide 27

Slide 27 text

POST /user HTTP/1.1 Host: atom.example.org Content-Type: application/atom+xml;type=entry Content-Length: 474 ramsey ... HTTP/1.1 201 Created Date: Tue, 22 Sep 2009 17:39:06 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Location: http://atom.example.org/user/ramsey Content-Length: 133 Content-Type: text/html; charset=utf-8
The content was created at the location http://atom.example.org/user/ramsey

Slide 28

Slide 28 text

HEAD ■ Identical to GET, except… ■ Returns only the headers, not the body ■ Useful for getting details about a resource representation before retrieving the full representation ■ Safe & idempotent

Slide 29

Slide 29 text

HEAD /content/1234.mp4 HTTP/1.1 Host: atom.example.org HTTP/1.1 200 OK Date: Tue, 22 Sep 2009 17:28:14 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 12334753 Content-Type: application/mp4

Slide 30

Slide 30 text

PUT ■ Opposite of GET ■ Storage of information ■ Transfers a representation of a resource from the client to the server ■ Not safe ■ Idempotent

Slide 31

Slide 31 text

PUT /user/ramsey/ HTTP/1.1 Host: atom.example.org Content-Type: application/atom+xml;type=entry Content-Length: 594 ramsey ... HTTP/1.1 200 OK Date: Tue, 22 Sep 2009 17:47:27 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 594 Content-Type: application/atom+xml;type=entry ramsey ...

Slide 32

Slide 32 text

DELETE ■ Requests that the resource identified be removed from public access ■ Not safe ■ Idempotent

Slide 33

Slide 33 text

DELETE /content/1234/ HTTP/1.1 Host: example.org HTTP/1.1 204 No Content Date: Tue, 22 Sep 2009 18:06:37 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 0 Content-Type: text/html; charset=utf-8

Slide 34

Slide 34 text

What the hell are safe & idempotent methods?

Slide 35

Slide 35 text

Safe methods ■ GET & HEAD should not take action other than retrieval ■ These are considered safe ■ Allows agents to represent POST, PUT, & DELETE in a special way

Slide 36

Slide 36 text

Idempotence ■ Side-effects 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 37

Slide 37 text

Status codes you didn’t know existed

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

The look-before- you-leap request (LBYL)

Slide 40

Slide 40 text

1.Client sends a request without a body and includes the Expect: 100-continue header and all other headers 2.Server determines whether it will accept the request and responds with 100 Continue (or a 4xx code on error) 3.Client sends the request again with the body and without the Expect header

Slide 41

Slide 41 text

1 POST /content/videos HTTP/1.1 Host: example.org Content-Type: video/mp4 Content-Length: 115910000 Authorization: Basic bWFkZTp5b3VfbG9vaw== Expect: 100-continue

Slide 42

Slide 42 text

2 HTTP/1.1 413 Request Entity Too Large Date: Thu, 21 May 2009 23:05:15 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 0 Connection: close Content-Type: text/html Failure state

Slide 43

Slide 43 text

2 HTTP/1.1 100 Continue Date: Thu, 21 May 2009 23:05:15 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 0 Content-Type: text/html Success state

Slide 44

Slide 44 text

3 POST /content/videos HTTP/1.1 Host: example.org Content-Type: video/mp4 Content-Length: 115910000 Authorization: Basic bWFkZTp5b3VfbG9vaw== {binary video data}

Slide 45

Slide 45 text

4 HTTP/1.1 201 Created Date: Thu, 21 May 2009 23:05:34 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 119 Content-Type: text/html Location: http://example.org/content/videos/1234

Video uploaded! Go here to see it.

Slide 46

Slide 46 text

■ There are some problems with supporting 100 Continue from PHP through Apache ■ One suggestion is to use X-Expect instead of Expect ■ But there are still odd problems occurring that I can’t explain Caveat

Slide 47

Slide 47 text

The created at another location response

Slide 48

Slide 48 text

1 POST /content/videos HTTP/1.1 Host: example.org Content-Type: video/mp4 Content-Length: 115910000 Authorization: Basic bWFkZTp5b3VfbG9vaw== {binary video data}

Slide 49

Slide 49 text

2 HTTP/1.x 201 Created Date: Thu, 21 May 2009 23:05:34 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 120 Content-Type: text/html Location: http://example.org/content/videos/1234

Video uploaded! Go here to see it.

Slide 50

Slide 50 text

The “it’s not you it’s me” response

Slide 51

Slide 51 text

i.e. I’ve accepted it but might have to do more processing

Slide 52

Slide 52 text

2 HTTP/1.x 202 Accepted Date: Thu, 21 May 2009 23:05:34 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 137 Content-Type: text/html Location: http://example.org/content/videos/1234/status

Video processing! Check here for the status.

Slide 53

Slide 53 text

The “I have nothing to say to you” response…

Slide 54

Slide 54 text

…but you were still successful

Slide 55

Slide 55 text

1 DELETE /content/videos/1234 HTTP/1.1 Host: example.org Authorization: Basic bWFkZTp5b3VfbG9vaw==

Slide 56

Slide 56 text

2 HTTP/1.x 204 No Content Date: Thu, 21 May 2009 23:28:34 GMT

Slide 57

Slide 57 text

The ranged request

Slide 58

Slide 58 text

■ Used when requests are made for ranges of bytes from a resource ■ Determine whether a server supports range requests by checking for the Accept-Ranges header with HEAD

Slide 59

Slide 59 text

1 HEAD /2390/2253727548_a413c88ab3_s.jpg HTTP/1.1 Host: farm3.static.flickr.com

Slide 60

Slide 60 text

2 HTTP/1.0 200 OK Date: Mon, 05 May 2008 00:33:14 GMT Server: Apache/2.0.52 (Red Hat) Accept-Ranges: bytes Content-Length: 3980 Content-Type: image/jpeg

Slide 61

Slide 61 text

3 GET /2390/2253727548_a413c88ab3_s.jpg HTTP/1.1 Host: farm3.static.flickr.com Range: bytes=0-999

Slide 62

Slide 62 text

4 HTTP/1.0 206 Partial Content Date: Mon, 05 May 2008 00:36:57 GMT Server: Apache/2.0.52 (Red Hat) Accept-Ranges: bytes Content-Length: 1000 Content-Range: bytes 0-999/3980 Content-Type: image/jpeg {binary data}

Slide 63

Slide 63 text

The GET me from another location response

Slide 64

Slide 64 text

■ 303 See Other ■ The response to your request can be found at another URL identified by the Location header ■ The client should make a GET request on that URL ■ The Location is not a substitute for this URL

Slide 65

Slide 65 text

1 POST /contact HTTP/1.1 Host: example.org Content-Type: application/x-www-form-urlencoded Content-Length: 1234 {url-encoded form values from a contact form}

Slide 66

Slide 66 text

2 HTTP/1.1 303 See Other Date: Tue, 22 Sep 2009 23:41:33 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Location: http://example.org/thankyou Content-Length: 0

Slide 67

Slide 67 text

The find me temporarily at this place response

Slide 68

Slide 68 text

■ 307 Temporary Redirect ■ The resource resides temporarily at the URL identified by the Location ■ The Location may change, so don’t update your links ■ If the request is not GET or HEAD, then you must allow the user to confirm the action

Slide 69

Slide 69 text

The permanent forwarding address response

Slide 70

Slide 70 text

■ 301 Moved Permanently ■ The resource has moved permanently to the URL indicated by the Location header ■ You should update your links accordingly ■ Great for forcing search engines, etc. to index the new URL instead of this one

Slide 71

Slide 71 text

But what about just finding the resource at another location?

Slide 72

Slide 72 text

■ 302 Found ■ The resource has been found at another URL identified by the Location header ■ The new URL might be temporary, so the client should continue to use this URL ■ Redirections SHOULD be confirmed by the user (in practice, browsers don’t respect this)

Slide 73

Slide 73 text

The data validation error response

Slide 74

Slide 74 text

■ 400 Bad Request ■ Generic error message ■ The client sent malformed syntax ■ The client needs to modify the request before sending it again (to fix errors)

Slide 75

Slide 75 text

POST /user/ HTTP/1.1 Host: atom.example.org Content-Type: application/atom+xml;type=entry Content-Length: 474 r@msey ... HTTP/1.1 400 Bad Request Date: Tue, 22 Sep 2009 23:51:00 GMT Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0 X-Powered-By: PHP/5.3.0 Content-Length: 123 Connection: close Content-Type: text/html; charset=utf-8
The following errors occurred:
  • Title contained invalid characters

Slide 76

Slide 76 text

The login required response

Slide 77

Slide 77 text

1 User requests page above their authorization level. Remember this? GET /protected/content/1234 HTTP/1.1 Host: example.org

Slide 78

Slide 78 text

2 User is redirected to a login page where they are prompted to increase their authorization level. HTTP/1.1 302 Found Date: Tue, 05 Nov 2009 17:34:24 GMT Server: Apache/2.2.14 (Unix) PHP/5.3.0 X-Powered-By: PHP/5.3.0 Location: /login Content-Length: 0 Content-Type: text/html; charset=utf-8

Slide 79

Slide 79 text

A more semantic way:

Slide 80

Slide 80 text

1 GET /protected/content/1234 HTTP/1.1 Host: example.org

Slide 81

Slide 81 text

2 HTTP/1.1 401 Unauthorized Date: Tue, 05 Nov 2009 18:31:33 GMT Server: Apache/2.2.14 (Unix) PHP/5.3.0 X-Powered-By: PHP/5.3.0 WWW-Authenticate: HTML form="login" Content-Length: 421 Content-Type: text/html; charset=utf-8

Slide 82

Slide 82 text

You must log in Username Password

Slide 83

Slide 83 text

■ Doesn’t imply the resource exists at another location ■ Tells clients the resource requires authorization ■ Clearly tells crawlers they can’t access the resource ■ Was originally in HTML5:
 http://blog.whatwg.org/this-week-in-html-5-episode-14 ■ No longer in HTML5, but it works

Slide 84

Slide 84 text

But wait! There’s more…

Slide 85

Slide 85 text

Working with HTTP in PHP

Slide 86

Slide 86 text

■ header() function
 http://php.net/header ■ Client URL library (cURL)
 http://php.net/curl ■ Streams
 http://php.net/streams ■ HTTP extension (pecl/http)
 http://php.net/http

Slide 87

Slide 87 text

Questions? ■ My website is benramsey.com ■ @ramsey on Twitter ■ Read the HTTP spec at
 tools.ietf.org/html/rfc2616 ■ My company is Schematic
 schematic.com

Slide 88

Slide 88 text

Hidden Gems in HTTP Copyright © Ben Ramsey. Some rights reserved. This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License. For uses not covered under this license, please contact the author.