debian-mirror-gitlab/workhorse/internal/senddata/injecter.go

36 lines
675 B
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package senddata
import (
"encoding/base64"
"encoding/json"
"net/http"
"strings"
)
type Injecter interface {
Match(string) bool
Inject(http.ResponseWriter, *http.Request, string)
Name() string
}
type Prefix string
func (p Prefix) Match(s string) bool {
return strings.HasPrefix(s, string(p))
}
func (p Prefix) Unpack(result interface{}, sendData string) error {
jsonBytes, err := base64.URLEncoding.DecodeString(strings.TrimPrefix(sendData, string(p)))
if err != nil {
return err
}
if err := json.Unmarshal([]byte(jsonBytes), result); err != nil {
return err
}
return nil
}
func (p Prefix) Name() string {
return strings.TrimSuffix(string(p), ":")
}