Skip to content

Commit

Permalink
Cleanup function order in OciRepositoryHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Jul 16, 2024
1 parent e1e5a4a commit c32dbfc
Showing 1 changed file with 75 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,81 +104,6 @@ internal class OciRepositoryHandler(
}
}

private fun getOrHeadMetadata(
registryUri: URI,
segments: List<String>,
isGet: Boolean,
response: HttpServerResponse,
): Publisher<Void> {
if (segments.size != 9) {
return response.sendNotFound()
}
val imageReference = try {
segments[4].unescapePathSegment().toOciImageReference()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val digest = try {
segments[5].toOciDigest()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val size = try {
segments[6].toInt()
} catch (e: NumberFormatException) {
return response.sendBadRequest()
}
val platform = try {
segments[7].toPlatform()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val metadataJsonMono =
getMultiArchImageMetadata(registryUri, imageReference, digest, size, credentials).handle { it, sink ->
val metadata = it.platformToMetadata[platform]
if (metadata == null) {
response.status(400)
} else {
sink.next(metadata.encodeToJsonString().toByteArray())
}
}
response.header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON)
return response.sendByteArray(metadataJsonMono, isGet)
}

private fun getOrHeadLayer(
registryUri: URI,
segments: List<String>,
isGet: Boolean,
response: HttpServerResponse,
): Publisher<Void> {
if (segments.size != 8) {
return response.sendNotFound()
}
val imageName = try {
segments[4].unescapePathSegment()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val digest = try {
segments[5].toOciDigest()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val size = try {
segments[6].toLong()
} catch (e: NumberFormatException) {
return response.sendBadRequest()
}
response.header(HttpHeaderNames.CONTENT_LENGTH, size.toString())
response.header(HttpHeaderNames.ETAG, digest.encodedHash)
return if (isGet) {
getLayer(registryUri, imageName, digest, size, credentials, response)
} else {
headLayer(registryUri, imageName, digest, credentials, response)
}
}

private fun getOrHeadGradleModuleMetadata(
registryUri: URI,
mappedComponent: MappedComponent,
Expand Down Expand Up @@ -280,6 +205,81 @@ internal class OciRepositoryHandler(
return response.sendByteArray(moduleJsonMono, isGet)
}

private fun getOrHeadMetadata(
registryUri: URI,
segments: List<String>,
isGet: Boolean,
response: HttpServerResponse,
): Publisher<Void> {
if (segments.size != 9) {
return response.sendNotFound()
}
val imageReference = try {
segments[4].unescapePathSegment().toOciImageReference()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val digest = try {
segments[5].toOciDigest()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val size = try {
segments[6].toInt()
} catch (e: NumberFormatException) {
return response.sendBadRequest()
}
val platform = try {
segments[7].toPlatform()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val metadataJsonMono =
getMultiArchImageMetadata(registryUri, imageReference, digest, size, credentials).handle { it, sink ->
val metadata = it.platformToMetadata[platform]
if (metadata == null) {
response.status(400)
} else {
sink.next(metadata.encodeToJsonString().toByteArray())
}
}
response.header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON)
return response.sendByteArray(metadataJsonMono, isGet)
}

private fun getOrHeadLayer(
registryUri: URI,
segments: List<String>,
isGet: Boolean,
response: HttpServerResponse,
): Publisher<Void> {
if (segments.size != 8) {
return response.sendNotFound()
}
val imageName = try {
segments[4].unescapePathSegment()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val digest = try {
segments[5].toOciDigest()
} catch (e: IllegalArgumentException) {
return response.sendBadRequest()
}
val size = try {
segments[6].toLong()
} catch (e: NumberFormatException) {
return response.sendBadRequest()
}
response.header(HttpHeaderNames.CONTENT_LENGTH, size.toString())
response.header(HttpHeaderNames.ETAG, digest.encodedHash)
return if (isGet) {
getLayer(registryUri, imageName, digest, size, credentials, response)
} else {
headLayer(registryUri, imageName, digest, credentials, response)
}
}

private fun getMultiArchImageMetadata(
registryUri: URI,
imageReference: OciImageReference,
Expand Down

0 comments on commit c32dbfc

Please sign in to comment.