SlideShare une entreprise Scribd logo
1  sur  72
Télécharger pour lire hors ligne
Hidden Gems in HTTP
Ben Ramsey ■ Code Works
Why HTTP?
Because you are a
Web developer.
HTTP is the Web.
That’s all I have to
say about that.
Some properties of
HTTP…
■ A client-server architecture
■ Atomic
■ Cacheable
■ A uniform interface
■ Layered
■ Code on demand
Now, what does
that sound like?
REST!
And, that’s all I have
to say about that,
too.
Our focus today…
■ Methods you’ve never used

■ Status codes you didn’t know existed

■ Working with HTTP in PHP
Methods you’ve
never used…
Well, not really
never.
GET
■
    You know GET
■
    Retrieval of information
■
    Transfers a representation of a resource
    from the server to the client
■
    Safe & idempotent
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

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
       xml:base="http://atom.example.org/">
 <title>ramsey</title>
 ...
</entry>
He just thinks he’s
funny.
Stop laughing.
You’re just
encouraging him.
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
POST /user HTTP/1.1
Host: atom.example.org
Content-Type: application/atom+xml;type=entry
Content-Length: 474

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
       xml:base="http://atom.example.org/">
 <title>ramsey</title>
 ...
</entry>

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

<div>
  The content was created at the location
  <a href="/user/ramsey">
    http://atom.example.org/user/ramsey
  </a>
</div>
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
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
PUT
■
    Opposite of GET
■
    Storage of information
■
    Transfers a representation of a resource
    from the client to the server
■
    Not safe
■
    Idempotent
PUT /user/ramsey/ HTTP/1.1
Host: atom.example.org
Content-Type: application/atom+xml;type=entry
Content-Length: 594

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
       xml:base="http://atom.example.org/">
 <title>ramsey</title>
 ...
</entry>

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

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
       xml:base="http://atom.example.org/">
 <title>ramsey</title>
 ...
</entry>
DELETE

■
    Requests that the resource identified be
    removed from public access
■
    Not safe
■
    Idempotent
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
What the hell are
safe & idempotent
methods?
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
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
Status codes you
didn’t know existed
■ Informational (1xx)

■ Successful (2xx)

■ Redirection (3xx)

■ Client error (4xx)

■ Server error (5xx)
The look-before-
you-leap request
(LBYL)
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
1



POST /content/videos HTTP/1.1
Host: example.org
Content-Type: video/mp4
Content-Length: 115910000
Authorization: Basic bWFkZTp5b3VfbG9vaw==
Expect: 100-continue
Failure state
                       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
Success state
                       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
3



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

{binary video data}
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

<html><body><p>Video uploaded! Go <a
href="http://example.org/content/videos/
1234">here</a> to see it.</p></body></html>
The created at
another location
response
1



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

{binary video data}
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

<html><body><p>Video uploaded! Go <a
href="http://example.org/content/videos/
1234">here</a> to see it.</p></body></html>
The “it’s not you it’s
me” response
i.e. I’ve accepted it
but might have to
do more processing
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

<html><body><p>Video processing! Check <a
href="http://example.org/content/videos/1234/
status">here</a> for the status.</p></body></
html>
The “I have nothing
to say to you”
response…
…but you were still
successful
1



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



HTTP/1.x 204 No Content
Date: Thu, 21 May 2009 23:28:34 GMT
The ranged request
■ 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
1



HEAD /2390/2253727548_a413c88ab3_s.jpg
HTTP/1.1
Host: farm3.static.flickr.com
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
3



GET /2390/2253727548_a413c88ab3_s.jpg HTTP/1.1
Host: farm3.static.flickr.com
Range: bytes=0-999
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}
The GET me from
another location
response
■ 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
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}
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
The find me
temporarily at this
place response
■ 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
The permanent
forwarding address
response
■ 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
But what about just
finding the resource
at another location?
■ 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)
The data validation
error response
■ 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)
POST /user/ HTTP/1.1
Host: atom.example.org
Content-Type: application/atom+xml;type=entry
Content-Length: 474

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
       xml:base="http://atom.example.org/">
 <title>r@msey</title>
 ...
</entry>

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

<div class="error">
  The following errors occurred:
  <ul>
    <li>Title contained invalid characters</li>
  </ul>
