This commit is contained in:
parent
9a781d2e64
commit
4c19cd0e99
4 changed files with 65 additions and 18 deletions
42
Request.go
42
Request.go
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue