使用 HTTP/2 时如何在 Cro 中获取 Host 或 :authority header

How to get the Host or :authority header in Cro when using HTTP/2

当将 Cro 与 HTTP1.1 一起使用时,我可以通过 Cro::Uri as well as the Host or :authority header sent by the browser via the request.header method in Cro::HTTP::Request 中的主机方法 request.uri.host 访问请求的主机。

但是,当我使用HTTP/2时,none这些工作。 Uri object 仅包含架构和路径。

我正在使用带有子域通配符的官方证书,并且 运行 通过将这些子域添加到我的主机文件来在本地进行此操作。 Chrome DevTools 说它已经在 HTTP/2 下发送了 :authority 请求 header,而 Firefox 开发者工具说它已经在 HTTP/2 下发送了 Host 请求 header。但是,如果我将 headers 写入如下日志,我会看到几个 headers,但看不到主机或 :authority header。

sub routes() is export {
  route {
    get -> {
      my $log = "/data/myapp/logs/cro.log";
      my $fh = open $log, :w;
      my $host = request.uri.host;
      $fh.say( "Host with host method: " ~ $host );
      $host = request.header('Host');
      $fh.say( "Host: " ~ $host );
      $host = request.header(':authority');
      $fh.say(":authority: " ~ $host );
      $fh.say( "Request headers:" );
      for request.headers {
        $fh.say( "{.name}: {.value}" );
      }
      $fh.close;
      content 'text/html', "<h1> MyApp </h1><p>Running";
    }
  }
}

我知道 HTTP/2 使用 Server Name Indication 并且主机名是作为 TLS 协商的一部分发送的。另一方面,这也是 Cro 模块 (Cro::TLS) 的一部分,并且 headers 是由浏览器发送的 none。

那么如何获取HTTP/2下的host呢?

这似乎是一个普通的错误(与 Raiph 命名的错误无关)。

我写了一个补丁并发送了一个 PR (https://github.com/croservices/cro-http/pull/104),合并后你可以更新你的 Cro::HTTP 发行版和你指定的两种方式(request.uri 构造或要求 header('Host')) 将起作用。