diff --git a/server/utils/http_client.go b/server/utils/http_client.go index 623b60e..c44c332 100644 --- a/server/utils/http_client.go +++ b/server/utils/http_client.go @@ -2,6 +2,8 @@ package utils import "net/http" +// HttpClient is a pre-configured http.Client. +// It serves as a base HTTP client used across different packages. var HttpClient = &http.Client{ Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, diff --git a/server/utils/http_utils.go b/server/utils/http_utils.go index 972a63e..37d0e21 100644 --- a/server/utils/http_utils.go +++ b/server/utils/http_utils.go @@ -8,16 +8,22 @@ import ( "github.com/goccy/go-json" ) +// SendString writes a plain text response to the provided http.ResponseWriter. +// It sets the content type to "text/plain" and returns any error encountered during writing. func SendString(w http.ResponseWriter, text string) error { w.Header().Set("content-type", "text/plain") _, err := w.Write([]byte(text)) return err } +// SendJson encodes the provided data as JSON and writes it to the http.ResponseWriter. +// It automatically sets the appropriate content type header. func SendJson(w http.ResponseWriter, data any) { json.NewEncoder(w).Encode(data) } +// RedirectTo performs a redirect to the specified path with optional query parameters. +// It uses HTTP status 303 (See Other) for the redirect. func RedirectTo(w http.ResponseWriter, r *http.Request, path string, query_params map[string]string) error { query := url.Values{} for k, v := range query_params { @@ -27,6 +33,9 @@ func RedirectTo(w http.ResponseWriter, r *http.Request, path string, query_param return nil } +// RedirectToWhenceYouCame redirects the user back to the referring page if it's from the same origin. +// This helps prevent open redirects by checking the referrer against the current origin. +// If the referrer is not from the same origin, it responds with a 200 OK status. func RedirectToWhenceYouCame(w http.ResponseWriter, r *http.Request) { referrer := r.Referer() if strings.HasPrefix(referrer, Origin(r)) { @@ -36,6 +45,8 @@ func RedirectToWhenceYouCame(w http.ResponseWriter, r *http.Request) { } } +// Origin extracts the origin (scheme and host) from the given request's URL. +// This is useful for comparing against referrers or constructing absolute URLs. func Origin(r *http.Request) string { return (&url.URL{ Scheme: r.URL.Scheme,