1 // Copyright 2015-present 650 Industries. All rights reserved.
2 package host.exp.exponent.kernel
3 
4 import android.net.Uri
5 import android.os.Build
6 import host.exp.exponent.Constants
7 import okhttp3.Request
8 
9 object ExponentUrls {
10   private val HTTPS_HOSTS = setOf(
11     "exp.host",
12     "exponentjs.com"
13   )
14 
15   private fun isHttpsHost(host: String?): Boolean {
16     return HTTPS_HOSTS.contains(host)
17   }
18 
19   @JvmStatic fun toHttp(rawUrl: String): String {
20     if (rawUrl.startsWith("http")) {
21       return rawUrl
22     }
23     val uri = Uri.parse(rawUrl)
24     val useHttps = isHttpsHost(uri.host) || rawUrl.startsWith("exps")
25     return uri.buildUpon().scheme(if (useHttps) "https" else "http").build().toString()
26   }
27 
28   @JvmStatic fun addExponentHeadersToUrl(urlString: String): Request.Builder {
29     // TODO: set user agent
30     val builder = Request.Builder()
31       .url(urlString)
32       .header("Exponent-SDK-Version", Constants.SDK_VERSIONS)
33       .header("Exponent-Platform", "android")
34     val versionName = ExpoViewKernel.instance.versionName
35     if (versionName != null) {
36       builder.header("Exponent-Version", versionName)
37     }
38     return builder
39   }
40 
41   @JvmStatic fun addExponentHeadersToManifestUrl(
42     urlString: String,
43     isShellAppManifest: Boolean,
44     sessionSecret: String?
45   ): Request.Builder {
46     val builder = addExponentHeadersToUrl(urlString)
47       .header("Accept", "application/expo+json,application/json")
48     if (KernelConfig.FORCE_UNVERSIONED_PUBLISHED_EXPERIENCES) {
49       builder.header("Exponent-SDK-Version", "UNVERSIONED")
50     }
51     val clientEnvironment: String = if (isShellAppManifest) {
52       builder.header("Expo-Release-Channel", Constants.RELEASE_CHANNEL)
53       "STANDALONE"
54     } else {
55       if (Build.FINGERPRINT.contains("vbox") || Build.FINGERPRINT.contains("generic")) "EXPO_SIMULATOR" else "EXPO_DEVICE"
56     }
57     builder.header("Expo-Api-Version", "1")
58       .header("Expo-Client-Environment", clientEnvironment)
59     if (sessionSecret != null) {
60       builder.header("Expo-Session", sessionSecret)
61     }
62     return builder
63   }
64 }
65