没有 http 标头的 Http 响应 [英] Http response with no http header

查看:33
本文介绍了没有 http 标头的 Http 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个极简的 http 服务器原型(深受 boost asio 示例的启发),目前我还没有在服务器响应中放置任何 http 标头,只有 html 字符串内容.令人惊讶的是它工作得很好.

I have written a mini-minimalist http server prototype ( heavily inspired by boost asio examples ), and for the moment I haven't put any http header in the server response, only the html string content. Surprisingly it works just fine.

在那个问题 OP想知道http响应中的必要字段,以及其中一个评论指出它们在服务器端可能并不重要.

In that question the OP wonders about necessary fields in the http response, and one of the comments states that they may not be really important from the server side.

我暂时还没有尝试响应二进制图像文件或 gzip 压缩文件,在这种情况下,我认为必须有一个 http 标头.

I have not tried yet to respond binary image files, or gzip compressed file for the moment, in which cases I suppose it is mandatory to have a http header.

但对于纯文本响应(html、css 和 xml 输出),是否可以从不在我的服务器响应中包含 http 标头?可能存在哪些风险/错误?

But for text only responses (html, css, and xml outputs), would it be ok never to include the http header in my server responses ? What are the risks / errors possible ?

推荐答案

您至少必须提供带有状态行和日期的标题.

At a minimum, you must provide a header with a status line and a date.

作为一个写过很多协议解析器的人,我求求你,拜托了,拜托,拜托,拜托不要仅仅因为你最喜欢的浏览器允许你就完全忽略规范摆脱它.

As someone who has written many protocol parsers, I am begging you, on my digital metaphoric knees, please oh please oh please don't just totally ignore the specification just because your favorite browser lets you get away with it.

只要生成的数据是正确的,就可以创建一个功能最少的程序.这不应该是一个主要的负担,因为您所要做的就是在响应的开头添加三行.其中一行是空白!请花几分钟时间编写两行出色的代码,这将使您的响应数据符合规范.

It is perfectly fine to create a program that is minimally functional, as long as the data it produces is correct. This should not be a major burden, since all you have to do is add three lines to the start of your response. And one of those lines is blank! Please take a few minutes to write the two glorious line of code that will bring your response data into line with the spec.

您真正应该提供的标题是:

The headers you really should supply are:

  • 状态行(必填)
  • 日期标题(必需)
  • 内容类型(强烈推荐)
  • content-length(强烈推荐),除非您使用分块编码
  • 如果您返回 HTTP/1.1 状态行,并且您没有提供有效的内容长度或使用分块编码,则将 Connection: close 添加到您的标头
  • 将标题与正文分开的空白行(必需)

您可以选择不随响应发送内容类型,但您必须了解客户端可能不知道如何处理数据.客户端必须猜测它是什么类型的数据.浏览器可能决定将其视为下载文件而不是显示它.自动化过程(某人的 bash/curl 脚本)可能会合理地确定数据不是预期的类型,因此应该将其丢弃.

You can choose not to send a content-type with the response, but you have to understand that the client might not know what to do with the data. The client has to guess what kind of data it is. A browser might decide to treat it as a downloaded file instead of displaying it. An automated process (someone's bash/curl script) might reasonably decide that the data isn't of the expected type so it should be thrown away.

来自 HTTP/1.1 规范 3.1 节.1.5.内容类型:

From the HTTP/1.1 Specification section 3.1.1.5. Content-Type:

生成包含有效负载正文的消息的发送者应该在该消息中生成一个 Content-Type 标头字段,除非所附表示的预期媒体类型是未知的发件人.如果 Content-Type 标头字段不存在,则收件人可以假设媒体类型为application/octet-stream".([RFC2046],第 4.5.1 节)或检查数据以确定其类型.

A sender that generates a message containing a payload body SHOULD generate a Content-Type header field in that message unless the intended media type of the enclosed representation is unknown to the sender. If a Content-Type header field is not present, the recipient MAY either assume a media type of "application/octet-stream" ([RFC2046], Section 4.5.1) or examine the data to determine its type.

这篇关于没有 http 标头的 Http 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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