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

Add request configuration block to allow customizing HTTP request #355

Merged
merged 4 commits into from
May 17, 2017

Conversation

zachwaugh
Copy link
Contributor

Replaces #321. I had a similar need to #211, where I want to customize request headers on a per request basis, so I added a request configuration block on PINRemoteImageManager as suggested in that thread. My specific use-case is I want to use PINRemoteImageManager to fetch authenticated images for me, which requires setting an Authorization header with our token. However, I don't want to send this for images to other domains, so I need it to be per request. This seemed like the most straightforward approach, but let me know if you think it should be implemented another way.

@zachwaugh
Copy link
Contributor Author

@garrettmoon you mentioned in the initial PR about locking access to the requestConfigurationHandler instance var. I tried that, but it was causing a deadlock. I'm no expert on locking, but I'm assuming because this method is called from sessionTaskWithURL:... which itself is called within a lock - https://github.com/pinterest/PINRemoteImage/blob/master/Source/Classes/PINRemoteImageManager.m#L747-L757

@@ -281,6 +282,16 @@ - (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSStrin
});
}

- (void)setRequestConfiguration:(PINRemoteImageManagerRequestConfigurationHandler)configurationBlock {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zachwaugh you're right about the deadlock.

Here's what I would suggest: We need to add a getter which holds the lock when it accesses requestConfigurationHandler or else it isn't thread safe to access by clients. However, since we know we have the lock in downloadDataWithURL: we can access the ivar directly instead of using the property.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@garrettmoon sorry for the confusion, but are you saying we do need to a getter, or we don't since we can just use the direct ivar access in downloadDataWithURL:.... I made that direct ivar change and it's the only place this variable is accessed currently.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do need a getter because the property is exposed publicly? Reading the property externally is currently not thread safe unless I'm mistaken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requestConfigurationHandler property isn't exposed publicly, only the setRequestConfiguration: method.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sheesh, I'm doing a terrible job reading this code for some reason. 🤦‍♂️

@@ -921,6 +932,10 @@ - (NSURLSessionDataTask *)downloadDataWithURL:(NSURL *)url
request.allHTTPHeaderFields = headers;
}

if (self.requestConfigurationHandler) {
request = [self.requestConfigurationHandler(request) mutableCopy];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this should be:

if (_requestConfigurationHandler) {
    request = [_requestConfigurationHandler(request) mutableCopy];
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, just pushed that change.

This method is already called within a lock. Locking again will cause a
deadlock, so we can use direct access here.
@@ -281,6 +282,16 @@ - (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSStrin
});
}

- (void)setRequestConfiguration:(PINRemoteImageManagerRequestConfigurationHandler)configurationBlock {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sheesh, I'm doing a terrible job reading this code for some reason. 🤦‍♂️

@garrettmoon
Copy link
Collaborator

@zachwaugh Thanks so much for adding this and addressing my misguided feedback!

@garrettmoon garrettmoon merged commit 54a736a into pinterest:master May 17, 2017
@zachwaugh
Copy link
Contributor Author

@garrettmoon no problem!

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

Successfully merging this pull request may close these issues.

2 participants