...
1 package handlers
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7
8 "github.com/docker/distribution"
9 dcontext "github.com/docker/distribution/context"
10 "github.com/docker/distribution/registry/api/errcode"
11 v2 "github.com/docker/distribution/registry/api/v2"
12 "github.com/docker/distribution/registry/auth"
13 "github.com/opencontainers/go-digest"
14 )
15
16
17
18
19 type Context struct {
20
21 *App
22 context.Context
23
24
25
26 Repository distribution.Repository
27
28
29 RepositoryRemover distribution.RepositoryRemover
30
31
32
33
34 Errors errcode.Errors
35
36 urlBuilder *v2.URLBuilder
37
38
39
40
41 }
42
43
44
45 func (ctx *Context) Value(key interface{}) interface{} {
46 return ctx.Context.Value(key)
47 }
48
49 func getName(ctx context.Context) (name string) {
50 return dcontext.GetStringValue(ctx, "vars.name")
51 }
52
53 func getReference(ctx context.Context) (reference string) {
54 return dcontext.GetStringValue(ctx, "vars.reference")
55 }
56
57 var errDigestNotAvailable = fmt.Errorf("digest not available in context")
58
59 func getDigest(ctx context.Context) (dgst digest.Digest, err error) {
60 dgstStr := dcontext.GetStringValue(ctx, "vars.digest")
61
62 if dgstStr == "" {
63 dcontext.GetLogger(ctx).Errorf("digest not available")
64 return "", errDigestNotAvailable
65 }
66
67 d, err := digest.Parse(dgstStr)
68 if err != nil {
69 dcontext.GetLogger(ctx).Errorf("error parsing digest=%q: %v", dgstStr, err)
70 return "", err
71 }
72
73 return d, nil
74 }
75
76 func getUploadUUID(ctx context.Context) (uuid string) {
77 return dcontext.GetStringValue(ctx, "vars.uuid")
78 }
79
80
81
82 func getUserName(ctx context.Context, r *http.Request) string {
83 username := dcontext.GetStringValue(ctx, auth.UserNameKey)
84
85
86 if username == "" {
87 var ok bool
88 uname, _, ok := basicAuth(r)
89 if ok {
90 username = uname
91 }
92 }
93
94 return username
95 }
96
View as plain text