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 UriPatcher to fix URI quirks #698

Merged
merged 1 commit into from Mar 18, 2016
Merged

Add UriPatcher to fix URI quirks #698

merged 1 commit into from Mar 18, 2016

Conversation

mmdriley
Copy link
Contributor

Fixes #636, #643.

Tested with:
nunit3-console.exe --domain=None --framework=net-4.0 Google.Apis.Tests.dll

I abandoned a previous approach that modified individual Uri objects in favor of a simpler and less fragile approach that makes process-wide changes. Oh well. It seems unlikely that customers writing to Google APIs are also depending on weird URL behavior.

We may need to move the call to PatchUriQuirks or make similar calls elsewhere, to ensure it anticipates all of our uses of Uri.

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Mar 18, 2016
@mmdriley
Copy link
Contributor Author

/cc @jskeet @chrsmith

@jskeet
Copy link
Collaborator

jskeet commented Mar 18, 2016

Can we get rid of the "problem 1" part due to #696, or does it still affect PCLs? (Sorry if that's explained - I'm still trying to take it all in.)

@mmdriley
Copy link
Contributor Author

I understand being confused. It's confusing. I can only mostly hold it in my head, and then just for small bits of time. :)

The problem is DLLs.

Thanks to #696, no one is going to use NuGet to import our libraries into an application (.exe) that targets .NET 4.0. However, folks might write DLLs that target .NET 4.5 and import our libraries. If that DLL, in turn, is loaded into an application that targets .NET 4.0, some of our requests will fail because we're getting quirked URI behavior.

The poster child for this use case is Powershell. Powershell loads cmdlet.dll which loads Google.Apis.Core.dll. Even though everything is running on .NET v4.6.2, Powershell has specified that the AppDomain should be created targeting v4.0, and all the quirks are enabled.

@mmdriley
Copy link
Contributor Author

The hardest part for me to remember is that we're writing all this code to handle old versions but it will only ever run on new versions. Everything is .NET 4.5+ now, even if it's lying to an unaware application and acting like 4.0.

This is code to tell .NET 4.5+ to stop pretending to be .NET 4.0 when it comes to URIs. That pretending happens when the entry assembly -- which isn't us, and may not be the library that compiled against us -- targets v4.0.

@jskeet
Copy link
Collaborator

jskeet commented Mar 18, 2016

Okay, so we definitely won't be running on 4.0, but the behaviour might be as if we were. I see that referenced in the code, but I'd misread it before. Right, time to read the code itself...

// https://github.com/Microsoft/referencesource/blob/d925d870f3cb3f6a/System/net/System/_UriSyntax.cs#L77
// https://github.com/Microsoft/referencesource/blob/d925d870f3cb3f6a/System/net/System/_UriSyntax.cs#L352

var setUpdatableFlagsMethod = uriParser.GetMethod("SetUpdatableFlags",

This comment was marked as spam.

This comment was marked as spam.

@mmdriley
Copy link
Contributor Author

@jskeet Updated -- PTAL.

// As a class library, we can't control app.config or the entry assembly, so we can't take
// either approach. Instead, we resort to reflection trickery to try to solve these problems
// if we detect they exist. Sorry.
static internal class UriPatcher

This comment was marked as spam.

if (parserField == null) { return; }
var parserInstance = parserField.GetValue(null);
if (parserInstance == null) { return; }
setUpdatableFlagsMethod.Invoke(parserInstance, new object[] { 0 });

This comment was marked as spam.

@jskeet
Copy link
Collaborator

jskeet commented Mar 18, 2016

"Looks least-worst-option to me". (Can't quite bring myself to say it looks good...)

Tested with:
`nunit3-console.exe --domain=None --framework=net-4.0 Google.Apis.Tests.dll`

Before, we saw three test failures as described in #636. Now all tests pass.
@mmdriley
Copy link
Contributor Author

Least-worst is what I was shooting for. At least I said "Sorry". :)

mmdriley added a commit that referenced this pull request Mar 18, 2016
Add UriPatcher to fix URI quirks
@mmdriley mmdriley merged commit 20a6031 into googleapis:master Mar 18, 2016
@mmdriley mmdriley deleted the uri branch March 18, 2016 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants