“http: multiple response.WriteHeader calls” 的坏影响是什么?

huangapple go评论75阅读模式
英文:

What's the bad effect of "http: multiple response.WriteHeader calls"?

问题

我的服务器表现良好,尽管我发现了“http: multiple response.WriteHeader calls”异常。这个异常并不会导致我的服务器崩溃或表现异常。

我已经进行了大量搜索,但只找到了如何解决这个问题的方法。没有文档描述这个异常的不良影响。有人能帮我找出为什么“http: multiple response.WriteHeader calls”是一个异常,以及它引起的不良影响是什么吗?提前感谢。

更新

我已经阅读了源代码这里

“http: multiple response.WriteHeader calls” 的坏影响是什么?

当多次调用WriteHeader时,它只打印一条日志,然后什么都不做。看起来多次调用WriteHeader不会导致服务器表现异常。

英文:

My server behaves well though I find the exception of "http: multiple response.WriteHeader calls". This exception doesn't cause my server panic or behaving weirdly.

I have searched a lot but only found how to solve this problem. There's no doc describing the bad effects of the exception. Could someone help me find why "http: multiple response.WriteHeader calls" is an exception and what the bad effects are that it causes? Thanks in advance.

UPDATE

I have read the source code here:

“http: multiple response.WriteHeader calls” 的坏影响是什么?

When calling WriteHeader multitimes,it only prints a log and then does nothing. It seems that calling WriteHeader multitimes doesn't cause the server behaves weiredly.

答案1

得分: 3

你只能在将响应发送给客户端之前写入一次响应头。因为你的代码试图在响应已发送给客户端后写入响应头,所以客户端将永远看不到你试图发送的最终响应。

要么你的代码存在错误,客户端收到了你稍后修改的错误响应,要么在正确的响应头被写入后,你执行了错误的代码。无论哪种情况,这都是一个错误。

英文:

You can only write a response header once before it is sent to the client. Because your code is attempting to write a response header after it has been sent to the client, the client will never see the final response you are trying to send.

You either have a bug where the client is getting the incorrect response which you later revised, or you have a bug where you are executing the wrong code after the correct response header has been written. Either way it's a bug.

答案2

得分: 2

坏处是实际结果很可能与你期望的不同。如果你在第10行调用WriteHeader(200),然后在第50行再次在同一个ResponseWriter上调用WriteHeader(404),你期望的响应代码是什么?是404还是200

WriteHeader会忽略后续的调用,并通过记录该消息来告诉你,所以当你得到的是200而不是你期望的404时,你只需查看服务器日志,就能知道发生了什么。这个消息是为了帮助你的。

英文:

The bad effect is that the actual result will most probably be different from what you expect. If you call WriteHeader(200) on line 10, and later on line 50 you call, on the same ResponseWriter, WriteHeader(404) what do you expect the response code to be? 404, or 200?

WriteHeader ignores subsequent calls and tells you about it by logging that message, so that when you're getting 200 instead of 404 as you probably expect, you just take a look at the server logs and you know what's up. The message is there to help you.

huangapple
  • 本文由 发表于 2017年3月20日 22:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/42906490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定