httphelper

package
v2.0.11-0...-ba100f2 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2021 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BackendRequestTimeout = 15 * time.Second // time after which requests to the ubirch backend will be canceled
	GatewayTimeout        = 90 * time.Second // time after which a 504 response will be sent if no timely response could be produced
	ShutdownTimeout       = 25 * time.Second // time after which the server will be shut down forcefully if graceful shutdown did not happen before
	ReadTimeout           = 1 * time.Second  // maximum duration for reading the entire request -> low since we only expect requests with small content
	WriteTimeout          = 99 * time.Second // time after which the connection will be closed if response was not written -> this should never happen
	IdleTimeout           = 60 * time.Second // time to wait for the next request when keep-alives are enabled

	UUIDKey          = "uuid"
	OperationKey     = "operation"
	VerifyPath       = "verify"
	HashEndpoint     = "hash"
	RegisterEndpoint = "register"

	BinType  = "application/octet-stream"
	TextType = "text/plain"
	JSONType = "application/json"

	HexEncoding = "hex"

	HashLen = 32
)
View Source
const (
	XAuthHeader            string = "x-auth-token"
	MimeTextPlain          string = "text/plain"
	MimeApplicationProblem string = "application/problem+json"
	HeaderContentType      string = "Content-Type"
)

Variables

This section is empty.

Functions

func AuthToken

func AuthToken(header http.Header) string

helper function to get "X-Auth-Token" from request header

func ContentEncoding

func ContentEncoding(header http.Header) string

helper function to get "Content-Transfer-Encoding" from request header

func ContentType

func ContentType(header http.Header) string

helper function to get "Content-Type" from request header

func Error

func Error(uid uuid.UUID, w http.ResponseWriter, err error, code int)

wrapper for http.Error that additionally logs the error message to std.Output

func GetSortedCompactJSON

func GetSortedCompactJSON(data []byte) ([]byte, error)

func GetUUID

func GetUUID(r *http.Request) (uuid.UUID, error)

getUUID returns the UUID parameter from the request URL

func Health

func Health(server string) http.HandlerFunc

func HttpFailed

func HttpFailed(StatusCode int) bool

func HttpSuccess

func HttpSuccess(StatusCode int) bool

func IsHashRequest

func IsHashRequest(r *http.Request) bool

func NewRouter

func NewRouter() *chi.Mux

func Ok

func Ok(w http.ResponseWriter, rsp string)

func ReadBody

func ReadBody(r *http.Request) ([]byte, error)

func Respond400

func Respond400(w http.ResponseWriter, detail string)

Respond400 sends a HTTP status 400 response to the provided HTTP response writer using application/problem+json media type and constructs the details property from the supplied error

func Respond406

func Respond406(w http.ResponseWriter, detail string)

Respond406 sends a HTTP status 406 response to the provided HTTP response writer using application/problem+json media type and constructs the details property from the supplied problem

func Respond409

func Respond409(w http.ResponseWriter, detail string)

Respond409 sends a HTTP status 409 response to the provided HTTP response writer using application/problem+json media type and constructs the details property from the supplied conflict

func RespondProblem

func RespondProblem(w http.ResponseWriter, pi HttpProblemInstance)

RespondProblem sends an application/problem+json response to the HTTP writer. The response is based on the provided problem instance

func SendResponse

func SendResponse(w http.ResponseWriter, resp HTTPResponse)

forwards response to sender

Types

type CheckIdentityExists

type CheckIdentityExists func(uid uuid.UUID) (bool, error)

type HTTPRequest

type HTTPRequest struct {
	ID   uuid.UUID
	Auth string
	Hash Sha256Sum
}

type HTTPResponse

type HTTPResponse struct {
	StatusCode int         `json:"statusCode"`
	Header     http.Header `json:"header"`
	Content    []byte      `json:"content"`
}

type HTTPServer

type HTTPServer struct {
	Router   *chi.Mux
	Addr     string
	TLS      bool
	CertFile string
	KeyFile  string
}

func (*HTTPServer) AddServiceEndpoint

func (srv *HTTPServer) AddServiceEndpoint(endpoint ServerEndpoint)

func (*HTTPServer) Serve

func (srv *HTTPServer) Serve(cancelCtx context.Context, serverReady context.CancelFunc) error

func (*HTTPServer) SetUpCORS

func (srv *HTTPServer) SetUpCORS(allowedOrigins []string, debug bool)

type HttpProblem

type HttpProblem struct {
	Type   string `json:"type"`
	Title  string `json:"title"`
	Status int    `json:"status,omitempty"`
}

HttpProblem is an implementation of https://tools.ietf.org/html/rfc7807. It should be used to define once in your program the problems in use. HttpProblemInstances should then be used to create individual instances of the problems.

type HttpProblemInstance

type HttpProblemInstance struct {
	HttpProblem
	Detail   string `json:"detail,omitempty"`
	Instance string `json:"instance,omitempty"`
}

HttpProblemInstance implements individual instances of defined-once HttpProblems.

type IdentityCreator

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

func NewIdentityCreator

func NewIdentityCreator(auth string) IdentityCreator

func (*IdentityCreator) Put

type IdentityPayload

type IdentityPayload struct {
	Uid string `json:"uuid"`
	Pwd string `json:"password"`
}

func IdentityFromBody

func IdentityFromBody(r *http.Request) (IdentityPayload, error)

type ServerEndpoint

type ServerEndpoint struct {
	Path string
	Service
}

func (*ServerEndpoint) HandleOptions

func (*ServerEndpoint) HandleOptions(http.ResponseWriter, *http.Request)

type Service

type Service interface {
	HandleRequest(w http.ResponseWriter, r *http.Request)
}

type Sha256Sum

type Sha256Sum [HashLen]byte

func GetHash

func GetHash(r *http.Request) (Sha256Sum, error)

GetHash returns the hash from the request body

type StoreIdentity

type StoreIdentity func(uid uuid.UUID, auth string) (csr []byte, err error)

Jump to

Keyboard shortcuts

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