Skip to content

Conversation

@mandel-macaque
Copy link
Contributor

This PR is built on top of the following two PRs:

As per the first PR:

With the original ModernHttpClient version of NSUrlSessionHandler it had 3 problems.

  1. The data in the request was loaded into memory using a byte array. code ref
  2. The data in the response was loaded into managed memory from unmanaged memory. And not properly disposed of. code ref
  3. The await response is returned before the stream is available, which is not how the .NET framework works. When the await response is returned in .NET the stream is available for reading. code ref

This pull request does the following the correct the above.

  1. Wraps a managed stream in an unmanaged stream and sends it to the request as a stream instead of byte array.
  2. Now directly uses NSData instead of transitioning to a managed byte array. And disposes of the NSData after fully read.
  3. The await response is now returned after the first piece of data is returned or the request is completed.

Please note this is a complete rewrite, which is why I didn't try to commit it to the ModernHttpClient repository. I am willing to move it some where else if it is more appropriate.

The only work done has been to fix the merge issues and ensure that we are ok with master. QA has already approved the merge.

nberardi and others added 10 commits May 20, 2016 08:52
This is a rewrite of the ModernHttpClient version of NSUrlSessionHandler, it has better handling for memory that provides a more consistant memory footprint. It accomplishes this by using NSInputStream for requests, and reading and disposing directly from NSData instead of transitioning the NSData to a byte[] array.
Mostly my comments in PR #174

* Add support for redirection [1]
* Add support for credentials [1]
* Add support for caching [2]
* Remove 2nd dictionary lookup in GetHeaderSeparator
* Avoid extraneous cast for credentialsToUse

PR 177 [3] adds tests that ensure no commits can remove, or change
default values, for handlers.

[1] breaking changes (feature, not API)
[2] breaking change (API removal)
[3] #177
Mostly my comments in PR #174

* Add support for redirection [1]
* Add support for credentials [1]
* Add support for caching [2]
* Remove 2nd dictionary lookup in GetHeaderSeparator
* Avoid extraneous cast for credentialsToUse

PR 177 [3] adds tests that ensure no commits can remove, or change
default values, for handlers.

[1] breaking changes (feature, not API)
[2] breaking change (API removal)
[3] #177
* Added ConfigureAwait(false) to Task.Delay to prevent DEADLOCK when the stream is being awaited on the UI thread
@mandel-macaque mandel-macaque added the enhancement The issue or pull request is an enhancement label Oct 3, 2016
@monojenkins
Copy link
Collaborator

Build failure


// we cannot do a bitmask but we can set the minimum based on ServicePointManager.SecurityProtocol minimum
var sp = ServicePointManager.SecurityProtocol;
if ((sp & SecurityProtocolType.Ssl3) != 0)
Copy link
Member

@rolfbjarne rolfbjarne Oct 3, 2016

Choose a reason for hiding this comment

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

CC @spouliot

This defaults to the least secure option (Ssl3), maybe we should reverse the conditions here to put TLS 1.2 first?

Copy link
Contributor

Choose a reason for hiding this comment

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

@rolfbjarne I'll double check (later, this can/must go in now @mandel-macaque) but IIRC that's the minimum you want to support (as the client) but it's the server side that has the final say on the protocol version to be used. Setting Tls12 would as the minimum would break all TLS 1.1 (and older) sites.

#endif
public partial class NSUrlSessionHandler : HttpMessageHandler
{
private readonly Dictionary<string, string> headerSeparators = new Dictionary<string, string> {
Copy link
Member

Choose a reason for hiding this comment

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

Style: we don't use private when it's the default.


protected override void Dispose (bool disposing)
{
lock(dataLock) {
Copy link
Member

Choose a reason for hiding this comment

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

Style: space before opening parenthesis.

}
}

await Task.Delay(50).ConfigureAwait(false);
Copy link
Member

Choose a reason for hiding this comment

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

Style: space before opening parenthesis.

@monojenkins
Copy link
Collaborator

Build failure

@monojenkins
Copy link
Collaborator

Build failure

@spouliot spouliot removed the enhancement The issue or pull request is an enhancement label Oct 3, 2016
@rolfbjarne
Copy link
Member

@spouliot Maybe it's a bit late to suggest this, but maybe we should provide the old NSUrlSessionHandler as well, so that customers can keep using it if there are bugs with this one? Once this one has been thoroughly tested (in a few cycles), we could remove the old one.

@spouliot
Copy link
Contributor

spouliot commented Oct 3, 2016

@rolfbjarne it's a refactoring of the existing one, i.e. it's pretty close, except for one important part of reusing the allocated memory, and ModernHttpClient remains a 3rd party component that can be used.

@monojenkins
Copy link
Collaborator

Build success

@mandel-macaque mandel-macaque merged commit da1478a into master Oct 3, 2016
@dotMorten
Copy link
Contributor

dotMorten commented Jun 22, 2018

@mandel-macaque I'm curious about this line:

CachePolicy = DisableCaching ? NSUrlRequestCachePolicy.ReloadIgnoringCacheData : NSUrlRequestCachePolicy.UseProtocolCachePolicy,

Why is the fallback cache policy not taken from the configuration that was parsed into the constructor instead?

@mandel-macaque mandel-macaque deleted the new-url-session-handler branch June 6, 2019 19:32
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.

8 participants