11package request
22
3- import "net/http"
3+ import (
4+ "fmt"
5+ "net/http"
6+ "strconv"
7+ )
48
5- // Query retrieves a query parameter value by name from the HTTP request URL.
6- // This extracts values from the URL query string like "?name=value&other=test"
7- // where the parameter name matches the provided key.
9+ // Query retrieves a query parameter value by name from the
10+ // HTTP request URL. This extracts values from the URL query
11+ // string like "?name=value&other=test" where the parameter
12+ // name matches the provided key.
813//
914// Parameters:
1015// - r: The HTTP request containing the URL with query parameters
@@ -15,9 +20,10 @@ func Query(r *http.Request, name string) string {
1520 return r .URL .Query ().Get (name )
1621}
1722
18- // HasQuery checks if a query parameter exists in the HTTP request URL,
19- // regardless of its value. This is useful for distinguishing between
20- // a parameter that doesn't exist and one that exists but has an empty value.
23+ // HasQuery checks if a query parameter exists in the HTTP
24+ // request URL, regardless of its value. This is useful for
25+ // distinguishing between a parameter that doesn't exist and
26+ // one that exists but has an empty value.
2127//
2228// Parameters:
2329// - r: The HTTP request containing the URL with query parameters
@@ -28,10 +34,12 @@ func HasQuery(r *http.Request, name string) bool {
2834 return r .URL .Query ().Has (name )
2935}
3036
31- // QueryOr retrieves a query parameter value by name, returning a default
32- // value if the parameter doesn't exist. Note that if the parameter exists
33- // but has an empty value, the empty value is returned, not the default.
34- // This is useful for providing fallback values for optional parameters.
37+ // QueryOr retrieves a query parameter value by name,
38+ // returning a default value if the parameter doesn't exist.
39+ // Note that if the parameter exists but has an empty value,
40+ // the empty value is returned, not the default. This is
41+ // useful for providing fallback values for optional
42+ // parameters.
3543//
3644// Parameters:
3745// - r: The HTTP request containing the URL with query parameters
@@ -46,3 +54,59 @@ func QueryOr(r *http.Request, name string, fallback string) string {
4654
4755 return fallback
4856}
57+
58+ // QueryInt retrieves a query parameter by name and parses
59+ // it as an integer. This prevents injection via malformed
60+ // numeric query parameters by validating that the value is
61+ // a well-formed integer.
62+ //
63+ // Parameters:
64+ // - r: The HTTP request containing the URL with query
65+ // parameters
66+ // - k: The name of the query parameter to parse
67+ //
68+ // Returns the parsed integer value and any parsing error.
69+ // Returns an error if the parameter is missing or is not
70+ // a valid integer string.
71+ func QueryInt (r * http.Request , k string ) (int , error ) {
72+ raw := Query (r , k )
73+
74+ if raw == "" {
75+ return 0 , fmt .Errorf ("query parameter %q is empty" , k )
76+ }
77+
78+ value , err := strconv .Atoi (raw )
79+
80+ if err != nil {
81+ return 0 , fmt .Errorf (
82+ "query parameter %q is not a valid integer: %w" ,
83+ k , err ,
84+ )
85+ }
86+
87+ return value , nil
88+ }
89+
90+ // QueryIntOr retrieves a query parameter by name and parses
91+ // it as an integer, returning the provided fallback value
92+ // if the parameter is missing or cannot be parsed. This is
93+ // useful when a numeric query parameter is optional or when
94+ // a sensible default exists (e.g., pagination page numbers).
95+ //
96+ // Parameters:
97+ // - r: The HTTP request containing the URL with query
98+ // parameters
99+ // - k: The name of the query parameter to parse
100+ // - d: The fallback value to return on failure
101+ //
102+ // Returns the parsed integer if valid, otherwise the
103+ // fallback value.
104+ func QueryIntOr (r * http.Request , k string , d int ) int {
105+ value , err := QueryInt (r , k )
106+
107+ if err != nil {
108+ return d
109+ }
110+
111+ return value
112+ }
0 commit comments