diff options
author | Nick Thomas <nick@gitlab.com> | 2019-09-13 18:46:52 +0300 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2019-09-13 18:46:52 +0300 |
commit | fc8f04d5fef6d36568f751e0ebb724a22959cb96 (patch) | |
tree | 4e80184def1dd2cca60c5cc2cd191fe9681bda9d | |
parent | 75d00b984b3edd2832f28a1fa397bd7ad9e4bbbd (diff) | |
parent | 1198f4ee2f3f216cde7b318505f7ebdfe8d7e338 (diff) |
Merge branch 'backstage/gb/rename-confusig-domain-struct' into 'master'1-9-stable
Rename confusing domain struct
See merge request gitlab-org/gitlab-pages!184
-rw-r--r-- | app.go | 8 | ||||
-rw-r--r-- | internal/domain/domain.go | 46 | ||||
-rw-r--r-- | internal/domain/domain_test.go | 50 | ||||
-rw-r--r-- | internal/domain/map.go | 8 | ||||
-rw-r--r-- | internal/logging/logging_test.go | 6 | ||||
-rw-r--r-- | internal/request/request.go | 6 | ||||
-rw-r--r-- | internal/request/request_test.go | 6 |
7 files changed, 65 insertions, 65 deletions
@@ -59,7 +59,7 @@ func (a *theApp) isReady() bool { return a.dm != nil } -func (a *theApp) domain(host string) *domain.D { +func (a *theApp) domain(host string) *domain.Domain { host = strings.ToLower(host) a.lock.RLock() defer a.lock.RUnlock() @@ -97,7 +97,7 @@ func (a *theApp) redirectToHTTPS(w http.ResponseWriter, r *http.Request, statusC http.Redirect(w, r, u.String(), statusCode) } -func (a *theApp) getHostAndDomain(r *http.Request) (host string, domain *domain.D) { +func (a *theApp) getHostAndDomain(r *http.Request) (host string, domain *domain.Domain) { host, _, err := net.SplitHostPort(r.Host) if err != nil { host = r.Host @@ -106,7 +106,7 @@ func (a *theApp) getHostAndDomain(r *http.Request) (host string, domain *domain. return host, a.domain(host) } -func (a *theApp) checkAuthenticationIfNotExists(domain *domain.D, w http.ResponseWriter, r *http.Request) bool { +func (a *theApp) checkAuthenticationIfNotExists(domain *domain.Domain, w http.ResponseWriter, r *http.Request) bool { if domain == nil || !domain.HasProject(r) { // Only if auth is supported @@ -132,7 +132,7 @@ func (a *theApp) checkAuthenticationIfNotExists(domain *domain.D, w http.Respons return false } -func (a *theApp) tryAuxiliaryHandlers(w http.ResponseWriter, r *http.Request, https bool, host string, domain *domain.D) bool { +func (a *theApp) tryAuxiliaryHandlers(w http.ResponseWriter, r *http.Request, https bool, host string, domain *domain.Domain) bool { // short circuit content serving to check for a status page if r.RequestURI == a.appConfig.StatusPath { a.healthCheck(w, r, https) diff --git a/internal/domain/domain.go b/internal/domain/domain.go index eeb5519d..2f499ff9 100644 --- a/internal/domain/domain.go +++ b/internal/domain/domain.go @@ -45,8 +45,8 @@ type project struct { ID uint64 } -// D is a domain that gitlab-pages can serve. -type D struct { +// Domain is a domain that gitlab-pages can serve. +type Domain struct { group // custom domains: @@ -59,7 +59,7 @@ type D struct { } // String implements Stringer. -func (d *D) String() string { +func (d *Domain) String() string { if d.group.name != "" && d.projectName != "" { return d.group.name + "/" + d.projectName } @@ -108,7 +108,7 @@ func handleGZip(w http.ResponseWriter, r *http.Request, fullPath string) string // Look up a project inside the domain based on the host and path. Returns the // project and its name (if applicable) -func (d *D) getProjectWithSubpath(r *http.Request) (*project, string, string) { +func (d *Domain) getProjectWithSubpath(r *http.Request) (*project, string, string) { // Check for a project specified in the URL: http://group.gitlab.io/projectA // If present, these projects shadow the group domain. split := strings.SplitN(r.URL.Path, "/", maxProjectDepth) @@ -132,7 +132,7 @@ func (d *D) getProjectWithSubpath(r *http.Request) (*project, string, string) { // IsHTTPSOnly figures out if the request should be handled with HTTPS // only by looking at group and project level config. -func (d *D) IsHTTPSOnly(r *http.Request) bool { +func (d *Domain) IsHTTPSOnly(r *http.Request) bool { if d == nil { return false } @@ -151,7 +151,7 @@ func (d *D) IsHTTPSOnly(r *http.Request) bool { } // IsAccessControlEnabled figures out if the request is to a project that has access control enabled -func (d *D) IsAccessControlEnabled(r *http.Request) bool { +func (d *Domain) IsAccessControlEnabled(r *http.Request) bool { if d == nil { return false } @@ -170,7 +170,7 @@ func (d *D) IsAccessControlEnabled(r *http.Request) bool { } // HasAcmeChallenge checks domain directory contains particular acme challenge -func (d *D) HasAcmeChallenge(token string) bool { +func (d *Domain) HasAcmeChallenge(token string) bool { if d == nil { return false } @@ -196,7 +196,7 @@ func (d *D) HasAcmeChallenge(token string) bool { } // IsNamespaceProject figures out if the request is to a namespace project -func (d *D) IsNamespaceProject(r *http.Request) bool { +func (d *Domain) IsNamespaceProject(r *http.Request) bool { if d == nil { return false } @@ -216,7 +216,7 @@ func (d *D) IsNamespaceProject(r *http.Request) bool { } // GetID figures out what is the ID of the project user tries to access -func (d *D) GetID(r *http.Request) uint64 { +func (d *Domain) GetID(r *http.Request) uint64 { if d == nil { return 0 } @@ -233,7 +233,7 @@ func (d *D) GetID(r *http.Request) uint64 { } // HasProject figures out if the project exists that the user tries to access -func (d *D) HasProject(r *http.Request) bool { +func (d *Domain) HasProject(r *http.Request) bool { if d == nil { return false } @@ -252,7 +252,7 @@ func (d *D) HasProject(r *http.Request) bool { // Detect file's content-type either by extension or mime-sniffing. // Implementation is adapted from Golang's `http.serveContent()` // See https://github.com/golang/go/blob/902fc114272978a40d2e65c2510a18e870077559/src/net/http/fs.go#L194 -func (d *D) detectContentType(path string) (string, error) { +func (d *Domain) detectContentType(path string) (string, error) { contentType := mime.TypeByExtension(filepath.Ext(path)) if contentType == "" { @@ -274,7 +274,7 @@ func (d *D) detectContentType(path string) (string, error) { return contentType, nil } -func (d *D) serveFile(w http.ResponseWriter, r *http.Request, origPath string) error { +func (d *Domain) serveFile(w http.ResponseWriter, r *http.Request, origPath string) error { fullPath := handleGZip(w, r, origPath) file, err := openNoFollow(fullPath) @@ -306,7 +306,7 @@ func (d *D) serveFile(w http.ResponseWriter, r *http.Request, origPath string) e return nil } -func (d *D) serveCustomFile(w http.ResponseWriter, r *http.Request, code int, origPath string) error { +func (d *Domain) serveCustomFile(w http.ResponseWriter, r *http.Request, code int, origPath string) error { fullPath := handleGZip(w, r, origPath) // Open and serve content of file @@ -340,7 +340,7 @@ func (d *D) serveCustomFile(w http.ResponseWriter, r *http.Request, code int, or // Resolve the HTTP request to a path on disk, converting requests for // directories to requests for index.html inside the directory if appropriate. -func (d *D) resolvePath(projectName string, subPath ...string) (string, error) { +func (d *Domain) resolvePath(projectName string, subPath ...string) (string, error) { publicPath := filepath.Join(d.group.name, projectName, "public") // Don't use filepath.Join as cleans the path, @@ -385,7 +385,7 @@ func (d *D) resolvePath(projectName string, subPath ...string) (string, error) { return fullPath, nil } -func (d *D) tryNotFound(w http.ResponseWriter, r *http.Request, projectName string) error { +func (d *Domain) tryNotFound(w http.ResponseWriter, r *http.Request, projectName string) error { page404, err := d.resolvePath(projectName, "404.html") if err != nil { return err @@ -398,7 +398,7 @@ func (d *D) tryNotFound(w http.ResponseWriter, r *http.Request, projectName stri return nil } -func (d *D) tryFile(w http.ResponseWriter, r *http.Request, projectName string, subPath ...string) error { +func (d *Domain) tryFile(w http.ResponseWriter, r *http.Request, projectName string, subPath ...string) error { fullPath, err := d.resolvePath(projectName, subPath...) if locationError, _ := err.(*locationDirectoryError); locationError != nil { @@ -427,7 +427,7 @@ func (d *D) tryFile(w http.ResponseWriter, r *http.Request, projectName string, return d.serveFile(w, r, fullPath) } -func (d *D) serveFileFromGroup(w http.ResponseWriter, r *http.Request) bool { +func (d *Domain) serveFileFromGroup(w http.ResponseWriter, r *http.Request) bool { project, projectName, subPath := d.getProjectWithSubpath(r) if project == nil { httperrors.Serve404(w) @@ -441,7 +441,7 @@ func (d *D) serveFileFromGroup(w http.ResponseWriter, r *http.Request) bool { return false } -func (d *D) serveNotFoundFromGroup(w http.ResponseWriter, r *http.Request) { +func (d *Domain) serveNotFoundFromGroup(w http.ResponseWriter, r *http.Request) { project, projectName, _ := d.getProjectWithSubpath(r) if project == nil { httperrors.Serve404(w) @@ -457,7 +457,7 @@ func (d *D) serveNotFoundFromGroup(w http.ResponseWriter, r *http.Request) { httperrors.Serve404(w) } -func (d *D) serveFileFromConfig(w http.ResponseWriter, r *http.Request) bool { +func (d *Domain) serveFileFromConfig(w http.ResponseWriter, r *http.Request) bool { // Try to serve file for http://host/... => /group/project/... if d.tryFile(w, r, d.projectName, r.URL.Path) == nil { return true @@ -466,7 +466,7 @@ func (d *D) serveFileFromConfig(w http.ResponseWriter, r *http.Request) bool { return false } -func (d *D) serveNotFoundFromConfig(w http.ResponseWriter, r *http.Request) { +func (d *Domain) serveNotFoundFromConfig(w http.ResponseWriter, r *http.Request) { // Try serving not found page for http://host/ => /group/project/404.html if d.tryNotFound(w, r, d.projectName) == nil { return @@ -477,7 +477,7 @@ func (d *D) serveNotFoundFromConfig(w http.ResponseWriter, r *http.Request) { } // EnsureCertificate parses the PEM-encoded certificate for the domain -func (d *D) EnsureCertificate() (*tls.Certificate, error) { +func (d *Domain) EnsureCertificate() (*tls.Certificate, error) { if d.config == nil { return nil, errors.New("tls certificates can be loaded only for pages with configuration") } @@ -494,7 +494,7 @@ func (d *D) EnsureCertificate() (*tls.Certificate, error) { } // ServeFileHTTP implements http.Handler. Returns true if something was served, false if not. -func (d *D) ServeFileHTTP(w http.ResponseWriter, r *http.Request) bool { +func (d *Domain) ServeFileHTTP(w http.ResponseWriter, r *http.Request) bool { if d == nil { httperrors.Serve404(w) return true @@ -508,7 +508,7 @@ func (d *D) ServeFileHTTP(w http.ResponseWriter, r *http.Request) bool { } // ServeNotFoundHTTP implements http.Handler. Serves the not found pages from the projects. -func (d *D) ServeNotFoundHTTP(w http.ResponseWriter, r *http.Request) { +func (d *Domain) ServeNotFoundHTTP(w http.ResponseWriter, r *http.Request) { if d == nil { httperrors.Serve404(w) return diff --git a/internal/domain/domain_test.go b/internal/domain/domain_test.go index 499510a8..d5db33c9 100644 --- a/internal/domain/domain_test.go +++ b/internal/domain/domain_test.go @@ -16,7 +16,7 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/testhelpers" ) -func serveFileOrNotFound(domain *D) http.HandlerFunc { +func serveFileOrNotFound(domain *Domain) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if !domain.ServeFileHTTP(w, r) { domain.ServeNotFoundHTTP(w, r) @@ -25,7 +25,7 @@ func serveFileOrNotFound(domain *D) http.HandlerFunc { } func testGroupServeHTTPHost(t *testing.T, host string) { - testGroup := &D{ + testGroup := &Domain{ projectName: "", group: group{ name: "group", @@ -79,7 +79,7 @@ func TestDomainServeHTTP(t *testing.T) { cleanup := setUpTests(t) defer cleanup() - testDomain := &D{ + testDomain := &Domain{ group: group{name: "group"}, projectName: "project2", config: &domainConfig{ @@ -101,13 +101,13 @@ func TestDomainServeHTTP(t *testing.T) { func TestIsHTTPSOnly(t *testing.T) { tests := []struct { name string - domain *D + domain *Domain url string expected bool }{ { name: "Custom domain with HTTPS-only enabled", - domain: &D{ + domain: &Domain{ group: group{name: "group"}, projectName: "project", config: &domainConfig{HTTPSOnly: true}, @@ -117,7 +117,7 @@ func TestIsHTTPSOnly(t *testing.T) { }, { name: "Custom domain with HTTPS-only disabled", - domain: &D{ + domain: &Domain{ group: group{name: "group"}, projectName: "project", config: &domainConfig{HTTPSOnly: false}, @@ -127,7 +127,7 @@ func TestIsHTTPSOnly(t *testing.T) { }, { name: "Default group domain with HTTPS-only enabled", - domain: &D{ + domain: &Domain{ projectName: "project", group: group{ name: "group", @@ -139,7 +139,7 @@ func TestIsHTTPSOnly(t *testing.T) { }, { name: "Default group domain with HTTPS-only disabled", - domain: &D{ + domain: &Domain{ projectName: "project", group: group{ name: "group", @@ -151,7 +151,7 @@ func TestIsHTTPSOnly(t *testing.T) { }, { name: "Case-insensitive default group domain with HTTPS-only enabled", - domain: &D{ + domain: &Domain{ projectName: "project", group: group{ name: "group", @@ -163,7 +163,7 @@ func TestIsHTTPSOnly(t *testing.T) { }, { name: "Other group domain with HTTPS-only enabled", - domain: &D{ + domain: &Domain{ projectName: "project", group: group{ name: "group", @@ -175,7 +175,7 @@ func TestIsHTTPSOnly(t *testing.T) { }, { name: "Other group domain with HTTPS-only disabled", - domain: &D{ + domain: &Domain{ projectName: "project", group: group{ name: "group", @@ -187,7 +187,7 @@ func TestIsHTTPSOnly(t *testing.T) { }, { name: "Unknown project", - domain: &D{ + domain: &Domain{ group: group{name: "group"}, projectName: "project", }, @@ -210,13 +210,13 @@ func TestHasAcmeChallenge(t *testing.T) { tests := []struct { name string - domain *D + domain *Domain token string expected bool }{ { name: "Project containing acme challenge", - domain: &D{ + domain: &Domain{ group: group{name: "group.acme"}, projectName: "with.acme.challenge", config: &domainConfig{HTTPSOnly: true}, @@ -226,7 +226,7 @@ func TestHasAcmeChallenge(t *testing.T) { }, { name: "Project containing acme challenge", - domain: &D{ + domain: &Domain{ group: group{name: "group.acme"}, projectName: "with.acme.challenge", config: &domainConfig{HTTPSOnly: true}, @@ -236,7 +236,7 @@ func TestHasAcmeChallenge(t *testing.T) { }, { name: "Project containing another token", - domain: &D{ + domain: &Domain{ group: group{name: "group.acme"}, projectName: "with.acme.challenge", config: &domainConfig{HTTPSOnly: true}, @@ -252,7 +252,7 @@ func TestHasAcmeChallenge(t *testing.T) { }, { name: "Domain without config", - domain: &D{ + domain: &Domain{ group: group{name: "group.acme"}, projectName: "with.acme.challenge", config: nil, @@ -299,7 +299,7 @@ func TestGroupServeHTTPGzip(t *testing.T) { cleanup := setUpTests(t) defer cleanup() - testGroup := &D{ + testGroup := &Domain{ projectName: "", group: group{ name: "group", @@ -366,7 +366,7 @@ func TestGroup404ServeHTTP(t *testing.T) { cleanup := setUpTests(t) defer cleanup() - testGroup := &D{ + testGroup := &Domain{ projectName: "", group: group{ name: "group.404", @@ -395,7 +395,7 @@ func TestDomain404ServeHTTP(t *testing.T) { cleanup := setUpTests(t) defer cleanup() - testDomain := &D{ + testDomain := &Domain{ group: group{name: "group.404"}, projectName: "domain.404", config: &domainConfig{ @@ -411,7 +411,7 @@ func TestPredefined404ServeHTTP(t *testing.T) { cleanup := setUpTests(t) defer cleanup() - testDomain := &D{ + testDomain := &Domain{ group: group{name: "group"}, } @@ -419,7 +419,7 @@ func TestPredefined404ServeHTTP(t *testing.T) { } func TestGroupCertificate(t *testing.T) { - testGroup := &D{ + testGroup := &Domain{ group: group{name: "group"}, projectName: "", } @@ -430,7 +430,7 @@ func TestGroupCertificate(t *testing.T) { } func TestDomainNoCertificate(t *testing.T) { - testDomain := &D{ + testDomain := &Domain{ group: group{name: "group"}, projectName: "project2", config: &domainConfig{ @@ -448,7 +448,7 @@ func TestDomainNoCertificate(t *testing.T) { } func TestDomainCertificate(t *testing.T) { - testDomain := &D{ + testDomain := &Domain{ group: group{name: "group"}, projectName: "project2", config: &domainConfig{ @@ -467,7 +467,7 @@ func TestCacheControlHeaders(t *testing.T) { cleanup := setUpTests(t) defer cleanup() - testGroup := &D{ + testGroup := &Domain{ group: group{ name: "group", projects: map[string]*project{ diff --git a/internal/domain/map.go b/internal/domain/map.go index 2891a272..7934860b 100644 --- a/internal/domain/map.go +++ b/internal/domain/map.go @@ -16,11 +16,11 @@ import ( ) // Map maps domain names to D instances. -type Map map[string]*D +type Map map[string]*Domain type domainsUpdater func(Map) -func (dm Map) updateDomainMap(domainName string, domain *D) { +func (dm Map) updateDomainMap(domainName string, domain *Domain) { if old, ok := dm[domainName]; ok { log.WithFields(log.Fields{ "domain_name": domainName, @@ -35,7 +35,7 @@ func (dm Map) updateDomainMap(domainName string, domain *D) { } func (dm Map) addDomain(rootDomain, groupName, projectName string, config *domainConfig) { - newDomain := &D{ + newDomain := &Domain{ group: group{name: groupName}, projectName: projectName, config: config, @@ -51,7 +51,7 @@ func (dm Map) updateGroupDomain(rootDomain, groupName, projectPath string, https groupDomain := dm[domainName] if groupDomain == nil { - groupDomain = &D{ + groupDomain = &Domain{ group: group{ name: groupName, projects: make(projects), diff --git a/internal/logging/logging_test.go b/internal/logging/logging_test.go index 8b49a413..32bd4603 100644 --- a/internal/logging/logging_test.go +++ b/internal/logging/logging_test.go @@ -15,19 +15,19 @@ func TestGetExtraLogFields(t *testing.T) { name string https bool host string - domain *domain.D + domain *domain.Domain }{ { name: "https", https: true, host: "githost.io", - domain: &domain.D{}, + domain: &domain.Domain{}, }, { name: "http", https: false, host: "githost.io", - domain: &domain.D{}, + domain: &domain.Domain{}, }, { name: "no_domain", diff --git a/internal/request/request.go b/internal/request/request.go index 730eb527..74982f33 100644 --- a/internal/request/request.go +++ b/internal/request/request.go @@ -28,7 +28,7 @@ func IsHTTPS(r *http.Request) bool { } // WithHostAndDomain saves host name and domain in the request's context -func WithHostAndDomain(r *http.Request, host string, domain *domain.D) *http.Request { +func WithHostAndDomain(r *http.Request, host string, domain *domain.Domain) *http.Request { ctx := r.Context() ctx = context.WithValue(ctx, ctxHostKey, host) ctx = context.WithValue(ctx, ctxDomainKey, domain) @@ -42,6 +42,6 @@ func GetHost(r *http.Request) string { } // GetDomain extracts the domain from request's context -func GetDomain(r *http.Request) *domain.D { - return r.Context().Value(ctxDomainKey).(*domain.D) +func GetDomain(r *http.Request) *domain.Domain { + return r.Context().Value(ctxDomainKey).(*domain.Domain) } diff --git a/internal/request/request_test.go b/internal/request/request_test.go index afb40142..e092448b 100644 --- a/internal/request/request_test.go +++ b/internal/request/request_test.go @@ -41,17 +41,17 @@ func TestWithHostAndDomain(t *testing.T) { tests := []struct { name string host string - domain *domain.D + domain *domain.Domain }{ { name: "values", host: "gitlab.com", - domain: &domain.D{}, + domain: &domain.Domain{}, }, { name: "no_host", host: "", - domain: &domain.D{}, + domain: &domain.Domain{}, }, } for _, tt := range tests { |