<commit_before>package toxiproxy

import (
	"encoding/json"
	"fmt"
	"log"
	"net"
	"net/http"
	"os"

	"github.com/Shopify/toxiproxy/toxics"
	"github.com/sirupsen/logrus"
	"github.com/gorilla/mux"
)

type ApiServer struct {
	Collection *ProxyCollection
}

func NewServer() *ApiServer {
	return &ApiServer{
		Collection: NewProxyCollection(),
	}
}

func (server *ApiServer) PopulateConfig(filename string) {
	file, err := os.Open(filename)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"config": filename,
			"error":  err,
		}).Error("Error reading config file")
	} else {
		proxies, err := server.Collection.PopulateJson(file)
		if err != nil {
			logrus.WithFields(logrus.Fields{
				"config": filename,
				"error":  err,
			}).Error("Failed to populate proxies from file")
		} else {
			logrus.WithFields(logrus.Fields{
				"config":  filename,
				"proxies": len(proxies),
			}).Info("Populated proxies from file")
		}
	}
}

func (server *ApiServer) Listen(host string, port string) {
	r := mux.NewRouter()
	r.HandleFunc("/reset", server.ResetState).Methods("POST")
	r.HandleFunc("/proxies", server.ProxyIndex).Methods("GET")
	r.HandleFunc("/proxies", server.ProxyCreate).Methods("POST")
	r.HandleFunc("/populate", server.Populate).Methods("POST")
	r.HandleFunc("/proxies/{proxy}", server.ProxyShow).Methods("GET")
	r.HandleFunc("/proxies/{proxy}", server.ProxyUpdate).Methods("POST")
	r.HandleFunc("/proxies/{proxy}", server.ProxyDelete).Methods("DELETE")
	r.HandleFunc("/proxies/{proxy}/toxics", server.ToxicIndex).Methods("GET")
	r.HandleFunc("/proxies/{proxy}/toxics", server.ToxicCreate).Methods("POST")
	r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicShow).Methods("GET")
	r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicUpdate).Methods("POST")
	r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicDelete).Methods("DELETE")

	r.HandleFunc("/version", server.Version).Methods("GET")
	http.Handle("/", r)<commit_msg>Address an issue where a toxiproxy can be used to bypass the Same-Origin Policy in web browsers<commit_after>package toxiproxy

import (
	"encoding/json"
	"fmt"
	"log"
	"net"
	"net/http"
	"os"
	"strings"

	"github.com/Shopify/toxiproxy/toxics"
	"github.com/gorilla/mux"
	"github.com/sirupsen/logrus"
)

type ApiServer struct {
	Collection *ProxyCollection
}

func NewServer() *ApiServer {
	return &ApiServer{
		Collection: NewProxyCollection(),
	}
}

func (server *ApiServer) PopulateConfig(filename string) {
	file, err := os.Open(filename)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"config": filename,
			"error":  err,
		}).Error("Error reading config file")
	} else {
		proxies, err := server.Collection.PopulateJson(file)
		if err != nil {
			logrus.WithFields(logrus.Fields{
				"config": filename,
				"error":  err,
			}).Error("Failed to populate proxies from file")
		} else {
			logrus.WithFields(logrus.Fields{
				"config":  filename,
				"proxies": len(proxies),
			}).Info("Populated proxies from file")
		}
	}
}

func StopBrowsersMiddleware(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if strings.HasPrefix(r.UserAgent(), "Mozilla/") {
			http.Error(w, "User agent not allowed", 403)
		} else {
			h.ServeHTTP(w, r)
		}
	})
}

func (server *ApiServer) Listen(host string, port string) {
	r := mux.NewRouter()
	r.HandleFunc("/reset", server.ResetState).Methods("POST")
	r.HandleFunc("/proxies", server.ProxyIndex).Methods("GET")
	r.HandleFunc("/proxies", server.ProxyCreate).Methods("POST")
	r.HandleFunc("/populate", server.Populate).Methods("POST")
	r.HandleFunc("/proxies/{proxy}", server.ProxyShow).Methods("GET")
	r.HandleFunc("/proxies/{proxy}", server.ProxyUpdate).Methods("POST")
	r.HandleFunc("/proxies/{proxy}", server.ProxyDelete).Methods("DELETE")
	r.HandleFunc("/proxies/{proxy}/toxics", server.ToxicIndex).Methods("GET")
	r.HandleFunc("/proxies/{proxy}/toxics", server.ToxicCreate).Methods("POST")
	r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicShow).Methods("GET")
	r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicUpdate).Methods("POST")
	r.HandleFunc("/proxies/{proxy}/toxics/{toxic}", server.ToxicDelete).Methods("DELETE")

	r.HandleFunc("/version", server.Version).Methods("GET")

	http.Handle("/", StopBrowsersMiddleware(r))
{'fraction_non_alphanumeric': 0.18070337806571032, 'fraction_numerical': 0.0006941230911614993, 'mean_word_length': 5.131914893617021, 'pattern_counts': {'":': 12, '<': 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': '1897838', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1490, 'n_words': 255}