</div>
But wait! There’s
more…
Working with HTTP
in PHP
■ 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
Questions?
■
    My website is benramsey.com
■
    @ramsey on Twitter
■
    Rate this talk at joind.in
■
    Read the HTTP spec at
    tools.ietf.org/html/rfc2616
■
    My company is Schematic
    schematic.com
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.

Contenu connexe

Tendances

HTTP - The Protocol of Our Lives
HTTP - The Protocol of Our LivesHTTP - The Protocol of Our Lives
HTTP - The Protocol of Our LivesBrent Shaffer
 
5 steps to faster web sites & HTML5 games - updated for DDDscot
5 steps to faster web sites & HTML5 games - updated for DDDscot5 steps to faster web sites & HTML5 games - updated for DDDscot
5 steps to faster web sites & HTML5 games - updated for DDDscotMichael Ewins
 
Apache httpd-2.4 : Watch out cloud!
Apache httpd-2.4 : Watch out cloud!Apache httpd-2.4 : Watch out cloud!
Apache httpd-2.4 : Watch out cloud!Jim Jagielski
 
WE18_Performance_Up.ppt
WE18_Performance_Up.pptWE18_Performance_Up.ppt
WE18_Performance_Up.pptwebhostingguy
 
[MathWorks] Versioning Infrastructure
[MathWorks] Versioning Infrastructure[MathWorks] Versioning Infrastructure
[MathWorks] Versioning InfrastructurePerforce
 
HTTP Caching in Web Application
HTTP Caching in Web ApplicationHTTP Caching in Web Application
HTTP Caching in Web ApplicationMartins Sipenko
 
Nginx, PHP, Apache and Spelix
Nginx, PHP, Apache and SpelixNginx, PHP, Apache and Spelix
Nginx, PHP, Apache and SpelixHarald Zeitlhofer
 
HAProxy scale out using open source
HAProxy scale out using open sourceHAProxy scale out using open source
HAProxy scale out using open sourceIngo Walz
 
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018Codemotion
 
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018Thijs Feryn
 
Bandwidth limiting howto
Bandwidth limiting howtoBandwidth limiting howto
Bandwidth limiting howtoDien Hien Tran
 
Load Balancing with Apache
Load Balancing with ApacheLoad Balancing with Apache
Load Balancing with ApacheBradley Holt
 
I got 99 problems, but ReST ain't one
I got 99 problems, but ReST ain't oneI got 99 problems, but ReST ain't one
I got 99 problems, but ReST ain't oneAdrian Cole
 
Introduction to HTTP/2
Introduction to HTTP/2Introduction to HTTP/2
Introduction to HTTP/2Ido Flatow
 
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...Michele Orru
 
HTTP/2 standard for video streaming
HTTP/2 standard for video streamingHTTP/2 standard for video streaming
HTTP/2 standard for video streamingHung Thai Le
 
Web Server Load Balancer
Web Server Load BalancerWeb Server Load Balancer
Web Server Load BalancerMobME Technical
 
Altitude San Francisco 2018: Testing with Fastly Workshop
Altitude San Francisco 2018: Testing with Fastly WorkshopAltitude San Francisco 2018: Testing with Fastly Workshop
Altitude San Francisco 2018: Testing with Fastly WorkshopFastly
 

Tendances (20)

HTTP - The Protocol of Our Lives
HTTP - The Protocol of Our LivesHTTP - The Protocol of Our Lives
HTTP - The Protocol of Our Lives
 
are available here
are available hereare available here
are available here
 
5 steps to faster web sites & HTML5 games - updated for DDDscot
5 steps to faster web sites & HTML5 games - updated for DDDscot5 steps to faster web sites & HTML5 games - updated for DDDscot
5 steps to faster web sites & HTML5 games - updated for DDDscot
 
Apache httpd-2.4 : Watch out cloud!
Apache httpd-2.4 : Watch out cloud!Apache httpd-2.4 : Watch out cloud!
Apache httpd-2.4 : Watch out cloud!
 
WE18_Performance_Up.ppt
WE18_Performance_Up.pptWE18_Performance_Up.ppt
WE18_Performance_Up.ppt
 
[MathWorks] Versioning Infrastructure
[MathWorks] Versioning Infrastructure[MathWorks] Versioning Infrastructure
[MathWorks] Versioning Infrastructure
 
