2024-03-28 12:22:45 +01:00

83 lines
1.5 KiB
Go

package web
import (
"bufio"
"git.akyoto.dev/go/router"
)
// Request is an interface for HTTP requests.
type Request interface {
Header(string) string
Host() string
Method() string
Path() string
Scheme() string
Param(string) string
}
// request represents the HTTP request used in the given context.
type request struct {
reader *bufio.Reader
scheme string
host string
method string
path string
query string
headers []Header
body []byte
params []router.Parameter
}
// Header returns the header value for the given key.
func (req *request) Header(key string) string {
for _, header := range req.headers {
if header.Key == key {
return header.Value
}
}
return ""
}
// Host returns the requested host.
func (req *request) Host() string {
return req.host
}
// Method returns the request method.
func (req *request) Method() string {
return req.method
}
// 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 ""
}
// Path returns the requested path.
func (req *request) Path() string {
return req.path
}
// Scheme returns either `http`, `https` or an empty string.
func (req request) Scheme() string {
return req.scheme
}
// 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,
})
}