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

There's this thing called HTTP

Thijs Feryn
October 25, 2012

There's this thing called HTTP

First time I've done this one. Presented at a Fronteersbe local user group meeting at Combell.

HTTP is out there and you should know how to use it!

Thijs Feryn

October 25, 2012
Tweet

More Decks by Thijs Feryn

Other Decks in Technology

Transcript

  1. GET  /  HTTP/1.1 Host:  fronteers.nl User-­‐Agent:  Mozilla/5.0  (Macintosh;  Intel  

    Mac  OS  X  10.8;  rv:16.0)  Gecko/20100101   Firefox/16.0 Accept:  text/html,application/xhtml +xml,application/xml;q=0.9,*/*;q=0.8 Accept-­‐Language:  en,nl;q=0.7,fr-­‐be;q=0.3 Accept-­‐Encoding:  gzip,  deflate Connection:  keep-­‐alive Cookie:  first_pv_60255=1;  _eventqueue=%7B %22heatmap%22%3A%5B%5D%2C%22events%22%3A%5B %5D%7D;  _jsuid=608348910;  unpoco_60255=1
  2. HTTP/1.1  200  OK Date:  Wed,  17  Oct  2012  13:19:58  GMT

    Server:  Apache Content-­‐Encoding:  gzip Vary:  Accept-­‐Encoding X-­‐UA-­‐Compatible:  IE=Edge,chrome=1 Content-­‐Length:  5566 Keep-­‐Alive:  timeout=11,  max=97 Connection:  Keep-­‐Alive Content-­‐Type:  text/html;  charset=UTF-­‐8
  3. 400 403 404 405 408 Bad  request Forbidden Not  found

    Method  not  allowed Request  7meout
  4. 405 406 407 408 409 Method  Not  Allowed Not  Acceptable

    Proxy  Authen7ca7on   Required Request  Timeout Conflict
  5. 410 411 412 413 414 Gone Length  Required Precondi7on  Failed

    Request  En7ty  Too  Large Request-­‐URI  Too  Long
  6. 500 501 502 503 504 505 Internal  server  error Not

     implemented Bad  gateway Service  unavailable Gateway  7meout HTTP  Version  Not   Supported
  7. Accept Accept-­‐Charset Accept-­‐Encoding Accept-­‐language Authorization Cache-­‐control Connection Cookie Content-­‐length Date

    text/plain u^8 gzip,  deflate en,nl;q=0.7,fr-­‐be;q=0.3 Basic  QWxhZGRpbjpvcGVuIHNlc2FtZQ== no-­‐cache close bla=ja;  foo=bar 1024 Tue,  15  Nov  1994  08:12:31  GMT
  8. Host If-­‐None-­‐Match Pragma Proxy-­‐Authorization Referer Upgrade User-­‐Agent Via www.fronteers.nl "737060cd8c284d8af7ad3082f209582d"

    no-­‐cache Basic   QWxhZGRpbjpvcGVuIHNl c2FtZQ== hQp://www.combell.com Mozilla/5.0   websocket 1.0  fred,  1.1  example.com  (Apache/1.1)
  9. Age Allow Cache-­‐control Connection Content-­‐Encoding Content-­‐Language Content-­‐Length Content-­‐Type Date 12

    GET,HEAD max-­‐age=3600 close gzip nl 1024 text/html;  charset=u^-­‐8 Tue,  15  Nov  1994  08:12:31  GMT
  10. ETag Expires Last-­‐Modified Location Pragma Server Set-­‐Cookie Transfer-­‐Encoding Vary 737060cd8c284d8af7ad3082f209582d

    Thu,  01  Dec  1994  16:00:00  GMT Thu,  01  Dec  1994  16:00:00  GMT hkp://www.combell.com no-­‐cache My  magic  server bla=abc;  session=12345 chuncked Accept-­‐encoding
  11. GET  /  HTTP/1.1 Host:  fronteers.nl User-­‐Agent:  Mozilla/5.0  (Macintosh;  Intel  

    Mac  OS  X  10.8;  rv:16.0)  Gecko/20100101   Firefox/16.0 Accept:  text/html,application/xhtml +xml,application/xml;q=0.9,*/*;q=0.8 Accept-­‐Language:  en,nl;q=0.7,fr-­‐be;q=0.3 Accept-­‐Encoding:  gzip,  deflate Connection:  keep-­‐alive Cookie:  first_pv_60255=1;  _eventqueue=%7B %22heatmap%22%3A%5B%5D%2C%22events%22%3A%5B %5D%7D;  _jsuid=608348910;  unpoco_60255=1
  12. HTTP/1.1  200  OK Date:  Wed,  17  Oct  2012  13:19:58  GMT

    Server:  Apache Content-­‐Encoding:  gzip Vary:  Accept-­‐Encoding X-­‐UA-­‐Compatible:  IE=Edge,chrome=1 Content-­‐Length:  5566 Keep-­‐Alive:  timeout=11,  max=97 Connection:  Keep-­‐Alive Content-­‐Type:  text/html;  charset=UTF-­‐8
  13. Max-­‐Age S-­‐maxage Public Private No-­‐cache No-­‐store Must-­‐revalidate Proxy-­‐revalidate TTL  for

     browsers  in  seconds TTL  for  proxies  in  seconds Proxies  &  browsers  can  cache Only  browsers  can  cache Revalidate  before  dropping  from  cache Don’t  cache  at  all Browser  revalidate  before  serving  from   cache   Proxy  revalidate  before  serving  from   cache  
  14. Only get & HEAD No cookie & auth headers No

    set-cookie headers cache ttl > 0 No vary “*” When will varnish cache?
  15. based on host/ip + urL hash By default in Memory

    Optionally on disk how will varnish cache?
  16. #  sub  vcl_recv  { #          if

     (req.restarts  ==  0)  { #              if  (req.http.x-­‐forwarded-­‐for)  { #                      set  req.http.X-­‐Forwarded-­‐For  = #                              req.http.X-­‐Forwarded-­‐For  +  ",  "  +  client.ip; #              }  else  { #                      set  req.http.X-­‐Forwarded-­‐For  =  client.ip; #              } #          } #          if  (req.request  !=  "GET"  && #              req.request  !=  "HEAD"  && #              req.request  !=  "PUT"  && #              req.request  !=  "POST"  && #              req.request  !=  "TRACE"  && #              req.request  !=  "OPTIONS"  && #              req.request  !=  "DELETE")  { #                  /*  Non-­‐RFC2616  or  CONNECT  which  is  weird.  */ #                  return  (pipe); #          } #          if  (req.request  !=  "GET"  &&  req.request  !=  "HEAD")  { #                  /*  We  only  deal  with  GET  and  HEAD  by  default  */ #                  return  (pass); #          } #          if  (req.http.Authorization  ||  req.http.Cookie)  { #                  /*  Not  cacheable  by  default  */ #                  return  (pass); #          } #          return  (lookup); #  }
  17. #  sub  vcl_hash  { #          hash_data(req.url);

    #          if  (req.http.host)  { #                  hash_data(req.http.host); #          }  else  { #                  hash_data(server.ip); #          } #          return  (hash); #  } #  sub  vcl_fetch  { #          if  (beresp.ttl  <=  0s  || #                  beresp.http.Set-­‐Cookie  || #                  beresp.http.Vary  ==  "*")  { #                              /* #                                *  Mark  as  "Hit-­‐For-­‐Pass"  for  the  next  2  minutes #                                */ #                              set  beresp.ttl  =  120  s; #                              return  (hit_for_pass); #          } #          return  (deliver); #  }
  18. curl  -­‐X  PURGE  http://example.com/some-­‐page acl  purge  { "localhost" } sub

     vcl_hit  {    if  (req.request  ==  "PURGE")  {        purge;        error  200  "Purged.";    } } sub  vcl_miss  {    if  (req.request  ==  "PURGE")  {        purge;        error  200  "Purged.";    } }
  19. <html> <body> <table> <tr> <td colspan="2" > <esi:include src="/header.php" />

    </td> </tr> <tr> <td><esi:include src="/menu.php" /></td> <td><esi:include src="/main.php" /></td> </tr> <tr> <td colspan="2" > <esi:include src="/footer.php" /> </td> </tr> </table> </body>
  20. ESI  VCL sub  vcl_recv  {        set  req.http.Surrogate-­‐Capability="key=ESI/1.0";

    } sub  vcl_fetch  {   if(beresp.http.Surrogate-­‐Control~"ESI/1.0")  {                  unset  beresp.http.Surrogate-­‐Control;          set  beresp.do_esi=true;        } }  
  21. Blocking var  results  =  db.query("SELECT  *  FROM  tablename"); //use  results

      Non-­‐blocking db.query("SELECT  *  FROM  tablename"  ,  function  (results)  {   //use  results   });   //do  something  else  
  22. Example var  http  =  require('http'); http.createServer(function  (req,  res)  {  

     res.writeHead(200,  {'Content-­‐Type':  'text/plain'});    res.end('Hello  World\n'); }).listen(1337,  '127.0.0.1'); console.log('Server  running  at  http://127.0.0.1:1337/');
  23. Example <?php require 'vendor/autoload.php'; $app = function ($request, $response) {

    $response->writeHead(200, array('Content- Type' => 'text/plain')); $response->end("Hello World\n"); }; $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server($loop); $http = new React\Http\Server($socket, $loop); $http->on('request', $app); echo "Server running at http://127.0.0.1:1337\n"; $socket->listen(1337); $loop->run();
  24. Websockets var conn = new WebSocket('ws:// some.host'); conn.onmessage = function(e)

    { console.log(e.data); }; conn.send('Hello World!'); Client
  25. <?php namespace MyApp; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; class Chat implements

    MessageComponentInterface { protected $clients; public function __construct() { $this->clients = new \SplObjectStorage; } public function onOpen(ConnectionInterface $conn) { $this->clients->attach($conn); } public function onMessage(ConnectionInterface $from, $msg) { foreach ($this->clients as $client) { if ($from !== $client) { $client->send('Response: '.$msg); } } } public function onClose(ConnectionInterface $conn) { $this->clients->detach($conn); } public function onError(ConnectionInterface $conn, \Exception $e) { echo "An error has occurred: {$e->getMessage()}\n"; $conn->close(); } }
  26. <?php require __DIR__.'/vendor/autoload.php'; require 'chat.php'; use Ratchet\WebSocket\WsServer; use Ratchet\Server\IoServer; use

    MyApp\Chat; $ws = new WsServer(new Chat()); $ws->disableVersion(0); $server = IoServer::factory($ws,80,'1.2.3.4'); $server->run();
  27. upstream  varnish  {    server  varnish1.example.com;    server  varnish2.example.com; }

    server  {    listen  443;    ssl  on;    ssl_certificate    /etc/nginx/example.ssl.cert;    ssl_certificate_key    /etc/nginx/example.ssl.key;    server_name  www.example.com;    access_log  /var/log/nginx/access.log;    error_log  /var/log/nginx/error.log;    location  /  {        proxy_pass    http://varnish;          proxy_redirect  off;        proxy_set_header  Host  $host;        proxy_set_header  X-­‐Scheme  $scheme;        proxy_set_header  X-­‐Forwarded-­‐For  $proxy_add_x_forwarded_for;        proxy_set_header  CLIENT_CERT  $ssl_client_raw_cert; } Make   your  app   aware
  28. upstream fpm { server php1.server.com:9000; server php2.server.com:9000; } upstream memcached

    { server memcached1.server.com:11211; server memcached2.server.com:11211; } server { root /var/www; index index.php index.html index.htm; server_name nginx.server.com; location / { try_files $uri $uri/ /index.php; } location @php { fastcgi_pass fpm; fastcgi_index index.php; include fastcgi_params; }
  29. location ~ \.php$ { set $memcached_key $request_uri; memcached_pass memcached; memcached_next_upstream

    not_found; default_type text/html; error_page 404 405 502 = @php; } location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { expires max; add_header Pragma public; add_header Cache-Control "public, must- revalidate, proxy-revalidate"; } }
  30. ✓A  URL  idenKfies  a  resource.   ✓Resources  have  a  hierarchy,

     so  you  know  that   something  with  addiKonal  slashes  is  a  subordinate   resource ✓Verbs  are  used  to  perform  operaKons  on   resources ✓The  operaKon  is  implicit  and  not  part  of  the  URL ✓A  hypermedia  format  is  used  to  represent  the   data ✓Link  relaKons  are  used  to  navigate  a  service Quodng   David  Zülke  @dzuelke
  31. GET  /products/1234  HTTP/1.1 Host:  example.com Accept:  application/vnd.com.example+xml   HTTP/1.1  200

     OK Content-­‐Type:  application/vnd.com.example+xml;  charset=utf-­‐8 Allow:  GET,  PUT,  DELETE <?  xml  version="1.0"  encoding="utf-­‐8"  ?> <product  xmlns="urn:com.example.products"  xmlns:atom="http:// w3.org/2005/Atom">    <id>1234</id>    <name  xml:lang="en">My  product</name>    <name  xml:lang="nl">Mijn  product</name> <price  currency="EUR">5</price> <price  currency="USD">6.5</price> <atom:link  rel="payment"  type="application/vnd.com.example +xml"  href="http://example.com/products/1234/payment" </product>   Thx   David!