mirror of
https://github.com/golang/go
synced 2024-11-22 02:14:40 -07:00
http: use ChunkWriter in Request.Write
R=rsc CC=golang-dev https://golang.org/cl/196079
This commit is contained in:
parent
43d2e59a81
commit
15da069a37
@ -181,34 +181,22 @@ func (req *Request) Write(w io.Writer) os.Error {
|
||||
io.WriteString(w, "\r\n")
|
||||
|
||||
if req.Body != nil {
|
||||
buf := make([]byte, chunkSize)
|
||||
Loop:
|
||||
for {
|
||||
var nr, nw int
|
||||
var er, ew os.Error
|
||||
if nr, er = req.Body.Read(buf); nr > 0 {
|
||||
if er == nil || er == os.EOF {
|
||||
fmt.Fprintf(w, "%x\r\n", nr)
|
||||
nw, ew = w.Write(buf[0:nr])
|
||||
fmt.Fprint(w, "\r\n")
|
||||
cw := NewChunkedWriter(w)
|
||||
if _, err := io.Copy(cw, req.Body); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cw.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
switch {
|
||||
case er != nil:
|
||||
if er == os.EOF {
|
||||
break Loop
|
||||
// TODO(petar): Write trailer here and append \r\n. For now, we
|
||||
// simply send the final \r\n:
|
||||
if _, err := fmt.Fprint(w, "\r\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
return er
|
||||
case ew != nil:
|
||||
return ew
|
||||
case nw < nr:
|
||||
return io.ErrShortWrite
|
||||
if err := req.Body.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
req.Body.Close()
|
||||
// last-chunk CRLF
|
||||
fmt.Fprint(w, "0\r\n\r\n")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user