go-zero/rest/httpx/util.go
2024-03-30 13:29:58 +08:00

48 lines
904 B
Go

package httpx
import (
"errors"
"net/http"
)
const xForwardedFor = "X-Forwarded-For"
// GetFormValues returns the form values.
func GetFormValues(r *http.Request) (map[string]any, error) {
if err := r.ParseForm(); err != nil {
return nil, err
}
if err := r.ParseMultipartForm(maxMemory); err != nil {
if !errors.Is(err, http.ErrNotMultipart) {
return nil, err
}
}
params := make(map[string]any, len(r.Form))
for name := range r.Form {
switch len(r.Form[name]) {
case 1:
formValue := r.Form.Get(name)
if len(formValue) > 0 {
params[name] = formValue
}
default:
// len(r.Form[name]) > 1, never be 0
params[name] = r.Form[name]
}
}
return params, nil
}
// GetRemoteAddr returns the peer address, supports X-Forward-For.
func GetRemoteAddr(r *http.Request) string {
v := r.Header.Get(xForwardedFor)
if len(v) > 0 {
return v
}
return r.RemoteAddr
}