github.com/opcr-io/oras-go/v2@v2.0.0-20231122155130-eb4260d8a0ae/registry/remote/url.go (about) 1 /* 2 Copyright The ORAS Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package remote 17 18 import ( 19 "fmt" 20 "net/url" 21 "strings" 22 23 "github.com/opcr-io/oras-go/v2/registry" 24 ) 25 26 // buildScheme returns HTTP scheme used to access the remote registry. 27 func buildScheme(plainHTTP bool) string { 28 if plainHTTP { 29 return "http" 30 } 31 return "https" 32 } 33 34 // buildRegistryBaseURL builds the URL for accessing the base API. 35 // Format: <scheme>://<registry>/v2/ 36 // Reference: https://docs.docker.com/registry/spec/api/#base 37 func buildRegistryBaseURL(plainHTTP bool, ref registry.Reference) string { 38 return fmt.Sprintf("%s://%s/v2/", buildScheme(plainHTTP), ref.Host()) 39 } 40 41 // buildRegistryCatalogURL builds the URL for accessing the catalog API. 42 // Format: <scheme>://<registry>/v2/_catalog 43 // Reference: https://docs.docker.com/registry/spec/api/#catalog 44 func buildRegistryCatalogURL(plainHTTP bool, ref registry.Reference) string { 45 return fmt.Sprintf("%s://%s/v2/_catalog", buildScheme(plainHTTP), ref.Host()) 46 } 47 48 // buildRepositoryBaseURL builds the base endpoint of the remote repository. 49 // Format: <scheme>://<registry>/v2/<repository> 50 func buildRepositoryBaseURL(plainHTTP bool, ref registry.Reference) string { 51 return fmt.Sprintf("%s://%s/v2/%s", buildScheme(plainHTTP), ref.Host(), ref.Repository) 52 } 53 54 // buildRepositoryTagListURL builds the URL for accessing the tag list API. 55 // Format: <scheme>://<registry>/v2/<repository>/tags/list 56 // Reference: https://docs.docker.com/registry/spec/api/#tags 57 func buildRepositoryTagListURL(plainHTTP bool, ref registry.Reference) string { 58 return buildRepositoryBaseURL(plainHTTP, ref) + "/tags/list" 59 } 60 61 // buildRepositoryManifestURL builds the URL for accessing the manifest API. 62 // Format: <scheme>://<registry>/v2/<repository>/manifests/<digest_or_tag> 63 // Reference: https://docs.docker.com/registry/spec/api/#manifest 64 func buildRepositoryManifestURL(plainHTTP bool, ref registry.Reference) string { 65 return strings.Join([]string{ 66 buildRepositoryBaseURL(plainHTTP, ref), 67 "manifests", 68 ref.Reference, 69 }, "/") 70 } 71 72 // buildRepositoryBlobURL builds the URL for accessing the blob API. 73 // Format: <scheme>://<registry>/v2/<repository>/blobs/<digest> 74 // Reference: https://docs.docker.com/registry/spec/api/#blob 75 func buildRepositoryBlobURL(plainHTTP bool, ref registry.Reference) string { 76 return strings.Join([]string{ 77 buildRepositoryBaseURL(plainHTTP, ref), 78 "blobs", 79 ref.Reference, 80 }, "/") 81 } 82 83 // buildRepositoryBlobUploadURL builds the URL for blob uploading. 84 // Format: <scheme>://<registry>/v2/<repository>/blobs/uploads/ 85 // Reference: https://docs.docker.com/registry/spec/api/#initiate-blob-upload 86 func buildRepositoryBlobUploadURL(plainHTTP bool, ref registry.Reference) string { 87 return buildRepositoryBaseURL(plainHTTP, ref) + "/blobs/uploads/" 88 } 89 90 // buildReferrersURL builds the URL for querying the Referrers API. 91 // Format: <scheme>://<registry>/v2/<repository>/referrers/<digest>?artifactType=<artifactType> 92 // Reference: https://github.com/opencontainers/distribution-spec/blob/v1.1.0-rc1/spec.md#listing-referrers 93 func buildReferrersURL(plainHTTP bool, ref registry.Reference, artifactType string) string { 94 var query string 95 if artifactType != "" { 96 v := url.Values{} 97 v.Set("artifactType", artifactType) 98 query = "?" + v.Encode() 99 } 100 101 return fmt.Sprintf( 102 "%s/referrers/%s%s", 103 buildRepositoryBaseURL(plainHTTP, ref), 104 ref.Reference, 105 query, 106 ) 107 }