/// <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("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() == "/") { var wwwroot = GetWebRoot(); var indexFile = "index.html"; var absolutePath = Path.Combine(wwwroot, indexFile); if (File.Exists(absolutePath)) { // If an index.html file exists in the root, we won't service root requests any longer. return false; } List<String> links = new List<String>(); foreach (var requestHandler in this._requestHandlers.OrderBy(obj => obj.Priority)) { links.Add(String.Format("<li><a href='{0}'>{0}</a> (Priority: {1})</li>", requestHandler.Name, requestHandler.Priority)); } String body = String.Format("<h1>Gnome Server</h1><ul>{0}</ul>", String.Join("", links.ToArray())); var tokens = TemplateHelper.GetTokenReplacements("Home", _requestHandlers, body); var template = TemplateHelper.PopulateTemplate("index", tokens); IResponseFormatter htmlResponseFormatter = new HtmlResponseFormatter(template); htmlResponseFormatter.WriteContent(response); return true; } return false; }
/// <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)); // 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 handlers = _requestHandlers.Where(obj => obj.ShouldHandle(request)).ToArray(); if (handlers.Length > 1) { LogMessage("Handler Resolution Error: Endpoint did not resolve to a single handler!"); } else if (handlers.Length == 1) { var handler = handlers[0]; 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("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); }