-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
94 lines (84 loc) · 2.51 KB
/
serve.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"github.com/JohanLindvall/diz/diz"
"github.com/JohanLindvall/diz/imagesource"
"github.com/JohanLindvall/diz/util"
)
func serve(zip string) error {
is, err := imagesource.NewZipImageSource(zip)
if err != nil {
return err
}
digestedTags, err := is.GetDigestToTags()
if err != nil {
return err
}
return http.ListenAndServe(":5000", &server{is: is, digestedTags: digestedTags})
}
type server struct {
is *imagesource.ZipImageSource
digestedTags map[string][]string
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.String())
if r.URL.Path == "/v2/" {
w.WriteHeader(http.StatusOK)
return
} else if r.URL.Path == "/v2/manifests" {
if js, err := json.MarshalIndent(s.is.Manifests(), "", " "); err == nil {
w.Write(js)
return
}
} else if match := blobRe.FindStringSubmatch(r.URL.Path); match != nil {
sum := match[2]
if repoTag, ok := s.digestedTags[sum]; ok && len(repoTag) > 0 {
if err := s.returnRegistryManifest(w, repoTag[0]); err == nil {
return
}
} else {
w.Header().Add("Content-Type", "application/octet-stream")
if err := s.is.WriteFileByHash(w, sum); err == nil {
return
}
}
} else if match := manifestRe.FindStringSubmatch(r.URL.Path); match != nil {
repoTag := match[1] + ":" + match[2]
if err := s.returnRegistryManifest(w, repoTag); err == nil {
return
}
} else if match := getRe.FindStringSubmatch(r.URL.Path); match != nil {
path := match[1]
if rdr, err := s.is.Read(path); err == nil && rdr != nil {
w.Header().Add("Content-Type", "application/octet-stream")
_, err = util.CopyAndClose(w, rdr)
return
}
}
w.WriteHeader(http.StatusNotFound)
}
func (s *server) returnRegistryManifest(w http.ResponseWriter, repoTag string) (err error) {
var repoManifest diz.RegistryManifest
if repoManifest, err = s.is.GetRegistryManifest(repoTag); err == nil {
err = sendDigestResponse(w, repoManifest)
}
return
}
func sendDigestResponse(w http.ResponseWriter, m diz.RegistryManifest) error {
if js, digest, err := diz.GetManifestBytes(m); err == nil {
w.Header().Set("Docker-Content-Digest", fmt.Sprintf("sha256:%x", digest))
w.Header().Set("Content-Type", m.MediaType)
_, err := w.Write(js)
return err
} else {
return err
}
}
var (
manifestRe = regexp.MustCompile("^/v2/([^/]+)/manifests/([^/]+)$")
blobRe = regexp.MustCompile("^/v2/[^/]+/(manifests|blobs)/sha256:([0-9a-f]{64})$")
getRe = regexp.MustCompile("^/get/(.*)$")
)