aboutsummaryrefslogtreecommitdiff
path: root/url.go
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-04-05 19:45:17 +0800
committerRunxi Yu <me@runxiyu.org>2025-04-05 19:45:17 +0800
commit54a19febc0c7c49caa014254cabab571abad60ab (patch)
treea0b5ffe5aaaff956d5d41b09ca1d2127f771d17a /url.go
parentgit2c: Fix regression in potential integer overflow (diff)
downloadforge-54a19febc0c7c49caa014254cabab571abad60ab.tar.gz
forge-54a19febc0c7c49caa014254cabab571abad60ab.tar.zst
forge-54a19febc0c7c49caa014254cabab571abad60ab.zip
misc: Move url.go into the misc package
Diffstat (limited to '')
-rw-r--r--misc/url.go (renamed from url.go)42
1 files changed, 21 insertions, 21 deletions
diff --git a/url.go b/misc/url.go
index ad5c8bb..b77d8ce 100644
--- a/url.go
+++ b/misc/url.go
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-package main
+package misc
import (
"errors"
@@ -11,13 +11,13 @@ import (
)
var (
- errDupRefSpec = errors.New("duplicate ref spec")
- errNoRefSpec = errors.New("no ref spec")
+ ErrDupRefSpec = errors.New("duplicate ref spec")
+ ErrNoRefSpec = errors.New("no ref spec")
)
// getParamRefTypeName looks at the query parameters in an HTTP request and
// returns its ref name and type, if any.
-func getParamRefTypeName(request *http.Request) (retRefType, retRefName string, err error) {
+func GetParamRefTypeName(request *http.Request) (retRefType, retRefName string, err error) {
rawQuery := request.URL.RawQuery
queryValues, err := url.ParseQuery(rawQuery)
if err != nil {
@@ -28,12 +28,12 @@ func getParamRefTypeName(request *http.Request) (retRefType, retRefName string,
refName, ok := queryValues[refType]
if ok {
if done {
- err = errDupRefSpec
+ err = ErrDupRefSpec
return
}
done = true
if len(refName) != 1 {
- err = errDupRefSpec
+ err = ErrDupRefSpec
return
}
retRefName = refName[0]
@@ -41,17 +41,17 @@ func getParamRefTypeName(request *http.Request) (retRefType, retRefName string,
}
}
if !done {
- err = errNoRefSpec
+ err = ErrNoRefSpec
}
return
}
-// parseReqURI parses an HTTP request URL, and returns a slice of path segments
+// ParseReqURI parses an HTTP request URL, and returns a slice of path segments
// and the query parameters. It handles %2F correctly.
-func parseReqURI(requestURI string) (segments []string, params url.Values, err error) {
+func ParseReqURI(requestURI string) (segments []string, params url.Values, err error) {
path, paramsStr, _ := strings.Cut(requestURI, "?")
- segments, err = pathToSegments(path)
+ segments, err = PathToSegments(path)
if err != nil {
return
}
@@ -60,7 +60,7 @@ func parseReqURI(requestURI string) (segments []string, params url.Values, err e
return
}
-func pathToSegments(path string) (segments []string, err error) {
+func PathToSegments(path string) (segments []string, err error) {
segments = strings.Split(strings.TrimPrefix(path, "/"), "/")
for i, segment := range segments {
@@ -73,10 +73,10 @@ func pathToSegments(path string) (segments []string, err error) {
return
}
-// redirectDir returns true and redirects the user to a version of the URL with
+// RedirectDir returns true and redirects the user to a version of the URL with
// a trailing slash, if and only if the request URL does not already have a
// trailing slash.
-func redirectDir(writer http.ResponseWriter, request *http.Request) bool {
+func RedirectDir(writer http.ResponseWriter, request *http.Request) bool {
requestURI := request.RequestURI
pathEnd := strings.IndexAny(requestURI, "?#")
@@ -95,10 +95,10 @@ func redirectDir(writer http.ResponseWriter, request *http.Request) bool {
return false
}
-// redirectNoDir returns true and redirects the user to a version of the URL
+// RedirectNoDir returns true and redirects the user to a version of the URL
// without a trailing slash, if and only if the request URL has a trailing
// slash.
-func redirectNoDir(writer http.ResponseWriter, request *http.Request) bool {
+func RedirectNoDir(writer http.ResponseWriter, request *http.Request) bool {
requestURI := request.RequestURI
pathEnd := strings.IndexAny(requestURI, "?#")
@@ -117,9 +117,9 @@ func redirectNoDir(writer http.ResponseWriter, request *http.Request) bool {
return false
}
-// redirectUnconditionally unconditionally redirects the user back to the
+// RedirectUnconditionally unconditionally redirects the user back to the
// current page while preserving query parameters.
-func redirectUnconditionally(writer http.ResponseWriter, request *http.Request) {
+func RedirectUnconditionally(writer http.ResponseWriter, request *http.Request) {
requestURI := request.RequestURI
pathEnd := strings.IndexAny(requestURI, "?#")
@@ -134,17 +134,17 @@ func redirectUnconditionally(writer http.ResponseWriter, request *http.Request)
http.Redirect(writer, request, path+rest, http.StatusSeeOther)
}
-// segmentsToURL joins URL segments to the path component of a URL.
+// SegmentsToURL joins URL segments to the path component of a URL.
// Each segment is escaped properly first.
-func segmentsToURL(segments []string) string {
+func SegmentsToURL(segments []string) string {
for i, segment := range segments {
segments[i] = url.PathEscape(segment)
}
return strings.Join(segments, "/")
}
-// anyContain returns true if and only if ss contains a string that contains c.
-func anyContain(ss []string, c string) bool {
+// AnyContain returns true if and only if ss contains a string that contains c.
+func AnyContain(ss []string, c string) bool {
for _, s := range ss {
if strings.Contains(s, c) {
return true