You could respond with "Connection: close" header

On Tue, Jan 17, 2017 at 12:41 AM, Tony Grosinger <tgrosin...@extrahop.com>
wrote:

> I would like to create an HTTP server which forces the connection to be
> closed after writing the response.
> For example:
>
> func closingHandler(w http.ResponseWriter, r *http.Request) {
>     // Respond before hijacking?
>     fmt.Fprintf(w, "Hello World")
>
>     hj, ok := w.(http.Hijacker)
>     if !ok {
>         log.Println("Unable to create hijacker")
>         return
>     }
>
>     conn, bufrw, err := hj.Hijack()
>     if err != nil {
>         log.Println("Unable to hijack request")
>         return
>     }
>
>     bufrw.Flush()
>     conn.Close()
> }
> func main() {
>     mux := http.NewServeMux()
>     mux.HandleFunc("/", closingHandler)
>
>     go func() {
>         err := http.ListenAndServe(":8080", mux)
>         if err != nil {
>             log.Println("Failed to run server"
>         }
>     }()
> }
>
> This example does not work however because the client issuing the request
> cannot successfully read the response. Is there another way to close the
> connection manually that would allow me to respond to the HTTP request
> first?
> ​
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to