From c42c391094e38500d641b32ebdd18df48506d44e Mon Sep 17 00:00:00 2001 From: kaimanhub Date: Tue, 8 Apr 2025 19:54:18 +0300 Subject: [PATCH] Use httpclient.Client instead of standard net/http --- cmd/gotosocial/action/server/server.go | 1 + .../api/wellknown/webfinger/webfingerget_test.go | 2 ++ internal/processing/admin/admin_test.go | 2 ++ internal/processing/processor.go | 4 +++- internal/processing/processor_test.go | 2 ++ internal/processing/status/create.go | 4 ++-- internal/processing/status/preview_card.go | 16 +++++++++------- internal/processing/status/status.go | 4 ++++ internal/processing/status/status_test.go | 2 ++ internal/webpush/realsender_test.go | 2 ++ testrig/processor.go | 2 ++ testrig/teststructs.go | 2 ++ 12 files changed, 33 insertions(+), 10 deletions(-) diff --git a/cmd/gotosocial/action/server/server.go b/cmd/gotosocial/action/server/server.go index f934aaadd..e81c94e01 100644 --- a/cmd/gotosocial/action/server/server.go +++ b/cmd/gotosocial/action/server/server.go @@ -385,6 +385,7 @@ var Start action.GTSAction = func(ctx context.Context) error { webPushSender, visFilter, intFilter, + client, ) // Schedule background cleaning tasks. diff --git a/internal/api/wellknown/webfinger/webfingerget_test.go b/internal/api/wellknown/webfinger/webfingerget_test.go index 94c084146..9f32d5826 100644 --- a/internal/api/wellknown/webfinger/webfingerget_test.go +++ b/internal/api/wellknown/webfinger/webfingerget_test.go @@ -37,6 +37,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/filter/interaction" "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/processing" "github.com/superseriousbusiness/gotosocial/internal/subscriptions" "github.com/superseriousbusiness/gotosocial/testrig" @@ -100,6 +101,7 @@ func (suite *WebfingerGetTestSuite) funkifyAccountDomain(host string, accountDom testrig.NewNoopWebPushSender(), visibility.NewFilter(&suite.state), interaction.NewFilter(&suite.state), + &httpclient.Client{}, // TODO: check if we need to replace it here ) suite.webfingerModule = webfinger.New(suite.processor) diff --git a/internal/processing/admin/admin_test.go b/internal/processing/admin/admin_test.go index 804abbc62..1f0e28a12 100644 --- a/internal/processing/admin/admin_test.go +++ b/internal/processing/admin/admin_test.go @@ -27,6 +27,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/filter/interaction" "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/messages" "github.com/superseriousbusiness/gotosocial/internal/oauth" @@ -120,6 +121,7 @@ func (suite *AdminStandardTestSuite) SetupTest() { testrig.NewNoopWebPushSender(), visibility.NewFilter(&suite.state), interaction.NewFilter(&suite.state), + &httpclient.Client{}, // TODO: check if we need to replace it here ) testrig.StartWorkers(&suite.state, suite.processor.Workers()) diff --git a/internal/processing/processor.go b/internal/processing/processor.go index 0324f49cf..85b1d280a 100644 --- a/internal/processing/processor.go +++ b/internal/processing/processor.go @@ -24,6 +24,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/filter/interaction" "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" mm "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/oauth" "github.com/superseriousbusiness/gotosocial/internal/processing/account" @@ -204,6 +205,7 @@ func NewProcessor( webPushSender webpush.Sender, visFilter *visibility.Filter, intFilter *interaction.Filter, + client *httpclient.Client, ) *Processor { parseMentionFunc := GetParseMentionFunc(state, federator) processor := &Processor{ @@ -241,7 +243,7 @@ func NewProcessor( processor.tags = tags.New(state, converter) processor.timeline = timeline.New(state, converter, visFilter) processor.search = search.New(state, federator, converter, visFilter) - processor.status = status.New(state, &common, &processor.polls, &processor.interactionRequests, federator, converter, visFilter, intFilter, parseMentionFunc) + processor.status = status.New(state, &common, &processor.polls, &processor.interactionRequests, federator, converter, visFilter, intFilter, parseMentionFunc, client) processor.user = user.New(state, converter, oauthServer, emailSender) // The advanced migrations processor sequences advanced migrations from all other processors. diff --git a/internal/processing/processor_test.go b/internal/processing/processor_test.go index 4b6406b03..4beb60501 100644 --- a/internal/processing/processor_test.go +++ b/internal/processing/processor_test.go @@ -30,6 +30,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/filter/interaction" "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/oauth" "github.com/superseriousbusiness/gotosocial/internal/processing" @@ -137,6 +138,7 @@ func (suite *ProcessingStandardTestSuite) SetupTest() { testrig.NewNoopWebPushSender(), visibility.NewFilter(&suite.state), interaction.NewFilter(&suite.state), + &httpclient.Client{}, // TODO: check if we need to replace it here ) testrig.StartWorkers(&suite.state, suite.processor.Workers()) diff --git a/internal/processing/status/create.go b/internal/processing/status/create.go index b6d790f43..747b01eec 100644 --- a/internal/processing/status/create.go +++ b/internal/processing/status/create.go @@ -190,9 +190,9 @@ func (p *Processor) Create( } // Get preview card - card, errWithCode := FetchPreview(content.Content) + card, errWithCode := FetchPreview(ctx, p.client, content.Content) if errWithCode != nil { - return nil, errWithCode + log.Errorf(ctx, "error loading preview card: %v", errWithCode) } if card != nil { diff --git a/internal/processing/status/preview_card.go b/internal/processing/status/preview_card.go index a27f5bf86..28b561521 100644 --- a/internal/processing/status/preview_card.go +++ b/internal/processing/status/preview_card.go @@ -18,6 +18,7 @@ package status import ( + "context" "fmt" "net/http" "net/url" @@ -27,6 +28,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" ) var urlRegex = regexp.MustCompile(`https?://[a-zA-Z0-9./?=_-]+`) @@ -40,7 +42,7 @@ func extractLastURL(text string) string { } // FetchPreview retrieves OpenGraph metadata from a URL. -func FetchPreview(text string) (*gtsmodel.Card, gtserror.WithCode) { +func FetchPreview(ctx context.Context, httpClient *httpclient.Client, text string) (*gtsmodel.Card, gtserror.WithCode) { link := extractLastURL(text) if link == "" { return nil, nil @@ -55,7 +57,12 @@ func FetchPreview(text string) (*gtsmodel.Card, gtserror.WithCode) { return nil, gtserror.NewErrorInternalError(fmt.Errorf("unsupported scheme: %s", parsed.Scheme)) } - resp, err := safeGet(parsed) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, link, nil) + if err != nil { + return nil, gtserror.NewErrorInternalError(err, "failed to create request") + } + + resp, err := httpClient.Do(req) if err != nil { return nil, gtserror.NewErrorInternalError(err, "request failed") } @@ -109,8 +116,3 @@ func FetchPreview(text string) (*gtsmodel.Card, gtserror.WithCode) { return card, nil } - -func safeGet(u *url.URL) (*http.Response, error) { - // #nosec G107 -- URL was already validated - return http.Get(u.String()) -} diff --git a/internal/processing/status/status.go b/internal/processing/status/status.go index 26dfd0d7a..5a724e25c 100644 --- a/internal/processing/status/status.go +++ b/internal/processing/status/status.go @@ -22,6 +22,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/filter/interaction" "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/processing/common" "github.com/superseriousbusiness/gotosocial/internal/processing/interactionrequests" "github.com/superseriousbusiness/gotosocial/internal/processing/polls" @@ -41,6 +42,7 @@ type Processor struct { intFilter *interaction.Filter formatter *text.Formatter parseMention gtsmodel.ParseMentionFunc + client *httpclient.Client // other processors polls *polls.Processor @@ -58,6 +60,7 @@ func New( visFilter *visibility.Filter, intFilter *interaction.Filter, parseMention gtsmodel.ParseMentionFunc, + client *httpclient.Client, ) Processor { return Processor{ c: common, @@ -70,5 +73,6 @@ func New( parseMention: parseMention, polls: polls, intReqs: intReqs, + client: client, } } diff --git a/internal/processing/status/status_test.go b/internal/processing/status/status_test.go index c163f95a7..1f5c49ab9 100644 --- a/internal/processing/status/status_test.go +++ b/internal/processing/status/status_test.go @@ -25,6 +25,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/filter/interaction" "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/processing" "github.com/superseriousbusiness/gotosocial/internal/processing/common" @@ -116,6 +117,7 @@ func (suite *StatusStandardTestSuite) SetupTest() { &suite.state, suite.federator, ), + &httpclient.Client{}, ) testrig.StandardDBSetup(suite.db, suite.testAccounts) diff --git a/internal/webpush/realsender_test.go b/internal/webpush/realsender_test.go index 28a5eae95..b711bb9c2 100644 --- a/internal/webpush/realsender_test.go +++ b/internal/webpush/realsender_test.go @@ -35,6 +35,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/filter/interaction" "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/oauth" "github.com/superseriousbusiness/gotosocial/internal/processing" @@ -130,6 +131,7 @@ func (suite *RealSenderStandardTestSuite) SetupTest() { suite.webPushSender, visibility.NewFilter(&suite.state), interaction.NewFilter(&suite.state), + &httpclient.Client{}, // TODO: check if we need to replace it here ) testrig.StartWorkers(&suite.state, suite.processor.Workers()) diff --git a/testrig/processor.go b/testrig/processor.go index 2df7ef197..d9def88c4 100644 --- a/testrig/processor.go +++ b/testrig/processor.go @@ -23,6 +23,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/federation" "github.com/superseriousbusiness/gotosocial/internal/filter/interaction" "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/internal/processing" "github.com/superseriousbusiness/gotosocial/internal/state" @@ -58,5 +59,6 @@ func NewTestProcessor( webPushSender, visibility.NewFilter(state), interaction.NewFilter(state), + &httpclient.Client{}, // TODO: check if we need to replace it here ) } diff --git a/testrig/teststructs.go b/testrig/teststructs.go index f8eb1b3ed..061eae603 100644 --- a/testrig/teststructs.go +++ b/testrig/teststructs.go @@ -23,6 +23,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/email" "github.com/superseriousbusiness/gotosocial/internal/filter/interaction" "github.com/superseriousbusiness/gotosocial/internal/filter/visibility" + "github.com/superseriousbusiness/gotosocial/internal/httpclient" "github.com/superseriousbusiness/gotosocial/internal/processing" "github.com/superseriousbusiness/gotosocial/internal/processing/common" "github.com/superseriousbusiness/gotosocial/internal/state" @@ -106,6 +107,7 @@ func SetupTestStructs( webPushSender, visFilter, intFilter, + &httpclient.Client{}, // TODO: check if we need to replace it here ) StartWorkers(&state, processor.Workers())