/** * We need to make sure that the url that we are trying to treat as a file * lies below the document root of the http server so that people can't grab * random files off your computer while this is running. */ public void WriteUrl() { Trace.WriteLine("HttpProcessor: writeURL started from thread \"" + Thread.CurrentThread.Name + "\" " + " ( " + Thread.CurrentThread.ManagedThreadId + " ) " + DateTime.Now.ToLongTimeString()); try { string file = "<html>\r\n" + "<body>\r\n" + "<h1>WinShooter</h1>\r\n" + "<li><a href=\"/patrullista\">Patrullista</a>\r\n" + "<li><a href=\"/resultat\">Resultatlista</a>\r\n" + "</body>\r\n" + "</html>\r\n"; byte[] bytes = null; string contentType = ""; if (_commonCode.GetCompetitionsCount() == 0) { // No competition exist file = "<html>\r\n" + "<body>\r\n" + "<h1>WinShooter</h1>\r\n" + "Ingen tävling finns ännu. Skapa en tävling från klienten.\r\n" + "</body>\r\n" + "</html>\r\n"; } var cache = HttpCache.GetInstance(); if (!cache.IsInCache(_url.ToLower(), _cacheTime)) { switch (_url.ToLower()) { #region Patrullista case "/patrullista": { file = _commonCode.InternetHtmlExportPatrols(); break; } #endregion #region Resultat case "/resultat": { const bool finalResults = false; file = _commonCode.InternetHtmlExportResults(finalResults); file = file.Replace("<head>", "<head>\r\n" + "<meta http-equiv=\"refresh\" content=\"120\">\r\n"); break; } #endregion #region Images case "/img/logga.jpg": { contentType = "image/jpeg"; bytes = Common.CEmbeddedResources.GetEmbeddedResourceBytes( "Allberg.Shooter.Common.WinShooterLogga.jpg"); break; } #endregion } // Make sure it is marked as UTF8 if (bytes == null) { file = file.Replace("<head>", "<head>\r\n<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n"); file = file.Replace("src=\"http://www.allberg.se/WinShooter/", "src=\"/"); bytes = Encoding.UTF8.GetBytes(file); } // Add to cache cache.AddToCache(_url.ToLower(), bytes, contentType); } else { // Page is in cache bytes = cache.ReturnFromCache(_url.ToLower()); contentType = cache.ReturnContentTypeFromCache(_url.ToLower()); } long left = bytes.Length; if (contentType == "") { WriteSuccess(left); } else { WriteSuccess(left, contentType); } // Copy the contents of the file to the stream, ensure that we never write // more than the content length we specified. Just in case the file somehow // changes out from under us, although I don't know if that is possible. _networkStream.Write(bytes, 0, bytes.Length); _networkStream.Flush(); Thread.Sleep(150); } catch (Exception exc) { Trace.WriteLine("HttpProcessor: WriteURL Exception: " + exc + " on thread \"" + Thread.CurrentThread.Name + "\" " + " ( " + Thread.CurrentThread.ManagedThreadId + " ) " + DateTime.Now.ToLongTimeString()); WriteFailure(exc.Message); } Trace.WriteLine("HttpProcessor: WriteUrl ended on thread \"" + Thread.CurrentThread.Name + "\" " + " ( " + Thread.CurrentThread.ManagedThreadId + " ) " + DateTime.Now.ToLongTimeString()); }