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)
| 87 | // in rw.WriteString which was created for performance reasons. |
| 88 | // Non-nil bytes win. |
| 89 | func (rw *ResponseRecorder) writeHeader(b []byte, str string) { |
| 90 | if rw.wroteHeader { |
| 91 | return |
| 92 | } |
| 93 | if len(str) > 512 { |
| 94 | str = str[:512] |
| 95 | } |
| 96 | |
| 97 | m := rw.Header() |
| 98 | |
| 99 | _, hasType := m["Content-Type"] |
| 100 | hasTE := m.Get("Transfer-Encoding") != "" |
| 101 | if !hasType && !hasTE { |
| 102 | if b == nil { |
| 103 | b = []byte(str) |
| 104 | } |
| 105 | m.Set("Content-Type", http.DetectContentType(b)) |
| 106 | } |
| 107 | |
| 108 | rw.WriteHeader(200) |
| 109 | } |
| 110 | |
| 111 | // Write implements http.ResponseWriter. The data in buf is written to |
| 112 | // rw.Body, if not nil. |
no test coverage detected