mirror of
https://git.phreedom.club/localhost_frssoft/compy.git
synced 2024-11-23 16:41:30 +00:00
proxy header-only responses properly
fixes redirects
This commit is contained in:
parent
343bdd5266
commit
9bf55d29a9
|
@ -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 {
|
||||||
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue