2024-09-21 16:52:56 +00:00
|
|
|
package h
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/maddalax/htmgo/framework/hx"
|
|
|
|
|
"net/url"
|
|
|
|
|
)
|
|
|
|
|
|
2024-09-21 17:08:23 +00:00
|
|
|
type Headers = map[string]string
|
|
|
|
|
|
2024-09-21 16:52:56 +00:00
|
|
|
func ReplaceUrlHeader(url string) *Headers {
|
|
|
|
|
return NewHeaders(hx.ReplaceUrlHeader, url)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-21 17:08:23 +00:00
|
|
|
func PushUrlHeader(url string) *Headers {
|
|
|
|
|
return NewHeaders(hx.PushUrlHeader, url)
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-22 19:37:24 +00:00
|
|
|
func PushQsHeader(ctx *RequestContext, qs *Qs) *Headers {
|
|
|
|
|
parsed, err := url.Parse(ctx.currentBrowserUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return NewHeaders()
|
|
|
|
|
}
|
|
|
|
|
return NewHeaders(hx.ReplaceUrlHeader, SetQueryParams(parsed.Path, qs))
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-21 16:52:56 +00:00
|
|
|
func CombineHeaders(headers ...*Headers) *Headers {
|
|
|
|
|
m := make(Headers)
|
|
|
|
|
for _, h := range headers {
|
|
|
|
|
for k, v := range *h {
|
|
|
|
|
m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return &m
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CurrentPath(ctx *RequestContext) string {
|
|
|
|
|
current := ctx.Request().Header.Get(hx.CurrentUrlHeader)
|
|
|
|
|
parsed, err := url.Parse(current)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return parsed.Path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewHeaders(headers ...string) *Headers {
|
|
|
|
|
if len(headers)%2 != 0 {
|
|
|
|
|
return &Headers{}
|
|
|
|
|
}
|
|
|
|
|
m := make(Headers)
|
|
|
|
|
for i := 0; i < len(headers); i++ {
|
|
|
|
|
m[headers[i]] = headers[i+1]
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
return &m
|
|
|
|
|
}
|