15.4 媒体类型和字符集

Content-Type 首部字段说明了实体主体的 MIME 类型。1 MIME 类型是标准化的名字,用以说明作为货物运载实体的基本媒体类型(比如:HTML 文件、Microsoft Word 文档或是 MPEG 视频等)。客户端应用程序使用 MIME 类型来解释和处理其内容。

1 在 HEAD 请求中,Content-Type 说明如果请求是 GET 时,将要发送的主体的类型。

Content-Type 的值是标准化的 MIME 类型,都在互联网号码分配机构(Internet Assigned Numbers Authority,简称 IANA)中注册。MIME 类型由一个主媒体类型(比如:text、image 或 audio 等 )后面跟一条斜线以及一个子类型组成,子类型用于进一步描述媒体类型。表 15-1 中列出了一些 Content-Type 首部中常用的 MIME 类型。附录 D 中列出了更多的 MIME 类型。

表15-1 常用媒体类型

媒体类型 描  述
text/html 实体主体是 HTML 文档
text/plain 实体主体是纯文本文档
image/gif 实体主体是 GIF 格式的图像
image/jpeg 实体主体是 JPEG 格式的图像
audio/x-wav 实体主体包含 WAV 格式声音数据
model/vrml 实体主体是三维的 VRML 模型
application/vnd.ms-powerpoint 实体主体是 Microsoft PowerPoint 演示文档
multipart/byteranges 实体主体有若干部分,每个部分都包含了完整文档中不同的字节范围
message/http 实体主体包含完整的 HTTP 报文(参见 TRACE)

要着重注意的是,Content-Type 首部说明的是原始实体主体的媒体类型。例如,如果实体经过内容编码的话,Content-Type 首部说明的仍是编码之前的实体主体的类型。

15.4.1 文本的字符编码

Content-Type 首部还支持可选的参数来进一步说明内容的类型。charset(字符集)参数就是个例子,它说明把实体中的比特转换为文本文件中的字符的方法:

  1. Content-Type: text/html; charset=iso-8859-4

我们将在第 16 章详细讨论字符集。

15.4.2 多部分媒体类型

MIME 中的 multipart(多部分)电子邮件报文中包含多个报文,它们合在一起作为单一的复杂报文发送。每一部分都是独立的,有各自的描述其内容的集;不同的部分之间用分界字符串连接在一起。

HTTP 也支持多部分主体。不过,通常只用在下列两种情形之一:提交填写好的表格,或是作为承载若干文档片段的范围响应。

15.4.3 多部分表格提交

当提交填写的 HTTP 表格时,变长的文本字段和上传的对象都作为多部分主体里面独立的部分发送,这样表格中就可以填写各种不同类型和长度的值。比如,你可能选择用昵称和小照片来填写询问你的名字和介绍信息的表格,而你的朋友可能填了她的全名并在介绍信息表内抱怨了一堆大众汽车的修理问题。

HTTP 使用 Content-Type:multipart/form-dataContent-Type:multipart/mixed 这样的首部以及多部分主体来发送这种请求,举例如下:

  1. Content-Type: multipart/form-data; boundary=[abcdefghijklmnopqrstuvwxyz]

其中的 boundary 参数说明了分割主体中不同部分所用的字符串。

下面的例子展示了 multipart/form-data 编码。假设我们有这样的表格:

  1. <FORM action=”http://server.com/cgi/handle”
  2. enctype="multipart/form-data"
  3. method="post">
  4. <P>
  5. What is your name? <INPUT type="text" name="submit-name"><BR>
  6. What files are you sending? <INPUT type="file" name="files"><BR>
  7. <INPUT type="submit" value="Send"> <INPUT type="reset">
  8. </FORM>

如果用户在文本输入字段中键入 Sally,并选择了文本文件 essayfile.txt,用户 Agent 代理可能会发回下面这样的数据:

  1. Content-Type: multipart/form-data; boundary=AaB03x
  2. --AaB03x
  3. Content-Disposition: form-data; name="submit-name"
  4. Sally
  5. --AaB03x
  6. Content-Disposition: form-data; name="files"; filename="essayfile.txt"
  7. Content-Type: text/plain
  8. ...contents of essayfile.txt...
  9. --AaB03x--

如果用户还选了另一个(图像)文件 image?le.gif,用户 Agent 代理可能像下面这样构造这个部分:

  1. Content-Type: multipart/form-data; boundary=AaB03x
  2. --AaB03x
  3. Content-Disposition: form-data; name="submit-name"
  4. Sally
  5. --AaB03x
  6. Content-Disposition: form-data; name="files"
  7. Content-Type: multipart/mixed; boundary=BbC04y
  8. --BbC04y
  9. Content-Disposition: file; filename="essayfile.txt"
  10. Content-Type: text/plain
  11. ...contents of essayfile.txt...
  12. --BbC04y
  13. Content-Disposition: file; filename="imagefile.gif"
  14. Content-Type: image/gif
  15. Content-Transfer-Encoding: binary
  16. ...contents of imagefile.gif...
  17. --BbC04y--
  18. --AaB03x--

15.4.4 多部分范围响应

HTTP 对范围请求的响应也可以是多部分的。这样的响应中有 Content-Type: multipart/byteranges 首部和带有不同范围的多部分主体。下面是一个例子,展示了对文档不同范围的请求产生的响应:

  1. HTTP/1.0 206 Partial content
  2. Server: Microsoft-IIS/5.0
  3. Date: Sun, 10 Dec 2000 19:11:20 GMT
  4. Content-Location: http://www.joes-hardware.com/gettysburg.txt
  5. Content-Type: multipart/x-byteranges; boundary=--[abcdefghijklmnopqrstu
  6. vwxyz]--
  7. Last-Modified: Sat, 09 Dec 2000 00:38:47 GMT
  8. --[abcdefghijklmnopqrstuvwxyz]--
  9. Content-Type: text/plain
  10. Content-Range: bytes 0-174/1441
  11. Fourscore and seven years ago our fathers brought forth on this
  12. continent a new nation, conceived in liberty and dedicated to the
  13. proposition that all men are created equal.
  14. --[abcdefghijklmnopqrstuvwxyz]--
  15. Content-Type: text/plain
  16. Content-Range: bytes 552-761/1441
  17. But in a larger sense, we can not dedicate, we can not consecrate,
  18. we can not hallow this ground. The brave men, living and dead who
  19. struggled here have consecrated it far above our poor power to add
  20. or detract.
  21. --[abcdefghijklmnopqrstuvwxyz]--
  22. Content-Type: text/plain
  23. Content-Range: bytes 1344-1441/1441
  24. and that government of the people, by the people, for the people shall
  25. not perish from the earth.
  26. --[abcdefghijklmnopqrstuvwxyz]--

本章后面将详细讨论范围请求。