From 85fed4050112a8e665a4b30016b17e24cfab4f1a Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 29 Oct 2024 22:03:30 +0000 Subject: [PATCH] Fix `LookupWellKnown` to read full response Fix a bug in `LookupWellKnown` which would cause it only to read part of the HTTP response body if it spanned multiple TCP segments. Fixes: https://github.com/matrix-org/gomatrixserverlib/issues/440 --- fclient/well_known.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/fclient/well_known.go b/fclient/well_known.go index 50f5af1a..102850a9 100644 --- a/fclient/well_known.go +++ b/fclient/well_known.go @@ -102,16 +102,10 @@ func LookupWellKnown(ctx context.Context, serverNameType spec.ServerName) (*Well // by checking Content-Length, but it's possible that header will be // missing. Better to be safe than sorry by reading no more than the // WellKnownMaxSize in any case. - bodyBuffer := make([]byte, WellKnownMaxSize) - limitedReader := &io.LimitedReader{ - R: resp.Body, - N: WellKnownMaxSize, - } - n, err := limitedReader.Read(bodyBuffer) - if err != nil && err != io.EOF { + body, err := io.ReadAll(&io.LimitedReader{R: resp.Body, N: WellKnownMaxSize}) + if err != nil { return nil, err } - body := bodyBuffer[:n] // Convert result to JSON wellKnownResponse := &WellKnownResult{