The need to achieve an intermediate proxy redirect and load out html modified base in the href, making it possible to successfully load other needs js, css ... and other documents.
First put the source code:
func main() {
go func() {
s := new(server)
http.Handle("/dev/", &server{})
http.Handle("/dev", &server{})
http.Handle("/", &server{})
fmt.Println("start")
http.ListenAndServe(":8081", s)
}()
time.Sleep(time.Hour * 200000)
}
type server struct {
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
bol := r.RequestURI == "/index.html"
if strings.HasPrefix(r.RequestURI, "/dev") || bol {
newReq := new(http.Request)
*newReq = *r
newReq.RequestURI = ``
newReq.URL.Host = "127.0.0.1:8888"//真实的server的端口
newReq.URL.Scheme = "http"
newReq.URL.Path = strings.TrimPrefix(r.URL.Path, "/dev")
newReq.Header = r.Header
matchChar, _ := regexp.Match(`^(/dev/?)||(/index.html/?)$`, []byte(r.RequestURI))
if matchChar {
resp, err := http.DefaultClient.Do(newReq)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
bb, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
text := string(bb)
replacestr := `<base id="base_url" href="/dev/">`
reg := regexp.MustCompile(`<base id="base_url" href="/">`)
str := reg.ReplaceAllString(text, replacestr)
b1 := []byte(str)
for k, v := range resp.Header {
fmt.Println(k, v[0])
if k != "Content-Length" {
for i := 0; i < len(v); i++ {
w.Header().Add(k, v[i])
}
}
}
w.Write(b1)
} else {
resp, err := http.DefaultClient.Do(newReq)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
defer resp.Body.Close()
io.Copy(w, resp.Body)
b, _ := ioutil.ReadAll(resp.Body)
for k, v := range resp.Header {
fmt.Println(k, v[0])
if k != "Content-Length" {
for i := 0; i < len(v); i++ {
w.Header().Add(k, v[i])
}
}
}
w.Write(b)
}
}
}
1,
there is a matching route, if the prefix of the URI is / dev, routing on redirect, change host, path.
2,
this match, if URI is /dev,/,/index.html, three, jump is home, you need to change the default routing base. Using regular expressions to replace the contents of html, tried before to write a js, and then read the file. But there is a logical problem, js is loaded, so the base is equal to no change.
And then return the contents of Body. But also return information in the header. I used for range will add responsewriter resp in the header. When running here reported a fault, is that content-length does not meet the length, thought yes, I changed to the URI certainly not the same length, then how to do it? I went to see the header source.
Turned some English, in simple terms, if not set Content-Length, header will give you added automatically. Oh? I do not like the way that field. The field and the "Content-Length" does not match the full-additive.
Then on it, happy.
But the next morning, they encountered a problem, visit 127.0.0.1:8081/dev, no load screen, the port is not occupied, I have not modified the source code, the server can access the actual visit. That where the problem is, as a white intern, I was crazy. Under desperation, I used the port 8082, and can smoothly visit. But this is what causes it? There are chiefs know what.