- Centralize environment variable loading in config.MustLoad - Parse and validate all env vars once at initialization - Make config fields private and read-only - Remove public Getenv usage in favor of typed accessors - Improve validation and initialization order - Normalize enum naming to be idiomatic and avoid constant collisions
50 lines
870 B
Go
50 lines
870 B
Go
package header
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
)
|
|
|
|
func NewRequest(r interface{}) (RequestHeader, error) {
|
|
switch v := r.(type) {
|
|
case []byte:
|
|
return parseHeadersFromBytes(v)
|
|
case *bufio.Reader:
|
|
return parseHeadersFromReader(v)
|
|
default:
|
|
return nil, fmt.Errorf("unsupported type: %T", r)
|
|
}
|
|
}
|
|
|
|
func (req *requestHeader) Value(key string) string {
|
|
val, ok := req.headers[key]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return val
|
|
}
|
|
|
|
func (req *requestHeader) Set(key string, value string) {
|
|
req.headers[key] = value
|
|
}
|
|
|
|
func (req *requestHeader) Remove(key string) {
|
|
delete(req.headers, key)
|
|
}
|
|
|
|
func (req *requestHeader) Method() string {
|
|
return req.method
|
|
}
|
|
|
|
func (req *requestHeader) Path() string {
|
|
return req.path
|
|
}
|
|
|
|
func (req *requestHeader) Version() string {
|
|
return req.version
|
|
}
|
|
|
|
func (req *requestHeader) Finalize() []byte {
|
|
return finalize(req.startLine, req.headers)
|
|
}
|