-
Notifications
You must be signed in to change notification settings - Fork 13
/
shimreg.go
154 lines (134 loc) · 4.29 KB
/
shimreg.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
)
type Mapping struct {
Method string
Regexp *regexp.Regexp
Handler func(http.ResponseWriter, *http.Request, [][]string)
}
type Handler struct {
OutDir string
RegFormat bool
Mappings []*Mapping
}
func (h *Handler) GetPing(w http.ResponseWriter, r *http.Request, p [][]string) {
w.Header().Add("X-Docker-Registry-Version", "0.0.1")
w.WriteHeader(200)
fmt.Fprint(w, "pong")
}
func (h *Handler) GetImageJson(w http.ResponseWriter, r *http.Request, p [][]string) {
w.Header().Add("Content-Type", "application/json")
idPrefix := p[0][2]
layerLock.Lock()
defer layerLock.Unlock()
// pretend we have everything but the layer we're trying to save
if strings.HasPrefix(layerId, idPrefix) {
w.WriteHeader(http.StatusNotFound)
} else {
w.WriteHeader(http.StatusOK)
w.Write([]byte("{}"))
}
}
func (h *Handler) PutImageResource(w http.ResponseWriter, r *http.Request, p [][]string) {
imageId := p[0][2]
resourceName := p[0][3]
layerLock.Lock()
defer layerLock.Unlock()
if imageId != layerId {
logger.Error("Client tried to push layer %s, rejecting", imageId)
w.WriteHeader(http.StatusForbidden)
return
}
if !h.RegFormat {
logger.Debug("Considering %s for special handling")
// Specifically skipping "checksum" here, but any new junk as well
if resourceName != "layer" && resourceName != "json" {
w.WriteHeader(http.StatusOK)
return
}
if resourceName == "layer" {
resourceName = "layer.tar"
}
}
path := filepath.Join(h.OutDir, layerId, resourceName)
logger.Info("Writing file: %s", filepath.Base(path))
out, err := os.Create(path)
if err == nil {
defer out.Close()
cnt, err := io.Copy(out, r.Body)
if err == nil {
logger.Debug("Wrote %d bytes", cnt)
}
}
if err != nil {
logger.Error("%s", err.Error())
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
func (h *Handler) PutRepository(w http.ResponseWriter, r *http.Request, p [][]string) {
w.Header().Add("X-Docker-Endpoints", r.Host)
w.Header().Add("WWW-Authenticate", `Token signature=123abc,repository="dynport/test",access=write`)
w.Header().Add("X-Docker-Token", "token")
w.WriteHeader(http.StatusOK)
}
func (h *Handler) Map(t, re string, f func(http.ResponseWriter, *http.Request, [][]string)) {
if h.Mappings == nil {
h.Mappings = make([]*Mapping, 0)
}
h.Mappings = append(h.Mappings, &Mapping{t, regexp.MustCompile("/v(\\d+)/" + re), f})
}
func (h *Handler) doHandle(w http.ResponseWriter, r *http.Request) (ok bool) {
for _, mapping := range h.Mappings {
if r.Method != mapping.Method {
continue
}
if res := mapping.Regexp.FindAllStringSubmatch(r.URL.String(), -1); len(res) > 0 {
mapping.Handler(w, r, res)
r.Body.Close()
return true
}
}
r.Body.Close()
return false
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
logger.Debug("Got request %s %s", r.Method, r.URL.String())
if ok := h.doHandle(w, r); !ok {
logger.Debug("Returning 404")
http.NotFound(w, r)
}
}
func DummyResponse(status int) func(http.ResponseWriter, *http.Request, [][]string) {
return func(w http.ResponseWriter, r *http.Request, p [][]string) {
logger.Debug("Ignoring request, returning %d", status)
w.WriteHeader(status)
}
}
func NewHandler(outDir string, regFormat bool) (handler *Handler) {
handler = &Handler{OutDir: outDir, RegFormat: regFormat}
// this isn't a full registry
handler.Map("GET", "users", DummyResponse(http.StatusNotImplemented))
handler.Map("GET", "images/(.*?)/ancestry", DummyResponse(http.StatusNotImplemented))
handler.Map("GET", "images/(.*?)/layer", DummyResponse(http.StatusNotImplemented))
handler.Map("GET", "repositories/(.*?)/tags", DummyResponse(http.StatusNotImplemented))
handler.Map("GET", "repositories/(.*?)/images", DummyResponse(http.StatusNotImplemented))
handler.Map("PUT", "repositories/(.*?)/tags/(.*)", DummyResponse(http.StatusOK))
handler.Map("PUT", "repositories/(.*?)/images", DummyResponse(http.StatusNoContent))
// dummies
handler.Map("GET", "_ping", handler.GetPing)
// images
handler.Map("GET", "images/(.*?)/json", handler.GetImageJson)
handler.Map("PUT", "images/(.*?)/(.*)", handler.PutImageResource)
// repositories
handler.Map("PUT", "repositories/(.*?)/$", handler.PutRepository)
return
}