mirror of
https://github.com/go-gitea/gitea.git
synced 2026-09-19 19:23:39 +09:00
fix: add default timeout and handle errors for HaveIBeenPwned API (#39316)
Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
co-authored by
silverwind
wxiaoguang
parent
7ebb2caa9e
commit
b2e11ddb37
@@ -83,7 +83,7 @@ type HookProcReceiveRefResult struct {
|
||||
HeadBranch string
|
||||
}
|
||||
|
||||
func newInternalRequestAPIForHooks(ctx context.Context, hookName, ownerName, repoName string, opts HookOptions) *httplib.Request {
|
||||
func newInternalRequestAPIForHooks(ctx context.Context, hookName, ownerName, repoName string, opts HookOptions) *httplib.ClientRequest {
|
||||
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/%s/%s/%s", hookName, url.PathEscape(ownerName), url.PathEscape(repoName))
|
||||
req := newInternalRequestAPI(ctx, reqURL, "POST", opts)
|
||||
// This "timeout" applies to http.Client's timeout: A Timeout of zero means no timeout.
|
||||
|
||||
@@ -89,7 +89,7 @@ var internalAPITransport = sync.OnceValue(func() http.RoundTripper {
|
||||
}
|
||||
})
|
||||
|
||||
func NewInternalRequest(ctx context.Context, url, method string) *httplib.Request {
|
||||
func NewInternalRequest(ctx context.Context, url, method string) *httplib.ClientRequest {
|
||||
if setting.InternalToken == "" {
|
||||
log.Fatal(`The INTERNAL_TOKEN setting is missing from the configuration file: %q.
|
||||
Ensure you are running in the correct environment or set the correct configuration file with -c.`, setting.CustomConf)
|
||||
@@ -99,14 +99,14 @@ Ensure you are running in the correct environment or set the correct configurati
|
||||
log.Fatal("Invalid internal request URL: %q", url)
|
||||
}
|
||||
|
||||
return httplib.NewRequest(url, method).
|
||||
return httplib.NewClientRequest(method, url).
|
||||
SetContext(ctx).
|
||||
SetTransport(internalAPITransport()).
|
||||
Header("X-Real-IP", getClientIP()).
|
||||
Header("X-Gitea-Internal-Auth", "Bearer "+setting.InternalToken)
|
||||
}
|
||||
|
||||
func newInternalRequestAPI(ctx context.Context, url, method string, body ...any) *httplib.Request {
|
||||
func newInternalRequestAPI(ctx context.Context, url, method string, body ...any) *httplib.ClientRequest {
|
||||
req := NewInternalRequest(ctx, url, method)
|
||||
if len(body) == 1 {
|
||||
req.Header("Content-Type", "application/json")
|
||||
|
||||
@@ -52,7 +52,7 @@ func (re responseError) Error() string {
|
||||
// * If the "res" is a struct pointer, the response will be parsed as JSON
|
||||
// * If the "res" is ResponseText pointer, the response will be stored as text in it
|
||||
// * If the "res" is responseCallback pointer, the callback function should set the ResponseExtra fields accordingly
|
||||
func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra ResponseExtra) {
|
||||
func requestJSONResp[T any](req *httplib.ClientRequest, res *T) (ret *T, extra ResponseExtra) {
|
||||
resp, err := req.Response()
|
||||
if err != nil {
|
||||
extra.UserMsg = "Internal Server Connection Error"
|
||||
@@ -118,7 +118,7 @@ func requestJSONResp[T any](req *httplib.Request, res *T) (ret *T, extra Respons
|
||||
|
||||
// requestJSONClientMsg sends a request to the gitea server, server only responds text message status=200 with "success" body
|
||||
// If the request succeeds (200), the argument clientSuccessMsg will be used as ResponseExtra.UserMsg.
|
||||
func requestJSONClientMsg(req *httplib.Request, clientSuccessMsg string) ResponseExtra {
|
||||
func requestJSONClientMsg(req *httplib.ClientRequest, clientSuccessMsg string) ResponseExtra {
|
||||
_, extra := requestJSONResp(req, &ResponseText{})
|
||||
if extra.HasError() {
|
||||
return extra
|
||||
|
||||
Reference in New Issue
Block a user