HTTP Caching in Web Application
HTTP Caching in Web ApplicationHTTP Caching in Web Application
HTTP Caching in Web Application
 
Nginx, PHP, Apache and Spelix
Nginx, PHP, Apache and SpelixNginx, PHP, Apache and Spelix
Nginx, PHP, Apache and Spelix
 
HTTP2 is Here!
HTTP2 is Here!HTTP2 is Here!
HTTP2 is Here!
 
HAProxy scale out using open source
HAProxy scale out using open sourceHAProxy scale out using open source
HAProxy scale out using open source
 
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Thijs Feryn - Codemotion Rome 2018
 
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
Leverage HTTP to deliver cacheable websites - Codemotion Rome 2018
 
Bandwidth limiting howto
Bandwidth limiting howtoBandwidth limiting howto
Bandwidth limiting howto
 
Load Balancing with Apache
Load Balancing with ApacheLoad Balancing with Apache
Load Balancing with Apache
 
I got 99 problems, but ReST ain't one
I got 99 problems, but ReST ain't oneI got 99 problems, but ReST ain't one
I got 99 problems, but ReST ain't one
 
Introduction to HTTP/2
Introduction to HTTP/2Introduction to HTTP/2
Introduction to HTTP/2
 
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...
 
HTTP/2 standard for video streaming
HTTP/2 standard for video streamingHTTP/2 standard for video streaming
HTTP/2 standard for video streaming
 
Web Server Load Balancer
Web Server Load BalancerWeb Server Load Balancer
Web Server Load Balancer
 
Altitude San Francisco 2018: Testing with Fastly Workshop
Altitude San Francisco 2018: Testing with Fastly WorkshopAltitude San Francisco 2018: Testing with Fastly Workshop
Altitude San Francisco 2018: Testing with Fastly Workshop
 

Similaire à Hidden Gems in HTTP

IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."Dongwook Lee
 
Implementing Early Hints in Chrome - Approaches and Challenges
Implementing Early Hints in Chrome - Approaches and ChallengesImplementing Early Hints in Chrome - Approaches and Challenges
Implementing Early Hints in Chrome - Approaches and ChallengesViet-Hoang Tran
 
Interactive web. O rly?
Interactive web. O rly?Interactive web. O rly?
Interactive web. O rly?timbc
 
HTTP fundamentals for developers
HTTP fundamentals for developersHTTP fundamentals for developers
HTTP fundamentals for developersMario Cardinal
 
Type URL, Enter, and Then …
Type URL, Enter, and Then …Type URL, Enter, and Then …
Type URL, Enter, and Then …Jinglun Li
 
Under the Covers with the Web
Under the Covers with the WebUnder the Covers with the Web
Under the Covers with the WebTrevor Lohrbeer
 
Benchmarking for HTTP/2
Benchmarking for HTTP/2Benchmarking for HTTP/2
Benchmarking for HTTP/2Kit Chan
 
PHP Training: Module 1
PHP Training: Module 1PHP Training: Module 1
PHP Training: Module 1hussulinux
 
Java EE 8: What Servlet 4.0 and HTTP/2 mean to you
Java EE 8: What Servlet 4.0 and HTTP/2 mean to youJava EE 8: What Servlet 4.0 and HTTP/2 mean to you
Java EE 8: What Servlet 4.0 and HTTP/2 mean to youAlex Theedom
 
Web II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksWeb II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksRandy Connolly
 
20190516 web security-basic
20190516 web security-basic20190516 web security-basic
20190516 web security-basicMksYi
 
HTTP Basic - PHP
HTTP Basic - PHPHTTP Basic - PHP
HTTP Basic - PHPSulaeman .
 

Similaire à Hidden Gems in HTTP (20)

IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
IBM dwLive, "Internet & HTTP - 잃어버린 패킷을 찾아서..."
 
Implementing Early Hints in Chrome - Approaches and Challenges
Implementing Early Hints in Chrome - Approaches and ChallengesImplementing Early Hints in Chrome - Approaches and Challenges
Implementing Early Hints in Chrome - Approaches and Challenges
 
Http2 kotlin
Http2   kotlinHttp2   kotlin
Http2 kotlin
 
Interactive web. O rly?
Interactive web. O rly?Interactive web. O rly?
Interactive web. O rly?
 
