httputil

package
v0.0.0-...-2b8ec9a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2023 License: Apache-2.0 Imports: 26 Imported by: 7

Documentation

Index

Constants

View Source
const BodyMaxSize = 1024 * 1024 * 10

Variables

View Source
var FlashMessageTypeCookieDef = &CookieDef{
	NameSuffix: "flash_message_type",
	Path:       "/",
	SameSite:   http.SameSiteNoneMode,
}

FlashMessageTypeCookieDef is a HTTP session cookie.

Functions

func BindJSONBody

func BindJSONBody(r *http.Request, w http.ResponseWriter, v *validation.SchemaValidator, payload interface{}) error

func CSPJoin

func CSPJoin(directives []string) string

func CheckContentType

func CheckContentType(raws []string) httproute.MiddlewareFunc

func CookieDomainWithoutPort

func CookieDomainWithoutPort(host string) string

CookieDomainWithoutPort derives host from r. If host has port, the port is removed. If host-1 is longer than ETLD+1, host-1 is returned. If ETLD+1 cannot be derived, an empty string is returned. The return value never have port.

func GetHost

func GetHost(r *http.Request, trustProxy bool) string

func GetIP

func GetIP(r *http.Request, trustProxy bool) (ip string)

func GetProto

func GetProto(r *http.Request, trustProxy bool) string

func GetRedirectURI

func GetRedirectURI(r *http.Request, trustProxy bool) (out string, err error)

func HealthCheckHandler

func HealthCheckHandler(w http.ResponseWriter, r *http.Request)

HealthCheckHandler is basic handler for server health check

func HostRelative

func HostRelative(u *url.URL) *url.URL

func IsJSONContentType

func IsJSONContentType(contentType string) bool

func NewExternalClient

func NewExternalClient(timeout time.Duration) *http.Client

func NewExternalClientWithOptions

func NewExternalClientWithOptions(timeout time.Duration, opts ExternalClientOptions) *http.Client

func NoCache

func NoCache(next http.Handler) http.Handler

NoCache allows caches to store a response but requires them to revalidate it before reuse.

func NoStore

func NoStore(next http.Handler) http.Handler

NoStore makes the browser not to store the requests.

func ParseJSONBody

func ParseJSONBody(r *http.Request, w http.ResponseWriter, parse func(io.Reader, interface{}) error, payload interface{}) error

func ShouldSendSameSiteNone

func ShouldSendSameSiteNone(useragent string, secure bool) bool

func StaticSecurityHeaders

func StaticSecurityHeaders(next http.Handler) http.Handler

func UpdateCookie

func UpdateCookie(w http.ResponseWriter, cookie *http.Cookie)

Types

type BodyDefaulter

type BodyDefaulter interface {
	SetDefaults()
}

type CookieDef

type CookieDef struct {
	// NameSuffix means the cookie could have prefix.
	NameSuffix string
	Path       string
	// Domain is omitted because it is controlled somewhere else.
	// Domain            string
	AllowScriptAccess bool
	SameSite          http.SameSite
	MaxAge            *int
}

type CookieManager

type CookieManager struct {
	Request      *http.Request
	TrustProxy   bool
	CookiePrefix string
	CookieDomain string
}

func (*CookieManager) ClearCookie

func (f *CookieManager) ClearCookie(def *CookieDef) *http.Cookie

ClearCookie generates a cookie that when set, the cookie is clear.

func (*CookieManager) CookieName

func (f *CookieManager) CookieName(def *CookieDef) string

CookieName returns the full name, that is, CookiePrefix followed by NameSuffix.

func (*CookieManager) GetCookie

func (f *CookieManager) GetCookie(r *http.Request, def *CookieDef) (*http.Cookie, error)

GetCookie is wrapper around http.Request.Cookie, taking care of cookie name.

func (*CookieManager) ValueCookie

func (f *CookieManager) ValueCookie(def *CookieDef, value string) *http.Cookie

