Skip to content

Commit

Permalink
feat: support for urlencoded and multipart forms (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
noxecane authored Aug 25, 2024
1 parent ed563ef commit 8a03f51
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 35 deletions.
29 changes: 29 additions & 0 deletions html/cookies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package html

import (
"net/http"
)

// SecureCookie makes sure the passed cookies is only accessible
// to the browser, over HTTPS from the server's domain(for PUT, POST e.t.c)
func SecureCookie(appEnv string, cookie *http.Cookie) *http.Cookie {
cookie.HttpOnly = true // No JS access
cookie.Secure = appEnv != "dev" // HTTPS only

if appEnv != "dev" {
cookie.SameSite = http.SameSiteLaxMode
}

return cookie
}

// LockCookie is SecureCookie with strict mode for same site settings
func LockCookie(appEnv string, cookie *http.Cookie) *http.Cookie {
SecureCookie(appEnv, cookie)

if appEnv != "dev" {
cookie.SameSite = http.SameSiteStrictMode
}

return cookie
}
35 changes: 0 additions & 35 deletions requests/query.go

This file was deleted.

62 changes: 62 additions & 0 deletions requests/urlencoded.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package requests

import (
"context"
"errors"
"net/http"

ozzo "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-viper/mapstructure/v2"
"github.com/noxecane/anansi"
)

// QueryParams converts the query values of the request into a struct using
// the "json" tag to map the keys. It supports transformations using modl
// and validation provided by ozzo.
func QueryParams(r *http.Request, v interface{}) error {
return parseParams(r.Context(), r.URL.Query(), v)
}

// FormData is QueryParams for x-www-form-urlencoded
func FormData(r *http.Request, v interface{}) error {
err := r.ParseForm()
if err != nil {
return err
}

return parseParams(r.Context(), r.Form, v)
}

// MultipartFormData converts the non-files in the form data of the request
// into a struct using the "json" tag to map the keys. It supports
// transformations using modl and validation provided by ozzo.
func MultipartFormData(r *http.Request, size int64, v interface{}) error {
err := r.ParseMultipartForm(size)
if err != nil {
return err
}

return parseParams(r.Context(), r.MultipartForm.Value, v)
}

func parseParams(ctx context.Context, values map[string][]string, v interface{}) error {
params := anansi.SimpleMap(values)

config := &mapstructure.DecoderConfig{Result: v, TagName: `json`, WeaklyTypedInput: true}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return errors.Join(err, errors.New("could not convert query parameters to struct"))
}

err = decoder.Decode(params)
if err != nil {
return err
}

// validate parsed JSON data
if err := generalMold.Struct(ctx, v); err != nil {
return err
}

return ozzo.Validate(v)
}
File renamed without changes.

0 comments on commit 8a03f51

Please sign in to comment.