HTTP
HTTPHTTP
HTTP
 
HTTP fundamentals for developers
HTTP fundamentals for developersHTTP fundamentals for developers
HTTP fundamentals for developers
 
Http methods
Http methodsHttp methods
Http methods
 
Web Cache Poisoning
Web Cache PoisoningWeb Cache Poisoning
Web Cache Poisoning
 
Type URL, Enter, and Then …
Type URL, Enter, and Then …Type URL, Enter, and Then …
Type URL, Enter, and Then …
 
Under the Covers with the Web
Under the Covers with the WebUnder the Covers with the Web
Under the Covers with the Web
 
Benchmarking for HTTP/2
Benchmarking for HTTP/2Benchmarking for HTTP/2
Benchmarking for HTTP/2
 
PHP Training: Module 1
PHP Training: Module 1PHP Training: Module 1
PHP Training: Module 1
 
Java EE 8: What Servlet 4.0 and HTTP/2 mean to you
Java EE 8: What Servlet 4.0 and HTTP/2 mean to youJava EE 8: What Servlet 4.0 and HTTP/2 mean to you
Java EE 8: What Servlet 4.0 and HTTP/2 mean to you
 
computer networking
computer networkingcomputer networking
computer networking
 
Speed = $$$
Speed = $$$Speed = $$$
Speed = $$$
 
5-WebServers.ppt
5-WebServers.ppt5-WebServers.ppt
5-WebServers.ppt
 
Web II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksWeb II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET Works
 
gofortution
gofortutiongofortution
gofortution
 
20190516 web security-basic
20190516 web security-basic20190516 web security-basic
20190516 web security-basic
 
HTTP Basic - PHP
HTTP Basic - PHPHTTP Basic - PHP
HTTP Basic - PHP
 

Plus de Ben Ramsey

Api Versioning
Api VersioningApi Versioning
Api VersioningBen Ramsey
 
Grokking REST (ZendCon 2010)
Grokking REST (ZendCon 2010)Grokking REST (ZendCon 2010)
Grokking REST (ZendCon 2010)Ben Ramsey
 
Desktop Apps with PHP and Titanium (ZendCon 2010)
Desktop Apps with PHP and Titanium (ZendCon 2010)Desktop Apps with PHP and Titanium (ZendCon 2010)
Desktop Apps with PHP and Titanium (ZendCon 2010)Ben Ramsey
 
Introduction to AtomPub Web Services
Introduction to AtomPub Web ServicesIntroduction to AtomPub Web Services
Introduction to AtomPub Web ServicesBen Ramsey
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APCBen Ramsey
 
Desktop Apps with PHP and Titanium
Desktop Apps with PHP and TitaniumDesktop Apps with PHP and Titanium
Desktop Apps with PHP and TitaniumBen Ramsey
 
Give Your Site a Boost with Memcache
Give Your Site a Boost with MemcacheGive Your Site a Boost with Memcache
Give Your Site a Boost with MemcacheBen Ramsey
 
Grokking the REST Architectural Style
Grokking the REST Architectural StyleGrokking the REST Architectural Style
Grokking the REST Architectural StyleBen Ramsey
 
Around the PHP Community
Around the PHP CommunityAround the PHP Community
Around the PHP CommunityBen Ramsey
 
You Look Like You Could Use Some REST!
You Look Like You Could Use Some REST!You Look Like You Could Use Some REST!
You Look Like You Could Use Some REST!Ben Ramsey
 
Distribution and Publication With Atom Web Services
Distribution and Publication With Atom Web ServicesDistribution and Publication With Atom Web Services
Distribution and Publication With Atom Web ServicesBen Ramsey
 
Distribution and Publication With Atom Web Services
Distribution and Publication With Atom Web ServicesDistribution and Publication With Atom Web Services
Distribution and Publication With Atom Web ServicesBen Ramsey
 

Plus de Ben Ramsey (12)

Api Versioning
Api VersioningApi Versioning
Api Versioning
 
Grokking REST (ZendCon 2010)
Grokking REST (ZendCon 2010)Grokking REST (ZendCon 2010)
Grokking REST (ZendCon 2010)
 
Desktop Apps with PHP and Titanium (ZendCon 2010)
Desktop Apps with PHP and Titanium (ZendCon 2010)Desktop Apps with PHP and Titanium (ZendCon 2010)
Desktop Apps with PHP and Titanium (ZendCon 2010)
 
