<commit_before>// Package proxy is middleware that proxies requests.
package proxy

import (
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"

	"github.com/mholt/caddy/middleware"
)

// New creates a new instance of proxy middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
	var rules []proxyRule

	for c.Next() {
		rule := proxyRule{}

		if !c.Args(&rule.from, &rule.to) {
			return nil, c.ArgErr()
		}

		rules = append(rules, rule)
	}

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

			for _, rule := range rules {
				if middleware.Path(r.URL.Path).Matches(rule.from) {
					var scheme string
					if r.TLS == nil {
						scheme = "http"
					} else {
						scheme = "https"
					}

					baseUrl, err := url.Parse(scheme + "://" + rule.to)
					if err != nil {
						log.Fatal(err)
					}
					r.Host = baseUrl.Host

					// TODO: Construct this before; not during every request, if possible
					proxy := httputil.NewSingleHostReverseProxy(baseUrl)
					proxy.ServeHTTP(w, r)

				} else {
					next(w, r)
				}
			}
		}
	}, nil
}

type proxyRule struct {
	from string
	to   string
}<commit_msg>Refactored proxy middleware to return errors<commit_after>// Package proxy is middleware that proxies requests.
package proxy

import (
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"

	"github.com/mholt/caddy/middleware"
)

// New creates a new instance of proxy middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
	var rules []proxyRule

	for c.Next() {
		rule := proxyRule{}

		if !c.Args(&rule.from, &rule.to) {
			return nil, c.ArgErr()
		}

		rules = append(rules, rule)
	}

	return func(next middleware.HandlerFunc) middleware.HandlerFunc {
		return func(w http.ResponseWriter, r *http.Request) (int, error) {

			for _, rule := range rules {
				if middleware.Path(r.URL.Path).Matches(rule.from) {
					var scheme string
					if r.TLS == nil {
						scheme = "http"
					} else {
						scheme = "https"
					}

					baseUrl, err := url.Parse(scheme + "://" + rule.to)
					if err != nil {
						log.Fatal(err)
					}
					r.Host = baseUrl.Host

					// TODO: Construct this before; not during every request, if possible
					proxy := httputil.NewSingleHostReverseProxy(baseUrl)
					proxy.ServeHTTP(w, r)
					return 0, nil
				}
			}

			return next(w, r)
		}
	}, nil
}

type proxyRule struct {
	from string
	to   string
}
{'fraction_non_alphanumeric': 0.12871690427698573, 'fraction_numerical': 0.0004073319755600815, 'mean_word_length': 2.721212121212121, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '740978', 'n_tokens_mistral': 1040, 'n_tokens_neox': 846, 'n_words': 222}