// corsRemovingTransport is a wrapper for an internal transport. It removes CORS headers // from the internal response. type corsRemovingTransport struct { http.RoundTripper } func (p *corsRemovingTransport) RoundTrip(req *http.Request) (*http.Response, error) { resp, err := p.RoundTripper.RoundTrip(req) if err != nil { return nil, err } removeCORSHeaders(resp) return resp, nil } var _ = net.RoundTripperWrapper(&corsRemovingTransport{}) func (rt *corsRemovingTransport) WrappedRoundTripper() http.RoundTripper { return rt.RoundTripper } // removeCORSHeaders strip CORS headers sent from the backend // This should be called on all responses before returning func removeCORSHeaders(resp *http.Response) { resp.Header.Del("Access-Control-Allow-Credentials") resp.Header.Del("Access-Control-Allow-Headers") resp.Header.Del("Access-Control-Allow-Methods") resp.Header.Del("Access-Control-Allow-Origin") }
return &chaosrt{rt, notify, c} } // RoundTrip gives each ChaosFunc an opportunity to intercept the request. The first // interceptor wins. func (rt *chaosrt) RoundTrip(req *http.Request) (*http.Response, error) { for _, c := range rt.c { if intercept, resp, err := c.Intercept(req); intercept { rt.notify.OnChaos(req, c) return resp, err } } return rt.rt.RoundTrip(req) } var _ = net.RoundTripperWrapper(&chaosrt{}) func (rt *chaosrt) WrappedRoundTripper() http.RoundTripper { return rt.rt } // Seed represents a consistent stream of chaos. type Seed struct { *rand.Rand } // NewSeed creates an object that assists in generating random chaotic events // based on a deterministic seed. func NewSeed(seed int64) Seed { return Seed{rand.New(rand.NewSource(seed))} }
if redirect := resp.Header.Get("Location"); redirect != "" { resp.Header.Set("Location", t.rewriteURL(redirect, req.URL)) return resp, nil } cType := resp.Header.Get("Content-Type") cType = strings.TrimSpace(strings.SplitN(cType, ";", 2)[0]) if cType != "text/html" { // Do nothing, simply pass through return resp, nil } return t.rewriteResponse(req, resp) } var _ = net.RoundTripperWrapper(&Transport{}) func (rt *Transport) WrappedRoundTripper() http.RoundTripper { return rt.RoundTripper } // rewriteURL rewrites a single URL to go through the proxy, if the URL refers // to the same host as sourceURL, which is the page on which the target URL // occurred. If any error occurs (e.g. parsing), it returns targetURL. func (t *Transport) rewriteURL(targetURL string, sourceURL *url.URL) string { url, err := url.Parse(targetURL) if err != nil { return targetURL } isDifferentHost := url.Host != "" && url.Host != sourceURL.Host