Skip to content

Usability: PathString.StartsWithSegments() with a trailing slash is not intuitive behavior #2713

Closed
@aspnet-hello

Description

@aspnet-hello

From @NickCraver on Sunday, January 15, 2017 7:34:28 AM

I was trying to do effectively this yesterday:

new PathString("/profiler/test").StartsWithSegments(new Path("/profiler/");

This was, to me, unexpectedly false. And I spent over an hour being an idiot. Once I was in source, I finally realized that a starts-with check of "/profiler" works, while including a trailing slash of "/profiler/" does not. Is this intended behavior? The PathString tests don't actually include a case testing it either way.

Current behavior examples:

	new PathString("/a/b").StartsWithSegments(new PathString("/a/"));  // false
	new PathString("/a/b").StartsWithSegments(new PathString("/a"));   // true
	new PathString("/a//b").StartsWithSegments(new PathString("/a/")); // true

Currently the check of value1[value2.Length] == '/' means whatever's checked against must match or have a trailing slash after it, unless it matches exactly. It seems to be that a /path/ should work here. Is there any objection to this working?

The check could change from:

value1.Length == value2.Length || value1[value2.Length] == '/'

to:

value1.Length == value2.Length || value1[value2.Length] == '/' || value2[value2.Length-1] == '/'

and in the remainder methods, it'd be slightly different with an additional if instead:

if (value2[value2.Length - 1] == '/')
{
    remaining = new PathString(value1.Substring(value2.Length - 1));
    return true;
}

Thoughts? Is there a reason it doesn't behave like this already, or just an ambiguous case? I'd be happy to submit a PR if it's a welcomed change.

Copied from original issue: aspnet/HttpAbstractions#756

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected-very-fewThis issue impacts very few customersarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-http-abstractionshelp candidateIndicates that the issues may be a good fit for community to help with. Requires work from eng. teamseverity-minorThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions