simplify junk.get code

This commit is contained in:
Ted Unangst 2023-07-30 16:49:34 -04:00
parent 035f23eec0
commit a8aa306af4
1 changed files with 6 additions and 39 deletions

View File

@ -132,64 +132,31 @@ var flightdeck = gate.NewSerializer()
var signGets = true 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 client := http.DefaultClient
if args.Client != nil { sign := func(req *http.Request) error {
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 {
var ki *KeyInfo var ki *KeyInfo
ok := ziggies.Get(userid, &ki) ok := ziggies.Get(userid, &ki)
if ok { if ok {
httpsig.SignRequest(ki.keyname, ki.seckey, req, nil) 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 { if develMode {
client = develClient client = develClient
sign = nil
} }
fn := func() (interface{}, error) { fn := func() (interface{}, error) {
at := thefakename at := thefakename
if strings.Contains(url, ".well-known/webfinger?resource") { if strings.Contains(url, ".well-known/webfinger?resource") {
at = "application/jrd+json" at = "application/jrd+json"
} }
j, err := junkGet(userid, url, junk.GetArgs{ j, err := junk.Get(url, junk.GetArgs{
Accept: at, Accept: at,
Agent: "honksnonk/5.0; " + serverName, Agent: "honksnonk/5.0; " + serverName,
Timeout: timeout, Timeout: timeout,
Client: client, Client: client,
Fixup: sign,
}) })
return j, err return j, err
} }