WriteHeader sends an HTTP response header with the provided status code.
(code int)
| 94 | |
| 95 | // WriteHeader sends an HTTP response header with the provided status code. |
| 96 | func (r *response) WriteHeader(code int) { |
| 97 | if r.wroteHeader { |
| 98 | return |
| 99 | } |
| 100 | r.wroteHeader = true |
| 101 | if code == http.StatusNotModified { |
| 102 | // Must not have body. |
| 103 | r.header.Del("Content-Type") |
| 104 | r.header.Del("Content-Length") |
| 105 | r.header.Del("Transfer-Encoding") |
| 106 | } else if r.header.Get("Content-Type") == "" { |
| 107 | r.header.Set("Content-Type", "text/html; charset=utf-8") |
| 108 | } |
| 109 | |
| 110 | if r.header.Get("Date") == "" { |
| 111 | r.header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) |
| 112 | } |
| 113 | |
| 114 | fmt.Fprintf(r.w, "Status: %d %s\r\n", code, http.StatusText(code)) |
| 115 | //nolint:errcheck // unable to propagate |
| 116 | r.header.Write(r.w) |
| 117 | //nolint:errcheck // unable to propagate |
| 118 | r.w.WriteString("\r\n") |
| 119 | } |
| 120 | |
| 121 | // Flush sends any buffered data to the client. |
| 122 | func (r *response) Flush() { |