first Multipart approach

This commit is contained in:
Patricio Whittingslow
2026-07-26 17:22:09 -03:00
parent dd7c4bb037
commit 4f1a178f29
9 changed files with 528 additions and 15 deletions
+30 -6
View File
@@ -4,15 +4,17 @@ import (
"bytes"
"io"
"slices"
"strconv"
)
const (
methodGet = "GET"
strHTTP11 = "HTTP/1.1"
strCRLF = "\r\n"
headerCookie = "Cookie"
headerConnection = "Connection"
strClose = "close"
methodGet = "GET"
strHTTP11 = "HTTP/1.1"
strCRLF = "\r\n"
headerCookie = "Cookie"
headerConnection = "Connection"
headerContentLength = "Content-Length"
strClose = "close"
)
// Flags is a bitset of signals gathered while parsing or building a header,
@@ -343,6 +345,28 @@ func (h *Header) Get(key string) []byte {
return nil
}
// ContentLength returns the body length declared by the Content-Length field.
// Fails with an error if the field is absent, which for a request means the
// message has no body at all unless a transfer coding applies, RFC 9112 6.3.
// The value must be digits only, so a negative or list-valued field is rejected
// rather than guessed at.
func (h *Header) ContentLength() (int64, error) {
kv := h.peekHeader(headerContentLength)
if !kv.isValid() {
return 0, errNoContentLength
}
value := trimOWS(h.hbuf.musttoken(kv.value))
if len(value) == 0 {
return 0, errBadContentLength
}
// Unsigned parse of 63 bits rejects a sign and anything past int64's range.
n, err := strconv.ParseUint(b2s(value), 10, 63)
if err != nil {
return 0, errBadContentLength // strconv's error allocates and is not comparable.
}
return int64(n), nil
}
// Add adds a new key-value pair to the HTTP header. Calling Add mangles the buffer.
func (h *Header) Add(key, value string) {
h.appendHeader(key, value)