<commit_before>package moxxiConf

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
	"text/template"
)

func CreateMux(handlers []HandlerConfig, l *log.Logger) *http.ServeMux {
	mux := http.NewServeMux()
	for _, handler := range handlers {
		switch handler.handlerType {
		case "json":
			mux.HandleFunc(handler.handlerRoute, JSONHandler(handler, l))
		case "form":
			mux.HandleFunc(handler.handlerRoute, FormHandler(handler, l))
		case "static":
			mux.HandleFunc(handler.handlerRoute, StaticHandler(handler, l))
		}
	}
	return mux
}

// FormHandler - creates and returns a Handler for both Query and Form requests
func FormHandler(config HandlerConfig, l *log.Logger) http.HandlerFunc {
	confWriter := confWrite(config)

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

		if extErr := r.ParseForm(); extErr != nil {
			http.Error(w, extErr.Error(), http.StatusBadRequest)
			return
		}

		if r.Form.Get("host") == "" {
			pkgErr := &NewErr{Code: ErrNoHostname}
			http.Error(w, pkgErr.Error(), http.StatusPreconditionFailed)
			l.Println(pkgErr.LogError(r))
			return
		}
		host := r.Form.Get("host")

		if r.Form.Get("ip") == "" {
			pkgErr := &NewErr{Code: ErrNoIP}
			http.Error(w, pkgErr.Error(), http.StatusPreconditionFailed)
			l.Println(pkgErr.LogError(r))
			return
		}

		tls := parseCheckbox(r.Form.Get("tls"))

		port, err := strconv.Atoi(r.Form.Get("port"))
		if err != nil {
			port = 80
		}

		vhost := siteParams{
			IntHost:      host,
			IntIP:        r.Form.Get("ip"),
			Encrypted:    tls,
			IntPort:      port,
			StripHeaders: r.Form["header"],
		}

		vhost, pkgErr := confCheck(vhost, config)
		if pkgErr != nil {
			http.Error(w, pkgErr.Error(), http.StatusPreconditionFailed)
			l.Println(pkgErr.LogError(r))
			return
		}

		if vhost, pkgErr = confWriter(vhost); pkgErr != nil {
			http.Error(w, pkgErr.Error(), http.StatusInternalServerError)
			l.Println(pkgErr.LogError(r))
			return
		}

		if extErr := config.resTempl.Execute(w, []siteParams{vhost}); extErr != nil {
			http.Error(w, extErr.Error(), http.StatusInternalServerError)
			l.Println(extErr.Error())
			return
		}
		return
	}
}

