package main
import (
"log"
"time"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("start")
select {
case <-time.After(time.Duration(10) * time.Second):
log.Print("OK")
w.Write([]byte("Hi"))
case <-w.(http.CloseNotifier).CloseNotify():
log.Print("KO: Connection closed")
//http.Error(w, "Client Closed Request", 499)
}
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
So how can i stop responding to this client?
(for now i'm using the commented line)
Thanks for your answer.
It's not causing real problem,
I just had an hard time understanding why the hell go is responding 200 OK when the client close the connection ... (and why is it responding at all)
Consider the case where the client is talking HTTP/1.0 without Connection: keep-alive.
Consider what exactly?Consider the case where the client is talking HTTP/1.0 without Connection: keep-alive.The connection is half-closed, and the packet is sent because the part of the server that send the response isn't notified about the close being initiated.
The incoming stream half-closing doesn't necessarily mean the HTTP conversation is over. It seems risky for net/http to assume that, and would need protocol level checks around it to stay HTTP/1.0 compliant.
The server side connection will sit in LAST_ACK anyway until it gets a response back. That response might as well be the RST sending a HTTP response triggers on aborted clients.
The incoming stream half-closing doesn't necessarily mean the HTTP conversation is over. It seems risky for net/http to assume that, and would need protocol level checks around it to stay HTTP/1.0 compliant.I'm just curious if I'm forgetting something. The server has received at least a FIN at this point, what part of the protocol would be violated by swallowing the response? (I'm not saying this needs to be fixed, just playing devil's advocate)
On Thursday, December 11, 2014 12:48:55 PM UTC-8, James Bardin wrote:The incoming stream half-closing doesn't necessarily mean the HTTP conversation is over. It seems risky for net/http to assume that, and would need protocol level checks around it to stay HTTP/1.0 compliant.I'm just curious if I'm forgetting something. The server has received at least a FIN at this point, what part of the protocol would be violated by swallowing the response? (I'm not saying this needs to be fixed, just playing devil's advocate)RFC1945 did require a Content-Length header for all requests with a body, but that wasn't followed very well. There are (hopefully never run anymore) HTTP/1.0 clients that did not use any of Content-Length: ..., Connection: keepalive, or Transfer-Encoding: chunked. They would signal end of the request body by a half-close. Ugly? Yes. Likely to cause corruption? Very much so. The world is a better place now, but sending that response is marginally better than not sending it; let RST happen if it was a real close and not a half-close.