定长包体

  • 发送HTTP消息时已能够确定包体的全部长度
  • 使用Content-Length头部明确指明包体长度
  • Content-Length = 1*DIGIT
  • 用10进制(不是16进制)表示包体中的字节个数,且必须与实际传输的包体长度一致

优点:接收端处理更简单

不定长包体

  • 发送HTTP消息时不能确定包体的全部长度
  • 使用Transfer-Encoding头部指明使用Chunk传输方式(Response)
  • 含Transfer-Encoding头部后Content-Length头部应被忽略

优点:

  • 基于长连接持续推送动态内容
  • 压缩体积较大的包体时,不必完全压缩完(计算出头部)再发送,可以变发送边压缩
  • 传递必须在包体传输完才能计算出的Trailer头部

image.png

  1. HTTP/1.1 200 OK
  2. Content-Type: text/plain
  3. Transfer-Encoding: chunked
  4. 25\r\n
  5. This is the data in the first chunk\r\n
  6. 1C\r\n
  7. and this is the second one\r\n
  8. 3\r\n
  9. con\r\n
  10. 8\r\n
  11. sequence\r\n
  12. 0\r\n
  13. \r\n

Trailer头部的传输

  • TE头部:客户端在请求在声明是否接受Trailer头部
    • TE: trailers
  • Trailer头部:服务器告知接下来chunk包体后会传输哪些Trailer头部
    • Trailer: Date
  • 以下头部不允许出现在Trailer的值中:
    • 用于信息分帧的首部(例如Transfer-Encoding和Content-Length)
    • 用于路由用途的首部(例如Host)
    • 请求修饰首部(例如控制类和条件类的,如Cache-Control, Max-Forwards, 或者TE)
    • 身份验证首部(例如Authorization或者Set-Cookie)
    • Content-Encoding,Content-Type,Content-Range,以及Trailer自身

Content-Disposition

disposition-type = “inline” | “attachment” | disp-ext-type

  • inline:指定包体是以inline内联的方式,作为页面的一部分展示
  • attachment:指定浏览器将包体以附件的方式下载
    • 例如:Content-Disposition: attachment
    • 例如:Content-Disposition: attachment;filename=”filename.jpg”
  • 在multiple/form-data类型应答中,可以用于子消息体部分
    • 如Content-Disposition: form-data;name=”fileName”;filename=”filename.jpg”