Introduction to AtomPub Web Services
Introduction to AtomPub Web ServicesIntroduction to AtomPub Web Services
Introduction to AtomPub Web Services
 
Caching with Memcached and APC
Caching with Memcached and APCCaching with Memcached and APC
Caching with Memcached and APC
 
Desktop Apps with PHP and Titanium
Desktop Apps with PHP and TitaniumDesktop Apps with PHP and Titanium
Desktop Apps with PHP and Titanium
 
Give Your Site a Boost with Memcache
Give Your Site a Boost with MemcacheGive Your Site a Boost with Memcache
Give Your Site a Boost with Memcache
 
Grokking the REST Architectural Style
Grokking the REST Architectural StyleGrokking the REST Architectural Style
Grokking the REST Architectural Style
 
Around the PHP Community
Around the PHP CommunityAround the PHP Community
Around the PHP Community
 
You Look Like You Could Use Some REST!
You Look Like You Could Use Some REST!You Look Like You Could Use Some REST!
You Look Like You Could Use Some REST!
 
Distribution and Publication With Atom Web Services
Distribution and Publication With Atom Web ServicesDistribution and Publication With Atom Web Services
Distribution and Publication With Atom Web Services
 
Distribution and Publication With Atom Web Services
Distribution and Publication With Atom Web ServicesDistribution and Publication With Atom Web Services
Distribution and Publication With Atom Web Services
 

Dernier

Spain Vs Italy Showdown Between Italy and Spain Could Determine UEFA Euro 202...
Spain Vs Italy Showdown Between Italy and Spain Could Determine UEFA Euro 202...Spain Vs Italy Showdown Between Italy and Spain Could Determine UEFA Euro 202...
Spain Vs Italy Showdown Between Italy and Spain Could Determine UEFA Euro 202...World Wide Tickets And Hospitality
 
Project & Portfolio, Market Analysis: WWE
Project & Portfolio, Market Analysis: WWEProject & Portfolio, Market Analysis: WWE
Project & Portfolio, Market Analysis: WWEDeShawn Ellis
 
Benifits of Individual And Team Sports-Group 7.pptx
Benifits of Individual And Team Sports-Group 7.pptxBenifits of Individual And Team Sports-Group 7.pptx
Benifits of Individual And Team Sports-Group 7.pptxsherrymieg19
 
DONAL88 >LINK SLOT PG SOFT TERGACOR 2024
DONAL88 >LINK SLOT PG SOFT TERGACOR 2024DONAL88 >LINK SLOT PG SOFT TERGACOR 2024
DONAL88 >LINK SLOT PG SOFT TERGACOR 2024DONAL88 GACOR
 
Clash of Titans_ PSG vs Barcelona (1).pdf
Clash of Titans_ PSG vs Barcelona (1).pdfClash of Titans_ PSG vs Barcelona (1).pdf
Clash of Titans_ PSG vs Barcelona (1).pdfMuhammad Hashim
 
Italy Vs Albania Euro Cup 2024 Italy's Strategy for Success.docx
Italy Vs Albania Euro Cup 2024 Italy's Strategy for Success.docxItaly Vs Albania Euro Cup 2024 Italy's Strategy for Success.docx
Italy Vs Albania Euro Cup 2024 Italy's Strategy for Success.docxWorld Wide Tickets And Hospitality
 
Introduction to Basketball-PowerPoint Presentation
Introduction to Basketball-PowerPoint PresentationIntroduction to Basketball-PowerPoint Presentation
Introduction to Basketball-PowerPoint PresentationJuliusMacaballug
 
JORNADA 2 LIGA MUROBASQUETBOL1 2024.docx
JORNADA 2 LIGA MUROBASQUETBOL1 2024.docxJORNADA 2 LIGA MUROBASQUETBOL1 2024.docx
JORNADA 2 LIGA MUROBASQUETBOL1 2024.docxArturo Pacheco Alvarez
 
PPT on INDIA VS PAKISTAN - A Sports Rivalry
PPT on INDIA VS PAKISTAN - A Sports RivalryPPT on INDIA VS PAKISTAN - A Sports Rivalry
PPT on INDIA VS PAKISTAN - A Sports Rivalryanirbannath184
 
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfJORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfArturo Pacheco Alvarez
 

