writeHeader writes a header if it was not written yet and detects Content-Type if needed. bytes or str are the beginning of the response body. We pass both to avoid unnecessarily generate garbage in rw.WriteString which was created for performance reasons. Non-nil bytes win.
(b []byte, str string)
| 77 | // in rw.WriteString which was created for performance reasons. |
| 78 | // Non-nil bytes win. |
| 79 | func (rw *ResponseRecorder) writeHeader(b []byte, str string) { |
| 80 | if rw.wroteHeader { |
| 81 | return |
| 82 | } |
| 83 | if len(str) > 512 { |
| 84 | str = str[:512] |
| 85 | } |
| 86 | |
| 87 | m := rw.Header() |
| 88 | |
| 89 | _, hasType := m["Content-Type"] |
| 90 | hasTE := m.Get("Transfer-Encoding") != "" |
| 91 | if !hasType && !hasTE { |
| 92 | if b == nil { |
| 93 | b = []byte(str) |
| 94 | } |
| 95 | m.Set("Content-Type", http.DetectContentType(b)) |
| 96 | } |
| 97 | |
| 98 | rw.WriteHeader(200) |
| 99 | } |
| 100 | |
| 101 | // Write implements http.ResponseWriter. The data in buf is written to |
| 102 | // rw.Body, if not nil. |
no test coverage detected