proxy header-only responses properly

fixes redirects
This commit is contained in:
Barna Csorogi 2015-03-29 16:09:48 +02:00
parent 343bdd5266
commit 9bf55d29a9
3 changed files with 22 additions and 19 deletions

View file

@ -79,7 +79,7 @@ func (p *Proxy) proxyResponse(w *ResponseWriter, r *ResponseReader) error {
w.takeHeaders(r) w.takeHeaders(r)
transcoder, found := p.transcoders[r.ContentType()] transcoder, found := p.transcoders[r.ContentType()]
if !found { if !found {
return w.writeFrom(r) return w.ReadFrom(r)
} }
w.setChunked() w.setChunked()
if err := transcoder.Transcode(w, r); err != nil { if err := transcoder.Transcode(w, r); err != nil {

View file

@ -55,28 +55,33 @@ func (w *ResponseWriter) takeHeaders(r *ResponseReader) {
w.WriteHeader(r.r.StatusCode) w.WriteHeader(r.r.StatusCode)
} }
func (w *ResponseWriter) WriteHeader(s int) {
w.statusCode = s
}
func (w *ResponseWriter) Header() http.Header { func (w *ResponseWriter) Header() http.Header {
return w.rw.Header() return w.rw.Header()
} }
func (w *ResponseWriter) writeFrom(r *ResponseReader) error { func (w *ResponseWriter) WriteHeader(s int) {
w.rw.WriteHeader(r.r.StatusCode) w.statusCode = s
_, err := io.Copy(w.rw, r) }
func (w *ResponseWriter) ReadFrom(r io.Reader) error {
w.flushHeaders()
_, err := io.Copy(w.Writer, r)
return err return err
} }
func (w *ResponseWriter) Write(b []byte) (int, error) {
w.flushHeaders()
return w.Writer.Write(b)
}
func (w *ResponseWriter) flushHeaders() {
if w.headersDone {
return
}
w.rw.WriteHeader(w.statusCode)
w.headersDone = true
}
func (w *ResponseWriter) setChunked() { func (w *ResponseWriter) setChunked() {
w.Header().Del("Content-Length") w.Header().Del("Content-Length")
} }
func (w *ResponseWriter) Write(b []byte) (int, error) {
if !w.headersDone {
w.rw.WriteHeader(w.statusCode)
w.headersDone = true
}
return w.Writer.Write(b)
}

View file

@ -2,12 +2,10 @@ package transcoder
import ( import (
"github.com/barnacs/compy/proxy" "github.com/barnacs/compy/proxy"
"io"
) )
type Identity struct{} type Identity struct{}
func (i *Identity) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error { func (i *Identity) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
_, err := io.Copy(w, r) return w.ReadFrom(r)
return err
} }