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

Image loads with stock, but can't be loaded with okhttp-integration #436

Closed
TWiStErRob opened this issue Apr 22, 2015 · 14 comments
Closed

Comments

@TWiStErRob
Copy link
Collaborator

TWiStErRob commented Apr 22, 2015

Glide Version/Integration library (if any): 3.6.0-SNAPSHOT, 1.3.0-SNAPTSHOT
Device/Android Version: S4/4.4
Issue details/Repro steps:
If you open this image in any browser or sane app on Laptop it gives HTTP 403. If you open it in Android Chrome or load using the stock android URLConnection it works, but not with okhttp-integration. They probably put a user-agent check there: confirmed with Chrome Dev Tools' device selector.
The weird thing is that the stock method is also using okhttp: com.android.okhttp.internal.http.HttpURLConnectionImpl, here's their setup I could fit on a screen:
image

Glide load line:

Glide
    .with(this)
    .load("http://img0.imgtn.bdimg.com/it/u=1892208653,1538092183&fm=21&gp=0.jpg")
    .placeholder(android.R.color.white)
    .error(android.R.color.black)
    .skipMemoryCache(true)
    .diskCacheStrategy(DiskCacheStrategy.NONE)
    .into(iv);

Stack trace:

04-22 19:48:06.513  16237-16237/net.twisterrob.app D/GenericRequest﹕ load failed
    java.io.IOException: Request failed with code: 403
            at com.bumptech.glide.integration.okhttp.OkHttpStreamFetcher.loadData(OkHttpStreamFetcher.java:43)
            at com.bumptech.glide.integration.okhttp.OkHttpStreamFetcher.loadData(OkHttpStreamFetcher.java:19)
            at com.bumptech.glide.load.model.ImageVideoModelLoader$ImageVideoFetcher.loadData(ImageVideoModelLoader.java:70)
            at com.bumptech.glide.load.model.ImageVideoModelLoader$ImageVideoFetcher.loadData(ImageVideoModelLoader.java:53)
            at com.bumptech.glide.load.engine.DecodeJob.decodeSource(DecodeJob.java:170)
            at com.bumptech.glide.load.engine.DecodeJob.decodeFromSource(DecodeJob.java:128)
            at com.bumptech.glide.load.engine.EngineRunnable.decodeFromSource(EngineRunnable.java:122)
            at com.bumptech.glide.load.engine.EngineRunnable.decode(EngineRunnable.java:101)
            at com.bumptech.glide.load.engine.EngineRunnable.run(EngineRunnable.java:58)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
            at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor$DefaultThreadFactory$1.run(FifoPriorityThreadPoolExecutor.java:52)
@sjudd
Copy link
Collaborator

sjudd commented Apr 26, 2015

I'm not sure where to start here. I'm tempted to just say this is WAI? At least our behavior is consistent with chrome on Android?

@TWiStErRob
Copy link
Collaborator Author

Let me reiterate:

  • Laptop Firefox: 403
  • Laptop IE: 403
  • Laptop Chrome: 403
  • Android Chrome: works
  • Glide with stock: works
  • Glide with okhttp: 403
  • Laptop Chrome with UA emulation (Android): works

So okhttp-integration doesn't tell the webserver that we're on mobile.
I think this could be tackled by comparing the stock (which uses com.android.okhttp.internal.http.HttpURLConnectionImpl) and the integration's okhttp connection setup (this is why I included the image, but I didn't have time to do the okhttp at that time).

@sjudd
Copy link
Collaborator

sjudd commented Apr 26, 2015

Ah ok I see what you're saying, thanks. I'll take a look.

@TWiStErRob
Copy link
Collaborator Author

I also didn't check how volley behaves.

@sjudd
Copy link
Collaborator

sjudd commented Apr 26, 2015

Any chance you could test this locally? It looks like you get a default user agent from system properties, which we could set if no user agent is set in the headers. For example:

String userAgent = System.getProperty( "http.agent" );
requestBuilder.addHeader("User-Agent", userAgent);

http://stackoverflow.com/questions/4103963/is-there-a-way-to-obtain-the-default-user-agent-string-aside-from-webview-getset

@sjudd
Copy link
Collaborator

sjudd commented Apr 26, 2015

Actually nevermind, I'll try this myself.

@TWiStErRob
Copy link
Collaborator Author

I put a breakpoint there and executed the code you quoted and it loads with okhttp.

@sjudd
Copy link
Collaborator

sjudd commented Apr 26, 2015

ok great thanks!

@sjudd sjudd closed this as completed in 46126e0 Apr 26, 2015
@TWiStErRob
Copy link
Collaborator Author

Volley uses AndroidHttpClient probably, so that makes it work somehow (didn't find it exactly).

@Edijae
Copy link

Edijae commented Nov 8, 2016

where do i add

String userAgent = System.getProperty( "http.agent" );
requestBuilder.addHeader("User-Agent", userAgent);

it does not fit here

public class OkHttpProgressGlideModule  implements GlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        builder.setDecodeFormat(DecodeFormat.ALWAYS_ARGB_8888);
    }


    @Override
    public void registerComponents(Context context, Glide glide) {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(createInterceptor(new DispatchingProgressListener()))
                .build();
        glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
    }

@TWiStErRob
Copy link
Collaborator Author

TWiStErRob commented Nov 8, 2016

@gikarasojo you don't need to add it anywhere, it's included in the 3.7.0 release.

If you want to apply global headers, configure the OkHttpClient; or if you want certain requests only, create a GlideUrl with LazyHeaders where you start the load.

@Edijae
Copy link

Edijae commented Nov 8, 2016

@TWiStErRob am using firebase and i want to download an image from my firebase storage. i just want to download an image with custom sizes, therefore i have this class

public class MyUriLoader extends BaseGlideUrlLoader<MyDataModel> {

  public MyUriLoader(Context context) {
    super(context);
  }

  @Override
  protected String getUrl(MyDataModel model, int width, int height) {
    // Construct the url for the correct size here.
    return model.buildUrl(width, height);
  }
}

and i download like this

Glide.with(context)
      .using(new MyUriLoader(context))
      .load(new CustomImageSize(item.getPhotoUri()))
      .asBitmap()
      .centerCrop()
      .into(holder.target);

am using OkHttpClient for purposes of showing percentage downloaded. it's working fine when i paste an image uri from google but throwing an error when i try to download from firebase storage

load failed
   java.io.IOException: Request failed with code: 403
     at com.bumptech.glide.integration.okhttp3.OkHttpStreamFetcher.loadData(OkHttpStreamFetcher.java:46)
                                                                              at com.bumptech.glide.integration.okhttp3.OkHttpStreamFetcher.loadData(OkHttpStreamFetcher.java:19)
                                                                              at com.bumptech.glide.load.model.ImageVideoModelLoader$ImageVideoFetcher.loadData(ImageVideoModelLoader.java:70)
                                                                              at com.bumptech.glide.load.model.ImageVideoModelLoader$ImageVideoFetcher.loadData(ImageVideoModelLoader.java:53)

@Edijae
Copy link

Edijae commented Nov 8, 2016

@TWiStErRob Is there a better way to show percentage download in glide?

@TWiStErRob
Copy link
Collaborator Author

You very likely need to use authenticated access for Firebase, but that's another issue unrelated to this one. Search for "Glide firebase", and open a new issue if you want help. I used OkHttp to show progress.

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

No branches or pull requests

3 participants