plc: better HTTP handling

This commit is contained in:
Michael Muré
2025-07-21 18:38:13 +02:00
parent de98cf811b
commit f77c413386

View File

@@ -78,11 +78,18 @@ func (d DidPlc) Document(opts ...did.ResolutionOption) (did.Document, error) {
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "go-did-it")
res, err := params.HttpClient().Do(req)
if err != nil {
return nil, fmt.Errorf("%w: %w", did.ErrResolutionFailure, err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: HTTP %d", did.ErrResolutionFailure, res.StatusCode)
}
var aux struct {
Did string `json:"did"`
@@ -95,8 +102,9 @@ func (d DidPlc) Document(opts ...did.ResolutionOption) (did.Document, error) {
} `json:"services"`
}
// limit at 1MB
err = json.NewDecoder(io.LimitReader(res.Body, 1<<20)).Decode(&aux)
// limit at 1MB to avoid abuse
limiter := io.LimitReader(res.Body, 1<<20)
err = json.NewDecoder(limiter).Decode(&aux)
if err != nil {
return nil, fmt.Errorf("%w: %w", did.ErrResolutionFailure, err)
}