Dernier (11)

Spain Vs Italy Showdown Between Italy and Spain Could Determine UEFA Euro 202...
Spain Vs Italy Showdown Between Italy and Spain Could Determine UEFA Euro 202...Spain Vs Italy Showdown Between Italy and Spain Could Determine UEFA Euro 202...
Spain Vs Italy Showdown Between Italy and Spain Could Determine UEFA Euro 202...
 
Project & Portfolio, Market Analysis: WWE
Project & Portfolio, Market Analysis: WWEProject & Portfolio, Market Analysis: WWE
Project & Portfolio, Market Analysis: WWE
 
Benifits of Individual And Team Sports-Group 7.pptx
Benifits of Individual And Team Sports-Group 7.pptxBenifits of Individual And Team Sports-Group 7.pptx
Benifits of Individual And Team Sports-Group 7.pptx
 
DONAL88 >LINK SLOT PG SOFT TERGACOR 2024
DONAL88 >LINK SLOT PG SOFT TERGACOR 2024DONAL88 >LINK SLOT PG SOFT TERGACOR 2024
DONAL88 >LINK SLOT PG SOFT TERGACOR 2024
 
Clash of Titans_ PSG vs Barcelona (1).pdf
Clash of Titans_ PSG vs Barcelona (1).pdfClash of Titans_ PSG vs Barcelona (1).pdf
Clash of Titans_ PSG vs Barcelona (1).pdf
 
Italy Vs Albania Euro Cup 2024 Italy's Strategy for Success.docx
Italy Vs Albania Euro Cup 2024 Italy's Strategy for Success.docxItaly Vs Albania Euro Cup 2024 Italy's Strategy for Success.docx
Italy Vs Albania Euro Cup 2024 Italy's Strategy for Success.docx
 
Introduction to Basketball-PowerPoint Presentation
Introduction to Basketball-PowerPoint PresentationIntroduction to Basketball-PowerPoint Presentation
Introduction to Basketball-PowerPoint Presentation
 
JORNADA 2 LIGA MUROBASQUETBOL1 2024.docx
JORNADA 2 LIGA MUROBASQUETBOL1 2024.docxJORNADA 2 LIGA MUROBASQUETBOL1 2024.docx
JORNADA 2 LIGA MUROBASQUETBOL1 2024.docx
 
PPT on INDIA VS PAKISTAN - A Sports Rivalry
PPT on INDIA VS PAKISTAN - A Sports RivalryPPT on INDIA VS PAKISTAN - A Sports Rivalry
PPT on INDIA VS PAKISTAN - A Sports Rivalry
 
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfJORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
 
NATIONAL SPORTS DAY WRITTEN QUIZ by QUI9
NATIONAL SPORTS DAY WRITTEN QUIZ by QUI9NATIONAL SPORTS DAY WRITTEN QUIZ by QUI9
NATIONAL SPORTS DAY WRITTEN QUIZ by QUI9
 

