remove buffering of HTTP requests

Only the http.ResponseWriter implements the http.Flusher interface.
We need to make sure that writes to the Request.Body are actually sent out.
This commit is contained in:
Marten Seemann 2020-06-24 14:50:56 +07:00
parent fc047d7904
commit 0f9fa588b1

View file

@ -1,7 +1,6 @@
package http3 package http3
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
@ -40,16 +39,15 @@ func newRequestWriter(logger utils.Logger) *requestWriter {
} }
func (w *requestWriter) WriteRequest(str quic.Stream, req *http.Request, gzip bool) error { func (w *requestWriter) WriteRequest(str quic.Stream, req *http.Request, gzip bool) error {
wr := bufio.NewWriter(str) buf := &bytes.Buffer{}
if err := w.writeHeaders(buf, req, gzip); err != nil {
if err := w.writeHeaders(wr, req, gzip); err != nil { return err
}
if _, err := str.Write(buf.Bytes()); err != nil {
return err return err
} }
// TODO: add support for trailers // TODO: add support for trailers
if req.Body == nil { if req.Body == nil {
if err := wr.Flush(); err != nil {
return err
}
str.Close() str.Close()
return nil return nil
} }
@ -70,20 +68,15 @@ func (w *requestWriter) WriteRequest(str quic.Stream, req *http.Request, gzip bo
} }
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
(&dataFrame{Length: uint64(n)}).Write(buf) (&dataFrame{Length: uint64(n)}).Write(buf)
if _, err := wr.Write(buf.Bytes()); err != nil { if _, err := str.Write(buf.Bytes()); err != nil {
w.logger.Errorf("Error writing request: %s", err) w.logger.Errorf("Error writing request: %s", err)
return return
} }
if _, err := wr.Write(b[:n]); err != nil { if _, err := str.Write(b[:n]); err != nil {
w.logger.Errorf("Error writing request: %s", err) w.logger.Errorf("Error writing request: %s", err)
return return
} }
} }
if err := wr.Flush(); err != nil {
fmt.Println(err)
w.logger.Errorf("Error writing request: %s", err)
return
}
str.Close() str.Close()
}() }()