ValueCookie generates a cookie that when set, the cookie is set to the specified value.

type ExternalClientOptions

type ExternalClientOptions struct {
	FollowRedirect bool
}

type FileServer

type FileServer struct {
	FileSystem          http.FileSystem
	FallbackToIndexHTML bool
}

FileServer is a specialized version of http.FileServer that assumes files rooted at FileSystem are name-hashed. cache-control are written specifically for index.html and name-hashed files.

func (*FileServer) ServeHTTP

func (s *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

type FilesystemCache

type FilesystemCache struct {
	// contains filtered or unexported fields
}

FilesystemCache is a helper to write the response into the tmp directory. The response is then served with http.FileServer, with the advantage of supporting range request and cache validation. If the file is not modified, the response is a 304. For even better performance, we need to add Cache-Control header to take advantage of the fact that the filename is hashed. However, http.FileServer does not support Cache-Control. Unconditionally adding Cache-Control for non-existent file is problematic.

func NewFilesystemCache

func NewFilesystemCache() *FilesystemCache

func (*FilesystemCache) Clear

func (c *FilesystemCache) Clear() error

func (*FilesystemCache) Serve

func (c *FilesystemCache) Serve(r *http.Request, make func() ([]byte, error)) (handler http.Handler)

type FlashMessage

type FlashMessage struct {
	Cookies FlashMessageCookieManager
}

func (*FlashMessage) Flash

func (f *FlashMessage) Flash(rw http.ResponseWriter, messageType string)

func (*FlashMessage) Pop

type FlashMessageCookieManager

type FlashMessageCookieManager interface {
	GetCookie(r *http.Request, def *CookieDef) (*http.Cookie, error)
	ValueCookie(def *CookieDef, value string) *http.Cookie
	ClearCookie(def *CookieDef) *http.Cookie
}

type HTTPHost

type HTTPHost string

type HTTPProto

type HTTPProto string

type JSONResponseWriter

type JSONResponseWriter struct {
	Logger JSONResponseWriterLogger
}

func (*JSONResponseWriter) WriteResponse

func (w *JSONResponseWriter) WriteResponse(rw http.ResponseWriter, resp *api.Response)

type JSONResponseWriterLogger

type JSONResponseWriterLogger struct{ *log.Logger }

func NewJSONResponseWriterLogger

func NewJSONResponseWriterLogger(lf *log.Factory) JSONResponseWriterLogger

type RemoteIP

type RemoteIP string

type Result

type Result interface {
	WriteResponse(rw http.ResponseWriter, r *http.Request)
	IsInternalError() bool
}

type ResultRedirect

type ResultRedirect struct {
	URL string
}

func (ResultRedirect) IsInternalError

func (re ResultRedirect) IsInternalError() bool

func (ResultRedirect) WriteResponse

func (re ResultRedirect) WriteResponse(rw http.ResponseWriter, r *http.Request)

type StaticCSPHeader

type StaticCSPHeader struct {
	CSPDirectives []string
}

func (StaticCSPHeader) Handle

func (m StaticCSPHeader) Handle(next http.Handler) http.Handler

type TutorialCookie

type TutorialCookie struct {
	Cookies FlashMessageCookieManager
}

func (*TutorialCookie) Pop

func (*TutorialCookie) SetAll

func (t *TutorialCookie) SetAll(rw http.ResponseWriter)

type TutorialCookieManager

type TutorialCookieManager interface {
	GetCookie(r *http.Request, def *CookieDef) (*http.Cookie, error)
	ValueCookie(def *CookieDef, value string) *http.Cookie
	ClearCookie(def *CookieDef) *http.Cookie
}

type TutorialCookieName

type TutorialCookieName string
const (
	SignupLoginTutorialCookieName TutorialCookieName = "signup_login_tutorial"
	SettingsTutorialCookieName    TutorialCookieName = "settings_tutorial"
)

type UserAgentString

type UserAgentString string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL