-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for urlencoded and multipart forms (#86)
- Loading branch information
Showing
4 changed files
with
91 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.