<commit_before>package handlers

import (
	"io"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/openfaas/faas/gateway/metrics"
	"github.com/openfaas/faas/gateway/types"
	"github.com/prometheus/client_golang/prometheus"
)

// MakeForwardingProxyHandler create a handler which forwards HTTP requests
func MakeForwardingProxyHandler(proxy *types.HTTPClientReverseProxy, metrics *metrics.MetricOptions) http.HandlerFunc {
	baseURL := proxy.BaseURL.String()
	if strings.HasSuffix(baseURL, "/") {
		baseURL = baseURL[0 : len(baseURL)-1]
	}

	return func(w http.ResponseWriter, r *http.Request) {

		requestURL := r.URL.String()

		log.Printf("> Forwarding [%s] to %s", r.Method, requestURL)
		start := time.Now()

		upstreamReq, _ := http.NewRequest(r.Method, baseURL+requestURL, nil)

		upstreamReq.Header["X-Forwarded-For"] = []string{r.RequestURI}

		if r.Body != nil {
			defer r.Body.Close()
			upstreamReq.Body = r.Body

		}

		res, resErr := proxy.Client.Do(upstreamReq)
		if resErr != nil {
			log.Printf("upstream client error: %s\n", resErr)
			return
		}

		if res.Body != nil {
			defer res.Body.Close()
		}

		for k, v := range res.Header {
			w.Header()[k] = v
		}

		io.CopyBuffer(w, res.Body, nil)<commit_msg>Pass along status code<commit_after>package handlers

import (
	"io"
	"log"
	"net/http"
	"strconv"
	"strings"
	"time"

	"github.com/openfaas/faas/gateway/metrics"
	"github.com/openfaas/faas/gateway/types"
	"github.com/prometheus/client_golang/prometheus"
)

// MakeForwardingProxyHandler create a handler which forwards HTTP requests
func MakeForwardingProxyHandler(proxy *types.HTTPClientReverseProxy, metrics *metrics.MetricOptions) http.HandlerFunc {
	baseURL := proxy.BaseURL.String()
	if strings.HasSuffix(baseURL, "/") {
		baseURL = baseURL[0 : len(baseURL)-1]
	}

	return func(w http.ResponseWriter, r *http.Request) {

		requestURL := r.URL.String()

		log.Printf("> Forwarding [%s] to %s", r.Method, requestURL)
		start := time.Now()

		upstreamReq, _ := http.NewRequest(r.Method, baseURL+requestURL, nil)

		upstreamReq.Header["X-Forwarded-For"] = []string{r.RequestURI}

		if r.Body != nil {
			defer r.Body.Close()
			upstreamReq.Body = r.Body

		}

		res, resErr := proxy.Client.Do(upstreamReq)
		if resErr != nil {
			log.Printf("upstream client error: %s\n", resErr)
			return
		}

		if res.Body != nil {
			defer res.Body.Close()
		}

		// Populate any headers received
		for k, v := range res.Header {
			w.Header()[k] = v
		}

		// Write status code
		w.WriteHeader(res.StatusCode)

		// Copy the body over
		io.CopyBuffer(w, res.Body, nil)
{'fraction_non_alphanumeric': 0.14583333333333334, 'fraction_numerical': 0.0015432098765432098, 'mean_word_length': 3.837686567164179, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '804271', 'n_tokens_mistral': 1035, 'n_tokens_neox': 937, 'n_words': 192}