From a8aa306af49f91c24ec13e838efe4449a875c9ba Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Sun, 30 Jul 2023 16:49:34 -0400 Subject: [PATCH] simplify junk.get code --- activity.go | 45 ++++++--------------------------------------- 1 file changed, 6 insertions(+), 39 deletions(-) diff --git a/activity.go b/activity.go index 6f5599b..3c211a1 100644 --- a/activity.go +++ b/activity.go @@ -132,64 +132,31 @@ var flightdeck = gate.NewSerializer() var signGets = true -func junkGet(userid int64, url string, args junk.GetArgs) (junk.Junk, error) { +func GetJunkTimeout(userid int64, url string, timeout time.Duration) (junk.Junk, error) { client := http.DefaultClient - if args.Client != nil { - client = args.Client - } - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, err - } - if args.Accept != "" { - req.Header.Set("Accept", args.Accept) - } - if args.Agent != "" { - req.Header.Set("User-Agent", args.Agent) - } - if signGets { + sign := func(req *http.Request) error { var ki *KeyInfo ok := ziggies.Get(userid, &ki) if ok { httpsig.SignRequest(ki.keyname, ki.seckey, req, nil) } + return nil } - if args.Timeout != 0 { - ctx, cancel := context.WithTimeout(context.Background(), args.Timeout) - defer cancel() - req = req.WithContext(ctx) - } - resp, err := client.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - switch resp.StatusCode { - case 200: - case 201: - case 202: - default: - return nil, fmt.Errorf("http get status: %d", resp.StatusCode) - } - return junk.Read(resp.Body) -} - -func GetJunkTimeout(userid int64, url string, timeout time.Duration) (junk.Junk, error) { - client := http.DefaultClient if develMode { client = develClient + sign = nil } fn := func() (interface{}, error) { at := thefakename if strings.Contains(url, ".well-known/webfinger?resource") { at = "application/jrd+json" } - j, err := junkGet(userid, url, junk.GetArgs{ + j, err := junk.Get(url, junk.GetArgs{ Accept: at, Agent: "honksnonk/5.0; " + serverName, Timeout: timeout, Client: client, + Fixup: sign, }) return j, err }