HttpListener SearchListener (string host, Uri uri, out ListenerPrefix prefix)
{
prefix = null;
if (uri == null)
return null;
//TODO: We should use a ReaderWriterLock between this and the add/remove operations.
if (host != null) {
int colon = host.IndexOf (':');
if (colon >= 0)
host = host.Substring (0, colon);
}
string path = HttpUtility.UrlDecode (uri.AbsolutePath);
string path_slash = path [path.Length - 1] == '/' ? path : path + "/";
HttpListener best_match = null;
int best_length = -1;
try {
plock.AcquireReaderLock (-1);
if (host != null && host != "") {
foreach (ListenerPrefix p in prefixes.Keys) {
string ppath = p.Path;
if (ppath.Length < best_length)
continue;
if (p.Host == host && (path.StartsWith (ppath) || path_slash.StartsWith (ppath))) {
best_length = ppath.Length;
best_match = (HttpListener) prefixes [p];
prefix = p;
}
}
if (best_length != -1)
return best_match;
}
best_match = MatchFromList (host, path, unhandled, out prefix);
if (best_match != null)
return best_match;
best_match = MatchFromList (host, path, all, out prefix);
if (best_match != null)
return best_match;
} finally {
try {
plock.ReleaseReaderLock ();
} catch {}
}
return null;
}