func forward()

in cmd/frontend/main.go [252:304]


func forward(logger log.Logger, target *url.URL, transport http.RoundTripper) http.Handler {
	client := http.Client{Transport: transport}

	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		u := *target
		u.Path = path.Join(u.Path, req.URL.Path)

		method := req.Method
		// Write all params into the URL and make a GET request to work around
		// /api/v1/series currently not accepting match[] params on POST.
		if req.URL.Path == "/api/v1/series" {
			method = "GET"
			if err := req.ParseForm(); err != nil {
				w.WriteHeader(http.StatusInternalServerError)
			}
			u.RawQuery = req.Form.Encode()
		} else {
			u.RawQuery = req.URL.RawQuery
		}

		newReq, err := http.NewRequestWithContext(req.Context(), method, u.String(), req.Body)
		if err != nil {
			//nolint:errcheck
			level.Warn(logger).Log("msg", "creating request failed", "err", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		copyHeader(newReq.Header, req.Header)

		resp, err := client.Do(newReq)
		if err != nil {
			//nolint:errcheck
			if errors.Is(err, context.Canceled) {
				level.Warn(logger).Log("msg", "request to GCM was canceled by the caller of frontend. If a program made the request, consider increasing the timeout", "err", err)
				w.WriteHeader(http.StatusBadRequest)
			} else {
				level.Warn(logger).Log("msg", "requesting GCM failed", "err", err)
				w.WriteHeader(http.StatusInternalServerError)
			}
			return
		}

		copyHeader(w.Header(), resp.Header)
		w.WriteHeader(resp.StatusCode)

		defer resp.Body.Close()
		if _, err := io.Copy(w, resp.Body); err != nil {
			//nolint:errcheck
			level.Warn(logger).Log("msg", "copying response body failed", "err", err)
			return
		}
	})
}