======================================================================
 HTTP::Handy PSGI Aide-memoire                       [FR] Francais
======================================================================

[ 1. Demarrer le serveur ]

  use HTTP::Handy;

  my $app = sub {
      my $env = shift;
      return [200, ['Content-Type', 'text/plain'], ['Hello']];
  };

  HTTP::Handy->run(
      app           => $app,       # requis: PSGI app code ref
      host          => '127.0.0.1',# optionnel: bind address (0.0.0.0)
      port          => 8080,       # optionnel: port (defaut: 8080)
      log           => 1,          # optionnel: access log (defaut: 1)
      max_post_size => 10485760,   # optionnel: max POST bytes (defaut: 10MB)
  );

  Executer directement comme script: perl lib/HTTP/Handy.pm [port]

[ 2. Variables d'environnement de requete ($env) ]

  REQUEST_METHOD    "GET" / "POST"
  PATH_INFO         /index.html
  QUERY_STRING      key=val&key2=val2
  SERVER_NAME       hostname
  SERVER_PORT       8080
  CONTENT_TYPE      application/x-www-form-urlencoded
  CONTENT_LENGTH    (bytes)
  HTTP_USER_AGENT   Mozilla/5.0 ...
  psgi.input        (Lire le corps POST (psgi.input)
  psgi.errors       \*STDERR
  psgi.url_scheme   "http"

[ 3. Format de reponse ]

  [$status, \@headers, \@body]

  [200, ['Content-Type', 'text/plain'], ['OK']]
  [404, ['Content-Type', 'text/plain'], ['Not Found']]
  [302, ['Location', '/new'], ['']]
  [204, ['Content-Length', '0'], []]

[ 4. Lire le corps POST (psgi.input) ]

  my $body = '';
  my $len  = $env->{CONTENT_LENGTH} || 0;
  $env->{'psgi.input'}->read($body, $len) if $len > 0;

  read($buf,$len)   read($buf,$len,$off)
  seek($pos,$wh)    tell()
  getline()         getlines()

[ 5. Methodes utilitaires ]

  parse_query($env->{QUERY_STRING})  ->  %hash
  parse_query($post_body)              ->  %hash
  url_decode('hello%20world')          ->  "hello world"
  mime_type('css')                     ->  "text/css"
  is_htmx($env)                        ->  1 / 0

[ 6. Methodes de construction de reponse ]

  response_html($html [,$code])   text/html; charset=utf-8
  response_text($text [,$code])   text/plain; charset=utf-8
  response_json($json [,$code])   application/json
  response_redirect($url [,$code]) Location: $url

[ 7. Servir des fichiers statiques ]

  serve_static($env, './htdocs')
  serve_static($env, './htdocs', cache_max_age => 3600)

[ 8. Modele de routage ]

  $method = $env->{REQUEST_METHOD};
  $path   = $env->{PATH_INFO};

  if ($method eq 'GET'  && $path eq '/') { ... }
  if ($method eq 'POST' && $path eq '/x') { ... }
  if ($path =~ m{^/api/}) { ... }
  if ($path =~ m{^/user/(\d+)$}) { my $id=$1; ... }

[ 9. Gestion des erreurs ]

  die "msg"  ->  500 response (auto)
  [500, ['Content-Type','text/plain'], ['error']]
  [403, ['Content-Type','text/plain'], ['forbidden']]
  [400, ['Content-Type','text/plain'], ['bad request']]

[ 10. Fichiers journaux crees par run() ]

  logs/access/YYYYMMDDHHm0.log.ltsv  journal d'acces (rotation 10min, format LTSV)
  logs/error/error.log                journal d'erreurs/demarrage

  LTSV: time  method  path  status  size  ua  referer

[ 11. Ressources officielles ]

  PSGI specification:
    https://github.com/plack/psgi-specs/blob/master/PSGI.pod

  PSGI (MetaCPAN):
    https://metacpan.org/pod/PSGI

  Plack:
    https://plackperl.org/

  HTTP::Handy (MetaCPAN):
    https://metacpan.org/dist/HTTP-Handy

======================================================================
