From 83f882b427e4e29f1dabededf1c309493788c84f Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Thu, 11 Dec 2014 21:59:59 -0800 Subject: [PATCH] Migrate webapp to use exported urls.URLBuilder package --- api_test.go | 36 ++++++++++++++++++++++-------------- app.go | 2 +- context.go | 3 ++- layerupload.go | 4 ++-- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/api_test.go b/api_test.go index d6cf34dd..e71edc17 100644 --- a/api_test.go +++ b/api_test.go @@ -14,6 +14,7 @@ import ( "testing" "github.com/docker/docker-registry/api/errors" + "github.com/docker/docker-registry/api/urls" "github.com/docker/docker-registry/common/testutil" "github.com/docker/docker-registry/configuration" "github.com/docker/docker-registry/digest" @@ -34,13 +35,13 @@ func TestCheckAPI(t *testing.T) { app := NewApp(config) server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app)) - builder, err := newURLBuilderFromString(server.URL) + builder, err := urls.NewURLBuilderFromString(server.URL) if err != nil { t.Fatalf("error creating url builder: %v", err) } - baseURL, err := builder.buildBaseURL() + baseURL, err := builder.BuildBaseURL() if err != nil { t.Fatalf("unexpected error building base url: %v", err) } @@ -81,7 +82,7 @@ func TestLayerAPI(t *testing.T) { app := NewApp(config) server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app)) - builder, err := newURLBuilderFromString(server.URL) + builder, err := urls.NewURLBuilderFromString(server.URL) if err != nil { t.Fatalf("error creating url builder: %v", err) @@ -98,7 +99,7 @@ func TestLayerAPI(t *testing.T) { // ----------------------------------- // Test fetch for non-existent content - layerURL, err := builder.buildLayerURL(imageName, layerDigest) + layerURL, err := builder.BuildBlobURL(imageName, layerDigest) if err != nil { t.Fatalf("error building url: %v", err) } @@ -121,7 +122,7 @@ func TestLayerAPI(t *testing.T) { // ------------------------------------------ // Upload a layer - layerUploadURL, err := builder.buildLayerUploadURL(imageName) + layerUploadURL, err := builder.BuildBlobUploadURL(imageName) if err != nil { t.Fatalf("error building upload url: %v", err) } @@ -196,7 +197,7 @@ func TestManifestAPI(t *testing.T) { app := NewApp(config) server := httptest.NewServer(handlers.CombinedLoggingHandler(os.Stderr, app)) - builder, err := newURLBuilderFromString(server.URL) + builder, err := urls.NewURLBuilderFromString(server.URL) if err != nil { t.Fatalf("unexpected error creating url builder: %v", err) } @@ -204,7 +205,7 @@ func TestManifestAPI(t *testing.T) { imageName := "foo/bar" tag := "thetag" - manifestURL, err := builder.buildManifestURL(imageName, tag) + manifestURL, err := builder.BuildManifestURL(imageName, tag) if err != nil { t.Fatalf("unexpected error getting manifest url: %v", err) } @@ -240,7 +241,7 @@ func TestManifestAPI(t *testing.T) { t.Fatalf("expected manifest unknown error: got %v", respErrs) } - tagsURL, err := builder.buildTagsURL(imageName) + tagsURL, err := builder.BuildTagsURL(imageName) if err != nil { t.Fatalf("unexpected error building tags url: %v", err) } @@ -427,8 +428,8 @@ func putManifest(t *testing.T, msg, url string, v interface{}) *http.Response { return resp } -func startPushLayer(t *testing.T, ub *urlBuilder, name string) string { - layerUploadURL, err := ub.buildLayerUploadURL(name) +func startPushLayer(t *testing.T, ub *urls.URLBuilder, name string) string { + layerUploadURL, err := ub.BuildBlobUploadURL(name) if err != nil { t.Fatalf("unexpected error building layer upload url: %v", err) } @@ -449,14 +450,21 @@ func startPushLayer(t *testing.T, ub *urlBuilder, name string) string { } // pushLayer pushes the layer content returning the url on success. -func pushLayer(t *testing.T, ub *urlBuilder, name string, dgst digest.Digest, uploadURLBase string, rs io.ReadSeeker) string { +func pushLayer(t *testing.T, ub *urls.URLBuilder, name string, dgst digest.Digest, uploadURLBase string, rs io.ReadSeeker) string { rsLength, _ := rs.Seek(0, os.SEEK_END) rs.Seek(0, os.SEEK_SET) - uploadURL := appendValues(uploadURLBase, url.Values{ + u, err := url.Parse(uploadURLBase) + if err != nil { + t.Fatalf("unexpected error parsing pushLayer url: %v", err) + } + + u.RawQuery = url.Values{ "digest": []string{dgst.String()}, "size": []string{fmt.Sprint(rsLength)}, - }) + }.Encode() + + uploadURL := u.String() // Just do a monolithic upload req, err := http.NewRequest("PUT", uploadURL, rs) @@ -472,7 +480,7 @@ func pushLayer(t *testing.T, ub *urlBuilder, name string, dgst digest.Digest, up checkResponse(t, "putting monolithic chunk", resp, http.StatusCreated) - expectedLayerURL, err := ub.buildLayerURL(name, dgst) + expectedLayerURL, err := ub.BuildBlobURL(name, dgst) if err != nil { t.Fatalf("error building expected layer url: %v", err) } diff --git a/app.go b/app.go index b34a77de..e1b299a7 100644 --- a/app.go +++ b/app.go @@ -115,7 +115,7 @@ func (app *App) dispatcher(dispatch dispatchFunc) http.Handler { context := &Context{ App: app, Name: vars["name"], - urlBuilder: newURLBuilderFromRequest(r), + urlBuilder: urls.NewURLBuilderFromRequest(r), } // Store vars for underlying handlers. diff --git a/context.go b/context.go index a1e47abe..23d669b8 100644 --- a/context.go +++ b/context.go @@ -3,6 +3,7 @@ package registry import ( "github.com/Sirupsen/logrus" "github.com/docker/docker-registry/api/errors" + "github.com/docker/docker-registry/api/urls" ) // Context should contain the request specific context for use in across @@ -28,5 +29,5 @@ type Context struct { // log provides a context specific logger. log *logrus.Entry - urlBuilder *urlBuilder + urlBuilder *urls.URLBuilder } diff --git a/layerupload.go b/layerupload.go index af8bd457..898b279b 100644 --- a/layerupload.go +++ b/layerupload.go @@ -151,7 +151,7 @@ func (luh *layerUploadHandler) CancelLayerUpload(w http.ResponseWriter, r *http. // chunk responses. This sets the correct headers but the response status is // left to the caller. func (luh *layerUploadHandler) layerUploadResponse(w http.ResponseWriter, r *http.Request) error { - uploadURL, err := luh.urlBuilder.forLayerUpload(luh.Upload) + uploadURL, err := luh.urlBuilder.BuildBlobUploadChunkURL(luh.Upload.Name(), luh.Upload.UUID()) if err != nil { logrus.Infof("error building upload url: %s", err) return err @@ -200,7 +200,7 @@ func (luh *layerUploadHandler) completeUpload(w http.ResponseWriter, r *http.Req return } - layerURL, err := luh.urlBuilder.forLayer(layer) + layerURL, err := luh.urlBuilder.BuildBlobURL(layer.Name(), layer.Digest()) if err != nil { luh.Errors.Push(errors.ErrorCodeUnknown, err) w.WriteHeader(http.StatusInternalServerError)