2024-03-22 15:08:24 +01:00
|
|
|
package web
|
2024-03-16 15:22:47 +01:00
|
|
|
|
2024-03-26 22:46:16 +01:00
|
|
|
import "git.akyoto.dev/go/router"
|
2024-03-16 15:22:47 +01:00
|
|
|
|
|
|
|
// Request is an interface for HTTP requests.
|
|
|
|
type Request interface {
|
2024-03-27 14:06:13 +01:00
|
|
|
Host() string
|
2024-03-16 15:22:47 +01:00
|
|
|
Method() string
|
|
|
|
Path() string
|
2024-03-27 14:06:13 +01:00
|
|
|
Scheme() string
|
2024-03-27 20:41:27 +01:00
|
|
|
Param(string) string
|
2024-03-16 15:22:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// request represents the HTTP request used in the given context.
|
|
|
|
type request struct {
|
2024-03-27 14:06:13 +01:00
|
|
|
scheme string
|
|
|
|
host string
|
2024-03-26 22:46:16 +01:00
|
|
|
method string
|
|
|
|
path string
|
2024-03-27 20:41:27 +01:00
|
|
|
query string
|
2024-03-26 22:46:16 +01:00
|
|
|
params []router.Parameter
|
2024-03-16 15:22:47 +01:00
|
|
|
}
|
|
|
|
|
2024-03-27 14:06:13 +01:00
|
|
|
// Host returns the requested host.
|
|
|
|
func (req *request) Host() string {
|
|
|
|
return req.host
|
|
|
|
}
|
|
|
|
|
2024-03-16 15:22:47 +01:00
|
|
|
// Method returns the request method.
|
2024-03-26 22:46:16 +01:00
|
|
|
func (req *request) Method() string {
|
|
|
|
return req.method
|
2024-03-16 15:22:47 +01:00
|
|
|
}
|
|
|
|
|
2024-03-27 20:41:27 +01:00
|
|
|
// Param retrieves a parameter.
|
|
|
|
func (req *request) Param(name string) string {
|
|
|
|
for i := range len(req.params) {
|
|
|
|
p := req.params[i]
|
|
|
|
|
|
|
|
if p.Key == name {
|
|
|
|
return p.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-03-16 15:22:47 +01:00
|
|
|
// Path returns the requested path.
|
2024-03-26 22:46:16 +01:00
|
|
|
func (req *request) Path() string {
|
|
|
|
return req.path
|
2024-03-16 15:22:47 +01:00
|
|
|
}
|
|
|
|
|
2024-03-27 14:06:13 +01:00
|
|
|
// Scheme returns either `http`, `https` or an empty string.
|
|
|
|
func (req request) Scheme() string {
|
|
|
|
return req.scheme
|
|
|
|
}
|
|
|
|
|
2024-03-26 22:46:16 +01:00
|
|
|
// addParameter adds a new parameter to the request.
|
|
|
|
func (req *request) addParameter(key string, value string) {
|
|
|
|
req.params = append(req.params, router.Parameter{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
})
|
2024-03-16 15:22:47 +01:00
|
|
|
}
|