Skip to content

Commit f72ba1a

Browse files
Fabtroncmaier
andauthored
Update user agent headers (Apps 727) (#182)
The old user agend header as been slighty adjustes and new headers have been added. The new format were taken from [iOS](https://github.com/snabble/snabble-ios-sdk/blob/12767c7dfda7d952007d2fa93c58dbab6131250a/Sources/Core/API/Snabble.swift#L626) Co-authored-by: Christian Maier <[email protected]>
1 parent 700e625 commit f72ba1a

File tree

5 files changed

+79
-45
lines changed

5 files changed

+79
-45
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
33

44
## UNRELEASED
55
### Added
6+
* core: add new and update existing user agent headers
67
### Changed
78
### Removed
89
### Fixed

core/src/main/java/io/snabble/sdk/OkHttpClientFactory.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@ package io.snabble.sdk
22

33
import android.app.Application
44
import androidx.annotation.RestrictTo
5-
import okhttp3.OkHttpClient
6-
import io.snabble.sdk.OkHttpLogger
7-
import io.snabble.sdk.Snabble
8-
import io.snabble.sdk.UserAgentInterceptor
9-
import io.snabble.sdk.OkHttpClientFactory
5+
import io.snabble.sdk.auth.useragent.UserAgentInterceptor
106
import io.snabble.sdk.utils.LetsEncryptCertHelper
117
import io.snabble.sdk.utils.Logger
128
import okhttp3.Cache
139
import okhttp3.CertificatePinner
14-
import okhttp3.Request
15-
import okhttp3.logging.HttpLoggingInterceptor
10+
import okhttp3.OkHttpClient
1611
import java.util.concurrent.TimeUnit
1712

1813
@RestrictTo(RestrictTo.Scope.LIBRARY)
@@ -62,4 +57,4 @@ internal object OkHttpClientFactory {
6257
LetsEncryptCertHelper.addLetsEncryptCertificatesForMarshmallowOrEarlier(builder)
6358
return builder.build()
6459
}
65-
}
60+
}

core/src/main/java/io/snabble/sdk/UserAgentInterceptor.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.snabble.sdk.auth.useragent
2+
3+
import android.content.Context
4+
import android.content.pm.PackageManager.NameNotFoundException
5+
import android.os.Build
6+
import io.snabble.sdk.BuildConfig
7+
import io.snabble.sdk.extensions.getPackageInfoCompat
8+
9+
/**
10+
* User agent header, example -> SnabbleSdkSample/1.0(1) Android/34 (Google;sdk_gphone64_arm64) SDK/dev
11+
*/
12+
fun Context.getUserAgentHeader(): String {
13+
val appDescription = "${getAppName()}/${getVersionNumber()}(${getBuildVersion()})"
14+
val osDescription = "Android/$ANDROID_VERSION"
15+
16+
return "$appDescription $osDescription ($HARDWARE_DESCRIPTOR) SDK/$SDK_VERSION"
17+
}
18+
19+
/**
20+
* HTTP headerFields using user agent keys defined in https://wicg.github.io/ua-client-hints/
21+
*
22+
* `Sec-CH-UA: "SnabbleSdkSample";v="1"`
23+
* `Sec-CH-UA-Full-Version-List: "SnabbleSdkSample";v="1.0-1","SDK";v="dev"`
24+
* `Sec-CH-UA-Platform: Android`
25+
* `Sec-CH-UA-Platform-Version: 34`
26+
* `Sec-CH-UA-Arch: Google;sdk_gphone64_arm64`
27+
*/
28+
fun Context.getHeaderFields(): Map<String, String> = mapOf(
29+
"Sec-CH-UA" to "\"${getAppName()}\";v=\"${getMajorVersion()}\"",
30+
"Sec-CH-UA-Full-Version-List" to "\"${getAppName()}\";v=\"${getVersionNumber()}-${getBuildVersion()}\",\"SDK\";v=\"$SDK_VERSION\"",
31+
"Sec-CH-UA-Platform" to "Android",
32+
"Sec-CH-UA-Platform-Version" to ANDROID_VERSION,
33+
"Sec-CH-UA-Arch" to HARDWARE_DESCRIPTOR
34+
)
35+
36+
private const val SDK_VERSION: String = BuildConfig.VERSION_NAME
37+
38+
private val ANDROID_VERSION: String = Build.VERSION.SDK_INT.toString()
39+
40+
private val HARDWARE_DESCRIPTOR: String = "${Build.MANUFACTURER};${Build.PRODUCT}"
41+
42+
private fun Context.getAppName(): String = packageManager.getApplicationLabel(applicationInfo).toString()
43+
44+
private fun Context.getBuildVersion(): String = try {
45+
@Suppress("DEPRECATION")
46+
packageManager.getPackageInfoCompat(applicationInfo.packageName)
47+
?.run { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) longVersionCode else versionCode }
48+
?.toString()
49+
?: "Unknown"
50+
} catch (ignored: NameNotFoundException) {
51+
"Unknown"
52+
}
53+
54+
private fun Context.getVersionNumber(): String = try {
55+
packageManager.getPackageInfoCompat(applicationInfo.packageName)?.versionName ?: "Unknown"
56+
} catch (ignored: NameNotFoundException) {
57+
"Unknown"
58+
}
59+
60+
private fun Context.getMajorVersion(): String = getVersionNumber()?.split(".")?.firstOrNull() ?: "Unknown"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.snabble.sdk.auth.useragent
2+
3+
import android.content.Context
4+
import okhttp3.Interceptor
5+
import okhttp3.Response
6+
7+
internal class UserAgentInterceptor(private val context: Context) : Interceptor {
8+
9+
override fun intercept(chain: Interceptor.Chain): Response =
10+
chain.proceed(chain.request().newBuilder()
11+
.addHeader("User-Agent", context.getUserAgentHeader())
12+
.apply { context.getHeaderFields().forEach { (key, value) -> addHeader(key, value) } }
13+
.build()
14+
)
15+
}

0 commit comments

Comments
 (0)