Skip to content

Commit 419412a

Browse files
authored
Add ASCII sanitization utility for HTTP header compatibility (#1596)
* fix: sanitize header content * chore: Remove unnecessary comment Removes a redundant `/**/` comment from the `buildUserAgent` function in `HeadersUtil.kt`. * Refactor: Optimize header sanitization regex This commit optimizes the `sanitize` function in `HeadersUtil` by extracting the regex for non-ASCII characters into a pre-compiled `private val`. This avoids recompiling the regular expression on every invocation of the function, improving performance. Additionally, detailed KDoc has been added to the `sanitize` function to better explain its purpose and behavior, including examples of character decomposition.
1 parent b9c9ceb commit 419412a

File tree

1 file changed

+17
-1
lines changed
  • stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/header

1 file changed

+17
-1
lines changed

stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/header/HeadersUtil.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import io.getstream.video.android.core.BuildConfig
2222
import io.getstream.video.android.core.StreamVideo
2323
import io.getstream.video.android.core.StreamVideoClient
2424
import io.getstream.video.android.core.internal.InternalStreamVideoApi
25+
import java.text.Normalizer
2526

2627
@InternalStreamVideoApi
2728
class HeadersUtil {
@@ -32,6 +33,7 @@ class HeadersUtil {
3233
@InternalStreamVideoApi
3334
@JvmStatic
3435
public var VERSION_PREFIX_HEADER: VersionPrefixHeader = VersionPrefixHeader.Default
36+
private val NON_ASCII_REGEX = "[^\\p{ASCII}]".toRegex()
3537
}
3638

3739
/**
@@ -81,7 +83,7 @@ class HeadersUtil {
8183
append("|device_model=${Build.MANUFACTURER} ${Build.MODEL}")
8284
append(buildAppVersionForHeader())
8385
append(buildAppName()) // Assumes buildAppName() returns a properly formatted string
84-
}
86+
}.sanitize()
8587
}
8688

8789
private fun buildAppVersionForHeader() = (StreamVideo.instanceOrNull() as? StreamVideoClient)?.let { streamVideoImpl ->
@@ -96,4 +98,18 @@ class HeadersUtil {
9698
private fun getContext(): Context? {
9799
return StreamVideo.instanceOrNull()?.context
98100
}
101+
102+
/**
103+
* Sanitizes a string to contain only ASCII characters.
104+
*
105+
* Uses Unicode NFD (canonical decomposition) to decompose accented characters
106+
* into base characters and combining marks, then strips all non-ASCII characters.
107+
* For example, "café" becomes "cafe", "Müller" becomes "Muller".
108+
*
109+
* @return ASCII-only version of the string.
110+
*/
111+
private fun String.sanitize(): String {
112+
return Normalizer.normalize(this, Normalizer.Form.NFD)
113+
.replace(NON_ASCII_REGEX, "")
114+
}
99115
}

0 commit comments

Comments
 (0)