Hidden Gems in HTTP

  • 1. Hidden Gems in HTTP Ben Ramsey ■ Code Works
  • 3. Because you are a Web developer.
  • 4. HTTP is the Web.
  • 5. That’s all I have to say about that.
  • 7. ■ A client-server architecture ■ Atomic ■ Cacheable ■ A uniform interface ■ Layered ■ Code on demand
  • 8. Now, what does that sound like?
  • 10. And, that’s all I have to say about that, too.
  • 12. ■ Methods you’ve never used ■ Status codes you didn’t know existed ■ Working with HTTP in PHP
  • 15. GET ■ You know GET ■ Retrieval of information ■ Transfers a representation of a resource from the server to the client ■ Safe & idempotent
  • 16. 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 <?xml version="1.0" encoding="utf-8"?> <entry xmlns="http://www.w3.org/2005/Atom" xml:base="http://atom.example.org/"> <title>ramsey</title> ... </entry>
  • 17. He just thinks he’s funny.
  • 19. 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
  • 20. POST /user HTTP/1.1 Host: atom.example.org Content-Type: application/atom+xml;type=entry Content-Length: 474 <?xml version="1.0" encoding="utf-8"?> <entry xmlns="http://www.w3.org/2005/Atom" xml:base="http://atom.example.org/"> <title>ramsey</title> ... </entry> 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 <div> The content was created at the location <a href="/user/ramsey"> http://atom.example.org/user/ramsey </a> </div>
  • 21. 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
  • 22. 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
  • 23. PUT ■ Opposite of GET ■ Storage of information ■ Transfers a representation of a resource from the client to the server ■ Not safe ■ Idempotent
  • 24. PUT /user/ramsey/ HTTP/1.1 Host: atom.example.org Content-Type: application/atom+xml;type=entry Content-Length: 594 <?xml version="1.0" encoding="utf-8"?> <entry xmlns="http://www.w3.org/2005/Atom" xml:base="http://atom.example.org/"> <title>ramsey</title> ... </entry> 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 <?xml version="1.0" encoding="utf-8"?> <entry xmlns="http://www.w3.org/2005/Atom" xml:base="http://atom.example.org/"> <title>ramsey</title> ... </entry>
  • 25. DELETE ■ Requests that the resource identified be removed from public access ■ Not safe ■ Idempotent
  • 26. 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
  • 27. What the hell are safe & idempotent methods?
  • 28. 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
  • 29. 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
  • 31. ■ Informational (1xx) ■ Successful (2xx) ■ Redirection (3xx) ■ Client error (4xx) ■ Server error (5xx)
  • 33. 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
  • 34. 1 POST /content/videos HTTP/1.1 Host: example.org Content-Type: video/mp4 Content-Length: 115910000 Authorization: Basic bWFkZTp5b3VfbG9vaw== Expect: 100-continue
  • 35. Failure state 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
  • 36. Success state 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
  • 37. 3 POST /content/videos HTTP/1.1 Host: example.org Content-Type: video/mp4 Content-Length: 115910000 Authorization: Basic bWFkZTp5b3VfbG9vaw== {binary video data}
  • 38. 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 <html><body><p>Video uploaded! Go <a href="http://example.org/content/videos/ 1234">here</a> to see it.</p></body></html>
  • 39. The created at another location response
  • 40. 1 POST /content/videos HTTP/1.1 Host: example.org Content-Type: video/mp4 Content-Length: 115910000 Authorization: Basic bWFkZTp5b3VfbG9vaw== {binary video data}
  • 41. 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 <html><body><p>Video uploaded! Go <a href="http://example.org/content/videos/ 1234">here</a> to see it.</p></body></html>
  • 42. The “it’s not you it’s me” response
  • 43. i.e. I’ve accepted it but might have to do more processing
  • 44. 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 <html><body><p>Video processing! Check <a href="http://example.org/content/videos/1234/ status">here</a> for the status.</p></body></ html>
  • 45. The “I have nothing to say to you” response…
  • 46. …but you were still successful
  • 47. 1 DELETE /content/videos/1234 HTTP/1.1 Host: example.org Authorization: Basic bWFkZTp5b3VfbG9vaw==
  • 48. 2 HTTP/1.x 204 No Content Date: Thu, 21 May 2009 23:28:34 GMT
  • 50. ■ 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
  • 52. 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
  • 53. 3 GET /2390/2253727548_a413c88ab3_s.jpg HTTP/1.1 Host: farm3.static.flickr.com Range: bytes=0-999
  • 54. 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}
  • 55. The GET me from another location response
  • 56. ■ 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
  • 57. 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}
  • 58. 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
  • 59. The find me temporarily at this place response
  • 60. ■ 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
  • 62. ■ 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
  • 63. But what about just finding the resource at another location?
  • 64. ■ 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)
  • 66. ■ 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)
  • 67. POST /user/ HTTP/1.1 Host: atom.example.org Content-Type: application/atom+xml;type=entry Content-Length: 474 <?xml version="1.0" encoding="utf-8"?> <entry xmlns="http://www.w3.org/2005/Atom" xml:base="http://atom.example.org/"> <title>r@msey</title> ... </entry> 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 <div class="error"> The following errors occurred: <ul> <li>Title contained invalid characters</li> </ul> </div>
  • 70. ■ 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
  • 71. Questions? ■ My website is benramsey.com ■ @ramsey on Twitter ■ Rate this talk at joind.in ■ Read the HTTP spec at tools.ietf.org/html/rfc2616 ■ My company is Schematic schematic.com
  • 72. 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.