diff options
-rw-r--r-- | HttpServer.sln | 17 | ||||
-rw-r--r-- | HttpServer.userprefs | 19 | ||||
-rw-r--r-- | HttpServer/HttpServer.csproj | 49 | ||||
-rw-r--r-- | HttpServer/HttpServer.csproj.user | 7 | ||||
-rw-r--r-- | HttpServer/Program.cs | 131 | ||||
-rw-r--r-- | HttpServer/Properties/AssemblyInfo.cs | 26 | ||||
-rw-r--r-- | HttpServer/bin/Debug/HttpServer.exe | bin | 0 -> 8192 bytes | |||
-rw-r--r-- | HttpServer/bin/Debug/HttpServer.pdb | bin | 0 -> 1796 bytes | |||
-rw-r--r-- | HttpServer/htdocs/404.html | 10 | ||||
-rw-r--r-- | HttpServer/htdocs/500.html | 10 | ||||
-rw-r--r-- | HttpServer/htdocs/index.html | 12 | ||||
-rw-r--r-- | HttpServer/htdocs/script.js | 1 | ||||
-rw-r--r-- | HttpServer/htdocs/style.css | 7 | ||||
-rw-r--r-- | HttpServer/obj/x86/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs | 2 | ||||
-rw-r--r-- | HttpServer/obj/x86/Debug/HttpServer.csproj.FilesWrittenAbsolute.txt | 5 | ||||
-rw-r--r-- | HttpServer/obj/x86/Debug/HttpServer.exe | bin | 0 -> 8192 bytes | |||
-rw-r--r-- | HttpServer/obj/x86/Debug/HttpServer.pdb | bin | 0 -> 1796 bytes |
17 files changed, 296 insertions, 0 deletions
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 @@ +<Properties StartupConfiguration="{E9A47513-1D71-4E5D-813C-05B757A2BDD3}|Default"> + <MonoDevelop.Ide.Workbench ActiveDocument="HttpServer/Program.cs"> + <Files> + <File FileName="HttpServer/Program.cs" Line="48" Column="36" /> + <File FileName="HttpServer/htdocs/index.html" Line="10" Column="22" /> + <File FileName="HttpServer/htdocs/style.css" Line="1" Column="1" /> + <File FileName="HttpServer/htdocs/script.js" Line="1" Column="20" /> + <File FileName="HttpServer/htdocs/404.html" Line="7" Column="7" /> + <File FileName="HttpServer/htdocs/500.html" Line="8" Column="109" /> + </Files> + </MonoDevelop.Ide.Workbench> + <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" /> + <MonoDevelop.Ide.DebuggingService.Breakpoints> + <BreakpointStore /> + </MonoDevelop.Ide.DebuggingService.Breakpoints> + <MonoDevelop.Ide.DebuggingService.PinnedWatches /> + <MultiItemStartupConfigurations /> + <MonoDevelop.Ide.ItemProperties.HttpServer PreferredExecutionTarget="MonoDevelop.Default" /> +</Properties>
\ 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 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+ <ProjectGuid>{E9A47513-1D71-4E5D-813C-05B757A2BDD3}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>HttpServer</RootNamespace>
+ <AssemblyName>HttpServer</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug</OutputPath>
+ <DefineConstants>DEBUG;</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ExternalConsole>true</ExternalConsole>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ExternalConsole>true</ExternalConsole>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Folder Include="htdocs\" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="htdocs\index.html" />
+ <None Include="htdocs\style.css" />
+ <None Include="htdocs\script.js" />
+ <None Include="htdocs\404.html" />
+ <None Include="htdocs\500.html" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>
\ 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 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup Condition=" '$(RunConfiguration)' == 'Default' ">
+ <StartAction>Project</StartAction>
+ <StartArguments>127.0.0.1 8080</StartArguments>
+ </PropertyGroup>
+</Project>
\ 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<string, string> requestFields = new Dictionary<string, string>(); + + 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 Binary files differnew file mode 100644 index 0000000..6b85c66 --- /dev/null +++ b/HttpServer/bin/Debug/HttpServer.exe diff --git a/HttpServer/bin/Debug/HttpServer.pdb b/HttpServer/bin/Debug/HttpServer.pdb Binary files differnew file mode 100644 index 0000000..ff11a44 --- /dev/null +++ b/HttpServer/bin/Debug/HttpServer.pdb 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 @@ + +<html> + <head> + <title>404 Not Found</title> + </head> + <body> + <h1>Not Found</h1> + <p>The requested URL was not found on this server.</p> + </body> +</html> 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 @@ + +<html> + <head> + <title>500 Internal Server Error</title> + </head> + <body> + <h1>Internal Server Error</h1> + <p>The server encountered an internal error or misonfiguration and was unable to complete your request</p> + </body> +</html> 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 @@ + +<html> + <head> + <title>Hello world!</title> + <link rel="stylesheet" type="text/css" href="style.css"> + </head> + <body> + <p>Hello world!</p> + + <script src="script.js"></script> + </body> +</html> 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 @@ +// <autogenerated /> +[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 Binary files differnew file mode 100644 index 0000000..6b85c66 --- /dev/null +++ b/HttpServer/obj/x86/Debug/HttpServer.exe diff --git a/HttpServer/obj/x86/Debug/HttpServer.pdb b/HttpServer/obj/x86/Debug/HttpServer.pdb Binary files differnew file mode 100644 index 0000000..ff11a44 --- /dev/null +++ b/HttpServer/obj/x86/Debug/HttpServer.pdb |