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

custom set User-Agent is overwritten #3975

Closed
scholt opened this issue Nov 12, 2019 · 1 comment
Closed

custom set User-Agent is overwritten #3975

scholt opened this issue Nov 12, 2019 · 1 comment

Comments

@scholt
Copy link

scholt commented Nov 12, 2019

Glide Version: 4.10.0

Integration libraries: okhttp3 same version (4.10.0)

Device/Android Version: any device

Issue details / Repro steps / Use case background:

Slightly related to this issue. I want to have a proper User Agent for Glide Requests. Because of untagged socket strict mode I added the okhttp integration to use an Interceptor to tag socket traffic, also this interceptor sets the User-Agent.

Upon notice that the User-Agent was not correct, I also checked our custom Loader and put a header in the GlideUrl I build. I confirmed by setting breakpoints, that the header there and in the okhttp-Interceptor is set.

Glide Module:

  override fun registerComponents(
    context: Context,
    glide: Glide,
    registry: Registry
  ) {
    super.registerComponents(context, glide, registry)

    registry.prepend(
      CacheKeyImage::class.java,
      InputStream::class.java,
      CacheKeyImageUrlLoader.Factory()
    )

    registry.prepend(
      GlideUrl::class.java,
      InputStream::class.java,
      OkHttpUrlLoader.Factory(/** with user agent interceptor configured okhttp instance*/)
    )
  }

Relevant Loader method

override fun buildLoadData(
    model: CacheKeyImage,
    width: Int,
    height: Int,
    options: Options
  ): ModelLoader.LoadData<InputStream>? {

    var result: CacheKeyImageUrl? = modelCache.get(model, width, height)
    if (result == null) {
      val stringURL = model.imageUrl
      if (TextUtils.isEmpty(stringURL)) {
        return null
      }

      result = CacheKeyImageUrl(model, defaultHeaders)

      modelCache.put(model, width, height, result)
    }

    val alternateUrls = getAlternateUrls(model, width, height, options)
    val concreteLoaderData = concreteLoader.buildLoadData(
      result, width, height, options)

    return if (concreteLoaderData == null || alternateUrls.isEmpty()) {
      concreteLoaderData
    } else {
      ModelLoader.LoadData(concreteLoaderData.sourceKey, getAlternateKeys(alternateUrls),
        concreteLoaderData.fetcher)
    }
  }

default headers used:

 private val defaultHeaders by lazy {
    LazyHeaders.Builder()
      .setHeader("User-Agent", ApoSyncApp.getUserAgent())
      .build()
  }

Custom Glide URL object

class CacheKeyImageUrl(
  val image: CacheKeyImage,
  headers: Headers
) : GlideUrl(image.imageUrl, headers) {
  override fun getCacheKey(): String = image.toString()
}

User Agent is always: User-Agent | okhttp/4.2.2

@scholt
Copy link
Author

scholt commented Nov 12, 2019

sorry, problem was related to some ssl or tls issue in okhttp in relation with charles proxy.

@scholt scholt closed this as completed Nov 12, 2019
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

1 participant