http trailer 与 http2

前言

最近做项目,发现一些项目使用 grpc,尤其是容器 K8S下,笔者在很久以前文章:https://blog.csdn.net/fenglllle/article/details/127829481

有一点非常奇怪的现象,在 h2 的返回的结果后面还有一个 header,这个实际上是 http trailer。

准备示例

在 github 为例:https://github.com/ma6174/blog/issues/22

go 语言

Go 复制代码
package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"os/signal"
	"syscall"
)

func grpcStatus(w http.ResponseWriter, r *http.Request) {
	fmt.Printf("header: %+v\n", r.Header)
	fmt.Printf("trailer before read body: %+v\n", r.Trailer)
	data, _ := io.ReadAll(r.Body)
	w.Header().Add("Transfer-Encoding", "chunked")
//	w.Header().Add("Content-Length", "-1")
	w.Header().Add("Trailer", "grpc-status")
	fmt.Println("------------", string(data))
	w.Write([]byte("haha"))

	w.WriteHeader(200)
	w.Header().Set("grpc-status", "0")
}

func main() {
	go func() {
		http.HandleFunc("/demo", grpcStatus)
		err := http.ListenAndServe(":8080", nil)
		if err != nil {
			fmt.Println(err)
		}
	}()
	signalCh := make(chan os.Signal, 1)
	signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)

	sig := <-signalCh
	fmt.Printf("Received signal: %v\n", sig)

	fmt.Println("hello")
}

使用 postman 发送请求 localhost:8080/demo

没什么奇特的地方,非常常规,但是当我们抓包时

奇怪的事情发生了,在 chunked 的结束时,发现了 trailer header 数据

这里是返回数据,同理,如果对请求数据进行处理也会出现类似的效果

go 示例

Go 复制代码
package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	_ "strconv"
	"strings"
)

type headerReader struct {
	reader io.Reader
	header http.Header
}

func (r *headerReader) Read(p []byte) (n int, err error) {
	n, err = r.reader.Read(p) //先读 body
	if err == io.EOF {
		//写 trailer header
		r.header.Set("grpc-status", "0")
	}
	return
}

func main() {
	h := &headerReader{
		reader: strings.NewReader("body"),
		header: http.Header{},
	}
	req, err := http.NewRequest("POST", "http://localhost:8080", h)
	if err != nil {
		panic(err)
	}
	req.ContentLength = -1
	req.Trailer = h.header //设置 trailer
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	fmt.Println(resp.Status)
	_, err = io.Copy(os.Stdout, resp.Body)
	if err != nil {
		panic(err)
	}
}

mac 下使用 nc -l 8080

nc 命令

-l Listen mode, for inbound connects

监听发现 trailer 的 header 在 body 后被读取出来了

因为 postman 没法在发送 body 后,再写 header,所以只能通过代码的方式

通过上面的实践,发现 http trailer 的执行方式:

  1. 使用 http chunked

  2. header 设置key 为Trailer的header key,多个 key 逗号,分割

  3. 先写,或者先读 body,然后设置 trailer 定义的 header key 和 value

使用 h2

回到最初的问题,如果真是 grpc,那么使用的 h2 的方式 trailer?换成 java 写一个springboot demo

java 复制代码
@RestController
public class DemoController {

    @GetMapping("/demo")
    public void demo(HttpServletResponse response) throws IOException {
        response.addHeader("Transfer-Encoding", "chunked");
        response.addHeader("Content-Length", String.valueOf(-1));
        response.addHeader("Trailer", "grpc-status");
        PrintWriter printWriter = response.getWriter();
        printWriter.print("body");
        printWriter.flush();
        response.addHeader("grpc-status", "0");
        printWriter.close();
    }
}

server.http2.enabled = true

然后强制发送 http2 的请求

huahua@huahuadeMac-mini ~ % curl --http2 http://localhost:8080/demo

curl: (92) Invalid HTTP header field was received: frame type: 1, stream: 1, name: [transfer-encoding], value: [chunked]

根据 http2 的标准:https://datatracker.ietf.org/doc/html/rfc7540#section-8.1

不能使用 chunked 编码,其实 http2 本身就是支持分块传输

但是 trailer 需要 chunked 传输,所以暂时还没有头绪解决,怎么使用 http2 执行 chunked http trailer

直到一篇文章:https://carlmastrangelo.com/blog/why-does-grpc-insist-on-trailers

讲述了 grpc 的设计,到是怎么失控的,在 http2 中分帧,所以不需要 chunked 编码了,可以直接发送 trailer

java 复制代码
@RestController
public class DemoController {

    @GetMapping("/demo")
    public void demo(HttpServletResponse response) throws IOException {
//        response.addHeader("Transfer-Encoding", "chunked");
//        response.addHeader("Content-Length", String.valueOf(-1));
        response.addHeader("Trailer", "grpc-status");
        PrintWriter printWriter = response.getWriter();
        printWriter.print("body");

//        response.addHeader("grpc-status", "0");
        response.setTrailerFields(()->{
            Map<String, String> map = new HashMap<>();
            map.put("grpc-status", "0");
            return map;
        });
        printWriter.flush();
        printWriter.close();
    }
}

发送请求,抓包,成功还原 http2 grpc 的设计理念

总结

经过实践还原了 grpc 的通信内容,通过 springboot 的 h2 达到 grpc 的 trailer 的方式,以后在 springboot 中试试,算是解决上一篇文章的遗留问题。不过居然有 http trailer 这玩意,笔者以前没来没用过,也没听到哪里有使用,算是对这个设计有了深刻的了解了。

相关推荐
张人玉1 小时前
Https协议数据格式
网络协议·http·https
越来越无动于衷1 小时前
HTTP 文件服务器 Windows 开机自启动全维度总结
服务器·windows·http
CoderYanger2 小时前
动态规划算法-简单多状态dp问题:15.买卖股票的最佳时机含冷冻期
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger3 小时前
递归、搜索与回溯-FloodFill:33.太平洋大西洋水流问题
java·算法·leetcode·1024程序员节
CoderYanger6 小时前
动态规划算法-斐波那契数列模型:2.三步问题
开发语言·算法·leetcode·面试·职场和发展·动态规划·1024程序员节
CoderYanger6 小时前
动态规划算法-简单多状态dp问题:16.买卖股票的最佳时机含手续费
开发语言·算法·leetcode·动态规划·1024程序员节
CoderYanger8 小时前
C.滑动窗口-求子数组个数-越短越合法——3258. 统计满足 K 约束的子字符串数量 I
java·开发语言·算法·leetcode·1024程序员节
苏小瀚8 小时前
[JavaSE] 网络原理(HTTP_HTTPS)
网络·tcp/ip·http
CoderYanger8 小时前
动态规划算法-路径问题:9.最小路径和
开发语言·算法·leetcode·动态规划·1024程序员节
HIT_Weston8 小时前
51、【Ubuntu】【Gitlab】拉出内网 Web 服务:http.server 单/多线程分析(三)
ubuntu·http·gitlab