From 619e5d24261aab903e66b93df9e4b184b27dc040 Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Mon, 29 Jul 2024 14:01:30 +0400 Subject: [PATCH 1/2] feat: add Content-Type Signed-off-by: knqyf263 --- registry/router.go | 7 +++++-- registry/server_test.go | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/registry/router.go b/registry/router.go index 9ff780b..ee56ea4 100644 --- a/registry/router.go +++ b/registry/router.go @@ -2,6 +2,7 @@ package registry import ( "context" + "encoding/json" "fmt" "io" "net/http" @@ -72,12 +73,14 @@ func (s *registryRouter) manifestHandler(ctx context.Context, w http.ResponseWri return errdefs.NotFound(xerrors.Errorf("unknown image: %s", filePath)) } - b, err := img.RawManifest() + m, err := img.Manifest() if err != nil { return errdefs.Unavailable(err) } - if _, err = w.Write(b); err != nil { + w.Header().Set("Content-Type", string(m.MediaType)) + w.WriteHeader(http.StatusOK) + if err = json.NewEncoder(w).Encode(m); err != nil { return errdefs.Unavailable(err) } return nil diff --git a/registry/server_test.go b/registry/server_test.go index a97d493..0f1d930 100644 --- a/registry/server_test.go +++ b/registry/server_test.go @@ -107,8 +107,9 @@ func TestNewDockerRegistry_manifestHandler(t *testing.T) { "v2/alpine:ref123": "testdata/alpine/alpine.tar", }, }, - expectedStatusCode: http.StatusOK, - expectedResponseBody: `{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","size":1512,"digest":"sha256:af341ccd2df8b0e2d67cf8dd32e087bfda4e5756ebd1c76bbf3efa0dc246590e"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":3029607,"digest":"sha256:988beb990993123f9c14951440e468cb469f9f1f4fe512fd9095b48f9c9e7130"}]}`, + expectedStatusCode: http.StatusOK, + expectedResponseBody: `{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","size":1512,"digest":"sha256:af341ccd2df8b0e2d67cf8dd32e087bfda4e5756ebd1c76bbf3efa0dc246590e"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":3029607,"digest":"sha256:988beb990993123f9c14951440e468cb469f9f1f4fe512fd9095b48f9c9e7130"}]} +`, }, { name: "sad path, image not found", From 6f64974b394412a6b4de46ab874e16fff02d941f Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Mon, 29 Jul 2024 14:26:02 +0400 Subject: [PATCH 2/2] fix: use json.Marshal Signed-off-by: knqyf263 --- registry/router.go | 8 +++++++- registry/server_test.go | 5 ++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/registry/router.go b/registry/router.go index ee56ea4..88b84b1 100644 --- a/registry/router.go +++ b/registry/router.go @@ -80,7 +80,13 @@ func (s *registryRouter) manifestHandler(ctx context.Context, w http.ResponseWri w.Header().Set("Content-Type", string(m.MediaType)) w.WriteHeader(http.StatusOK) - if err = json.NewEncoder(w).Encode(m); err != nil { + + // Use json.Marshal instead of json.NewEncoder to avoid writing a newline + b, err := json.Marshal(m) + if err != nil { + return errdefs.Unavailable(err) + } + if _, err = w.Write(b); err != nil { return errdefs.Unavailable(err) } return nil diff --git a/registry/server_test.go b/registry/server_test.go index 0f1d930..a97d493 100644 --- a/registry/server_test.go +++ b/registry/server_test.go @@ -107,9 +107,8 @@ func TestNewDockerRegistry_manifestHandler(t *testing.T) { "v2/alpine:ref123": "testdata/alpine/alpine.tar", }, }, - expectedStatusCode: http.StatusOK, - expectedResponseBody: `{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","size":1512,"digest":"sha256:af341ccd2df8b0e2d67cf8dd32e087bfda4e5756ebd1c76bbf3efa0dc246590e"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":3029607,"digest":"sha256:988beb990993123f9c14951440e468cb469f9f1f4fe512fd9095b48f9c9e7130"}]} -`, + expectedStatusCode: http.StatusOK, + expectedResponseBody: `{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","size":1512,"digest":"sha256:af341ccd2df8b0e2d67cf8dd32e087bfda4e5756ebd1c76bbf3efa0dc246590e"},"layers":[{"mediaType":"application/vnd.docker.image.rootfs.diff.tar.gzip","size":3029607,"digest":"sha256:988beb990993123f9c14951440e468cb469f9f1f4fe512fd9095b48f9c9e7130"}]}`, }, { name: "sad path, image not found",