This repository has been archived on 2024-04-07. You can view files and clone it, but cannot push or open issues or pull requests.
summer-utils/utils/server-utils.go
2023-04-16 11:56:17 +01:00

56 lines
1.3 KiB
Go

package utils
import (
"errors"
"log"
"net/http"
"regexp"
"strconv"
"strings"
)
func logHttpServerError(prefix string, err error) {
if err != nil {
if err == http.ErrServerClosed {
log.Printf("[%s] The http server shutdown successfully\n", prefix)
} else {
log.Printf("[%s] Error trying to host the http server: %s\n", prefix, err.Error())
}
}
}
func RunBackgroundHttp(prefix string, s *http.Server) {
logHttpServerError(prefix, s.ListenAndServe())
}
func RunBackgroundHttps(prefix string, s *http.Server) {
logHttpServerError(prefix, s.ListenAndServeTLS("", ""))
}
var symbolicHostParse = regexp.MustCompile("^([^:~/]+?)(?::([0-9]+?))?(/[^~]+?)?(?:~(.+?))?$")
// ParseSymbolicHost converts keyed hosts to host and path combo
func ParseSymbolicHost(host string) (dst string, port uint16, path string, flags []string, ref bool, err error) {
sub := symbolicHostParse.FindStringSubmatch(host)
if len(sub) != 5 {
return "", 0, "", nil, false, errors.New("symbolic host parsing error")
}
var a uint64
if sub[2] != "" {
a, err = strconv.ParseUint(sub[2], 10, 16)
if err != nil {
return
}
}
dst = sub[1]
port = uint16(a)
path = sub[3]
if sub[4] != "" {
flags = strings.Split(sub[4], ",")
}
ref = len(dst) >= 1 && dst[0] == '*'
return
}