61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"net/url"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
func Sanitize(s string) string {
|
|
return regexp.MustCompile(`[<>:"/\\|?*]`).ReplaceAllString(s, "_")
|
|
}
|
|
|
|
func SanitizePath(path string) string {
|
|
parts := strings.Split(path, "/")
|
|
cleanParts := make([]string, len(parts))
|
|
for i, part := range parts {
|
|
cleanParts[i] = Sanitize(part)
|
|
}
|
|
|
|
return filepath.Join(cleanParts...)
|
|
}
|
|
|
|
func ResolveAction(baseURL, html string) string {
|
|
match := regexp.MustCompile(`(?i)action="([^"]+)"`).FindStringSubmatch(html)
|
|
if len(match) < 2 {
|
|
return baseURL
|
|
}
|
|
u, _ := url.Parse(baseURL)
|
|
ref, _ := url.Parse(match[1])
|
|
return u.ResolveReference(ref).String()
|
|
}
|
|
|
|
func ExtractFormFields(html string) url.Values {
|
|
fields := url.Values{}
|
|
|
|
reInput := regexp.MustCompile(`(?si)<input\s+[^>]*>`)
|
|
matches := reInput.FindAllString(html, -1)
|
|
|
|
for _, tag := range matches {
|
|
nameMatch := regexp.MustCompile(`(?si)name=["']([^"']+)["']`).FindStringSubmatch(tag)
|
|
if len(nameMatch) < 2 {
|
|
continue
|
|
}
|
|
name := nameMatch[1]
|
|
|
|
val := ""
|
|
valMatch := regexp.MustCompile(`(?si)value=["']([^"']*)["']`).FindStringSubmatch(tag)
|
|
if len(valMatch) > 1 {
|
|
val = valMatch[1]
|
|
|
|
val = strings.ReplaceAll(val, """, "\"")
|
|
val = strings.ReplaceAll(val, "&", "&")
|
|
val = strings.ReplaceAll(val, "'", "'")
|
|
}
|
|
|
|
fields.Set(name, val)
|
|
}
|
|
return fields
|
|
}
|