Glide: Edge Cases: set html headers and/or session cookies

Created on 17 Oct 2014  路  9Comments  路  Source: bumptech/glide

If there server you are accessing use secure, then we need a way to set httml headers of session cookies.

enhancement

Most helpful comment

GlideUrl url = new GlideUrl("http://....", new LazyHeaders.Builder()
                .addHeader("User-Agent", USER_AGENT)
                .addHeader("App-Agent", APP_AGENT)
                .build());
....load(url)....;

All 9 comments

The current answer for this is to write a custom ModelLoader, you can check out either of the integration projects for how to do this: https://github.com/bumptech/glide/tree/master/integration/okhttp/src/main/java/com/bumptech/glide/integration/okhttp, or https://github.com/bumptech/glide/tree/master/integration/volley/src/main/java/com/bumptech/glide/integration/volley.

It's usually relatively straight forward.

I'd be happy to add an interface to make this easier, please feel free to contribute code or ideas on what such an API should look like.

I'm an experience developer, but I'm not finding this easy.
I _can't_ add volley or okhttp to the project easily (client red tape).

All I really need is something similar to the setDefaultOptions calls, that allows us to set an interceptor for the request object. You'd simply manipulate the request object directly before the http method was executed.

Glide.intercept(new BasicInterceptor(){
    public void prepare(Request request){
        request.getCookies().add(key, value);
        request.addHeader("MYHEADER", "... 123abc...");
        ... 
    }
});

Doing it that way would give the user excellent control over the request and allow pretty much any sort of customization that might be needed.

You may need to provide typed versions depending on what loaders the developer used, but that should be fairly easy to handle.

I think the problem here is that you don't always have a Request (i.e. which library's request?).
For example by default (if I'm not mistaken) Glide uses HttpURLConnection so there's no Request you want... see HttpUrlGlideUrlLoader (a ModelLoader that creates a converter from GlideUrl to InputStream) which creates a HttpUrlFetcher (DataFetcher retrieves the stream from a GlideUrl).

Assuming by "secure" you meant HTTPS, but I guess the same stands for HTTP with cookies:
As far as I see if you need to work around (add cookies and stuff) how an url string is converted into an InputStream you just need to copy the two classes from okhttp integration replacing "okhttp" with "MyFancyHTTPS" and register the factory via:

Glide.get(this).register(GlideUrl.class, InputStream.class, new MyFancyHTTPSLoader.Factory({static configuration for your custom loader}));

which will override the default url loading logic (String, Uri, and other similar methods will eventually be routed to the GlideUrl model loader).
Then implement MyFancyHTTPSLoader to create MyFancyHTTPSFetcher which in turn does something like this SO instead of using the new Request.Builder() from okhttp.

_Disclaimer_: I'm just a user of Glide like you, and this is how I'd tackle the problem. It doesn't seem that hard and there is not a more "excellent control over the request" than full control.

@sjudd please correct me if I got anything wrong here.

@bpappin Thanks for the suggestion, I'll look in to a way to add an API like that.

For now, @TWiStErRob is on the right track, you can get around this by implementing a custom ModelLoader. He suggested using the OkHttp classes, but since you said you can't add either Volley or OkHttp, you can follow the same general steps and instead fork Glide's default classes.

First fork Glide's ModelLoader class: https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/load/model/stream/HttpUrlGlideUrlLoader.java.

You only need to change one line, number 58, where instead of creating Glide's fetcher you want to create an instance of the class you fork.

Second fork Glide's DataFetcher class:
https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java

Add whatever headers you'd like to your fork of the class (maybe see http://stackoverflow.com/questions/12732422/adding-header-for-httpurlconnection) , and then make sure to change your fork of HttpUrlGlideUrlLoader to return your fork of HttpUrlFetcher.

Finally you need to register your ModelLoader fork.

In onCreate of your main Activity, or in your Application object:

public void onCreate(...) {
    Glide.get(this).register(GlideUrl.class, InputStream.class, 
        new YourHttpUrlGlideUrlLoaderFork.Factory())
}

By registering your factory for GlideUrls and InputStreams, you replace Glide's default http loading code with your own, and any subsequent requests will go through your custom model loader.

Hope that helps, I agree this isn't straightforward and we should either add an API or a base class/interface that makes this easier.

I suggested okhttp because those 2 classes are simpler than the default ones, though that caching must be needed...

If you do some refactoring and extract the new HttpUrlFetcher into a protected method. It will be much easier with an extends and an @Override. Also make the fetcher more extensible so that parts of it can be changed.

@bpappin do you think the above would help or you're sticking to that static API?

Can't use okhttp because i'm working with a hybrid app and Cordova embedded a very old version of okhttp that conflicts with most other libs.

I'm still not suggesting to use okhttp :)
What I'm saying is to write your own integration module based on those two okhttp classes and _replace okhttp calls with whatever lib you want_. See

instead of using the new Request.Builder() from okhttp.

I am not able to find any documentation on how to set the custom headers ... Any help would be appreciated.

GlideUrl url = new GlideUrl("http://....", new LazyHeaders.Builder()
                .addHeader("User-Agent", USER_AGENT)
                .addHeader("App-Agent", APP_AGENT)
                .build());
....load(url)....;
Was this page helpful?
0 / 5 - 0 ratings