<lambda>null1 // Copyright 2015-present 650 Industries. All rights reserved.
2 package host.exp.exponent.kernel
3 
4 import android.net.Uri
5 import expo.modules.jsonutils.require
6 import host.exp.exponent.Constants
7 import okhttp3.Request
8 import org.json.JSONObject
9 
10 object ExponentUrls {
11   private val HTTPS_HOSTS = setOf(
12     "exp.host",
13     "exponentjs.com",
14     "u.expo.dev",
15     "staging-u.expo.dev"
16   )
17 
18   private fun isHttpsHost(host: String?): Boolean {
19     return HTTPS_HOSTS.contains(host)
20   }
21 
22   @JvmStatic fun toHttp(rawUrl: String): String {
23     if (rawUrl.startsWith("http")) {
24       return rawUrl
25     }
26     val uri = Uri.parse(rawUrl)
27     val useHttps = isHttpsHost(uri.host) || rawUrl.startsWith("exps")
28     return uri.buildUpon().scheme(if (useHttps) "https" else "http").build().toString()
29   }
30 
31   @JvmStatic fun addExponentHeadersToUrl(urlString: String): Request.Builder {
32     // TODO: set user agent
33     val builder = Request.Builder()
34       .url(urlString)
35       .header("Exponent-SDK-Version", Constants.SDK_VERSIONS)
36       .header("Exponent-Platform", "android")
37     val versionName = ExpoViewKernel.instance.versionName
38     if (versionName != null) {
39       builder.header("Exponent-Version", versionName)
40     }
41     return builder
42   }
43 
44   fun Request.Builder.addHeadersFromJSONObject(headers: JSONObject?): Request.Builder {
45     if (headers == null) {
46       return this
47     }
48 
49     headers.keys().asSequence().forEach { key ->
50       header(key, headers.require<Any>(key).toString())
51     }
52     return this
53   }
54 }
55