forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Android: Support HTTP headers for source prop on <Image> components
Summary: A copy of facebook#7791 because of our very imperfect tools that mirror the changes from pull requests in the fb monorepo. The internal Phabricator revision for facebook#7791 is in an 'abandoned' state (by foghina probably because of changing teams) and Phabricator doesn't allow me to claim that revision and merge it. Therefore I'm creating a new one. (It's not foghina's fault, no one probably knew about this "abandoned Phabricator revision" edge case, don't remember we hit it before.) Will try to keep attribution (git blame) to rigdern when merging. Closes facebook#12448 Differential Revision: D4584743 Pulled By: mkonicek fbshipit-source-id: 66e5b88134fca1980adc4cd8a2ff17c42e10022c
- Loading branch information
Martin Konicek
authored and
dudeinthemirror
committed
Mar 1, 2017
1 parent
e84fcf2
commit 6935a63
Showing
13 changed files
with
180 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
ReactAndroid/src/main/java/com/facebook/react/modules/fresco/ReactNetworkImageRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* <p/> | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.modules.fresco; | ||
|
||
import com.facebook.imagepipeline.request.ImageRequest; | ||
import com.facebook.imagepipeline.request.ImageRequestBuilder; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
/** Extended ImageRequest with request headers */ | ||
public class ReactNetworkImageRequest extends ImageRequest { | ||
|
||
/** Headers for the request */ | ||
private final ReadableMap mHeaders; | ||
|
||
public static ReactNetworkImageRequest fromBuilderWithHeaders(ImageRequestBuilder builder, | ||
ReadableMap headers) { | ||
return new ReactNetworkImageRequest(builder, headers); | ||
} | ||
|
||
protected ReactNetworkImageRequest(ImageRequestBuilder builder, ReadableMap headers) { | ||
super(builder); | ||
this.mHeaders = headers; | ||
} | ||
|
||
public ReadableMap getHeaders() { | ||
return mHeaders; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
ReactAndroid/src/main/java/com/facebook/react/modules/fresco/ReactOkHttpNetworkFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* <p/> | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.modules.fresco; | ||
|
||
import android.net.Uri; | ||
import android.os.SystemClock; | ||
|
||
import com.facebook.imagepipeline.backends.okhttp3.OkHttpNetworkFetcher; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.ReadableMapKeySetIterator; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.Executor; | ||
|
||
import okhttp3.CacheControl; | ||
import okhttp3.Headers; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
|
||
class ReactOkHttpNetworkFetcher extends OkHttpNetworkFetcher { | ||
|
||
private static final String TAG = "ReactOkHttpNetworkFetcher"; | ||
|
||
private final OkHttpClient mOkHttpClient; | ||
private final Executor mCancellationExecutor; | ||
|
||
/** | ||
* @param okHttpClient client to use | ||
*/ | ||
public ReactOkHttpNetworkFetcher(OkHttpClient okHttpClient) { | ||
super(okHttpClient); | ||
mOkHttpClient = okHttpClient; | ||
mCancellationExecutor = okHttpClient.dispatcher().executorService(); | ||
} | ||
|
||
private Map<String, String> getHeaders(ReadableMap readableMap) { | ||
if (readableMap == null) { | ||
return null; | ||
} | ||
ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); | ||
Map<String, String> map = new HashMap<>(); | ||
while (iterator.hasNextKey()) { | ||
String key = iterator.nextKey(); | ||
String value = readableMap.getString(key); | ||
map.put(key, value); | ||
} | ||
return map; | ||
} | ||
|
||
@Override | ||
public void fetch(final OkHttpNetworkFetchState fetchState, final Callback callback) { | ||
fetchState.submitTime = SystemClock.elapsedRealtime(); | ||
final Uri uri = fetchState.getUri(); | ||
Map<String, String> requestHeaders = null; | ||
if (fetchState.getContext().getImageRequest() instanceof ReactNetworkImageRequest) { | ||
ReactNetworkImageRequest networkImageRequest = (ReactNetworkImageRequest) | ||
fetchState.getContext().getImageRequest(); | ||
requestHeaders = getHeaders(networkImageRequest.getHeaders()); | ||
} | ||
if (requestHeaders == null) { | ||
requestHeaders = Collections.emptyMap(); | ||
} | ||
final Request request = new Request.Builder() | ||
.cacheControl(new CacheControl.Builder().noStore().build()) | ||
.url(uri.toString()) | ||
.headers(Headers.of(requestHeaders)) | ||
.get() | ||
.build(); | ||
|
||
fetchWithRequest(fetchState, callback, request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.