func()

in artifactserver/cmd/artifactserver/main.go [107:150]


func (s *Redirector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	klog.Infof("request %s %s", r.Method, r.URL)

	tokens := strings.Split(strings.TrimPrefix(r.URL.Path, "/"), "/")

	if len(tokens) < 1 {
		klog.Infof("%s -> 404", r.URL.Path)
		w.WriteHeader(http.StatusNotFound)
		return
	}

	backend := s.backends[tokens[0]]

	if backend == nil {
		// healthz check endpoint
		if r.Method == "GET" && len(tokens) == 2 && tokens[0] == "_" && tokens[1] == "healthz" {
			klog.Infof("%s -> 200 OK", r.URL.Path)
			w.WriteHeader(http.StatusOK)
			return
		}

		klog.Infof("%s -> 404", r.URL.Path)
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if r.Method != "GET" && r.Method != "HEAD" {
		klog.Infof("%s %s -> 405", r.Method, r.URL.Path)
		w.WriteHeader(http.StatusMethodNotAllowed)
		return
	}

	target := &url.URL{
		Scheme: "https",
		Host:   backend.Host,
	}

	path := strings.TrimPrefix(r.URL.Path, "/"+backend.Name)
	path = strings.TrimPrefix(path, "/")
	target.Path = backend.PathPrefix + "/" + path

	klog.Infof("%s -> 302 %s", r.URL.Path, target)
	http.Redirect(w, r, target.String(), 302)
}