2025-06-16 22:19:00 +05:30
|
|
|
// Copyright 2024 Patial Tech (Ankit Patial).
|
2025-06-16 22:26:47 +05:30
|
|
|
//
|
|
|
|
|
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
|
|
|
|
|
// See http://opensource.org/licenses/MIT
|
2025-06-16 22:19:00 +05:30
|
|
|
|
|
|
|
|
package request
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2025-11-17 19:23:38 +05:30
|
|
|
"errors"
|
2025-06-16 22:19:00 +05:30
|
|
|
"fmt"
|
2025-11-17 19:23:38 +05:30
|
|
|
"io"
|
2025-06-16 22:19:00 +05:30
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"code.patial.tech/go/appcore/validate"
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-17 19:23:38 +05:30
|
|
|
// MaxRequestBodySize is the maximum allowed size for request bodies (1MB default).
|
|
|
|
|
// This prevents resource exhaustion attacks from large payloads.
|
|
|
|
|
// Override this value if you need to accept larger requests.
|
|
|
|
|
var MaxRequestBodySize int64 = 1 << 20 // 1MB
|
|
|
|
|
|
2025-06-16 22:19:00 +05:30
|
|
|
func FormField(r *http.Request, key string) (string, error) {
|
|
|
|
|
f, err := Payload[map[string]any](r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v", f[key]), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PayloadMap(r *http.Request) (map[string]any, error) {
|
|
|
|
|
return Payload[map[string]any](r)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PayloadWithValidate[T any](r *http.Request) (T, error) {
|
|
|
|
|
p, err := Payload[T](r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return p, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := validate.Struct(p); err != nil {
|
|
|
|
|
return p, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Payload[T any](r *http.Request) (T, error) {
|
|
|
|
|
var p T
|
2025-11-17 19:23:38 +05:30
|
|
|
|
|
|
|
|
// Limit request body size to prevent resource exhaustion
|
|
|
|
|
limited := io.LimitReader(r.Body, MaxRequestBodySize)
|
|
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(limited)
|
|
|
|
|
if err := decoder.Decode(&p); err != nil {
|
|
|
|
|
// Check if we hit the size limit
|
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
|
// Try to read one more byte to see if there's more data
|
|
|
|
|
var buf [1]byte
|
|
|
|
|
if n, _ := limited.Read(buf[:]); n == 0 {
|
|
|
|
|
// We hit the limit
|
|
|
|
|
return p, errors.New("request body too large")
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-16 22:19:00 +05:30
|
|
|
return p, err
|
|
|
|
|
}
|
|
|
|
|
return p, nil
|
|
|
|
|
}
|
2025-11-17 19:23:38 +05:30
|
|
|
|
|
|
|
|
// DecodeJSON decodes JSON from the request body into the provided pointer.
|
|
|
|
|
// This is useful when you want to decode into an existing variable.
|
|
|
|
|
// The request body size is limited by MaxRequestBodySize to prevent DoS attacks.
|
|
|
|
|
func DecodeJSON(r *http.Request, v any) error {
|
|
|
|
|
// Limit request body size to prevent resource exhaustion
|
|
|
|
|
limited := io.LimitReader(r.Body, MaxRequestBodySize)
|
|
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(limited)
|
|
|
|
|
if err := decoder.Decode(v); err != nil {
|
|
|
|
|
// Check if we hit the size limit
|
|
|
|
|
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
|
|
|
// Try to read one more byte to see if there's more data
|
|
|
|
|
var buf [1]byte
|
|
|
|
|
if n, _ := limited.Read(buf[:]); n == 0 {
|
|
|
|
|
// We hit the limit
|
|
|
|
|
return errors.New("request body too large")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|