<commit_before>package http

import (
	"encoding/json"
	"net/http"
	"strconv"
	"time"

	"github.com/hashicorp/vault/vault"
)

func handleSysHealth(core *vault.Core) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			handleSysHealthGet(core, w, r)
		default:
			respondError(w, http.StatusMethodNotAllowed, nil)
		}
	})
}

func handleSysHealthGet(core *vault.Core, w http.ResponseWriter, r *http.Request) {

	// Check if being a standby is allowed for the purpose of a 200 OK
	_, standbyOK := r.URL.Query()["standbyok"]

	// FIXME: Change the sealed code to http.StatusServiceUnavailable at some
	// point
	sealedCode := http.StatusInternalServerError
	standbyCode := http.StatusTooManyRequests // Consul warning code
	activeCode := http.StatusOK

	var err error
	sealedCodeStr, sealedCodeOk := r.URL.Query()["sealedcode"]
	if sealedCodeOk {
		if len(sealedCodeStr) < 1 {
			respondError(w, http.StatusBadRequest, nil)
			return
		}
		sealedCode, err = strconv.Atoi(sealedCodeStr[0])
		if err != nil {
			respondError(w, http.StatusBadRequest, nil)
			return
		}
	}
	standbyCodeStr, standbyCodeOk := r.URL.Query()["standbycode"]
	if standbyCodeOk {
		if len(standbyCodeStr) < 1 {
			respondError(w, http.StatusBadRequest, nil)
			return
		}
		standbyCode, err = strconv.Atoi(standbyCodeStr[0])
		if err != nil {
			respondError(w, http.StatusBadRequest, nil)
			return
		}
	}

	activeCodeStr, activeCodeOk := r.URL.Query()["activecode"]
	if activeCodeOk {
		if len(activeCodeStr) < 1 {
			respondError(w, http.StatusBadRequest, nil)
			return
		}

		activeCode, err = strconv.Atoi(activeCodeStr[0])
		if err != nil {
			respondError(w, http.StatusBadRequest, nil)
			return
		}
	}

	// Check system status
	sealed, _ := core.Sealed()
	standby, _ := core.Standby()
	init, err := core.Initialized()
	if err != nil {
		respondError(w, http.StatusInternalServerError, err)
		return
	}

	// Determine the status code
	code := activeCode
	switch {
	case !init:
		code = http.StatusInternalServerError<commit_msg>Refactor fetching sys/health parameters<commit_after>package http

import (
	"encoding/json"
	"net/http"
	"strconv"
	"time"

	"github.com/hashicorp/vault/vault"
)

func handleSysHealth(core *vault.Core) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "GET":
			handleSysHealthGet(core, w, r)
		default:
			respondError(w, http.StatusMethodNotAllowed, nil)
		}
	})
}

func fetchStatusCode(r *http.Request, field string) (int, bool, bool) {
	var err error
	statusCode := http.StatusOK
	if statusCodeStr, statusCodeOk := r.URL.Query()[field]; statusCodeOk {
		statusCode, err = strconv.Atoi(statusCodeStr[0])
		if err != nil || len(statusCodeStr) < 1 {
			return http.StatusBadRequest, false, false
		}
		return statusCode, true, true
	}
	return statusCode, false, true
}

func handleSysHealthGet(core *vault.Core, w http.ResponseWriter, r *http.Request) {

	// Check if being a standby is allowed for the purpose of a 200 OK
	_, standbyOK := r.URL.Query()["standbyok"]

	// FIXME: Change the sealed code to http.StatusServiceUnavailable at some
	// point
	sealedCode := http.StatusInternalServerError
	if code, found, ok := fetchStatusCode(r, "sealedcode"); !ok {
		respondError(w, http.StatusBadRequest, nil)
		return
	} else if found {
		sealedCode = code
	}

	standbyCode := http.StatusTooManyRequests // Consul warning code
	if code, found, ok := fetchStatusCode(r, "standbycode"); !ok {
		respondError(w, http.StatusBadRequest, nil)
		return
	} else if found {
		standbyCode = code
	}

	activeCode := http.StatusOK
	if code, found, ok := fetchStatusCode(r, "activecode"); !ok {
		respondError(w, http.StatusBadRequest, nil)
		return
	} else if found {
		activeCode = code
	}

	// Check system status
	sealed, _ := core.Sealed()
	standby, _ := core.Standby()
	init, err := core.Initialized()
	if err != nil {
		respondError(w, http.StatusInternalServerError, err)
		return
	}

	// Determine the status code
	code := activeCode
	switch {
	case !init:
		code = http.StatusInternalServerError
{'fraction_non_alphanumeric': 0.11995163240628778, 'fraction_numerical': 0.003385731559854897, 'mean_word_length': 4.125154894671623, 'pattern_counts': {'":': 2, '<': 7, '<?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': '7176350', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1430, 'n_words': 353}