passes tests with no rewrites
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"io"
|
||||
"local/rproxy3/config"
|
||||
"local/rproxy3/storage/packable"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -10,15 +12,27 @@ import (
|
||||
)
|
||||
|
||||
type redirPurge struct {
|
||||
proxyHost string
|
||||
targetHost string
|
||||
proxyHost string
|
||||
targetHost string
|
||||
baseTransport http.RoundTripper
|
||||
}
|
||||
|
||||
type rewrite struct {
|
||||
rewrites map[string]string
|
||||
baseTransport http.RoundTripper
|
||||
}
|
||||
|
||||
func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||
newURL, err := s.lookup(r.Host)
|
||||
transport := &redirPurge{
|
||||
proxyHost: r.Host,
|
||||
targetHost: newURL.Host,
|
||||
var transport http.RoundTripper
|
||||
transport = &redirPurge{
|
||||
proxyHost: r.Host,
|
||||
targetHost: newURL.Host,
|
||||
baseTransport: http.DefaultTransport,
|
||||
}
|
||||
transport = &rewrite{
|
||||
rewrites: config.GetRewrites(),
|
||||
baseTransport: transport,
|
||||
}
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
@@ -40,7 +54,7 @@ func (s *Server) lookup(host string) (*url.URL, error) {
|
||||
}
|
||||
|
||||
func (rp *redirPurge) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
resp, err := http.DefaultTransport.RoundTrip(r)
|
||||
resp, err := rp.baseTransport.RoundTrip(r)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
@@ -49,3 +63,30 @@ func (rp *redirPurge) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (rw *rewrite) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
resp, err := rw.baseTransport.RoundTrip(r)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
pr, pw := io.Pipe()
|
||||
body := resp.Body
|
||||
resp.Body = pr
|
||||
go func() {
|
||||
buff := make([]byte, 1024)
|
||||
for n, err := body.Read(buff); err == nil || n > 0; n, err = body.Read(buff) {
|
||||
chunk := buff[:n]
|
||||
m := 0
|
||||
for m < n {
|
||||
l, err := pw.Write(chunk[m:])
|
||||
if err != nil {
|
||||
pw.CloseWithError(err)
|
||||
return
|
||||
}
|
||||
m += l
|
||||
}
|
||||
}
|
||||
pw.CloseWithError(err)
|
||||
}()
|
||||
return resp, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user