Use httpclient.Client instead of standard net/http

This commit is contained in:
kaimanhub 2025-04-08 19:54:18 +03:00
parent d11efa1e2f
commit c42c391094
12 changed files with 33 additions and 10 deletions

View file

@ -385,6 +385,7 @@ var Start action.GTSAction = func(ctx context.Context) error {
webPushSender,
visFilter,
intFilter,
client,
)
// Schedule background cleaning tasks.

View file

@ -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)

View file

@ -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())

View file

@ -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.

View file

@ -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())

View file

@ -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 {

View file

@ -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())
}

View file

@ -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,
}
}

View file

@ -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)

View file

@ -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())

View file

@ -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
)
}

View file

@ -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())