internal void HandleRequest(HTTPRequest request, HTTPResponse response, Stream stream)
{
try
{
bool matchFound = false;
RouteMapper mapper = null;
int i = 0;
for (;;)
{
lock (_routes)
{
if (i < _routes.Count)
{
mapper = _routes[i++];
}
else
{
break;
}
}
string regex = "^" + mapper.Route;
if (!mapper.AcceptSubroutes)
{
regex += "$";
}
if (Regex.IsMatch(request.BaseURL, regex))
{
if ((mapper.Methods & request.Method) != 0)
{
if (mapper.AcceptSubroutes)
{
int pos = request.BaseURL.IndexOf(mapper.Route);
request.Subroute = request.BaseURL.Substring(0, pos) + request.BaseURL.Substring(pos + mapper.Route.Length);
}
matchFound = true;
break;
}
}
}
if (!matchFound)
{
response.WriteError(HTTPStatusCode.NotFound, "Not Found");
response.Respond(stream);
OnError(RoutingErrorType.NO_MATCHES, "No route found for request", request);
}
else
{
mapper.HandleRequest(request, response);
response.Respond(stream);
}
}
catch (Exception e)
{
response.WriteError(HTTPStatusCode.InternalServerError, String.Format("Internal Server Error {0}", e.Message));
response.Respond(stream);
OnError(RoutingErrorType.INTERNAL, "A RouteHandler threw an exception.", request);
}
}