abort aborts the client connection associated with rwa. The last http chunk in the response stream is intentionally written incorrectly, so the client, which reads the response, could notice this error.
()
| 622 | // The last http chunk in the response stream is intentionally written incorrectly, |
| 623 | // so the client, which reads the response, could notice this error. |
| 624 | func (rwa *responseWriterWithAbort) abort() { |
| 625 | if !rwa.sentHeaders { |
| 626 | logger.Panicf("BUG: abort can be called only after http response headers are sent") |
| 627 | } |
| 628 | if rwa.aborted { |
| 629 | // Nothing to do. The connection has been already aborted. |
| 630 | return |
| 631 | } |
| 632 | hj, ok := rwa.ResponseWriter.(http.Hijacker) |
| 633 | if !ok { |
| 634 | logger.Panicf("BUG: ResponseWriter must implement http.Hijacker interface") |
| 635 | } |
| 636 | conn, bw, err := hj.Hijack() |
| 637 | if err != nil { |
| 638 | logger.WarnfSkipframes(2, "cannot hijack response connection: %s", err) |
| 639 | return |
| 640 | } |
| 641 | |
| 642 | // Just write an error message into the client connection as is without http chunked encoding. |
| 643 | // This is needed in order to notify the client about the aborted connection. |
| 644 | _, _ = bw.WriteString("\nthe connection has been aborted; see the last line in the response and/or in the server log for the reason\n") |
| 645 | _ = bw.Flush() |
| 646 | |
| 647 | // Forcibly close the client connection in order to break http keep-alive at client side. |
| 648 | _ = conn.Close() |
| 649 | |
| 650 | rwa.aborted = true |
| 651 | } |
| 652 | |
| 653 | // Errorf writes formatted error message to w and to logger. |
| 654 | func Errorf(w http.ResponseWriter, r *http.Request, format string, args ...any) { |
no test coverage detected