引言
在服务器校验头之前就上传大正文的客户端,在服务器本会拒绝请求时浪费带宽与时间。Expect: 100-continue 握手让客户端先问:它带 Expect 头发送头、等 100 Continue(或最终错误)、然后才发正文。服务器可提前拒绝、省下上传。这是小协议细节,在大上传与慢连接上最要紧,且有足够边界情形使两端都必须正确实现。
握手
客户端带 Expect: 100-continue 发送请求头并在正文前暂停。服务器若要正文则回临时 100 Continue,客户端发正文,服务器再发最终响应。若服务器会拒绝请求,它立即发最终错误,客户端永不上传。
发临时 100 Continue 对服务器是可选的,客户端不得永远等它:短超时后客户端应无论如何发正文,因为服务器可能未实现握手。握手是优化而非要求。
为何省工作
对大上传,正文可占请求大部。若服务器会拒绝它——如认证失败或内容长度超限——先上传浪费该带宽与客户端时间。握手把决定移到正文前。
节省在慢连接与大正文上最大。快内部连接与小正文上,多一次往返可能比省的多,这就是客户端常只对大上传用握手的原因。
客户端实现
客户端发送头、设计时器(通常一两秒),并在 100 Continue 时继续、计时器到期时继续、或在最终错误响应时中止。它必须处理三者:临时状态、超时、以及代替临时而来的错误。
不处理超时的客户端会挂等可能永不来的临时响应;不处理早错误的客户端会照样上传。两者都是常见 bug。客户端还必须处理服务器发 100 Continue 后正文后再发最终错误,这对接受正文但稍后失败的服务器是正常流程。
服务端实现
服务器读头并决定:将处理请求则发 100 Continue 再读正文并响应;将拒绝则读正文前发最终错误。它不得在发 100 Continue 前读正文,且必须处理不等就发正文的客户端。
服务器应尽可能按头拒绝:认证、内容长度限制、不支持的内容类型。正文前拒绝才是要点。但它也必须为客户端立即发送正文(旧客户端或超时者)做好准备,因此不应假定握手完成。
代理与逐跳
Expect: 100-continue 是逐跳的:代理必须为其自身那一跳处理它,而非盲目转发。把 Expect 转发到下一跳并中继 100 Continue 的代理没问题,但不支持它的代理(或 HTTP/2 层)不得卡住连接。这正是支持不一致、客户端看到意外行为之处。
HTTP/2 与 HTTP/3 用流控与请求正文完成前的响应处理等价情形,不需要 Expect 机制。HTTP/2 上的客户端可能根本不需要握手,支持两者的服务器必须处理任一。
与超时和重试的交互
客户端对 100 Continue 的等待由超时界定,该超时必须短于整体请求截止时间,否则不实现的服务器会让请求挂起。用过握手的大正文请求重试时必须重复握手,而非直接发正文。
重试时客户端可能从先前尝试得知服务器不实现 100 Continue 并跳过握手。按主机记住这点避免每次后续上传到忽略该头的服务器都白等。
常见陷阱与小结
不要无限等 100 Continue;用有界超时。不要在服务器已发最终错误时上传正文。服务端不要在发临时响应前读正文,也不要假定握手完成。也不要忘记它逐跳且 HTTP/2+ 处理方式不同。
Expect: 100-continue 通过先发头、仅在 100 Continue 或短超时后发正文,让客户端避免上传服务器将拒绝的正文。两端都实现:客户端配有界等待与早错误处理,服务端配临时响应与早拒绝,并考虑代理与 HTTP/2 机制的差异。它在本会被拒绝的请求上省下上传。
代码示例
# the flow
client -> headers (Expect: 100-continue), then WAITS
server -> 100 Continue (interim) -> client sends the body -> final response
server -> 4xx/5xx (final, immediate) -> client does NOT send the body
# a client that gets no interim within a short timeout sends the body anyway
# where 100-continue pays off
[ ] large body (upload)
[ ] slow client connection (the upload would be costly)
[ ] a server that can reject on headers (auth, size, content-type)
# where it may not
[ ] small body on a fast link (extra round trip costs more)
# clients often enable it only above a size threshold
// Sketch: headers first, body only after 100 Continue or a timeout.
async function uploadWithExpect(url: string, headers: Headers, body: Readable) {
const res = await sendHeadersOnly(url, { ...headers, Expect: '100-continue' });
if (res.status === 100) return sendBody(res, body); // server wants it
if (res.status >= 200) return res; // final error, no body
// no interim: wait a bounded time, then send the body anyway
const interim = await waitForInterim(res, 1000);
return interim ? sendBody(res, body) : sendBody(res, body);
}
# server decision
will handle -> send "100 Continue" (interim), then read body, then respond
will reject -> send the final error (e.g. 401/413) WITHOUT reading the body
# must tolerate a client that sends the body without waiting
# must not read the body before sending 100 Continue
# hop-by-hop
Expect: 100-continue applies per hop; a proxy handles it for its hop
# a proxy that does not support it must not stall the connection
# HTTP/2/3: flow control + early response replace the Expect handshake
# the client can abort the body when the server responds early
# client timeout discipline
[ ] the 100-continue wait < the overall request deadline
[ ] on a server that ignores it: send the body after the short timeout
[ ] on retry: repeat the handshake for a large body (or cache "server ignores it")
# a wait longer than the deadline => the request hangs