MCPcopy Create free account
hub / github.com/gogs/gogs / deliver

Method deliver

internal/database/webhook.go:696–788  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

694}
695
696func (t *HookTask) deliver() {
697 payloadURL, err := url.Parse(t.URL)
698 if err != nil {
699 t.ResponseContent = fmt.Sprintf(`{"body": "Cannot parse payload URL: %v"}`, err)
700 return
701 }
702 if netutil.IsBlockedLocalHostname(payloadURL.Hostname(), conf.Security.LocalNetworkAllowlist) {
703 t.ResponseContent = `{"body": "Payload URL resolved to a local network address that is implicitly blocked."}`
704 return
705 }
706
707 t.IsDelivered = true
708
709 timeout := time.Duration(conf.Webhook.DeliverTimeout) * time.Second
710 req := httplib.Post(t.URL).SetTimeout(timeout, timeout).
711 Header("X-Github-Delivery", t.UUID).
712 Header("X-Github-Event", string(t.EventType)).
713 Header("X-Gogs-Delivery", t.UUID).
714 Header("X-Gogs-Signature", t.Signature).
715 Header("X-Gogs-Event", string(t.EventType)).
716 SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Webhook.SkipTLSVerify}).
717 SetCheckRedirect(func(req *http.Request, _ []*http.Request) error {
718 // The webhook target is explicitly configured by the user, so any
719 // redirect would silently retarget the signed payload. Refuse all
720 // redirects rather than chase them.
721 return errors.Newf("refusing to follow webhook redirect to %q", req.URL.Redacted())
722 })
723
724 switch t.ContentType {
725 case JSON:
726 req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
727 case FORM:
728 req.Param("payload", t.PayloadContent)
729 }
730
731 // Record delivery information.
732 t.RequestInfo = &HookRequest{
733 Headers: map[string]string{},
734 }
735 for k, vals := range req.Headers() {
736 t.RequestInfo.Headers[k] = strings.Join(vals, ",")
737 }
738
739 t.ResponseInfo = &HookResponse{
740 Headers: map[string]string{},
741 }
742
743 defer func() {
744 t.Delivered = time.Now().UnixNano()
745 if t.IsSucceed {
746 log.Trace("Hook delivered: %s", t.UUID)
747 } else {
748 log.Trace("Hook delivery failed: %s", t.UUID)
749 }
750
751 // Update webhook last delivery status.
752 w, err := GetWebhookByID(t.HookID)
753 if err != nil {

Callers 1

DeliverHooksFunction · 0.80

Calls 14

IsBlockedLocalHostnameFunction · 0.92
PostFunction · 0.92
stringFunction · 0.85
GetWebhookByIDFunction · 0.85
UpdateWebhookFunction · 0.85
SetCheckRedirectMethod · 0.80
SetTLSClientConfigMethod · 0.80
HeaderMethod · 0.80
SetTimeoutMethod · 0.80
BodyMethod · 0.80
ParamMethod · 0.80
HeadersMethod · 0.80

Tested by

no test coverage detected