======================================================================
 HTTP::Handy PSGI 速查表                                 [ZH] 中文（简体）
======================================================================

[ 1. 启动服务器 ]

  use HTTP::Handy;

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

  HTTP::Handy->run(
      app           => $app,       # 必填: PSGI app code reference
      host          => '127.0.0.1',# 可选: bind address (default: 0.0.0.0)
      port          => 8080,       # 可选: port (default: 8080)
      log           => 1,          # 可选: access log (default: 1)
      max_post_size => 10485760,   # 可选: max POST bytes (default: 10MB)
  );

  perl lib/HTTP/Handy.pm [port]

[ 2. 请求环境变量 ($env) ]

  REQUEST_METHOD    字符串  "GET" / "POST"
  PATH_INFO         字符串  /index.html
  QUERY_STRING      字符串  key=val
  SERVER_NAME       字符串  hostname
  SERVER_PORT       整数  8080
  CONTENT_TYPE      字符串  content-type
  CONTENT_LENGTH    整数  (bytes)
  HTTP_*            字符串  HTTP_USER_AGENT ...
  psgi.input        对象  (POST body)
  psgi.errors       glob  \*STDERR
  psgi.url_scheme   字符串  "http"

[ 3. 响应格式 ]

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

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

[ 4. 读取 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. 实用方法 ]

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

[ 6. 响应构建方法 ]

  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. 静态文件服务 ]

  return HTTP::Handy->serve_static($env, './htdocs');
  return HTTP::Handy->serve_static($env, './htdocs',
      cache_max_age => 3600);

[ 8. 路由模式 ]

  my $method = $env->{REQUEST_METHOD};
  my $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. 错误处理 ]

  die "msg"  ->  500 自动响应 500
  [500, ['Content-Type','text/plain'], ['error']]
  [403, ['Content-Type','text/plain'], ['forbidden']]
  [400, ['Content-Type','text/plain'], ['bad request']]

[ 10. run() 创建的日志文件 ]

  logs/access/YYYYMMDDHHm0.log.ltsv  访问日志（10分钟轮转，LTSV格式）
  logs/error/error.log                错误/启动日志

  LTSV: time  method  path  status  size  ua  referer

[ 11. 官方资源 ]

  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: https://metacpan.org/dist/HTTP-Handy

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