Move SSD remote endpoint normalization to typed Kotlin
ober
3ba8a71404c82659157a5a2f7043b732ab67af1d
--- a/templates/ssd-review.ss +++ b/templates/ssd-review.ss @@ -543,7 +543,23 @@ (typed-kotlin-file "com/sfb/ssdreview/NetworkSafety.kt" (typed-library (com sfb ssdreview) - (export localTruthHttpAllowed) + (export normalizeRemoteEndpointValue localTruthHttpAllowed) + (def (normalizeRemoteEndpointValue (value : String) (defaultEndpoint : String)) : String + (let ((trimmed (string-trim value))) + (let ((base (if (string-blank? trimmed) defaultEndpoint trimmed))) + (let ((withScheme + (if (string-starts-with? base "https://") + base + (string-append "https://" base)))) + (let ((endpoint + (string-replace-regex + withScheme + "10\\.66\\.69\\.2" + "10.66.60.2"))) + (let ((stripped (string-replace-regex endpoint "/+$" ""))) + (if (string-ends-with? stripped "/api") + endpoint + (string-append stripped "/api")))))))) (def (localTruthHttpAllowed (host : String)) : Bool (let ((lower (string-lowercase host))) (if (or (or (equal? lower "localhost") @@ -4174,12 +4190,7 @@ " }" "" " private fun normalizeRemoteEndpoint(value: String): String {" - " var endpoint = value.trim().ifBlank { DEFAULT_TRUTH_API_URL }" - " if (!endpoint.startsWith(\"https://\")) {" - " endpoint = \"https://$endpoint\"" - " }" - " endpoint = endpoint.replace(\"10.66.69.2\", \"10.66.60.2\")" - " return if (endpoint.trimEnd('/').endsWith(\"/api\")) endpoint else endpoint.trimEnd('/') + \"/api\"" + " return normalizeRemoteEndpointValue(value, DEFAULT_TRUTH_API_URL)" " }" "" " private fun remoteUrl(relative: String): URL {"