package client import ( "io" "io/ioutil" "net/http" "net/http/cookiejar" "net/url" "strings" "git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/logging" "golang.org/x/net/publicsuffix" ) type Cfg struct { *config.BaseCfg Url string } var GlobalCookieJar http.CookieJar func Send(act lib.Action) bool { ctx, spec, msg := act.Context(), act.Spec(), act.Message() _ = spec cfg := lib.GetCfg[*Cfg](ctx) url, _ := url.JoinPath(cfg.Url, msg.Slice()...) method := "GET" var pdata io.Reader if pl := msg.Payload(); pl != nil { pdata = strings.NewReader(pl.String()) method = "POST" } resp, err := SendRequest(method, url, pdata) if err != nil { logging.ErrorA(act, err).Msg("client.Send") return true } body, _ := ioutil.ReadAll(resp.Body) bodyStr := string(body) logging.InfoA(act).Str("data", bodyStr).Msg("client.Send") // core.Forward(newMsg) return true } func SendRequest(method, url string, pdata io.Reader) (*http.Response, error) { req, _ := http.NewRequest(method, url, pdata) client := http.DefaultClient client.Jar = GlobalCookieJar resp, err := client.Do(req) return resp, err } // CookieJar initialization func init() { GlobalCookieJar, _ = cookiejar.New( &cookiejar.Options{PublicSuffixList: publicsuffix.List}) }