public void ShouldNotHaveHandleAfterClose() { var f = new MyFile(); f.Open(); f.Close(); Assert.IsTrue(f.Handle == MyFile.NO_HANDLE); }
private void writeFile(MyFile myfile, int statusCode = 200) { writeHeader(statusCode, myfile.GetContentType(), myfile.GetLength()); byte[] buffer = new byte[1024]; while (myfile.Read(buffer, buffer.Length) > 0) { write(buffer); } myfile.Close(); }
public void Run() { MyFile myfile = null; String HtmlPage = null; try { socket_in = socket; socket_out = socket; request = new Request(socket_in, remoteIpEndPoint.ToString()); string actualPath = request.path; IPageHandler _pageHandler = _pageHandlerFactory.Create(actualPath); if (_pageHandler != null) { // Map and parse the relative path to get the actual file info string requestedFile = ConfigurationManager.AppSettings.Get("configpath") + actualPath; switch (request.command) { case "GET": HtmlPage = _pageHandler.HandleGet(request, requestedFile); break; case "POST": _pageHandler.HandlePost(request, requestedFile); break; } } else { myfile = new MyFile(AppConfigProcessor.Get().WebRoot + request.getPath()); } //System.Console.WriteLine(myfile.indexPageExists()); if (AppConfigProcessor.Get().DirectoryBrowsing == true && (myfile != null && myfile.indexPageExists() == true)) { HtmlPage directoryList = new DirectoryList(AppConfigProcessor.Get().WebRoot, request.getPath().Substring(0, request.getPath().LastIndexOf('/'))); writeString(directoryList.getHtmlPage()); } else { if (HtmlPage is string) { writeString(HtmlPage); } else { writeFile(myfile); } } } catch (RedirectException e) { Console.WriteLine(e); writeRedirectString(e.getPath(), 302); } catch (BadRequestException e) { Console.WriteLine(e); myfile = new MyFile((new Error(400)).getHtmlPath(ConfigurationManager.AppSettings.Get("configpath"))); writeFile(myfile, 400); } catch (FileNotFoundException e) { Console.WriteLine(e); myfile = new MyFile((new Error(404)).getHtmlPath(ConfigurationManager.AppSettings.Get("configpath"))); writeFile(myfile, 404); } catch (IOException e) { Console.WriteLine(e); myfile = new MyFile((new Error(500)).getHtmlPath(ConfigurationManager.AppSettings.Get("configpath"))); writeFile(myfile, 500); } catch (Exception e) { Console.WriteLine(e); } if (myfile != null) { myfile.Close(); } }