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

CSHARP-5200: Allow valid SRV hostnames with less than 3 parts #1527

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

papafe
Copy link
Contributor

@papafe papafe commented Nov 1, 2024

No description provided.

@papafe papafe requested a review from a team as a code owner November 1, 2024 13:24
@papafe papafe requested review from sanych-sun and removed request for a team November 1, 2024 13:24
var exception = Record.Exception(() => connectionString.Resolve());

exception.Should().BeOfType<MongoConfigurationException>().
Which.Message.Should().Be("Hosts in the SRV record must have the same parent domain as the seed host.");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

How much specific should we be with the error message? If we wanted to be very precise it could become:
"Hosts in the SRV record must have the same parent domain as the seed host. If the seed host has less than three separated dot separated parts, then the returned hostname must have at least one more domain level than the seed".
Would it be too much?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sanych-sun What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I'm OK with the current message.

Func<string, string[]> getParentParts = x => x.Split('.').Skip(1).ToArray();
var host = resolvedEndPoint.Host;

var hostParts = host.Split('.').ToArray();
Copy link
Member

Choose a reason for hiding this comment

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

Can we avoid array allocation here? By counting number of '.' chars for length and checking if resolved host ends with original where we are comparing parts now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense! I've rewritten the method, hope it's readable enough

for (int ai = a.Length - 1, bi = b.Length - 1; bi >= 0; ai--, bi--)
// We check that all the parts are equal, going from the end and arriving at the
// first dot of original (where the subdomain is, if we continued)
for (int hostIndex = host.Length - 1, originalIndex = original.Length - 1;
Copy link
Member

Choose a reason for hiding this comment

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

Can this loop also be replaced with EndsWith?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. I've rewritten the method to use EndsWith and use spans as much as possible to avoid string allocations

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