// JSONHandler - creates and returns a Handler for JSON body requests
func JSONHandler(config HandlerConfig, l *log.Logger) http.HandlerFunc {

	var tStart, tEnd, tBody *template.Template

	for _, each := range config.resTempl.Templates() {
		switch each.Name() {
		case "start":
			tStart = each
		case "end":
			tEnd = each
		case "body":
			tBody = each
		}
	}

	if tStart == nil || tEnd == nil || tBody == nil {
		return InvalidHandler("bad template", http.StatusInternalServerError)
	}

	confWriter := confWrite(config)

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

		var emptyInterface interface{}
		type locSiteParams struct {
			ExtHost      string
			IntHost      string
			IntIP        string
			IntPort      int
			Encrypted    bool
			StripHeaders []string
			ErrorString  string
			Error        Err
		}

		tStart.Execute(w, emptyInterface)

		decoder := json.NewDecoder(r.Body)

		for decoder.More() {
			var v siteParams
			if err := decoder.Decode(&v); err != nil {
				continue
			}

			v, err := confCheck(v, config)
			if err == nil || err.GetCode() == ErrBadHostnameTrace {
				v, err = confWriter(v)
			}

			var vPlus = struct {
				ExtHost      string
				IntHost      string
				IntIP        string
				IntPort      int
				Encrypted    bool
				StripHeaders []string
				Error        string
			}{
				ExtHost:      v.ExtHost,
				IntHost:      v.IntHost,
				IntIP:        v.IntIP,
				IntPort:      v.IntPort,
				Encrypted:    v.Encrypted,
				StripHeaders: v.StripHeaders,
			}

			if err != nil {
				l.Println(err.LogError(r))
				vPlus.Error = err.Error()
			}

			if err := tBody.Execute(w, vPlus); err != nil {<commit_msg>changed the handler config to write out a conf and honor errors even if stuff's bad<commit_after>package moxxiConf

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
	"text/template"
)

func CreateMux(handlers []HandlerConfig, l *log.Logger) *http.ServeMux {
	mux := http.NewServeMux()
	for _, handler := range handlers {
		switch handler.handlerType {
		case "json":
			mux.HandleFunc(handler.handlerRoute, JSONHandler(handler, l))
		case "form":
			mux.HandleFunc(handler.handlerRoute, FormHandler(handler, l))
		case "static":
			mux.HandleFunc(handler.handlerRoute, StaticHandler(handler, l))
		}
	}
	return mux
}

// FormHandler - creates and returns a Handler for both Query and Form requests
func FormHandler(config HandlerConfig, l *log.Logger) http.HandlerFunc {
	confWriter := confWrite(config)

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

		if extErr := r.ParseForm(); extErr != nil {
			http.Error(w, extErr.Error(), http.StatusBadRequest)
			return
		}

		if r.Form.Get("host") == "" {
			pkgErr := &NewErr{Code: ErrNoHostname}
			http.Error(w, pkgErr.Error(), http.StatusPreconditionFailed)
			l.Println(pkgErr.LogError(r))
			return
		}
		host := r.Form.Get("host")

		if r.Form.Get("ip") == "" {
			pkgErr := &NewErr{Code: ErrNoIP}
			http.Error(w, pkgErr.Error(), http.StatusPreconditionFailed)
			l.Println(pkgErr.LogError(r))
			return
		}

		tls := parseCheckbox(r.Form.Get("tls"))

		port, err := strconv.Atoi(r.Form.Get("port"))
		if err != nil {
			port = 80
		}

		vhost := siteParams{
			IntHost:      host,
			IntIP:        r.Form.Get("ip"),
			Encrypted:    tls,
			IntPort:      port,
			StripHeaders: r.Form["header"],
		}

		vhost, pkgErr := confCheck(vhost, config)
		if pkgErr != nil {
			http.Error(w, pkgErr.Error(), http.StatusPreconditionFailed)
			l.Println(pkgErr.LogError(r))
			return
		}

		if vhost, pkgErr = confWriter(vhost); pkgErr != nil {
			http.Error(w, pkgErr.Error(), http.StatusInternalServerError)
			l.Println(pkgErr.LogError(r))
			return
		}

		if extErr := config.resTempl.Execute(w, []siteParams{vhost}); extErr != nil {
			http.Error(w, extErr.Error(), http.StatusInternalServerError)
			l.Println(extErr.Error())
			return
		}
		return
	}
}

// JSONHandler - creates and returns a Handler for JSON body requests
func JSONHandler(config HandlerConfig, l *log.Logger) http.HandlerFunc {

	var tStart, tEnd, tBody *template.Template

	for _, each := range config.resTempl.Templates() {
		switch each.Name() {
		case "start":
			tStart = each
		case "end":
			tEnd = each
		case "body":
			tBody = each
		}
	}

	if tStart == nil || tEnd == nil || tBody == nil {
		return InvalidHandler("bad template", http.StatusInternalServerError)
	}

	confWriter := confWrite(config)

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

		var emptyInterface interface{}
		type locSiteParams struct {
			ExtHost      string
			IntHost      string
			IntIP        string
			IntPort      int
			Encrypted    bool
			StripHeaders []string
			ErrorString  string
			Error        Err
		}

		tStart.Execute(w, emptyInterface)

		decoder := json.NewDecoder(r.Body)

		for decoder.More() {
			var v siteParams
			if err := decoder.Decode(&v); err != nil {
				continue
			}

			v, err := confCheck(v, config)
			if err == nil {
				v, err = confWriter(v)
			} else if err.GetCode() == ErrBadHostnameTrace {
				var newErr Error
				v, newErr = confWriter(v)
				if newErr != nil {
					err = newErr
				}
			}

			var vPlus = struct {
				ExtHost      string
				IntHost      string
				IntIP        string
				IntPort      int
				Encrypted    bool
				StripHeaders []string
				Error        string
			}{
				ExtHost:      v.ExtHost,
				IntHost:      v.IntHost,
				IntIP:        v.IntIP,
				IntPort:      v.IntPort,
				Encrypted:    v.Encrypted,
				StripHeaders: v.StripHeaders,
			}

			if err != nil {
				l.Println(err.LogError(r))
				vPlus.Error = err.Error()
			}

			if err := tBody.Execute(w, vPlus); err != nil {
{'fraction_non_alphanumeric': 0.12852022529441884, 'fraction_numerical': 0.0005120327700972862, 'mean_word_length': 2.9964194373401534, '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': '6498458', 'n_tokens_mistral': 3148, 'n_tokens_neox': 2670, 'n_words': 628}