diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2025-04-07 16:41:09 +0200 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2025-04-08 10:15:40 +0000 |
| commit | b899e60d5c5004525d9a554d1d5074d062c3fb95 (patch) | |
| tree | 67909f2e7cc8fec4a50ba6bb9b3bf7ed478e55b9 | |
| parent | 9e5bf1f8e420b27de381ad20643abb794ea5900e (diff) | |
pkg/html: split off URL helpers
The existing pkg/html package transitively takes too many dependencies,
which complicates the reuse of some of its methods.
Split off a pkg/html/urlutil package.
| -rw-r--r-- | dashboard/app/main.go | 23 | ||||
| -rw-r--r-- | pkg/html/html.go | 44 | ||||
| -rw-r--r-- | pkg/html/urlutil/urls.go | 56 | ||||
| -rw-r--r-- | pkg/html/urlutil/urls_test.go (renamed from pkg/html/html_test.go) | 2 |
4 files changed, 69 insertions, 56 deletions
diff --git a/dashboard/app/main.go b/dashboard/app/main.go index 93ee73c00..2f04e7152 100644 --- a/dashboard/app/main.go +++ b/dashboard/app/main.go @@ -25,6 +25,7 @@ import ( "github.com/google/syzkaller/pkg/email" "github.com/google/syzkaller/pkg/hash" "github.com/google/syzkaller/pkg/html" + "github.com/google/syzkaller/pkg/html/urlutil" "github.com/google/syzkaller/pkg/subsystem" "github.com/google/syzkaller/pkg/vcs" "golang.org/x/sync/errgroup" @@ -113,7 +114,7 @@ func makeUIBugFilter(c context.Context, filter *userBugFilter) *uiBugFilter { return &uiBugFilter{ Filter: filter, DropURL: func(name, value string) string { - return html.DropParam(url, name, value) + return urlutil.DropParam(url, name, value) }, } } @@ -1028,11 +1029,11 @@ func handleAdmin(c context.Context, w http.ResponseWriter, r *http.Request) erro MemcacheStats: memcacheStats, Stopped: alreadyStopped, MoreStopClicks: 2, - StopLink: html.AmendURL("/admin", "stop_clicked", "1"), + StopLink: urlutil.SetParam("/admin", "stop_clicked", "1"), } if r.FormValue("stop_clicked") != "" { data.MoreStopClicks = 1 - data.StopLink = html.AmendURL("/admin", "action", "emergency_stop") + data.StopLink = urlutil.SetParam("/admin", "action", "emergency_stop") } if r.FormValue("job_type") != "" { data.TypeJobs = &uiJobList{Title: "Last jobs:", Jobs: typeJobs} @@ -1041,8 +1042,8 @@ func handleAdmin(c context.Context, w http.ResponseWriter, r *http.Request) erro data.RecentJobs = &uiJobList{Title: "Recent jobs:", Jobs: recentJobs} data.RunningJobs = &uiJobList{Title: "Running jobs:", Jobs: runningJobs} data.PendingJobs = &uiJobList{Title: "Pending jobs:", Jobs: pendingJobs} - data.FixBisectionsLink = html.AmendURL("/admin", "job_type", fmt.Sprintf("%d", JobBisectFix)) - data.CauseBisectionsLink = html.AmendURL("/admin", "job_type", fmt.Sprintf("%d", JobBisectCause)) + data.FixBisectionsLink = urlutil.SetParam("/admin", "job_type", fmt.Sprintf("%d", JobBisectFix)) + data.CauseBisectionsLink = urlutil.SetParam("/admin", "job_type", fmt.Sprintf("%d", JobBisectCause)) } return serveTemplate(w, "admin.html", data) } @@ -1211,7 +1212,7 @@ func handleBug(c context.Context, w http.ResponseWriter, r *http.Request) error LabelGroups: getLabelGroups(c, bug), } if accessLevel == AccessAdmin && !bug.hasUserSubsystems() { - data.DebugSubsystems = html.AmendURL(data.Bug.Link, "debug_subsystems", "1") + data.DebugSubsystems = urlutil.SetParam(data.Bug.Link, "debug_subsystems", "1") } // bug.BisectFix is set to BisectNot in three cases : // - no fix bisections have been performed on the bug @@ -1326,7 +1327,7 @@ func makeBugLabelUI(c context.Context, bug *Bug, entry BugLabel) *uiBugLabel { if !strings.HasPrefix(url, "/"+bug.Namespace) { link = fmt.Sprintf("/%s", bug.Namespace) } - link = html.TransformURL(link, "label", func(oldLabels []string) []string { + link = urlutil.TransformParam(link, "label", func(oldLabels []string) []string { return mergeLabelSet(oldLabels, entry.String()) }) ret := &uiBugLabel{ @@ -1458,11 +1459,11 @@ func handleSubsystemsList(c context.Context, w http.ResponseWriter, r *http.Requ Name: "", Open: uiSubsystemStats{ Count: cached.NoSubsystem.Open, - Link: html.AmendURL("/"+hdr.Namespace, "no_subsystem", "true"), + Link: urlutil.SetParam("/"+hdr.Namespace, "no_subsystem", "true"), }, Fixed: uiSubsystemStats{ Count: cached.NoSubsystem.Fixed, - Link: html.AmendURL("/"+hdr.Namespace+"/fixed", "no_subsystem", "true"), + Link: urlutil.SetParam("/"+hdr.Namespace+"/fixed", "no_subsystem", "true"), }, } sort.Slice(list, func(i, j int) bool { return list[i].Name < list[j].Name }) @@ -1471,7 +1472,7 @@ func handleSubsystemsList(c context.Context, w http.ResponseWriter, r *http.Requ List: list, Unclassified: unclassified, SomeHidden: someHidden, - ShowAllURL: html.AmendURL(getCurrentURL(c), "all", "true"), + ShowAllURL: urlutil.SetParam(getCurrentURL(c), "all", "true"), }) } @@ -1487,7 +1488,7 @@ func createUISubsystem(ns string, item *subsystem.Subsystem, cached *Cached) *ui }, Fixed: uiSubsystemStats{ Count: stats.Fixed, - Link: html.AmendURL("/"+ns+"/fixed", "label", BugLabel{ + Link: urlutil.SetParam("/"+ns+"/fixed", "label", BugLabel{ Label: SubsystemLabel, Value: item.Name, }.String()), 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" |
