/// <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); }
/// <summary> /// Services requests to <c>~/Log</c> /// </summary> private Boolean ServiceLog(HttpListenerRequest request, HttpListenerResponse response) { if (request.Url.AbsolutePath.ToLower() == "/log") { { String body = String.Format("<h1>Server Log</h1><pre>{0}</pre>", String.Join("", _logLines.ToArray())); var tokens = TemplateHelper.GetTokenReplacements(_cityName, "Log", _requestHandlers, body); var template = TemplateHelper.PopulateTemplate("index", tokens); IResponseFormatter htmlResponseFormatter = new HtmlResponseFormatter(template); htmlResponseFormatter.WriteContent(response); return(true); } } return(false); }
/// <summary> /// Services requests to <c>~/</c> /// </summary> private Boolean ServiceRoot(HttpListenerRequest request, HttpListenerResponse response) { if (request.Url.AbsolutePath.ToLower() == "/") { List <String> links = new List <String>(); foreach (var requestHandler in this._requestHandlers.OrderBy(obj => obj.Priority)) { links.Add(String.Format("<li><a href='{1}'>{0}</a> by {2} (Priority: {3})</li>", requestHandler.Name, requestHandler.MainPath, requestHandler.Author, requestHandler.Priority)); } String body = String.Format("<h1>Cities: Skylines - Integrated Web Server</h1><ul>{0}</ul>", String.Join("", links.ToArray())); var tokens = TemplateHelper.GetTokenReplacements(_cityName, "Home", _requestHandlers, body); var template = TemplateHelper.PopulateTemplate("index", tokens); IResponseFormatter htmlResponseFormatter = new HtmlResponseFormatter(template); htmlResponseFormatter.WriteContent(response); return(true); } return(false); }