Welcome to mirror list, hosted at ThFree Co, Russian Federation.

request.go « request « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 74982f33f6cc492b0556bbf52ca8eaba3d934e15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package request

import (
	"context"
	"net/http"

	"gitlab.com/gitlab-org/gitlab-pages/internal/domain"
)

type ctxKey string

const (
	ctxHTTPSKey  ctxKey = "https"
	ctxHostKey   ctxKey = "host"
	ctxDomainKey ctxKey = "domain"
)

// WithHTTPSFlag saves https flag in request's context
func WithHTTPSFlag(r *http.Request, https bool) *http.Request {
	ctx := context.WithValue(r.Context(), ctxHTTPSKey, https)

	return r.WithContext(ctx)
}

// IsHTTPS restores https flag from request's context
func IsHTTPS(r *http.Request) bool {
	return r.Context().Value(ctxHTTPSKey).(bool)
}

// WithHostAndDomain saves host name and domain in the request's context
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)

	return r.WithContext(ctx)
}

// GetHost extracts the host from request's context
func GetHost(r *http.Request) string {
	return r.Context().Value(ctxHostKey).(string)
}

// GetDomain extracts the domain from request's context
func GetDomain(r *http.Request) *domain.Domain {
	return r.Context().Value(ctxDomainKey).(*domain.Domain)
}