step/providers/http/http.go
Sasha Koshka bf668b0cf7 Things I did while unable to commit
- Log rotation
- Execution cancellation
- HTTP redirect, error functions
- Changed naming of document parsing/loading functions
2024-12-10 20:37:40 -05:00

61 lines
1.5 KiB
Go

package http
import "net/url"
import "net/http"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
import shttp "git.tebibyte.media/sashakoshka/step/http"
var _ step.FuncProvider = new(Provider)
// Provider provides HTTP and URL functions.
type Provider struct {
}
// Package fulfills the step.Provider interface.
func (this *Provider) Package () string {
return "http"
}
// FuncMap fulfills the step.FuncProvider interface.
func (this *Provider) FuncMap () template.FuncMap {
return template.FuncMap {
"statusText": http.StatusText,
"parseQuery": funcParseQuery,
"parseForm": funcParseForm,
"error": funcError,
"redirect": funcRedirect,
}
}
func funcParseQuery (query string) url.Values {
// wrapped here because the query might contain all sorts of nonsense,
// and returning an error in a template function causes everything to
// stop rather ungracefully
values, err := url.ParseQuery(query)
if err != nil { return nil }
return values
}
func funcError (status int, message any) (string, error) {
return "", shttp.Error {
Status: status,
Message: message,
}
}
func funcRedirect (res shttp.WrappedResponseWriter, status int, pat string) (string, error) {
// TODO remove parameters
res.Header.Add("Location", pat)
res.WriteHeader(status)
return "", step.ErrExecutionCanceled
}
func funcParseForm (req *http.Request) url.Values {
// FIXME there is already a parse form method lol this can be removed
err := req.ParseForm()
if err != nil { return nil }
return req.Form
}