Added support for io.ReadAll on requests
All checks were successful
/ test (push) Successful in 18s

This commit is contained in:
Eduard Urbach 2025-06-21 11:37:39 +02:00
parent 9a781d2e64
commit 4c19cd0e99
Signed by: eduard
GPG key ID: 49226B848C78F6C8
4 changed files with 65 additions and 18 deletions

View file

@ -3,6 +3,8 @@ package web
import (
"bufio"
"io"
"strconv"
"strings"
"git.urbach.dev/go/router"
)
@ -20,20 +22,22 @@ type Request interface {
// request represents the HTTP request used in the given context.
type request struct {
bufio.Reader
scheme string
host string
method string
path string
query string
headers []Header
params []router.Parameter
reader bufio.Reader
scheme string
host string
method string
path string
query string
headers []Header
params []router.Parameter
length int
consumed int
}
// 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 {
if strings.EqualFold(header.Key, key) {
return header.Value
}
}
@ -69,6 +73,26 @@ func (req *request) Path() string {
return req.path
}
// Read implements the io.Reader interface.
func (req *request) Read(p []byte) (n int, err error) {
if req.length == 0 {
req.length, _ = strconv.Atoi(req.Header("Content-Length"))
if req.length == 0 {
return 0, io.EOF
}
}
n, err = req.reader.Read(p)
req.consumed += n
if req.consumed < req.length {
return n, err
}
return n - (req.consumed - req.length), io.EOF
}
// Scheme returns either `http`, `https` or an empty string.
func (req request) Scheme() string {
return req.scheme