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     "u.expo.dev",
13     "staging-u.expo.dev"
14   )
15 
16   private fun isHttpsHost(host: String?): Boolean {
17     return HTTPS_HOSTS.contains(host)
18   }
19 
20   @JvmStatic fun toHttp(rawUrl: String): String {
21     if (rawUrl.startsWith("http")) {
22       return rawUrl
23     }
24     val uri = Uri.parse(rawUrl)
25     val useHttps = isHttpsHost(uri.host) || rawUrl.startsWith("exps")
26     return uri.buildUpon().scheme(if (useHttps) "https" else "http").build().toString()
27   }
28 
29   @JvmStatic fun addExponentHeadersToUrl(urlString: String): Request.Builder {
30     // TODO: set user agent
31     val builder = Request.Builder()
32       .url(urlString)
33       .header("Exponent-SDK-Version", Constants.SDK_VERSIONS)
34       .header("Exponent-Platform", "android")
35     val versionName = ExpoViewKernel.instance.versionName
36     if (versionName != null) {
37       builder.header("Exponent-Version", versionName)
38     }
39     return builder
40   }
41 }
42