关于伪标头那些事

前言

看到伪标头,不少同学可能会比较陌生,因为谁让它默默无闻呢? 当然博主把它比喻为一个来自传输层的“共享盒子”。提到共享,我想大家有所体会了。这里给大家贴一张直观的图例,可以静静观摩之。

在这里插入图片描述

Q:什么是标头

标头是由“键:值”组成。描述了客户端或者服务器的属性、被传输的资源以及预期实现连接信息。包含通用类型、请求类型、响应类型、实体类型四种。
数据格式:name:value

下面博主做一个简单的介绍:

标头类型标头简介
通用类不限于特定种类的消息或者消息组件,主要用于传达有关消息本身的信息。比如Cache-Control、Connection、Date等。
请求类主要用于客户端发送 HTTP 请求到服务器中所使用的字段。比如Accept之类、From、Host、User-Agent等。
响应类是由服务器发送给客户端响应中所包含的字段,也就是服务器返回的字段。比如Location、Accept-Ranges、Server等。
实体类定义被传送资源的信息,可用于请求,也可用于响应。比如Content-之类、Last-Modified等。

有了前面的铺垫,接着博主带着各位,进入本博正题。

Q:什么是伪标头

通过上面简单叙述,或许各位从“踌躇满怀”转为“眉开眼笑”了,这不就是传说中的“header”嘛~

例如(fiddler可以抓包):
在这里插入图片描述

话不多说,我们接着聊聊,伪是何意?

伪:假也,即在物理世界摸不着、看不见、闻不到的东东。

严肃的说:

伪标头是在计算机网络中传输层的一个概念,它是在UDP和TCP协议中使用的一种虚拟的数据结构,用于存储一些必要的信息,以便在传输过程中进行地址转换和数据检验等操作。

当然我们这里从学术领域,已经从http1进入http2了。请各位把目光聚焦下方这张图:
在这里插入图片描述
为了减轻请求负载,提高资源使用率(几乎渗透到了各行各业),http2专门引入了1伪标头的概念。如对英文感兴趣,可直接移步这里:Hypertext Transfer Protocol Version 2 (HTTP/2)

Q:如何使用伪标头

http2中的伪标头主要有以下4个(原汁原味,喜欢的同学可以自己揣摩):

The :method pseudo-header field includes the HTTP method ([RFC7231], Section 4).
The :scheme pseudo-header field includes the scheme portion of the target URI ([RFC3986], Section 3.1).:scheme is not restricted to http and https schemed URIs. A proxy or gateway can translate requests for non-HTTP schemes, enabling the use of HTTP to interact with non-HTTP services.
The :authority pseudo-header field includes the authority portion of the target URI ([RFC3986], Section 3.2). The authority MUST NOT include the deprecated userinfo subcomponent for http or https schemed URIs.
To ensure that the HTTP/1.1 request line can be reproduced accurately, this pseudo-header field MUST be omitted when translating from an HTTP/1.1 request that has a request target in origin or asterisk form (see [RFC7230], Section 5.3). Clients that generate HTTP/2 requests directly SHOULD use the :authority pseudo-header field instead of the Host header field. An intermediary that converts an HTTP/2 request to HTTP/1.1 MUST create a Host header field if one is not present in a request by copying the value of the :authority pseudo-header field.
The :path pseudo-header field includes the path and query parts of the target URI (the path-absolute production and optionally a ‘?’ character followed by the query production (see Sections 3.3 and 3.4 of [RFC3986]). A request in asterisk form includes the value ‘*’ for the :path pseudo-header field.

这里,博主做个小结:

伪标头定义
:method该字段包括HTTP方法,比如GET/POST等
:scheme该字段包括URI的schema部分,比如https。在这里插入图片描述
:authority说明服务主机和端口,等价于Host。
:path请求的路径和参数

结语

关于伪标头的故事,博主就叙述到此,喜欢的盆友可以多了解一下。
感谢捧场,有求必应(广告:如感兴趣,可以关注我的公众号: 三度经纬。刚开通,会陆续发布有价值的文章)!

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值