Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using OkHttp Interceptor: Redundant User Agents #3692

Closed
shalfknight opened this issue Jun 3, 2019 · 2 comments
Closed

Using OkHttp Interceptor: Redundant User Agents #3692

shalfknight opened this issue Jun 3, 2019 · 2 comments

Comments

@shalfknight
Copy link

Hi folks,

when specifying a particular user agent value in my http header via an OKHttp3 interceptor, e.g. with MY_CUSTOM_USER_AGENT_RIGHT_HERE, the user agent field seems to get mixed with the default http.agent system property defined in LazyHeaders.java:

String defaultUserAgent = System.getProperty("http.agent");

So the result looks as follows and thereby the request to my backend (infrastructure) starts failing (as my backend only expects and accepts the defined value):

Dalvik/2.1.0 (Linux; U; Android 9; Android SDK built for x86 Build/PSR1.180720.075); MY_CUSTOM_USER_AGENT_RIGHT_HERE

Imho it seems that both user agent values are getting mixed in the end by the OkHttpStreamFetcher.java in loadData() when the built up request + client are coming together. Quite similar to a problem ~ 3 years ago: #546

Issue details / Repro steps / Use case background:

My Interceptor:

import okhttp3.Interceptor
import okhttp3.Response

class UserAgentInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response = chain.proceed(
            chain.request()
                    .newBuilder()
                    .addHeader("User-Agent", "MY_CUSTOM_USER_AGENT_RIGHT_HERE")
                    .build())
}

My used Glide Module:

import android.content.Context
import com.bumptech.glide.Glide
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader.Factory
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.module.AppGlideModule
import okhttp3.Dispatcher
import okhttp3.OkHttpClient
import java.io.InputStream

@GlideModule
class OkHttpGlideModule : AppGlideModule() {

    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        val client = OkHttpClient.Builder()
                .addInterceptor(ConnectivityInterceptor(context, false))
                .build()
        registry.replace(GlideUrl::class.java, InputStream::class.java, Factory(client))
    }
}

Glide load line:
I'm using data binding to set the loading mechanism for ImageViews

object ImageViewBinding {

    @JvmStatic
    @BindingAdapter(value = ["url", "default"], requireAll = false)
    fun setImageUrl(view: ImageView, url: String?, @DrawableRes default: Drawable?) {
        default?.let {
            view.setImageDrawable(it)
        }

        url?.let {
            Glide.with(view.context).load(it).placeholder(default).into(view)
        }
    }

Glide Version:
Glide 4.9, latest release

Integration libraries:
Glide-OkHttp3-integration, 4.9 as well

Other libraries:
OkHttp3, 3.12.1

Device/Android Version:
Can be tested on an AVD Pie x86 image but also reproducible on Samsung S6, S7, Google Pixel or Huawei devices. Imho not device specific related

Question:

  • Is it required to set the user agent somewhere else instead of using an interceptor? -> From your point of view works as designed? If yes, what would be the right way?
  • Or Is this actually a bug that such conflicts should not occur?
@technoir42
Copy link
Contributor

Your requests have 2 User-Agent headers since Glide adds one and you add one too.
You have to use header(key, value) instead of addHeader(key, value) to replace the existing header.

@shalfknight
Copy link
Author

shalfknight commented Jun 4, 2019

Thx!

So simple it is by just knowing that addHeader starts working like a combine, not replacing previous values.

Final solution:

class UserAgentInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response = chain.proceed(
            chain.request()
                    .newBuilder()
                    .header("User-Agent", "MY_CUSTOM_USER_AGENT_RIGHT_HERE")
                    .build())
}

Thx for the fast response time and your patience!

May the next guy who is facing that "problem" be guided by this thread.

Problem solved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants