/// <summary>; /// Handles the specified request. /// </summary> /// <remarks> /// Defers execution to an appropriate request handler, except for requests to the reserved endpoints: <c>~/</c> and <c>~/Log</c>.<br /> /// Returns a default error message if an appropriate request handler can not be found. /// </remarks> private void HandleRequest(HttpListenerRequest request, HttpListenerResponse response) { LogMessage(String.Format("{0} {1}", request.HttpMethod, request.RawUrl)); var simulationManager = Singleton <SimulationManager> .instance; _cityName = simulationManager.m_metaData.m_CityName; // There are two reserved endpoints: "/" and "/Log". // These take precedence over all other request handlers. if (ServiceRoot(request, response)) { return; } if (ServiceLog(request, response)) { return; } // Get the request handler associated with the current request. var handler = _requestHandlers.FirstOrDefault(obj => obj.ShouldHandle(request)); if (handler != null) { try { IResponseFormatter responseFormatterWriter = handler.Handle(request); responseFormatterWriter.WriteContent(response); return; } catch (Exception ex) { String errorBody = String.Format("<h1>An error has occurred!</h1><pre>{0}</pre>", ex); var tokens = TemplateHelper.GetTokenReplacements(_cityName, "Error", _requestHandlers, errorBody); var template = TemplateHelper.PopulateTemplate("index", tokens); IResponseFormatter errorResponseFormatter = new HtmlResponseFormatter(template); errorResponseFormatter.WriteContent(response); return; } } var wwwroot = GetWebRoot(); // At this point, we can guarantee that we don't need any game data, so we can safely start a new thread to perform the remaining tasks. ServiceFileRequest(wwwroot, request, response); }