aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/html
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/html')
-rw-r--r--pkg/html/html.go44
-rw-r--r--pkg/html/urlutil/urls.go56
-rw-r--r--pkg/html/urlutil/urls_test.go (renamed from pkg/html/html_test.go)2
3 files changed, 57 insertions, 45 deletions
diff --git a/pkg/html/html.go b/pkg/html/html.go
index 4893b8377..9d15c8777 100644
--- a/pkg/html/html.go
+++ b/pkg/html/html.go
@@ -6,7 +6,6 @@ package html
import (
"fmt"
"html/template"
- "net/url"
"path/filepath"
"reflect"
"strings"
@@ -214,46 +213,3 @@ func dereferencePointer(v interface{}) interface{} {
func commitLink(repo, commit string) string {
return vcs.CommitLink(repo, commit)
}
-
-func AmendURL(baseURL, key, value string) string {
- return TransformURL(baseURL, key, func(_ []string) []string {
- if value == "" {
- return nil
- }
- return []string{value}
- })
-}
-
-func DropParam(baseURL, key, value string) string {
- return TransformURL(baseURL, key, func(oldValues []string) []string {
- if value == "" {
- return nil
- }
- var newValues []string
- for _, iterVal := range oldValues {
- if iterVal != value {
- newValues = append(newValues, iterVal)
- }
- }
- return newValues
- })
-}
-
-func TransformURL(baseURL, key string, f func([]string) []string) string {
- if baseURL == "" {
- return ""
- }
- parsed, err := url.Parse(baseURL)
- if err != nil {
- return ""
- }
- values := parsed.Query()
- ret := f(values[key])
- if len(ret) == 0 {
- values.Del(key)
- } else {
- values[key] = ret
- }
- parsed.RawQuery = values.Encode()
- return parsed.String()
-}
diff --git a/pkg/html/urlutil/urls.go b/pkg/html/urlutil/urls.go
new file mode 100644
index 000000000..42ab505c8
--- /dev/null
+++ b/pkg/html/urlutil/urls.go
@@ -0,0 +1,56 @@
+// Copyright 2025 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+package urlutil
+
+import "net/url"
+
+// SetParam overrides the specific key from the URL.
+// It does not take into account that there may be multiple parameters with the same name.
+func SetParam(baseURL, key, value string) string {
+ return TransformParam(baseURL, key, func(_ []string) []string {
+ if value == "" {
+ return nil
+ }
+ return []string{value}
+ })
+}
+
+// DropParam removes the specific key=value pair from the URL
+// (it's possible to have many of parameters with the same name).
+// Set value="" to remove the key regardless of the value.
+func DropParam(baseURL, key, value string) string {
+ return TransformParam(baseURL, key, func(oldValues []string) []string {
+ if value == "" {
+ return nil
+ }
+ var newValues []string
+ for _, iterVal := range oldValues {
+ if iterVal != value {
+ newValues = append(newValues, iterVal)
+ }
+ }
+ return newValues
+ })
+}
+
+// TransformParam is a generic method that transforms the set of values
+// for the specified URL parameter key.
+func TransformParam(baseURL, key string, f func([]string) []string) string {
+ if baseURL == "" {
+ return ""
+ }
+ parsed, err := url.Parse(baseURL)
+ if err != nil {
+ return ""
+ }
+ values := parsed.Query()
+ ret := f(values[key])
+ if len(ret) == 0 {
+ values.Del(key)
+ } else {
+ values[key] = ret
+ }
+ parsed.RawQuery = values.Encode()
+ return parsed.String()
+}
diff --git a/pkg/html/html_test.go b/pkg/html/urlutil/urls_test.go
index d824e0246..a8782c35e 100644
--- a/pkg/html/html_test.go
+++ b/pkg/html/urlutil/urls_test.go
@@ -1,7 +1,7 @@
// Copyright 2023 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
-package html
+package urlutil
import (
"testing"