35 Star 389 Fork 70

GVPrancher / rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
validator.go 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
package catalog
import (
"net/http"
"net/url"
"regexp"
"strings"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
)
var (
controlChars = regexp.MustCompile("[[:cntrl:]]")
controlEncoded = regexp.MustCompile("%[0-1][0-9,a-f,A-F]")
)
func Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
if pathURL, _ := data["url"].(string); pathURL != "" {
if err := validateURL(pathURL); err != nil {
return err
}
if u, err := url.Parse(pathURL); err == nil {
u.Scheme = strings.ToLower(u.Scheme) // git commands are case-sensitive
data["url"] = u.String()
}
return nil
} else if request.Method == http.MethodPost {
return httperror.NewAPIError(httperror.MissingRequired, "Catalog URL not specified")
}
return nil
}
func validateURL(pathURL string) error {
if controlChars.FindStringIndex(pathURL) != nil || controlEncoded.FindStringIndex(pathURL) != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent, "Invalid characters in catalog URL")
}
return nil
}
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.13

搜索帮助