From fecf27f2cd0fb32248f5876113795b3173fd89d3 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Tue, 10 Oct 2017 11:10:19 +0200 Subject: Initial commit --- HttpServer.sln | 17 +++ HttpServer.userprefs | 19 +++ HttpServer/HttpServer.csproj | 49 ++++++++ HttpServer/HttpServer.csproj.user | 7 ++ HttpServer/Program.cs | 131 +++++++++++++++++++++ HttpServer/Properties/AssemblyInfo.cs | 26 ++++ HttpServer/bin/Debug/HttpServer.exe | Bin 0 -> 8192 bytes HttpServer/bin/Debug/HttpServer.pdb | Bin 0 -> 1796 bytes HttpServer/htdocs/404.html | 10 ++ HttpServer/htdocs/500.html | 10 ++ HttpServer/htdocs/index.html | 12 ++ HttpServer/htdocs/script.js | 1 + HttpServer/htdocs/style.css | 7 ++ ....NETFramework,Version=v4.5.AssemblyAttribute.cs | 2 + .../HttpServer.csproj.FilesWrittenAbsolute.txt | 5 + HttpServer/obj/x86/Debug/HttpServer.exe | Bin 0 -> 8192 bytes HttpServer/obj/x86/Debug/HttpServer.pdb | Bin 0 -> 1796 bytes 17 files changed, 296 insertions(+) create mode 100644 HttpServer.sln create mode 100644 HttpServer.userprefs create mode 100644 HttpServer/HttpServer.csproj create mode 100644 HttpServer/HttpServer.csproj.user create mode 100644 HttpServer/Program.cs create mode 100644 HttpServer/Properties/AssemblyInfo.cs create mode 100644 HttpServer/bin/Debug/HttpServer.exe create mode 100644 HttpServer/bin/Debug/HttpServer.pdb create mode 100644 HttpServer/htdocs/404.html create mode 100644 HttpServer/htdocs/500.html create mode 100644 HttpServer/htdocs/index.html create mode 100644 HttpServer/htdocs/script.js create mode 100644 HttpServer/htdocs/style.css create mode 100644 HttpServer/obj/x86/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs create mode 100644 HttpServer/obj/x86/Debug/HttpServer.csproj.FilesWrittenAbsolute.txt create mode 100644 HttpServer/obj/x86/Debug/HttpServer.exe create mode 100644 HttpServer/obj/x86/Debug/HttpServer.pdb diff --git a/HttpServer.sln b/HttpServer.sln new file mode 100644 index 0000000..a899653 --- /dev/null +++ b/HttpServer.sln @@ -0,0 +1,17 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpServer", "HttpServer\HttpServer.csproj", "{E9A47513-1D71-4E5D-813C-05B757A2BDD3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E9A47513-1D71-4E5D-813C-05B757A2BDD3}.Debug|x86.ActiveCfg = Debug|x86 + {E9A47513-1D71-4E5D-813C-05B757A2BDD3}.Debug|x86.Build.0 = Debug|x86 + {E9A47513-1D71-4E5D-813C-05B757A2BDD3}.Release|x86.ActiveCfg = Release|x86 + {E9A47513-1D71-4E5D-813C-05B757A2BDD3}.Release|x86.Build.0 = Release|x86 + EndGlobalSection +EndGlobal diff --git a/HttpServer.userprefs b/HttpServer.userprefs new file mode 100644 index 0000000..bc6df63 --- /dev/null +++ b/HttpServer.userprefs @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/HttpServer/HttpServer.csproj b/HttpServer/HttpServer.csproj new file mode 100644 index 0000000..449cb25 --- /dev/null +++ b/HttpServer/HttpServer.csproj @@ -0,0 +1,49 @@ + + + + Debug + x86 + {E9A47513-1D71-4E5D-813C-05B757A2BDD3} + Exe + HttpServer + HttpServer + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/HttpServer/HttpServer.csproj.user b/HttpServer/HttpServer.csproj.user new file mode 100644 index 0000000..3a615b2 --- /dev/null +++ b/HttpServer/HttpServer.csproj.user @@ -0,0 +1,7 @@ + + + + Project + 127.0.0.1 8080 + + \ No newline at end of file diff --git a/HttpServer/Program.cs b/HttpServer/Program.cs new file mode 100644 index 0000000..5c084ae --- /dev/null +++ b/HttpServer/Program.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace HttpServer +{ + class MainClass + { + static TcpListener serverSocket; + static bool status; + + public static void Main(string[] args) + { + serverSocket = new TcpListener(IPAddress.Parse(args[0]), int.Parse(args[1])); + serverSocket.Start(); + Console.WriteLine($"Server started on {args[0]}:{args[1]}."); + status = true; + Task.Run(() => HttpListener()); + Console.WriteLine("Server is listening for HTTP requests.\n"); + Task.Run(() => Stopper(args[0], 8081)); + Task.Run(() => Terminal()); + while (status) { }; + } + + static void HttpListener() + { + while (status) + { + if (serverSocket.Pending()) + { + TcpClient connectionSocket = serverSocket.AcceptTcpClient(); + NetworkStream ns = connectionSocket.GetStream(); + StreamReader sr = new StreamReader(ns); + StreamWriter sw = new StreamWriter(ns); + sw.AutoFlush = true; + + try + { + bool firstLine = true; + string[] requestLine; + string requestedFile = ""; + Dictionary requestFields = new Dictionary(); + + while (connectionSocket.Connected) + { + string request = sr.ReadLine(); + if (request == "") break; + if (firstLine) + { + requestLine = request.Split(' '); + requestedFile = requestLine[1]; + firstLine = false; + } + else + { + string[] requestField = Regex.Split(request, @": "); + requestFields.Add(requestField[0], requestField[1]); + } + } + if (requestedFile == "/") requestedFile = "/index.html"; + + string responseLine = "HTTP/1.1 200 OK"; + + string contentType; + requestFields.TryGetValue("Accept", out contentType); + if (contentType.Contains("text/html")) contentType = "text/html"; + else if (contentType.Contains("text/css")) contentType = "text/css"; + else if (contentType.Contains("text/javascript")) contentType = "text/javascript"; + + if (!File.Exists($"../../htdocs{requestedFile}")) + { + responseLine = "HTTP/1.1 404 Not Found"; + contentType = "text/html"; + requestedFile = "/404.html"; + } + + sw.WriteLine($"{responseLine}\n" + + "Server: Marcin's HTTP Server\n" + + //"Content-Encoding: gzip\n" + + $"Content-Type: {contentType}\n" + + "Accept-Ranges: bytes\n" + + $"Date: {DateTime.Now}\n" + + "Connection: close\n"); + using (FileStream fs = File.Open($"../../htdocs{requestedFile}", FileMode.Open)) fs.CopyTo(ns); + + } + catch + { + sw.WriteLine("HTTP/1.1 500 Internal Server Error\n" + + "Server: Marcin's HTTP Server\n" + + //"Content-Encoding: gzip\n" + + "Content-Type: text/html\n" + + "Accept-Ranges: bytes\n" + + "Date: {DateTime.Now}\n" + + "Connection: close\n"); + using (FileStream fs = File.Open($"../../htdocs/500.html", FileMode.Open)) fs.CopyTo(ns); + } + finally + { + connectionSocket.Close(); + } + } + } + } + + static void Stopper(string ipAddress, int port) + { + TcpListener stopperSocket = new TcpListener(IPAddress.Parse(ipAddress), port); + stopperSocket.Start(); + TcpClient connectionSocket = stopperSocket.AcceptTcpClient(); + Console.WriteLine("\nServer was shut down remotely."); + status = false; + } + + static void Terminal() + { + string command = ""; + while (status && command != "quit") + { + Console.Write("$ "); + command = Console.ReadLine(); + } + Console.WriteLine("Server is going to close now..."); + status = false; + } + } +} diff --git a/HttpServer/Properties/AssemblyInfo.cs b/HttpServer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..be74a0d --- /dev/null +++ b/HttpServer/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("HttpServer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("${AuthorCopyright}")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/HttpServer/bin/Debug/HttpServer.exe b/HttpServer/bin/Debug/HttpServer.exe new file mode 100644 index 0000000..6b85c66 Binary files /dev/null and b/HttpServer/bin/Debug/HttpServer.exe differ diff --git a/HttpServer/bin/Debug/HttpServer.pdb b/HttpServer/bin/Debug/HttpServer.pdb new file mode 100644 index 0000000..ff11a44 Binary files /dev/null and b/HttpServer/bin/Debug/HttpServer.pdb differ diff --git a/HttpServer/htdocs/404.html b/HttpServer/htdocs/404.html new file mode 100644 index 0000000..791f7f5 --- /dev/null +++ b/HttpServer/htdocs/404.html @@ -0,0 +1,10 @@ + + + + 404 Not Found + + +

Not Found

+

The requested URL was not found on this server.

+ + diff --git a/HttpServer/htdocs/500.html b/HttpServer/htdocs/500.html new file mode 100644 index 0000000..013460e --- /dev/null +++ b/HttpServer/htdocs/500.html @@ -0,0 +1,10 @@ + + + + 500 Internal Server Error + + +

Internal Server Error

+

The server encountered an internal error or misonfiguration and was unable to complete your request

+ + diff --git a/HttpServer/htdocs/index.html b/HttpServer/htdocs/index.html new file mode 100644 index 0000000..28163b4 --- /dev/null +++ b/HttpServer/htdocs/index.html @@ -0,0 +1,12 @@ + + + + Hello world! + + + +

Hello world!

+ + + + diff --git a/HttpServer/htdocs/script.js b/HttpServer/htdocs/script.js new file mode 100644 index 0000000..0ec2e21 --- /dev/null +++ b/HttpServer/htdocs/script.js @@ -0,0 +1 @@ +console.log("test") \ No newline at end of file diff --git a/HttpServer/htdocs/style.css b/HttpServer/htdocs/style.css new file mode 100644 index 0000000..e94179b --- /dev/null +++ b/HttpServer/htdocs/style.css @@ -0,0 +1,7 @@ +body { + background: black; + } + +p { + color: white; + } \ No newline at end of file diff --git a/HttpServer/obj/x86/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs b/HttpServer/obj/x86/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs new file mode 100644 index 0000000..fdcb678 --- /dev/null +++ b/HttpServer/obj/x86/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs @@ -0,0 +1,2 @@ +// +[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.5", FrameworkDisplayName = "")] diff --git a/HttpServer/obj/x86/Debug/HttpServer.csproj.FilesWrittenAbsolute.txt b/HttpServer/obj/x86/Debug/HttpServer.csproj.FilesWrittenAbsolute.txt new file mode 100644 index 0000000..4e3f0cb --- /dev/null +++ b/HttpServer/obj/x86/Debug/HttpServer.csproj.FilesWrittenAbsolute.txt @@ -0,0 +1,5 @@ +/home/marcin/MonoDevelop Projects/HttpServer/HttpServer/obj/x86/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs +/home/marcin/MonoDevelop Projects/HttpServer/HttpServer/bin/Debug/HttpServer.pdb +/home/marcin/MonoDevelop Projects/HttpServer/HttpServer/bin/Debug/HttpServer.exe +/home/marcin/MonoDevelop Projects/HttpServer/HttpServer/obj/x86/Debug/HttpServer.exe +/home/marcin/MonoDevelop Projects/HttpServer/HttpServer/obj/x86/Debug/HttpServer.pdb diff --git a/HttpServer/obj/x86/Debug/HttpServer.exe b/HttpServer/obj/x86/Debug/HttpServer.exe new file mode 100644 index 0000000..6b85c66 Binary files /dev/null and b/HttpServer/obj/x86/Debug/HttpServer.exe differ diff --git a/HttpServer/obj/x86/Debug/HttpServer.pdb b/HttpServer/obj/x86/Debug/HttpServer.pdb new file mode 100644 index 0000000..ff11a44 Binary files /dev/null and b/HttpServer/obj/x86/Debug/HttpServer.pdb differ -- cgit v1.2.3