1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-06 19:44:01 +08:00
Files
kubo/core/corehttp/mutex_profile.go
Steven Allen 23d35184c3 fix: use http.Error for sending errors
This sets a few headers that prevent browsers from misinterpreting the error
text.
2019-05-25 10:45:17 -07:00

44 lines
1.1 KiB
Go

package corehttp
import (
"net"
"net/http"
"runtime"
"strconv"
core "github.com/ipfs/go-ipfs/core"
)
// MutexFractionOption allows to set runtime.SetMutexProfileFraction via HTTP
// using POST request with parameter 'fraction'.
func MutexFractionOption(path string) ServeOption {
return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "only POST allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
asfr := r.Form.Get("fraction")
if len(asfr) == 0 {
http.Error(w, "parameter 'fraction' must be set", http.StatusBadRequest)
return
}
fr, err := strconv.Atoi(asfr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Infof("Setting MutexProfileFraction to %d", fr)
runtime.SetMutexProfileFraction(fr)
})
return mux, nil
}
}