Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ inline void fromRawValue(
result.headers.push_back(header);
}
}

if (items.find("body") != items.end() &&
items.at("body").hasType<std::string>()) {
result.body = (std::string)items.at("body");
}

if (items.find("method") != items.end() &&
items.at("method").hasType<std::string>()) {
result.method = (std::string)items.at("method");
}

if (items.find("cache") != items.end() &&
items.at("cache").hasType<std::string>()) {
auto cache = (std::string)items.at("cache");
if (cache == "reload") {
result.cache = ImageSource::CacheStategy::Reload;
} else if (cache == "force-cache") {
result.cache = ImageSource::CacheStategy::ForceCache;
} else if (cache == "only-if-cached") {
result.cache = ImageSource::CacheStategy::OnlyIfCached;
}
}

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import <UIKit/UIKit.h>

#import <React/RCTConvert.h>
#import <React/RCTImageLoader.h>
#import <react/renderer/imagemanager/primitives.h>

Expand Down Expand Up @@ -44,6 +45,25 @@ inline std::string toString(const facebook::react::ImageResizeMode &value)
}
}

inline static NSURLRequestCachePolicy NSURLRequestCachePolicyFromImageSource(
const facebook::react::ImageSource &imageSource)
{
switch (imageSource.cache) {
case facebook::react::ImageSource::CacheStategy::Reload:
return NSURLRequestReloadIgnoringLocalCacheData;
break;
case facebook::react::ImageSource::CacheStategy::ForceCache:
return NSURLRequestReturnCacheDataElseLoad;
break;
case facebook::react::ImageSource::CacheStategy::OnlyIfCached:
return NSURLRequestReturnCacheDataDontLoad;
break;
default:
return NSURLRequestUseProtocolCachePolicy;
break;
}
}

inline static NSURL *NSURLFromImageSource(const facebook::react::ImageSource &imageSource)
{
// `NSURL` has a history of crashing with bad input, so let's be safe.
Expand Down Expand Up @@ -102,13 +122,21 @@ inline static NSURLRequest *NSURLRequestFromImageSource(const facebook::react::I

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

/*
// TODO(shergin): To be implemented.
request.HTTPBody = ...;
request.HTTPMethod = ...;
request.cachePolicy = ...;
request.allHTTPHeaderFields = ...;
*/
NSString *method = @"GET";
if (!imageSource.method.empty()) {
method = [[NSString alloc] initWithUTF8String:imageSource.method.c_str()].uppercaseString;
}
NSData *body = nil;
if (!imageSource.body.empty()) {
body = [NSData dataWithBytes:imageSource.body.c_str() length:imageSource.body.size()];
}
NSURLRequestCachePolicy cachePolicy = NSURLRequestCachePolicyFromImageSource(imageSource);

if ([method isEqualToString:@"GET"] && imageSource.headers.empty() && body == nil &&
cachePolicy == NSURLRequestUseProtocolCachePolicy) {
return request;
}

for (const auto &header : imageSource.headers) {
NSString *key = [NSString stringWithUTF8String:header.first.c_str()];
NSString *value = [NSString stringWithUTF8String:header.second.c_str()];
Expand All @@ -117,5 +145,9 @@ inline static NSURLRequest *NSURLRequestFromImageSource(const facebook::react::I
}
}

return [request copy];
request.HTTPBody = body;
request.HTTPMethod = method;
request.cachePolicy = cachePolicy;

return request;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ namespace facebook::react {
class ImageSource {
public:
enum class Type { Invalid, Remote, Local };
enum class CacheStategy { Default, Reload, ForceCache, OnlyIfCached };

Type type{};
std::string uri{};
std::string bundle{};
Float scale{3};
Size size{0};
std::string body{};
std::string method{};
CacheStategy cache = CacheStategy::Default;
std::vector<std::pair<std::string, std::string>> headers{};

bool operator==(const ImageSource& rhs) const {
Expand Down