MCPcopy Index your code
hub / github.com/tailscale/tailscale / writeHeader

Method writeHeader

tempfork/httprec/httprec.go:79–99  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

77// in rw.WriteString which was created for performance reasons.
78// Non-nil bytes win.
79func (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.

Callers 2

WriteMethod · 0.95
WriteStringMethod · 0.95

Calls 4

HeaderMethod · 0.95
WriteHeaderMethod · 0.95
GetMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected