1 // Copyright 2015-present 650 Industries. All rights reserved.
2 package host.exp.exponent.kernel
3 
4 import android.net.Uri
5 import host.exp.exponent.Constants
6 import okhttp3.Request
7 
8 object ExponentUrls {
9   private val HTTPS_HOSTS = setOf(
10     "exp.host",
11     "exponentjs.com"
12   )
13 
14   private fun isHttpsHost(host: String?): Boolean {
15     return HTTPS_HOSTS.contains(host)
16   }
17 
18   @JvmStatic fun toHttp(rawUrl: String): String {
19     if (rawUrl.startsWith("http")) {
20       return rawUrl
21     }
22     val uri = Uri.parse(rawUrl)
23     val useHttps = isHttpsHost(uri.host) || rawUrl.startsWith("exps")
24     return uri.buildUpon().scheme(if (useHttps) "https" else "http").build().toString()
25   }
26 
27   @JvmStatic fun addExponentHeadersToUrl(urlString: String): Request.Builder {
28     // TODO: set user agent
29     val builder = Request.Builder()
30       .url(urlString)
31       .header("Exponent-SDK-Version", Constants.SDK_VERSIONS)
32       .header("Exponent-Platform", "android")
33     val versionName = ExpoViewKernel.instance.versionName
34     if (versionName != null) {
35       builder.header("Exponent-Version", versionName)
36     }
37     return builder
38   }
39 }
40