是Content-Type的HTTP头总是需要? [英] Is Content-Type HTTP header always required?

查看:128
本文介绍了是Content-Type的HTTP头总是需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题涉及到浏览器的行为,以及用于链接,导入,包括或网页,JS文件或C​​SS文件中ajaxing CSS,JS,图片和其他文件协议规范。

This question relates to browser behavior as well as protocol specification for linking, importing, including or ajaxing CSS, JS, image and other files from within webpages, js files or css files.

在测试静态文件和COM pressed内容交付,我发现,如果从约定搬走一些浏览器创建的问题。像:IE6创建问题,如果你不发内容处置:内联; 所有内嵌CSS头,JS等文件,和一个不那么老版本的Safari确实不能正确处理COM $ p $如果您在使用文件扩展名。广州像pssed CSS文件主styles.css.gz

While testing static files and compressed content delivery, I found that some browsers create problem if you move away from conventions. Like: IE6 creates problem if you do not send Content-Disposition: inline; header for all inline CSS, JS etc files, and a not-so-old version of safari does not properly handle compressed CSS files if you use file extension .gz like in main-styles.css.gz.

我想请教一下浏览器的约内容类型响应头的行为。
由于<链接> <脚本> < IMG> 已指定内容类型时,可这个头是安全的跳过(按照规范),或者做一些浏览器需要它由于某种原因?

I want to ask about the behavior of browsers about Content-Type response header. Since <link>, <script> and <img> already specify the content type, can this header be safely skipped (as per specification), or do some browser require it for some reason?

推荐答案

在短,不,它不是必需的。但建议。
据我所知,大多数浏览器会将&LT;链接&GT; &LT;脚本&GT; &LT; IMG&GT; 如果适当它们不与头一起发送,但没有真正的好理由不发送头。基本上,没有内容类型头,浏览器是留给尝试和基于内容猜测。

In short, no, it's not required. But it's recommended. Most browser that I know of will treat <link>, <script>, and <img> properly if they are not sent with headers, but there's no real good reason not to send the headers. Basically, without Content-Type headers, the browser is left to try and guess based on the content.

从RFC2616:

的Content-Type指定了基础数据的媒体类型。结果
  内容编码可被用于表示任何额外内容搜索
  的编码施加到数据,通常数据搜索的目的
  玉米pression,是所请求的资源的属性。有结果
  没有默认的编码。

Content-Type specifies the media type of the underlying data.
Content-Encoding may be used to indicate any additional content
codings applied to the data, usually for the purpose of data
compression, that are a property of the requested resource. There is
no default encoding.

包含实体主体应该包括结果的任何HTTP / 1.1消息
  Content-Type头字段定义该机构的媒体类型。如果结果
  且仅当媒体类型不是由内容类型字段中给出的结果,
  收件人可尝试经由其结果的检验猜测媒体类型
  内容和/或URI的扩展名(多个)用于识别结果
  资源。如果媒体类型仍然是未知的,接受方应当结果
  把它作为类型application /八位字节流。

Any HTTP/1.1 message containing an entity-body SHOULD include a
Content-Type header field defining the media type of that body. If
and only if the media type is not given by a Content-Type field, the
recipient MAY attempt to guess the media type via inspection of its
content and/or the name extension(s) of the URI used to identify the
resource. If the media type remains unknown, the recipient SHOULD
treat it as type "application/octet-stream".

关于关键字应,在RFC2119规定的:

Regarding the keyword SHOULD, specified in RFC2119:

应:这个词,或建议的形容词,意味着有结果
  可能存在在特定情况下忽略一个结果正当的理由
  特定的项目,但全面影响必须被理解和结果
  选择不同的做法之前,请仔细权衡。

SHOULD: This word, or the adjective "RECOMMENDED", mean that there
may exist valid reasons in particular circumstances to ignore a
particular item, but the full implications must be understood and
carefully weighed before choosing a different course.

这篇关于是Content-Type的HTTP头总是需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