func()

in tools/integration_tests/emulator_tests/proxy_server/main.go [92:159]


func (ph ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	targetURL := fmt.Sprintf("%s%s", gConfig.TargetHost, r.RequestURI)
	req, err := http.NewRequest(r.Method, targetURL, r.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	for name, values := range r.Header {
		for _, value := range values {
			req.Header.Add(name, value)
		}
	}
	// Determine the request type and instruction (e.g., read, write, metadata) based on the incoming request.
	reqTypeAndInstruction := deduceRequestTypeAndInstruction(r)

	// Add a unique retry ID to the request headers, associating it with the
	// deduced request type and instruction. This is used for adding custom failures on requests.
	err = AddRetryID(req, reqTypeAndInstruction)
	if err != nil {
		log.Printf("AddRetryID: %v", err)
	}

	// Send the request to the target server
	client := &http.Client{}
	start := time.Now()
	resp, err := client.Do(req)
	elapsed := time.Since(start)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	respURL, err := resp.Location()
	// Change the response URL host to the proxy server host.
	// This is necessary because, from the client's perspective, the proxy server is the endpoint.
	// Therefore, the response must appear to originate from the proxy host.
	if err == nil {
		// Parse the original URL.
		u, err := url.Parse(respURL.String())
		if err != nil {
			log.Println("Error parsing URL:", err)
			return
		}

		u.Host = "localhost:" + gPort
		resp.Header.Set("Location", u.String())
	}

	defer resp.Body.Close()

	// Copy headers from the target server's response
	for name, values := range resp.Header {
		for _, value := range values {
			w.Header().Add(name, value)
		}
	}

	// Copy the response body
	w.WriteHeader(resp.StatusCode)
	_, err = io.Copy(w, resp.Body)
	if err != nil {
		log.Printf("Error in coping response body: %v", err)
	}
	if *fDebug {
		log.Printf("Respnse Status: %d\n", resp.StatusCode)
		log.Printf("Elapsed Time: %.3fs\n", elapsed.Seconds())
	}
}