Skip to content

Commit 0627eab

Browse files
committed
Fix two bugs in FileSystemHandler:
* Wildcards in folder names would sometimes throw an exception * Folder names ending in “.” would throw because Directory.Exists accepts them but DirectoryInfo.GetDirectories doesn’t find them
1 parent 7e9d361 commit 0627eab

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ csharp_indent_block_contents = true
154154
csharp_indent_braces = false
155155
csharp_indent_case_contents = true
156156
csharp_indent_case_contents_when_block = true
157-
csharp_indent_labels = one_less_than_current
157+
csharp_indent_labels = no_change
158158
csharp_indent_switch_labels = true
159159
csharp_indent_nested_for_stmt = true
160160
csharp_indent_nested_foreach_stmt = true

Src/FileSystemHandler.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,16 @@ void processConfig()
160160
string nextSoFar = soFar + Path.DirectorySeparatorChar + suitablePiece;
161161
string curPath = p + nextSoFar;
162162

163-
if (allowWildcards && !File.Exists(curPath) && !Directory.Exists(curPath) && curPath.Contains('*') && (dirAuth == null || dirAuth(request) == null))
164-
curPath = new DirectoryInfo(p + soFar).GetFileSystemInfos(suitablePiece).Select(fs => fs.FullName).FirstOrDefault() ?? curPath;
163+
if (allowWildcards && !File.Exists(curPath) && !Directory.Exists(curPath) && curPath.Contains('*') && (dirAuth == null || dirAuth(request) == null)
164+
&& new DirectoryInfo(p + soFar).EnumerateFileSystemInfos(suitablePiece).FirstOrDefault() is { } newPiece)
165+
{
166+
nextSoFar = soFar + Path.DirectorySeparatorChar + newPiece.Name;
167+
curPath = newPiece.FullName;
168+
}
165169

166170
if (File.Exists(curPath))
167171
{
168-
soFarUrl += "/" + new DirectoryInfo(p + soFar).GetFiles(suitablePiece)[0].Name.UrlEscape();
172+
soFarUrl += "/" + new DirectoryInfo(p + soFar).EnumerateFiles(suitablePiece).First().Name.UrlEscape();
169173

170174
if (request.Url.Path != soFarUrl)
171175
return handleHeaderProcessor(FileSystemResponseType.Redirect, HttpResponse.Redirect(request.Url.WithPath(soFarUrl)));
@@ -175,7 +179,15 @@ void processConfig()
175179
}
176180
else if (Directory.Exists(curPath))
177181
{
178-
soFarUrl += "/" + new DirectoryInfo(p + soFar).GetDirectories(suitablePiece)[0].Name.UrlEscape();
182+
var examinePiece = suitablePiece;
183+
tryAgain:
184+
var dirs = new DirectoryInfo(p + soFar).GetDirectories(examinePiece);
185+
if (dirs.Length == 0 && examinePiece.EndsWith('.'))
186+
{
187+
examinePiece = examinePiece[..^1];
188+
goto tryAgain;
189+
}
190+
soFarUrl += "/" + dirs[0].Name.UrlEscape();
179191
soFar = nextSoFar;
180192
processConfig();
181193
goto foundOne;
@@ -187,7 +199,7 @@ void processConfig()
187199
return HttpResponse.Create(File.Open(file404, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), file404type, HttpStatusCode._404_NotFound);
188200
throw new HttpNotFoundException(request.Url.WithPathOnly(soFarUrl + "/" + piece).ToHref());
189201

190-
foundOne:;
202+
foundOne:;
191203
}
192204

193205
// If this point is reached, it’s a directory.

0 commit comments

Comments
 (0)