1
0
mirror of https://github.com/golang/go synced 2024-11-22 09:24:41 -07:00

http: clarify use of w.conn.body in Write when sniffing.

R=gri, r, r, rsc
CC=golang-dev
https://golang.org/cl/4794047
This commit is contained in:
David Symonds 2011-07-22 13:52:21 +10:00
parent 3e79c958c4
commit 99e5d48e59

View File

@ -359,8 +359,7 @@ func (w *response) sniff() {
w.needSniff = false w.needSniff = false
data := w.conn.body data := w.conn.body
fmt.Fprintf(w.conn.buf, "Content-Type: %s\r\n", DetectContentType(data)) fmt.Fprintf(w.conn.buf, "Content-Type: %s\r\n\r\n", DetectContentType(data))
io.WriteString(w.conn.buf, "\r\n")
if len(data) == 0 { if len(data) == 0 {
return return
@ -408,10 +407,14 @@ func (w *response) Write(data []byte) (n int, err os.Error) {
// We need to sniff the beginning of the output to // We need to sniff the beginning of the output to
// determine the content type. Accumulate the // determine the content type. Accumulate the
// initial writes in w.conn.body. // initial writes in w.conn.body.
body := w.conn.body // Cap m so that append won't allocate.
m = copy(body[len(body):cap(body)], data) m := cap(w.conn.body) - len(w.conn.body)
w.conn.body = body[:len(body)+m] if m > len(data) {
if m == len(data) { m = len(data)
}
w.conn.body = append(w.conn.body, data[:m]...)
data = data[m:]
if len(data) == 0 {
// Copied everything into the buffer. // Copied everything into the buffer.
// Wait for next write. // Wait for next write.
return m, nil return m, nil
@ -423,7 +426,6 @@ func (w *response) Write(data []byte) (n int, err os.Error) {
// of the data as a normal Write. // of the data as a normal Write.
// Calling sniff clears needSniff. // Calling sniff clears needSniff.
w.sniff() w.sniff()
data = data[m:]
} }
// TODO(rsc): if chunking happened after the buffering, // TODO(rsc): if chunking happened after the buffering,