@@ -240,6 +240,32 @@ public string ToBaseRelativePath(string uri)
240
240
throw new ArgumentException ( message ) ;
241
241
}
242
242
243
+ internal ReadOnlySpan < char > ToBaseRelativePath ( ReadOnlySpan < char > uri )
244
+ {
245
+ if ( MemoryExtensions . StartsWith ( uri , _baseUri ! . OriginalString . AsSpan ( ) , StringComparison . Ordinal ) )
246
+ {
247
+ // The absolute URI must be of the form "{baseUri}something" (where
248
+ // baseUri ends with a slash), and from that we return "something"
249
+ return uri [ _baseUri . OriginalString . Length ..] ;
250
+ }
251
+
252
+ var pathEndIndex = uri . IndexOfAny ( '#' , '?' ) ;
253
+ var uriPathOnly = pathEndIndex < 0 ? uri : uri [ ..pathEndIndex ] ;
254
+ if ( _baseUri . OriginalString . EndsWith ( '/' ) && MemoryExtensions . Equals ( uriPathOnly , _baseUri . OriginalString . AsSpan ( 0 , _baseUri . OriginalString . Length - 1 ) , StringComparison . Ordinal ) )
255
+ {
256
+ // Special case: for the base URI "/something/", if you're at
257
+ // "/something" then treat it as if you were at "/something/" (i.e.,
258
+ // with the trailing slash). It's a bit ambiguous because we don't know
259
+ // whether the server would return the same page whether or not the
260
+ // slash is present, but ASP.NET Core at least does by default when
261
+ // using PathBase.
262
+ return uri [ ..( _baseUri . OriginalString . Length - 1 ) ] ;
263
+ }
264
+
265
+ var message = $ "The URI '{ uri } ' is not contained by the base URI '{ _baseUri } '.";
266
+ throw new ArgumentException ( message ) ;
267
+ }
268
+
243
269
internal static string NormalizeBaseUri ( string baseUri )
244
270
{
245
271
var lastSlashIndex = baseUri . LastIndexOf ( '/' ) ;
0 commit comments