private static void SendLocalResoures(Socket handler, string address) { byte[] msg_bytes = { 0x20 }; string msg_text = StatusCode200; switch (address) { case "/favicon.ico": msg_bytes = System.IO.File.ReadAllBytes(@FaviconPath); break; case "/style.css": msg_text += File.ReadAllText(@CssPath); msg_bytes = Encoding.UTF8.GetBytes(msg_text); break; case "/scripts.js": msg_text += File.ReadAllText(@JsPath); msg_bytes = Encoding.UTF8.GetBytes(msg_text); break; } handler.Send(msg_bytes); Logmsg = "File " + address + " was sent to client" + "\n\n"; Console.WriteLine(Logmsg); LoggingClass.Log(Logmsg); }
private static void SendAnswer(Socket handler, FileInfo[] Files, DirectoryInfo[] Dirs, string BackButton) { string html = StatusCode200; string FolderList = "<h3>Folders</h3><ul style='list-style-type:circle'>"; string FilesList = "<h3>Files</h3><ul style='list-style-type:circle'>"; int i = 0; foreach (FileInfo file in Files) // make list of files with popups { string PrettySize = PrettySizeClass.ToPrettySize(file.Length); FilesList += "<li> <div class='popup' id='" + i + "' onclick='myFunction(this.id)'>" + file.Name + " <span class='popuptext' id='myPopup" + i + "'>" + PrettySize + "</span> </div> </li>"; i++; } foreach (DirectoryInfo dir in Dirs) // make list of folders with links { string url = "http://" + ServerAddress + "?adress=" + Uri.EscapeDataString(dir.FullName); FolderList += "<li>" + "<a href='" + url + "'>" + dir.Name + "</a>" + "</li>"; } FolderList += "</ul>"; FilesList += "</ul>"; html += HtmlParts[0] + "<body>" + GoToRoot + BackButton + FolderList + FilesList + HtmlParts[1]; //make final html byte[] msg = Encoding.UTF8.GetBytes(html); handler.Send(msg); Logmsg = "Answer was sent to client" + "\n\n"; Console.WriteLine(Logmsg); LoggingClass.Log(Logmsg); }
private static void CloseConnection(Socket handler) { Logmsg = "Connection closed."; Console.WriteLine(Logmsg); LoggingClass.Log(Logmsg); handler.Shutdown(SocketShutdown.Both); handler.Close(); }
private static void CheckHtml() { if (HtmlParts.Length != 2) { Logmsg = "Exeption: Wrong html, something wrong with <body> tag" + "\n\n"; Console.WriteLine(Logmsg); LoggingClass.Log(Logmsg); throw new Exception(); } }
private static (string, int) ParseRequest(string data) { Logmsg = "REQUEST: " + data + "\n\n"; Console.Write(Logmsg); LoggingClass.Log(Logmsg); string address = DefaultDir; int ErrorCode = 0; if (data.Length > 0) { string ParamsLine = data.Split('\n')[0]; string ParamsString = ParamsLine.Split(" ")[1]; if (ParamsString != "/") // in case "/" address = DefaultDir { address = ParamsString.Replace("/?adress=", ""); address = Uri.UnescapeDataString(address); } } Logmsg = "ADDRESS: " + address + "\n\n"; Console.Write(Logmsg); LoggingClass.Log(Logmsg); return(address, ErrorCode); }
public void StartListening() { try { IPHostEntry ipHost = Dns.GetHostEntry(Host); IPAddress ipAddr = ipHost.AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, Port); Socket sListener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp); sListener.Bind(ipEndPoint); sListener.Listen(ClientLimit); while (true) { Logmsg = "Waiting for connection at " + ipEndPoint.ToString(); Console.WriteLine(Logmsg); LoggingClass.Log(Logmsg); Socket handler = sListener.Accept(); try // Now, after connection with client, we can catch errors and send answer about specific type of errors to client { string data = null; byte[] bytes = new byte[BufferSize]; while (handler.Connected) { int bytesRec = handler.Receive(bytes); //Enable this for debug //Logmsg = "Reading " + bytesRec + " bytes \n\n"; //Console.WriteLine(Logmsg); //LoggingClass.Log(Logmsg); data += Encoding.ASCII.GetString(bytes, 0, bytesRec); if (Regex.IsMatch(@data, @EndOfRequestPattern) || bytesRec < 1) { break; } } (string address, int Error) = ParseRequest(data); BackButton = GetBackButton(address); ErrorCode = Error; if (address == "/favicon.ico" || address == "/style.css" || address == "/scripts.js") { SendLocalResoures(handler, address); } else { CheckHtml(); (FileInfo[] Files, DirectoryInfo[] Dirs) = GetFilesAndDirs(address); SendAnswer(handler, Files, Dirs, BackButton); } } catch (System.UnauthorizedAccessException ex) { ErrorCode = 1; Logmsg = "Exeption: " + ex.ToString() + "\n\n"; Console.WriteLine(Logmsg); LoggingClass.Log(Logmsg); } catch (System.IO.DirectoryNotFoundException ex) { ErrorCode = 2; Logmsg = "Exeption: " + ex.ToString() + "\n\n"; Console.WriteLine(Logmsg); LoggingClass.Log(Logmsg); } catch (Exception ex) { ErrorCode = 3; //there we can also just answer with 500 code. Logmsg = "Exeption: " + ex.ToString() + "\n\n"; Console.WriteLine(Logmsg); LoggingClass.Log(Logmsg); } if (ErrorCode > 0) { SendWarnAnswer(handler, ErrorCode, BackButton); } //make and send html with error message to client CloseConnection(handler); } } catch (Exception ex) { Logmsg = "Exeption: " + ex.ToString() + "\n\n"; Console.WriteLine(Logmsg); LoggingClass.Log(Logmsg); } }