| 20 | | string token = tokens[index].ToLower(); |
|---|
| 21 | | foreach (FileSystemInfo fsi in directories) { |
|---|
| 22 | | string name = fsi.Name.ToLower(); |
|---|
| 23 | | if(fsi is DirectoryInfo) { |
|---|
| 24 | | DirectoryInfo dir = (DirectoryInfo)fsi; |
|---|
| 25 | | if(name.Contains(token)) { |
|---|
| 26 | | if(matches.Contains(dir.Parent.FullName)) |
|---|
| 27 | | matches.Remove(dir.Parent.FullName); |
|---|
| 28 | | matches.Add(dir.FullName); |
|---|
| 29 | | FindMatch(dir.FullName, matches, tokens, index+1); |
|---|
| 30 | | } else if (StringMatching.AreNeighbors(token, name, 2)) { |
|---|
| 31 | | if(matches.Contains(dir.Parent.FullName)) |
|---|
| 32 | | matches.Remove(dir.Parent.FullName); |
|---|
| 33 | | matches.Add(dir.FullName); |
|---|
| 34 | | FindMatch(dir.FullName, matches, tokens, index+1); |
|---|
| 35 | | } |
|---|
| 36 | | } else if(fsi is FileInfo) { |
|---|
| 37 | | FileInfo fi = (FileInfo)fsi; |
|---|
| 38 | | if(name.Contains(token)) { |
|---|
| 39 | | if(matches.Contains(fi.DirectoryName)) |
|---|
| 40 | | matches.Remove(fi.DirectoryName); |
|---|
| 41 | | matches.Add(fi.FullName); |
|---|
| 42 | | } else if (StringMatching.AreNeighbors(token, name, 2)) { |
|---|
| 43 | | if(matches.Contains(fi.DirectoryName)) |
|---|
| 44 | | matches.Remove(fi.DirectoryName); |
|---|
| 45 | | matches.Add(fi.FullName); |
|---|
| 46 | | } |
|---|
| 47 | | } |
|---|
| 48 | | } |
|---|
| | 22 | string token = tokens[index].ToLower(); |
|---|
| | 23 | foreach (FileSystemInfo fsi in directories) |
|---|
| | 24 | { |
|---|
| | 25 | string name = fsi.Name.ToLower(); |
|---|
| | 26 | if (fsi is DirectoryInfo) |
|---|
| | 27 | { |
|---|
| | 28 | DirectoryInfo dir = (DirectoryInfo)fsi; |
|---|
| | 29 | if (name.Contains(token)) |
|---|
| | 30 | { |
|---|
| | 31 | if (matches.Contains(dir.Parent.FullName)) |
|---|
| | 32 | matches.Remove(dir.Parent.FullName); |
|---|
| | 33 | matches.Add(dir.FullName); |
|---|
| | 34 | FindMatch(dir.FullName, matches, tokens, index + 1); |
|---|
| | 35 | } |
|---|
| | 36 | else if (StringMatching.AreNeighbors(token, name, 2)) |
|---|
| | 37 | { |
|---|
| | 38 | if (matches.Contains(dir.Parent.FullName)) |
|---|
| | 39 | matches.Remove(dir.Parent.FullName); |
|---|
| | 40 | matches.Add(dir.FullName); |
|---|
| | 41 | FindMatch(dir.FullName, matches, tokens, index + 1); |
|---|
| | 42 | } |
|---|
| | 43 | } |
|---|
| | 44 | else if (fsi is FileInfo) |
|---|
| | 45 | { |
|---|
| | 46 | FileInfo fi = (FileInfo)fsi; |
|---|
| | 47 | if (name.Contains(token)) |
|---|
| | 48 | { |
|---|
| | 49 | if (matches.Contains(fi.DirectoryName)) |
|---|
| | 50 | matches.Remove(fi.DirectoryName); |
|---|
| | 51 | matches.Add(fi.FullName); |
|---|
| | 52 | } |
|---|
| | 53 | else if (StringMatching.AreNeighbors(token, name, 2)) |
|---|
| | 54 | { |
|---|
| | 55 | if (matches.Contains(fi.DirectoryName)) |
|---|
| | 56 | matches.Remove(fi.DirectoryName); |
|---|
| | 57 | matches.Add(fi.FullName); |
|---|
| | 58 | } |
|---|
| | 59 | } |
|---|
| | 60 | } |
|---|
| | 61 | } |
|---|
| | 62 | |
|---|
| | 63 | public void ProcessRequest(HttpContext context) |
|---|
| | 64 | { |
|---|
| | 65 | HttpRequest req = context.Request; |
|---|
| | 66 | HttpResponse resp = context.Response; |
|---|
| | 67 | |
|---|
| | 68 | IList<string> matches = new List<string>(); |
|---|
| | 69 | string[] tokens = req.Path.Trim('/').ToLower().Split('/'); |
|---|
| | 70 | string current = Directory.GetCurrentDirectory(); |
|---|
| | 71 | FindMatch(current, matches, tokens, 0); |
|---|
| | 72 | |
|---|
| | 73 | StringBuilder sb = new StringBuilder(String.Format("<html><head/><body><p>Could not find {0}.</p>", HttpUtility.HtmlEncode(req.Path))); |
|---|
| | 74 | |
|---|
| | 75 | if (matches.Count != 0) |
|---|
| | 76 | { |
|---|
| | 77 | sb.Append("<p>We have however found potential match:</p><ul>"); |
|---|
| | 78 | foreach (string m in matches) |
|---|
| | 79 | { |
|---|
| | 80 | sb.Append(String.Format("<li>{0}</li>", HttpUtility.HtmlEncode(m))); |
|---|
| | 81 | } |
|---|
| | 82 | |
|---|
| | 83 | sb.Append("</ul>"); |
|---|
| | 84 | } |
|---|
| | 85 | sb.Append("<p>Or maybe you'd rather create that resource instead?</p></body></html>"); |
|---|
| | 86 | |
|---|
| | 87 | resp.Write(sb.ToString()); |
|---|
| | 88 | |
|---|
| | 89 | // Even if we build a friendly page, this is still a Page Not Found |
|---|
| | 90 | // let's not break HTTP |
|---|
| | 91 | resp.StatusCode = 404; |
|---|
| | 92 | resp.StatusDescription = "Not Found"; |
|---|
| | 93 | } |
|---|
| | 94 | |
|---|
| | 95 | public bool IsReusable |
|---|
| | 96 | { |
|---|
| | 97 | get { return false; } |
|---|
| | 98 | } |
|---|
| 50 | | |
|---|
| 51 | | public void ProcessRequest(HttpContext context) |
|---|
| 52 | | { |
|---|
| 53 | | HttpRequest req = context.Request; |
|---|
| 54 | | HttpResponse resp = context.Response; |
|---|
| 55 | | |
|---|
| 56 | | IList<string> matches = new List<string>(); |
|---|
| 57 | | string[] tokens = req.Path.Trim('/').ToLower().Split('/'); |
|---|
| 58 | | string current = Directory.GetCurrentDirectory(); |
|---|
| 59 | | FindMatch(current, matches, tokens, 0); |
|---|
| 60 | | |
|---|
| 61 | | StringBuilder sb = new StringBuilder(String.Format("<html><head/><body><p>Could not find {0}.</p>", HttpUtility.HtmlEncode(req.Path))); |
|---|
| 62 | | |
|---|
| 63 | | if(matches.Count != 0) { |
|---|
| 64 | | sb.Append("<p>We have however found potential match:</p><ul>"); |
|---|
| 65 | | foreach(string m in matches) { |
|---|
| 66 | | sb.Append(String.Format("<li>{0}</li>", HttpUtility.HtmlEncode(m))); |
|---|
| 67 | | } |
|---|
| 68 | | |
|---|
| 69 | | sb.Append("</ul>"); |
|---|
| 70 | | } |
|---|
| 71 | | sb.Append("<p>Or maybe you'd rather create that resource instead?</p></body></html>"); |
|---|
| 72 | | |
|---|
| 73 | | resp.Write(sb.ToString()); |
|---|
| 74 | | |
|---|
| 75 | | // Even if we build a friendly page, this is still a Page Not Found |
|---|
| 76 | | // let's not break HTTP |
|---|
| 77 | | resp.StatusCode = 404; |
|---|
| 78 | | resp.StatusDescription = "Not Found"; |
|---|
| 79 | | } |
|---|
| 80 | | |
|---|
| 81 | | public bool IsReusable |
|---|
| 82 | | { |
|---|
| 83 | | get { return false; } |
|---|
| 84 | | } |
|---|
| 85 | | } |
|---|