mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-31 14:52:30 -05:00 
			
		
		
		
	Use httpclient.Client instead of standard net/http
This commit is contained in:
		
					parent
					
						
							
								d11efa1e2f
							
						
					
				
			
			
				commit
				
					
						c42c391094
					
				
			
		
					 12 changed files with 33 additions and 10 deletions
				
			
		|  | @ -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()) | ||||
|  |  | |||
|  | @ -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. | ||||
|  |  | |||
|  | @ -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()) | ||||
| 
 | ||||
|  |  | |||
|  | @ -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 { | ||||
|  |  | |||
|  | @ -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()) | ||||
| } | ||||
|  |  | |||
|  | @ -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, | ||||
| 	} | ||||
| } | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue