diff options
Diffstat (limited to 'internal/handlers/handlers.go')
-rw-r--r-- | internal/handlers/handlers.go | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 76724387..a3988c58 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -4,30 +4,33 @@ import ( "net/http" "gitlab.com/gitlab-org/gitlab-pages/internal" + "gitlab.com/gitlab-org/gitlab-pages/internal/config" "gitlab.com/gitlab-org/gitlab-pages/internal/logging" ) // Handlers take care of handling specific requests type Handlers struct { + config *config.Config Auth internal.Auth Artifact internal.Artifact } // New when provided the arguments defined herein, returns a pointer to an // Handlers that is used to handle requests. -func New(auth internal.Auth, artifact internal.Artifact) *Handlers { +func New(config *config.Config, auth internal.Auth, artifact internal.Artifact) *Handlers { return &Handlers{ + config: config, Auth: auth, Artifact: artifact, } } -func (a *Handlers) checkIfLoginRequiredOrInvalidToken(w http.ResponseWriter, r *http.Request, token string) func(*http.Response) bool { +func (h *Handlers) checkIfLoginRequiredOrInvalidToken(w http.ResponseWriter, r *http.Request, token string) func(*http.Response) bool { return func(resp *http.Response) bool { // API will return 403 if the project does not have public pipelines (public_builds flag) if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusForbidden { if token == "" { - if !a.Auth.IsAuthSupported() { + if !h.Auth.IsAuthSupported() { // Auth is not supported, probably means no access or does not exist but we cannot try with auth return false } @@ -35,7 +38,7 @@ func (a *Handlers) checkIfLoginRequiredOrInvalidToken(w http.ResponseWriter, r * logging.LogRequest(r).Debugf("Artifact API response was %d without token, try with authentication", resp.StatusCode) // Authenticate user - if a.Auth.RequireAuth(w, r) { + if h.Auth.RequireAuth(w, r) { return true } } else { @@ -43,7 +46,7 @@ func (a *Handlers) checkIfLoginRequiredOrInvalidToken(w http.ResponseWriter, r * } } - if a.Auth.CheckResponseForInvalidToken(w, r, resp) { + if h.Auth.CheckResponseForInvalidToken(w, r, resp) { return true } @@ -52,18 +55,18 @@ func (a *Handlers) checkIfLoginRequiredOrInvalidToken(w http.ResponseWriter, r * } // HandleArtifactRequest handles all artifact related requests, will return true if request was handled here -func (a *Handlers) HandleArtifactRequest(host string, w http.ResponseWriter, r *http.Request) bool { - // In the event a host is prefixed with the artifact prefix an artifact +func (h *Handlers) HandleArtifactRequest(host string, w http.ResponseWriter, r *http.Request) bool { + // In the event h host is prefixed with the artifact prefix an artifact // value is created, and an attempt to proxy the request is made // Always try to add token to the request if it exists - token, err := a.Auth.GetTokenIfExists(w, r) + token, err := h.Auth.GetTokenIfExists(w, r) if err != nil { return true } // nolint: bodyclose - // a.checkIfLoginRequiredOrInvalidToken returns a response.Body, closing this body is responsibility + // h.checkIfLoginRequiredOrInvalidToken returns h response.Body, closing this body is responsibility // of the TryMakeRequest implementation - return a.Artifact.TryMakeRequest(host, w, r, token, a.checkIfLoginRequiredOrInvalidToken(w, r, token)) + return h.Artifact.TryMakeRequest(host, w, r, token, h.checkIfLoginRequiredOrInvalidToken(w, r, token)) } |