From 24e3b82d5b8543797ba55f41d35c36fb9639e6f1 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 22 Oct 2021 16:07:38 +0100 Subject: [PATCH 1/8] Add SimpleHttpServer --- .../Aura_OS/Properties/VersionInfo.cs | 2 +- .../Network/SimpleHttpServer/HttpBuilder.cs | 41 ++ .../Network/SimpleHttpServer/HttpProcessor.cs | 191 +++++ .../Network/SimpleHttpServer/HttpServer.cs | 54 ++ .../SimpleHttpServer/Models/HttpDiscussion.cs | 14 + .../SimpleHttpServer/Models/HttpRequest.cs | 33 + .../SimpleHttpServer/Models/HttpResponse.cs | 72 ++ .../Network/SimpleHttpServer/Models/Route.cs | 17 + .../RouteHandlers/FileSystemRouteHandler.cs | 683 ++++++++++++++++++ .../SimpleHttpServer/SimpleHttpServer.csproj | 92 +++ .../System/Shell/cmdIntr/CommandManager.cs | 1 + .../Shell/cmdIntr/Network/HttpServer.cs | 78 ++ 12 files changed, 1277 insertions(+), 1 deletion(-) create mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs create mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs create mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs create mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpDiscussion.cs create mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs create mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpResponse.cs create mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs create mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/RouteHandlers/FileSystemRouteHandler.cs create mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/SimpleHttpServer.csproj create mode 100644 Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs diff --git a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs index 5b423ed7..094562f4 100644 --- a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs +++ b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs @@ -2,6 +2,6 @@ namespace Aura_OS { public class VersionInfo { - public static string revision = "221020210008"; + public static string revision = "221020211603"; } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs new file mode 100644 index 00000000..b24e7a79 --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs @@ -0,0 +1,41 @@ +using SimpleHttpServer.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SimpleHttpServer +{ + class HttpBuilder + { + public static HttpResponse InternalServerError() + { + string content = @"

Internal Server Error

+ +by SimpleHtppServer"; + + return new HttpResponse() + { + ReasonPhrase = "InternalServerError", + StatusCode = "500", + ContentAsUTF8 = content + }; + } + + public static HttpResponse NotFound() + { + string content = @"

Not Found

+ +by SimpleHtppServer"; + + return new HttpResponse() + { + ReasonPhrase = "NotFound", + StatusCode = "404", + ContentAsUTF8 = content + }; + } + } +} diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs new file mode 100644 index 00000000..4999218c --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs @@ -0,0 +1,191 @@ +// Copyright (C) 2016 by David Jeske, Barend Erasmus and donated to the public domain + +using Cosmos.System.Network.IPv4; +using Cosmos.System.Network.IPv4.TCP; +using SimpleHttpServer.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +namespace SimpleHttpServer +{ + public class HttpProcessor + { + + #region Fields + + private static int MAX_POST_SIZE = 10 * 1024 * 1024; // 10MB + + private List Routes = new List(); + + #endregion + + #region Constructors + + public HttpProcessor() + { + } + + #endregion + + #region Public Methods + public void HandleClient(TcpClient tcpClient) + { + HttpRequest request = GetRequest(tcpClient); + + // route and handle the request... + HttpResponse response = RouteRequest(tcpClient, request); + + Console.WriteLine("{0} {1}",response.StatusCode,request.Url); + // build a default response for errors + if (response.Content == null) { + if (response.StatusCode != "200") { + response.ContentAsUTF8 = string.Format("{0} {1}

{2}", response.StatusCode, request.Url, response.ReasonPhrase); + } + } + + WriteResponse(tcpClient, response); + } + + // this formats the HTTP response... + private static void WriteResponse(TcpClient client, HttpResponse response) { + if (response.Content == null) { + response.Content = new byte[]{}; + } + + // default to text/html content type + if (!response.Headers.ContainsKey("Content-Type")) { + response.Headers["Content-Type"] = "text/html"; + } + + response.Headers["Content-Length"] = response.Content.Length.ToString(); + + var sb = new StringBuilder(); + sb.Append(string.Format("HTTP/1.0 {0} {1}\r\n", response.StatusCode, response.ReasonPhrase)); + sb.Append(string.Join("\r\n", response.Headers.Select(x => string.Format("{0}: {1}", x.Key, x.Value)))); + sb.Append("\r\n\r\n"); + + client.Send(Encoding.ASCII.GetBytes(sb.ToString())); + } + + public void AddRoute(Route route) + { + this.Routes.Add(route); + } + + #endregion + + #region Private Methods + + protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest request) + { + if (!Routes.Any()) + return HttpBuilder.NotFound(); + + var route = Routes.SingleOrDefault(x => x.Method == request.Method); + + if (route == null) + { + return new HttpResponse() + { + ReasonPhrase = "Method Not Allowed", + StatusCode = "405", + + }; + } + + // trigger the route handler... + request.Route = route; + try { + var discussion = new HttpDiscussion() { Request = request, Response = null }; + route.Callable(discussion); + return discussion.Response; + } catch(Exception ex) { + Console.WriteLine(ex); + return HttpBuilder.InternalServerError(); + } + } + + private HttpRequest GetRequest(TcpClient client) + { + var ep = new EndPoint(Address.Zero, 0); + //Read Request Line + string request = Encoding.ASCII.GetString(client.Receive(ref ep)); + + var lines = request.Split("\r\n"); + + string[] tokens = lines[0].Split(' '); + if (tokens.Length != 3) + { + throw new Exception("invalid http request line"); + } + string method = tokens[0].ToUpper(); + string url = tokens[1]; + string protocolVersion = tokens[2]; + + //Read Headers + Dictionary headers = new Dictionary(); + string line; + + for (int i = 1; i < lines.Length; i++) + { + if (lines[i].Equals("")) + { + break; + } + + int separator = lines[i].IndexOf(':'); + if (separator == -1) + { + throw new Exception("invalid http header line: " + lines[i]); + } + string name = lines[i].Substring(0, separator); + int pos = separator + 1; + while ((pos < lines[i].Length) && (lines[i][pos] == ' ')) + { + pos++; + } + + string value = lines[i].Substring(pos, lines[i].Length - pos); + headers.Add(name, value); + } + + /* + + string content = null; + if (headers.ContainsKey("Content-Length")) + { + int totalBytes = Convert.ToInt32(headers["Content-Length"]); + int bytesLeft = totalBytes; + byte[] bytes = new byte[totalBytes]; + + while(bytesLeft > 0) + { + byte[] buffer = new byte[bytesLeft > 1024? 1024 : bytesLeft]; + int n = inputStream.Read(buffer, 0, buffer.Length); + buffer.CopyTo(bytes, totalBytes - bytesLeft); + + bytesLeft -= n; + } + + content = Encoding.ASCII.GetString(bytes); + }*/ + + + return new HttpRequest() + { + Method = method, + Url = url, + Headers = headers, + Content = "content" //TODO: add content + }; + } + + #endregion + + + } +} diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs new file mode 100644 index 00000000..067664bb --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs @@ -0,0 +1,54 @@ +// Copyright (C) 2016 by David Jeske, Barend Erasmus and donated to the public domain + +using Cosmos.System.Network.IPv4.TCP; +using SimpleHttpServer; +using SimpleHttpServer.Models; +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; + +namespace SimpleHttpServer +{ + + public class HttpServer + { + #region Fields + + private int Port; + private TcpListener Listener; + private HttpProcessor Processor; + private bool IsActive = true; + + #endregion + + #region Public Methods + public HttpServer(int port, List routes) + { + this.Port = port; + this.Processor = new HttpProcessor(); + + foreach (var route in routes) + { + this.Processor.AddRoute(route); + } + } + + public void Listen() + { + this.Listener = new TcpListener((ushort)Port); + this.Listener.Start(); + while (this.IsActive) + { + TcpClient s = this.Listener.AcceptTcpClient(); + this.Processor.HandleClient(s); + } + } + + #endregion + + } +} + + + diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpDiscussion.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpDiscussion.cs new file mode 100644 index 00000000..d81f7bee --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpDiscussion.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SimpleHttpServer.Models +{ + public class HttpDiscussion + { + public HttpRequest Request; + public HttpResponse Response; + } +} diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs new file mode 100644 index 00000000..062dc447 --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs @@ -0,0 +1,33 @@ +// Copyright (C) 2016 by Barend Erasmus and donated to the public domain + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SimpleHttpServer.Models +{ + public class HttpRequest + { + public string Method { get; set; } + public string Url { get; set; } + public string Path { get; set; } // either the Url, or the first regex group + public string Content { get; set; } + public Route Route { get; set; } + public Dictionary Headers { get; set; } + + public HttpRequest() + { + this.Headers = new Dictionary(); + } + + public override string ToString() + { + if (!string.IsNullOrWhiteSpace(this.Content)) + if (!this.Headers.ContainsKey("Content-Length")) + this.Headers.Add("Content-Length", this.Content.Length.ToString()); + + return string.Format("{0} {1} HTTP/1.0\r\n{2}\r\n\r\n{3}", this.Method, this.Url, string.Join("\r\n", this.Headers.Select(x => string.Format("{0}: {1}", x.Key, x.Value))), this.Content); + } + } +} diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpResponse.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpResponse.cs new file mode 100644 index 00000000..3d963af2 --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpResponse.cs @@ -0,0 +1,72 @@ +// Copyright (C) 2016 by Barend Erasmus and donated to the public domain + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +// NOTE: two consequences of this simplified response model are: +// +// (a) it's not possible to send 8-bit clean responses (like file content) +// (b) it's +// must be loaded into memory in the the Content property. If you want to send large files, +// this has to be reworked so a handler can write to the output stream instead. + +namespace SimpleHttpServer.Models +{ + public enum HttpStatusCode + { + // for a full list of status codes, see.. + // https://en.wikipedia.org/wiki/List_of_HTTP_status_codes + + Continue = 100, + + Ok = 200, + Created = 201, + Accepted = 202, + MovedPermanently = 301, + Found = 302, + NotModified = 304, + BadRequest = 400, + Forbidden = 403, + NotFound = 404, + MethodNotAllowed = 405, + InternalServerError = 500 + } + + public class HttpResponse + { + public string StatusCode { get; set; } + public string ReasonPhrase { get; set; } + public byte[] Content { get; set; } + + public Dictionary Headers { get; set; } + + public string ContentAsUTF8 + { + set + { + this.setContent(value, encoding: Encoding.UTF8); + } + } + public void setContent(string content, Encoding encoding = null) + { + if (encoding == null) + { + encoding = Encoding.UTF8; + } + Content = encoding.GetBytes(content); + } + + public HttpResponse() + { + this.Headers = new Dictionary(); + } + + // informational only tostring... + public override string ToString() + { + return string.Format("HTTP status {0} {1}", this.StatusCode, this.ReasonPhrase); + } + } +} diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs new file mode 100644 index 00000000..1fea557f --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs @@ -0,0 +1,17 @@ +// Copyright (C) 2016 by Barend Erasmus and donated to the public domain + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace SimpleHttpServer.Models +{ + public class Route + { + public string Name { get; set; } // descriptive name for debugging + public string Method { get; set; } + + public Action Callable; + } +} diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/RouteHandlers/FileSystemRouteHandler.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/RouteHandlers/FileSystemRouteHandler.cs new file mode 100644 index 00000000..4c0f5c94 --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/RouteHandlers/FileSystemRouteHandler.cs @@ -0,0 +1,683 @@ +// Copyright (C) 2016 by Barend Erasmus, David Jeske and donated to the public domain + +using SimpleHttpServer.Models; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SimpleHttpServer.RouteHandlers +{ + /* + public class FileSystemRouteHandler + { + + public string BasePath { get; set; } + public bool ShowDirectories { get; set; } + + public HttpResponse Handle(HttpRequest request) { + var url_part = request.Path; + + // do some basic sanitization of the URL, attempting to make sure they can't read files outside the basepath + // NOTE: this is probably not bulletproof/secure + url_part = url_part.Replace("\\..\\", "\\"); + url_part = url_part.Replace("/../", "/"); + url_part = url_part.Replace("//","/"); + url_part = url_part.Replace(@"\\",@"\"); + url_part = url_part.Replace(":",""); + url_part = url_part.Replace("/",Path.DirectorySeparatorChar.ToString()); + + // make sure the first part of the path is not + if (url_part.Length > 0) { + var first_char = url_part.ElementAt(0); + if (first_char == '/' || first_char == '\\') { + url_part = "." + url_part; + } + } + var local_path = Path.Combine(this.BasePath, url_part); + + if (ShowDirectories && Directory.Exists(local_path)) { + // Console.WriteLine("FileSystemRouteHandler Dir {0}",local_path); + return Handle_LocalDir(request, local_path); + } else if (File.Exists(local_path)) { + // Console.WriteLine("FileSystemRouteHandler File {0}", local_path); + return Handle_LocalFile(request, local_path); + } else { + return new HttpResponse { + StatusCode = "404", + ReasonPhrase = string.Format("Not Found ({0}) handler({1})",local_path,request.Route.Name), + }; + } + } + + HttpResponse Handle_LocalFile(HttpRequest request, string local_path) { + var file_extension = Path.GetExtension(local_path); + + var response = new HttpResponse(); + response.StatusCode = "200"; + response.ReasonPhrase = "Ok"; + response.Headers["Content-Type"] = QuickMimeTypeMapper.GetMimeType(file_extension); + response.Content = File.ReadAllBytes(local_path); + + return response; + } + + HttpResponse Handle_LocalDir(HttpRequest request, string local_path) { + var output = new StringBuilder(); + output.Append(string.Format("

Directory: {0}

",request.Url)); + + foreach (var entry in Directory.GetFiles(local_path)) { + var file_info = new System.IO.FileInfo(entry); + + var filename = file_info.Name; + output.Append(string.Format("{1}
",filename,filename)); + } + + return new HttpResponse() { + StatusCode = "200", + ReasonPhrase = "Ok", + ContentAsUTF8 = output.ToString(), + }; + } + } + + // HTTP requires that resposnes contain the proper MIME type. This quick mapping list below + // contains many more mimetypes than System.Web.MimeMapping + + // http://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension + + public class QuickMimeTypeMapper + { + + public static string GetMimeType(string extension) { + if (extension == null) { + throw new ArgumentNullException("extension"); + } + + if (!extension.StartsWith(".")) { + extension = "." + extension; + } + + string mime; + + return _mappings.TryGetValue(extension, out mime) ? mime : "application/octet-stream"; + } + + + private static IDictionary _mappings = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { + + #region Big freaking list of mime types + + // combination of values from Windows 7 Registry and + // from C:\Windows\System32\inetsrv\config\applicationHost.config + // some added, including .7z and .dat + {".323", "text/h323"}, + {".3g2", "video/3gpp2"}, + {".3gp", "video/3gpp"}, + {".3gp2", "video/3gpp2"}, + {".3gpp", "video/3gpp"}, + {".7z", "application/x-7z-compressed"}, + {".aa", "audio/audible"}, + {".AAC", "audio/aac"}, + {".aaf", "application/octet-stream"}, + {".aax", "audio/vnd.audible.aax"}, + {".ac3", "audio/ac3"}, + {".aca", "application/octet-stream"}, + {".accda", "application/msaccess.addin"}, + {".accdb", "application/msaccess"}, + {".accdc", "application/msaccess.cab"}, + {".accde", "application/msaccess"}, + {".accdr", "application/msaccess.runtime"}, + {".accdt", "application/msaccess"}, + {".accdw", "application/msaccess.webapplication"}, + {".accft", "application/msaccess.ftemplate"}, + {".acx", "application/internet-property-stream"}, + {".AddIn", "text/xml"}, + {".ade", "application/msaccess"}, + {".adobebridge", "application/x-bridge-url"}, + {".adp", "application/msaccess"}, + {".ADT", "audio/vnd.dlna.adts"}, + {".ADTS", "audio/aac"}, + {".afm", "application/octet-stream"}, + {".ai", "application/postscript"}, + {".aif", "audio/x-aiff"}, + {".aifc", "audio/aiff"}, + {".aiff", "audio/aiff"}, + {".air", "application/vnd.adobe.air-application-installer-package+zip"}, + {".amc", "application/x-mpeg"}, + {".application", "application/x-ms-application"}, + {".art", "image/x-jg"}, + {".asa", "application/xml"}, + {".asax", "application/xml"}, + {".ascx", "application/xml"}, + {".asd", "application/octet-stream"}, + {".asf", "video/x-ms-asf"}, + {".ashx", "application/xml"}, + {".asi", "application/octet-stream"}, + {".asm", "text/plain"}, + {".asmx", "application/xml"}, + {".aspx", "application/xml"}, + {".asr", "video/x-ms-asf"}, + {".asx", "video/x-ms-asf"}, + {".atom", "application/atom+xml"}, + {".au", "audio/basic"}, + {".avi", "video/x-msvideo"}, + {".axs", "application/olescript"}, + {".bas", "text/plain"}, + {".bcpio", "application/x-bcpio"}, + {".bin", "application/octet-stream"}, + {".bmp", "image/bmp"}, + {".c", "text/plain"}, + {".cab", "application/octet-stream"}, + {".caf", "audio/x-caf"}, + {".calx", "application/vnd.ms-office.calx"}, + {".cat", "application/vnd.ms-pki.seccat"}, + {".cc", "text/plain"}, + {".cd", "text/plain"}, + {".cdda", "audio/aiff"}, + {".cdf", "application/x-cdf"}, + {".cer", "application/x-x509-ca-cert"}, + {".chm", "application/octet-stream"}, + {".class", "application/x-java-applet"}, + {".clp", "application/x-msclip"}, + {".cmx", "image/x-cmx"}, + {".cnf", "text/plain"}, + {".cod", "image/cis-cod"}, + {".config", "application/xml"}, + {".contact", "text/x-ms-contact"}, + {".coverage", "application/xml"}, + {".cpio", "application/x-cpio"}, + {".cpp", "text/plain"}, + {".crd", "application/x-mscardfile"}, + {".crl", "application/pkix-crl"}, + {".crt", "application/x-x509-ca-cert"}, + {".cs", "text/plain"}, + {".csdproj", "text/plain"}, + {".csh", "application/x-csh"}, + {".csproj", "text/plain"}, + {".css", "text/css"}, + {".csv", "text/csv"}, + {".cur", "application/octet-stream"}, + {".cxx", "text/plain"}, + {".dat", "application/octet-stream"}, + {".datasource", "application/xml"}, + {".dbproj", "text/plain"}, + {".dcr", "application/x-director"}, + {".def", "text/plain"}, + {".deploy", "application/octet-stream"}, + {".der", "application/x-x509-ca-cert"}, + {".dgml", "application/xml"}, + {".dib", "image/bmp"}, + {".dif", "video/x-dv"}, + {".dir", "application/x-director"}, + {".disco", "text/xml"}, + {".dll", "application/x-msdownload"}, + {".dll.config", "text/xml"}, + {".dlm", "text/dlm"}, + {".doc", "application/msword"}, + {".docm", "application/vnd.ms-word.document.macroEnabled.12"}, + {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, + {".dot", "application/msword"}, + {".dotm", "application/vnd.ms-word.template.macroEnabled.12"}, + {".dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template"}, + {".dsp", "application/octet-stream"}, + {".dsw", "text/plain"}, + {".dtd", "text/xml"}, + {".dtsConfig", "text/xml"}, + {".dv", "video/x-dv"}, + {".dvi", "application/x-dvi"}, + {".dwf", "drawing/x-dwf"}, + {".dwp", "application/octet-stream"}, + {".dxr", "application/x-director"}, + {".eml", "message/rfc822"}, + {".emz", "application/octet-stream"}, + {".eot", "application/octet-stream"}, + {".eps", "application/postscript"}, + {".etl", "application/etl"}, + {".etx", "text/x-setext"}, + {".evy", "application/envoy"}, + {".exe", "application/octet-stream"}, + {".exe.config", "text/xml"}, + {".fdf", "application/vnd.fdf"}, + {".fif", "application/fractals"}, + {".filters", "Application/xml"}, + {".fla", "application/octet-stream"}, + {".flr", "x-world/x-vrml"}, + {".flv", "video/x-flv"}, + {".fsscript", "application/fsharp-script"}, + {".fsx", "application/fsharp-script"}, + {".generictest", "application/xml"}, + {".gif", "image/gif"}, + {".group", "text/x-ms-group"}, + {".gsm", "audio/x-gsm"}, + {".gtar", "application/x-gtar"}, + {".gz", "application/x-gzip"}, + {".h", "text/plain"}, + {".hdf", "application/x-hdf"}, + {".hdml", "text/x-hdml"}, + {".hhc", "application/x-oleobject"}, + {".hhk", "application/octet-stream"}, + {".hhp", "application/octet-stream"}, + {".hlp", "application/winhlp"}, + {".hpp", "text/plain"}, + {".hqx", "application/mac-binhex40"}, + {".hta", "application/hta"}, + {".htc", "text/x-component"}, + {".htm", "text/html"}, + {".html", "text/html"}, + {".htt", "text/webviewhtml"}, + {".hxa", "application/xml"}, + {".hxc", "application/xml"}, + {".hxd", "application/octet-stream"}, + {".hxe", "application/xml"}, + {".hxf", "application/xml"}, + {".hxh", "application/octet-stream"}, + {".hxi", "application/octet-stream"}, + {".hxk", "application/xml"}, + {".hxq", "application/octet-stream"}, + {".hxr", "application/octet-stream"}, + {".hxs", "application/octet-stream"}, + {".hxt", "text/html"}, + {".hxv", "application/xml"}, + {".hxw", "application/octet-stream"}, + {".hxx", "text/plain"}, + {".i", "text/plain"}, + {".ico", "image/x-icon"}, + {".ics", "application/octet-stream"}, + {".idl", "text/plain"}, + {".ief", "image/ief"}, + {".iii", "application/x-iphone"}, + {".inc", "text/plain"}, + {".inf", "application/octet-stream"}, + {".inl", "text/plain"}, + {".ins", "application/x-internet-signup"}, + {".ipa", "application/x-itunes-ipa"}, + {".ipg", "application/x-itunes-ipg"}, + {".ipproj", "text/plain"}, + {".ipsw", "application/x-itunes-ipsw"}, + {".iqy", "text/x-ms-iqy"}, + {".isp", "application/x-internet-signup"}, + {".ite", "application/x-itunes-ite"}, + {".itlp", "application/x-itunes-itlp"}, + {".itms", "application/x-itunes-itms"}, + {".itpc", "application/x-itunes-itpc"}, + {".IVF", "video/x-ivf"}, + {".jar", "application/java-archive"}, + {".java", "application/octet-stream"}, + {".jck", "application/liquidmotion"}, + {".jcz", "application/liquidmotion"}, + {".jfif", "image/pjpeg"}, + {".jnlp", "application/x-java-jnlp-file"}, + {".jpb", "application/octet-stream"}, + {".jpe", "image/jpeg"}, + {".jpeg", "image/jpeg"}, + {".jpg", "image/jpeg"}, + {".js", "application/x-javascript"}, + {".json", "application/json"}, + {".jsx", "text/jscript"}, + {".jsxbin", "text/plain"}, + {".latex", "application/x-latex"}, + {".library-ms", "application/windows-library+xml"}, + {".lit", "application/x-ms-reader"}, + {".loadtest", "application/xml"}, + {".lpk", "application/octet-stream"}, + {".lsf", "video/x-la-asf"}, + {".lst", "text/plain"}, + {".lsx", "video/x-la-asf"}, + {".lzh", "application/octet-stream"}, + {".m13", "application/x-msmediaview"}, + {".m14", "application/x-msmediaview"}, + {".m1v", "video/mpeg"}, + {".m2t", "video/vnd.dlna.mpeg-tts"}, + {".m2ts", "video/vnd.dlna.mpeg-tts"}, + {".m2v", "video/mpeg"}, + {".m3u", "audio/x-mpegurl"}, + {".m3u8", "audio/x-mpegurl"}, + {".m4a", "audio/m4a"}, + {".m4b", "audio/m4b"}, + {".m4p", "audio/m4p"}, + {".m4r", "audio/x-m4r"}, + {".m4v", "video/x-m4v"}, + {".mac", "image/x-macpaint"}, + {".mak", "text/plain"}, + {".man", "application/x-troff-man"}, + {".manifest", "application/x-ms-manifest"}, + {".map", "text/plain"}, + {".master", "application/xml"}, + {".mda", "application/msaccess"}, + {".mdb", "application/x-msaccess"}, + {".mde", "application/msaccess"}, + {".mdp", "application/octet-stream"}, + {".me", "application/x-troff-me"}, + {".mfp", "application/x-shockwave-flash"}, + {".mht", "message/rfc822"}, + {".mhtml", "message/rfc822"}, + {".mid", "audio/mid"}, + {".midi", "audio/mid"}, + {".mix", "application/octet-stream"}, + {".mk", "text/plain"}, + {".mmf", "application/x-smaf"}, + {".mno", "text/xml"}, + {".mny", "application/x-msmoney"}, + {".mod", "video/mpeg"}, + {".mov", "video/quicktime"}, + {".movie", "video/x-sgi-movie"}, + {".mp2", "video/mpeg"}, + {".mp2v", "video/mpeg"}, + {".mp3", "audio/mpeg"}, + {".mp4", "video/mp4"}, + {".mp4v", "video/mp4"}, + {".mpa", "video/mpeg"}, + {".mpe", "video/mpeg"}, + {".mpeg", "video/mpeg"}, + {".mpf", "application/vnd.ms-mediapackage"}, + {".mpg", "video/mpeg"}, + {".mpp", "application/vnd.ms-project"}, + {".mpv2", "video/mpeg"}, + {".mqv", "video/quicktime"}, + {".ms", "application/x-troff-ms"}, + {".msi", "application/octet-stream"}, + {".mso", "application/octet-stream"}, + {".mts", "video/vnd.dlna.mpeg-tts"}, + {".mtx", "application/xml"}, + {".mvb", "application/x-msmediaview"}, + {".mvc", "application/x-miva-compiled"}, + {".mxp", "application/x-mmxp"}, + {".nc", "application/x-netcdf"}, + {".nsc", "video/x-ms-asf"}, + {".nws", "message/rfc822"}, + {".ocx", "application/octet-stream"}, + {".oda", "application/oda"}, + {".odc", "text/x-ms-odc"}, + {".odh", "text/plain"}, + {".odl", "text/plain"}, + {".odp", "application/vnd.oasis.opendocument.presentation"}, + {".ods", "application/oleobject"}, + {".odt", "application/vnd.oasis.opendocument.text"}, + {".one", "application/onenote"}, + {".onea", "application/onenote"}, + {".onepkg", "application/onenote"}, + {".onetmp", "application/onenote"}, + {".onetoc", "application/onenote"}, + {".onetoc2", "application/onenote"}, + {".orderedtest", "application/xml"}, + {".osdx", "application/opensearchdescription+xml"}, + {".p10", "application/pkcs10"}, + {".p12", "application/x-pkcs12"}, + {".p7b", "application/x-pkcs7-certificates"}, + {".p7c", "application/pkcs7-mime"}, + {".p7m", "application/pkcs7-mime"}, + {".p7r", "application/x-pkcs7-certreqresp"}, + {".p7s", "application/pkcs7-signature"}, + {".pbm", "image/x-portable-bitmap"}, + {".pcast", "application/x-podcast"}, + {".pct", "image/pict"}, + {".pcx", "application/octet-stream"}, + {".pcz", "application/octet-stream"}, + {".pdf", "application/pdf"}, + {".pfb", "application/octet-stream"}, + {".pfm", "application/octet-stream"}, + {".pfx", "application/x-pkcs12"}, + {".pgm", "image/x-portable-graymap"}, + {".pic", "image/pict"}, + {".pict", "image/pict"}, + {".pkgdef", "text/plain"}, + {".pkgundef", "text/plain"}, + {".pko", "application/vnd.ms-pki.pko"}, + {".pls", "audio/scpls"}, + {".pma", "application/x-perfmon"}, + {".pmc", "application/x-perfmon"}, + {".pml", "application/x-perfmon"}, + {".pmr", "application/x-perfmon"}, + {".pmw", "application/x-perfmon"}, + {".png", "image/png"}, + {".pnm", "image/x-portable-anymap"}, + {".pnt", "image/x-macpaint"}, + {".pntg", "image/x-macpaint"}, + {".pnz", "image/png"}, + {".pot", "application/vnd.ms-powerpoint"}, + {".potm", "application/vnd.ms-powerpoint.template.macroEnabled.12"}, + {".potx", "application/vnd.openxmlformats-officedocument.presentationml.template"}, + {".ppa", "application/vnd.ms-powerpoint"}, + {".ppam", "application/vnd.ms-powerpoint.addin.macroEnabled.12"}, + {".ppm", "image/x-portable-pixmap"}, + {".pps", "application/vnd.ms-powerpoint"}, + {".ppsm", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12"}, + {".ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow"}, + {".ppt", "application/vnd.ms-powerpoint"}, + {".pptm", "application/vnd.ms-powerpoint.presentation.macroEnabled.12"}, + {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, + {".prf", "application/pics-rules"}, + {".prm", "application/octet-stream"}, + {".prx", "application/octet-stream"}, + {".ps", "application/postscript"}, + {".psc1", "application/PowerShell"}, + {".psd", "application/octet-stream"}, + {".psess", "application/xml"}, + {".psm", "application/octet-stream"}, + {".psp", "application/octet-stream"}, + {".pub", "application/x-mspublisher"}, + {".pwz", "application/vnd.ms-powerpoint"}, + {".qht", "text/x-html-insertion"}, + {".qhtm", "text/x-html-insertion"}, + {".qt", "video/quicktime"}, + {".qti", "image/x-quicktime"}, + {".qtif", "image/x-quicktime"}, + {".qtl", "application/x-quicktimeplayer"}, + {".qxd", "application/octet-stream"}, + {".ra", "audio/x-pn-realaudio"}, + {".ram", "audio/x-pn-realaudio"}, + {".rar", "application/octet-stream"}, + {".ras", "image/x-cmu-raster"}, + {".rat", "application/rat-file"}, + {".rc", "text/plain"}, + {".rc2", "text/plain"}, + {".rct", "text/plain"}, + {".rdlc", "application/xml"}, + {".resx", "application/xml"}, + {".rf", "image/vnd.rn-realflash"}, + {".rgb", "image/x-rgb"}, + {".rgs", "text/plain"}, + {".rm", "application/vnd.rn-realmedia"}, + {".rmi", "audio/mid"}, + {".rmp", "application/vnd.rn-rn_music_package"}, + {".roff", "application/x-troff"}, + {".rpm", "audio/x-pn-realaudio-plugin"}, + {".rqy", "text/x-ms-rqy"}, + {".rtf", "application/rtf"}, + {".rtx", "text/richtext"}, + {".ruleset", "application/xml"}, + {".s", "text/plain"}, + {".safariextz", "application/x-safari-safariextz"}, + {".scd", "application/x-msschedule"}, + {".sct", "text/scriptlet"}, + {".sd2", "audio/x-sd2"}, + {".sdp", "application/sdp"}, + {".sea", "application/octet-stream"}, + {".searchConnector-ms", "application/windows-search-connector+xml"}, + {".setpay", "application/set-payment-initiation"}, + {".setreg", "application/set-registration-initiation"}, + {".settings", "application/xml"}, + {".sgimb", "application/x-sgimb"}, + {".sgml", "text/sgml"}, + {".sh", "application/x-sh"}, + {".shar", "application/x-shar"}, + {".shtml", "text/html"}, + {".sit", "application/x-stuffit"}, + {".sitemap", "application/xml"}, + {".skin", "application/xml"}, + {".sldm", "application/vnd.ms-powerpoint.slide.macroEnabled.12"}, + {".sldx", "application/vnd.openxmlformats-officedocument.presentationml.slide"}, + {".slk", "application/vnd.ms-excel"}, + {".sln", "text/plain"}, + {".slupkg-ms", "application/x-ms-license"}, + {".smd", "audio/x-smd"}, + {".smi", "application/octet-stream"}, + {".smx", "audio/x-smd"}, + {".smz", "audio/x-smd"}, + {".snd", "audio/basic"}, + {".snippet", "application/xml"}, + {".snp", "application/octet-stream"}, + {".sol", "text/plain"}, + {".sor", "text/plain"}, + {".spc", "application/x-pkcs7-certificates"}, + {".spl", "application/futuresplash"}, + {".src", "application/x-wais-source"}, + {".srf", "text/plain"}, + {".SSISDeploymentManifest", "text/xml"}, + {".ssm", "application/streamingmedia"}, + {".sst", "application/vnd.ms-pki.certstore"}, + {".stl", "application/vnd.ms-pki.stl"}, + {".sv4cpio", "application/x-sv4cpio"}, + {".sv4crc", "application/x-sv4crc"}, + {".svc", "application/xml"}, + {".swf", "application/x-shockwave-flash"}, + {".t", "application/x-troff"}, + {".tar", "application/x-tar"}, + {".tcl", "application/x-tcl"}, + {".testrunconfig", "application/xml"}, + {".testsettings", "application/xml"}, + {".tex", "application/x-tex"}, + {".texi", "application/x-texinfo"}, + {".texinfo", "application/x-texinfo"}, + {".tgz", "application/x-compressed"}, + {".thmx", "application/vnd.ms-officetheme"}, + {".thn", "application/octet-stream"}, + {".tif", "image/tiff"}, + {".tiff", "image/tiff"}, + {".tlh", "text/plain"}, + {".tli", "text/plain"}, + {".toc", "application/octet-stream"}, + {".tr", "application/x-troff"}, + {".trm", "application/x-msterminal"}, + {".trx", "application/xml"}, + {".ts", "video/vnd.dlna.mpeg-tts"}, + {".tsv", "text/tab-separated-values"}, + {".ttf", "application/octet-stream"}, + {".tts", "video/vnd.dlna.mpeg-tts"}, + {".txt", "text/plain"}, + {".u32", "application/octet-stream"}, + {".uls", "text/iuls"}, + {".user", "text/plain"}, + {".ustar", "application/x-ustar"}, + {".vb", "text/plain"}, + {".vbdproj", "text/plain"}, + {".vbk", "video/mpeg"}, + {".vbproj", "text/plain"}, + {".vbs", "text/vbscript"}, + {".vcf", "text/x-vcard"}, + {".vcproj", "Application/xml"}, + {".vcs", "text/plain"}, + {".vcxproj", "Application/xml"}, + {".vddproj", "text/plain"}, + {".vdp", "text/plain"}, + {".vdproj", "text/plain"}, + {".vdx", "application/vnd.ms-visio.viewer"}, + {".vml", "text/xml"}, + {".vscontent", "application/xml"}, + {".vsct", "text/xml"}, + {".vsd", "application/vnd.visio"}, + {".vsi", "application/ms-vsi"}, + {".vsix", "application/vsix"}, + {".vsixlangpack", "text/xml"}, + {".vsixmanifest", "text/xml"}, + {".vsmdi", "application/xml"}, + {".vspscc", "text/plain"}, + {".vss", "application/vnd.visio"}, + {".vsscc", "text/plain"}, + {".vssettings", "text/xml"}, + {".vssscc", "text/plain"}, + {".vst", "application/vnd.visio"}, + {".vstemplate", "text/xml"}, + {".vsto", "application/x-ms-vsto"}, + {".vsw", "application/vnd.visio"}, + {".vsx", "application/vnd.visio"}, + {".vtx", "application/vnd.visio"}, + {".wav", "audio/wav"}, + {".wave", "audio/wav"}, + {".wax", "audio/x-ms-wax"}, + {".wbk", "application/msword"}, + {".wbmp", "image/vnd.wap.wbmp"}, + {".wcm", "application/vnd.ms-works"}, + {".wdb", "application/vnd.ms-works"}, + {".wdp", "image/vnd.ms-photo"}, + {".webarchive", "application/x-safari-webarchive"}, + {".webtest", "application/xml"}, + {".wiq", "application/xml"}, + {".wiz", "application/msword"}, + {".wks", "application/vnd.ms-works"}, + {".WLMP", "application/wlmoviemaker"}, + {".wlpginstall", "application/x-wlpg-detect"}, + {".wlpginstall3", "application/x-wlpg3-detect"}, + {".wm", "video/x-ms-wm"}, + {".wma", "audio/x-ms-wma"}, + {".wmd", "application/x-ms-wmd"}, + {".wmf", "application/x-msmetafile"}, + {".wml", "text/vnd.wap.wml"}, + {".wmlc", "application/vnd.wap.wmlc"}, + {".wmls", "text/vnd.wap.wmlscript"}, + {".wmlsc", "application/vnd.wap.wmlscriptc"}, + {".wmp", "video/x-ms-wmp"}, + {".wmv", "video/x-ms-wmv"}, + {".wmx", "video/x-ms-wmx"}, + {".wmz", "application/x-ms-wmz"}, + {".wpl", "application/vnd.ms-wpl"}, + {".wps", "application/vnd.ms-works"}, + {".wri", "application/x-mswrite"}, + {".wrl", "x-world/x-vrml"}, + {".wrz", "x-world/x-vrml"}, + {".wsc", "text/scriptlet"}, + {".wsdl", "text/xml"}, + {".wvx", "video/x-ms-wvx"}, + {".x", "application/directx"}, + {".xaf", "x-world/x-vrml"}, + {".xaml", "application/xaml+xml"}, + {".xap", "application/x-silverlight-app"}, + {".xbap", "application/x-ms-xbap"}, + {".xbm", "image/x-xbitmap"}, + {".xdr", "text/plain"}, + {".xht", "application/xhtml+xml"}, + {".xhtml", "application/xhtml+xml"}, + {".xla", "application/vnd.ms-excel"}, + {".xlam", "application/vnd.ms-excel.addin.macroEnabled.12"}, + {".xlc", "application/vnd.ms-excel"}, + {".xld", "application/vnd.ms-excel"}, + {".xlk", "application/vnd.ms-excel"}, + {".xll", "application/vnd.ms-excel"}, + {".xlm", "application/vnd.ms-excel"}, + {".xls", "application/vnd.ms-excel"}, + {".xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12"}, + {".xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"}, + {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, + {".xlt", "application/vnd.ms-excel"}, + {".xltm", "application/vnd.ms-excel.template.macroEnabled.12"}, + {".xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template"}, + {".xlw", "application/vnd.ms-excel"}, + {".xml", "text/xml"}, + {".xmta", "application/xml"}, + {".xof", "x-world/x-vrml"}, + {".XOML", "text/plain"}, + {".xpm", "image/x-xpixmap"}, + {".xps", "application/vnd.ms-xpsdocument"}, + {".xrm-ms", "text/xml"}, + {".xsc", "application/xml"}, + {".xsd", "text/xml"}, + {".xsf", "text/xml"}, + {".xsl", "text/xml"}, + {".xslt", "text/xml"}, + {".xsn", "application/octet-stream"}, + {".xss", "application/xml"}, + {".xtp", "application/octet-stream"}, + {".xwd", "image/x-xwindowdump"}, + {".z", "application/x-compress"}, + {".zip", "application/x-zip-compressed"}, + #endregion + + }; + + + }*/ + +} diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/SimpleHttpServer.csproj b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/SimpleHttpServer.csproj new file mode 100644 index 00000000..9c69cdca --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/SimpleHttpServer.csproj @@ -0,0 +1,92 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {9E139539-C272-488A-BFCF-CADDF458DF9D} + Library + Properties + SimpleHttpServer + SimpleHttpServer + 512 + v4.5 + + + + x86 + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + x86 + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + false + + + + + + AnyCPU + bin\Debug\ + + + AnyCPU + bin\Release\ + + + + ..\packages\log4net.2.0.5\lib\net45-full\log4net.dll + True + + + + + + + + + + + + + + + + + + + + + + + + + + Always + + + Always + + + + + \ No newline at end of file diff --git a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/CommandManager.cs b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/CommandManager.cs index 1896fcec..68d0a4c8 100644 --- a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/CommandManager.cs +++ b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/CommandManager.cs @@ -47,6 +47,7 @@ public static void RegisterAllCommands() CMDs.Add(new CommandDns(new string[] { "dns" })); CMDs.Add(new CommandWget(new string[] { "wget" })); CMDs.Add(new CommandFtp(new string[] { "ftp" })); + CMDs.Add(new CommandHttpServer(new string[] { "httpserver" })); CMDs.Add(new CommandVersion(new string[] { "version", "ver" })); CMDs.Add(new CommandSystemInfo(new string[] { "systeminfo", "sysinfo" })); diff --git a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs new file mode 100644 index 00000000..76414767 --- /dev/null +++ b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs @@ -0,0 +1,78 @@ +/* +* PROJECT: Aura Operating System Development +* CONTENT: Command Interpreter - Http Server command +* PROGRAMMER(S): Valentin Charbonnier +*/ + +using System; +using System.Collections.Generic; +using Cosmos.System.Network.IPv4.UDP.DNS; +using Cosmos.System.Network.IPv4; +using Cosmos.System.Network.Config; +using Cosmos.System.Network.IPv4.TCP; +using System.Text; +using Cosmos.System.Network.IPv4.TCP.FTP; +using SimpleHttpServer; +using SimpleHttpServer.Models; + +namespace Aura_OS.System.Shell.cmdIntr.Network +{ + class CommandHttpServer : ICommand + { + /// + /// Empty constructor. + /// + public CommandHttpServer(string[] commandvalues) : base(commandvalues, CommandType.Network) + { + Description = "to start an HTTP Server."; + } + + /// + /// CommandDns + /// + /// Arguments + public override ReturnInfo Execute() + { + try + { + var route_config = new List() { + new Route { + Name = "Hello Handler", + Method = "GET", + Callable = (HttpDiscussion result) => { + result.Response = new HttpResponse() + { + Content = Encoding.ASCII.GetBytes(@"" + + "\t

Hello from AuraOS!

" + + "\t

Server hour: " + DateTime.Now.ToString() + "

" + + ""), + ReasonPhrase = "OK", + StatusCode = "200" + }; + } + }, + }; + + HttpServer httpServer = new HttpServer(80, route_config); + + httpServer.Listen(); + + } + catch (Exception ex) + { + return new ReturnInfo(this, ReturnCode.ERROR, ex.Message); + } + + return new ReturnInfo(this, ReturnCode.OK); + } + + /// + /// Print /help information + /// + public override void PrintHelp() + { + Console.WriteLine("Usage:"); + Console.WriteLine(" - httpserver"); + } + } +} \ No newline at end of file From 7d24a02e528f08a739cef3477a6f85df039b3f9b Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 22 Oct 2021 16:09:29 +0100 Subject: [PATCH 2/8] Remove useless files. --- .../SimpleHttpServer/SimpleHttpServer.csproj | 92 ------------------- 1 file changed, 92 deletions(-) delete mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/SimpleHttpServer.csproj diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/SimpleHttpServer.csproj b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/SimpleHttpServer.csproj deleted file mode 100644 index 9c69cdca..00000000 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/SimpleHttpServer.csproj +++ /dev/null @@ -1,92 +0,0 @@ - - - - Debug - x86 - 8.0.30703 - 2.0 - {9E139539-C272-488A-BFCF-CADDF458DF9D} - Library - Properties - SimpleHttpServer - SimpleHttpServer - 512 - v4.5 - - - - x86 - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - x86 - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - false - - - - - - AnyCPU - bin\Debug\ - - - AnyCPU - bin\Release\ - - - - ..\packages\log4net.2.0.5\lib\net45-full\log4net.dll - True - - - - - - - - - - - - - - - - - - - - - - - - - - Always - - - Always - - - - - \ No newline at end of file From 734f39466da297011d7feca8a3a034083512243e Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 22 Oct 2021 17:39:00 +0100 Subject: [PATCH 3/8] Remove linq calls. Linq still not supported by Cosmos. --- .../Aura_OS/Properties/VersionInfo.cs | 2 +- .../Network/SimpleHttpServer/HttpProcessor.cs | 20 ++++++++++++++----- .../SimpleHttpServer/Models/HttpRequest.cs | 11 +++++++++- .../Shell/cmdIntr/Network/HttpServer.cs | 14 ++++++------- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs index 094562f4..c0c5d42a 100644 --- a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs +++ b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs @@ -2,6 +2,6 @@ namespace Aura_OS { public class VersionInfo { - public static string revision = "221020211603"; + public static string revision = "221020211725"; } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs index 4999218c..13f0f7fa 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs @@ -65,7 +65,11 @@ private static void WriteResponse(TcpClient client, HttpResponse response) { var sb = new StringBuilder(); sb.Append(string.Format("HTTP/1.0 {0} {1}\r\n", response.StatusCode, response.ReasonPhrase)); - sb.Append(string.Join("\r\n", response.Headers.Select(x => string.Format("{0}: {1}", x.Key, x.Value)))); + + foreach (var header in response.Headers) + { + sb.Append(header.Key + ": " + header.Value + "\r\n"); + } sb.Append("\r\n\r\n"); client.Send(Encoding.ASCII.GetBytes(sb.ToString())); @@ -82,10 +86,18 @@ public void AddRoute(Route route) protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest request) { + Route route = null; + if (!Routes.Any()) return HttpBuilder.NotFound(); - var route = Routes.SingleOrDefault(x => x.Method == request.Method); + foreach (var xroute in Routes) + { + if (xroute.Method == request.Method) + { + route = xroute; + } + } if (route == null) { @@ -93,7 +105,6 @@ protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest reques { ReasonPhrase = "Method Not Allowed", StatusCode = "405", - }; } @@ -172,8 +183,7 @@ private HttpRequest GetRequest(TcpClient client) } content = Encoding.ASCII.GetString(bytes); - }*/ - + }*/ return new HttpRequest() { diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs index 062dc447..8a69d680 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs @@ -27,7 +27,16 @@ public override string ToString() if (!this.Headers.ContainsKey("Content-Length")) this.Headers.Add("Content-Length", this.Content.Length.ToString()); - return string.Format("{0} {1} HTTP/1.0\r\n{2}\r\n\r\n{3}", this.Method, this.Url, string.Join("\r\n", this.Headers.Select(x => string.Format("{0}: {1}", x.Key, x.Value))), this.Content); + var sb = new StringBuilder(); + sb.Append(this.Method + " " + this.Url + " HTTP/1.0\r\n"); + foreach (var header in Headers) + { + sb.Append(header.Key + ": " + header.Value + "\r\n"); + } + sb.Append("\r\n\r\n"); + sb.Append(this.Content); + + return sb.ToString(); } } } diff --git a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs index 76414767..f3eef27a 100644 --- a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs +++ b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs @@ -36,16 +36,16 @@ public override ReturnInfo Execute() try { var route_config = new List() { - new Route { - Name = "Hello Handler", - Method = "GET", - Callable = (HttpDiscussion result) => { + new Route { + Name = "Hello Handler", + Method = "GET", + Callable = (HttpDiscussion result) => { result.Response = new HttpResponse() { Content = Encoding.ASCII.GetBytes(@"" + - "\t

Hello from AuraOS!

" + - "\t

Server hour: " + DateTime.Now.ToString() + "

" + - ""), + "\t

Hello from AuraOS!

" + + "\t

Server hour: " + DateTime.Now.ToString() + "

" + + ""), ReasonPhrase = "OK", StatusCode = "200" }; From 81b4eb31f4ebec8ebad5fee0a784181fc1098b56 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 22 Oct 2021 18:20:47 +0100 Subject: [PATCH 4/8] Forgot to add content. --- .../Aura_OS/Properties/VersionInfo.cs | 2 +- .../Network/SimpleHttpServer/HttpProcessor.cs | 24 ++++++++++--------- .../Network/SimpleHttpServer/HttpServer.cs | 17 +++++++++---- .../SimpleHttpServer/Models/HttpRequest.cs | 2 +- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs index c0c5d42a..359021f3 100644 --- a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs +++ b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs @@ -2,6 +2,6 @@ namespace Aura_OS { public class VersionInfo { - public static string revision = "221020211725"; + public static string revision = "221020211809"; } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs index 13f0f7fa..b0176a19 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs @@ -34,20 +34,21 @@ public HttpProcessor() #region Public Methods public void HandleClient(TcpClient tcpClient) { - HttpRequest request = GetRequest(tcpClient); + HttpRequest request = GetRequest(tcpClient); - // route and handle the request... - HttpResponse response = RouteRequest(tcpClient, request); + // route and handle the request... + HttpResponse response = RouteRequest(tcpClient, request); - Console.WriteLine("{0} {1}",response.StatusCode,request.Url); - // build a default response for errors - if (response.Content == null) { - if (response.StatusCode != "200") { - response.ContentAsUTF8 = string.Format("{0} {1}

{2}", response.StatusCode, request.Url, response.ReasonPhrase); - } + Console.WriteLine("{0} {1}", response.StatusCode, request.Url); + + // build a default response for errors + if (response.Content == null) { + if (response.StatusCode != "200") { + response.ContentAsUTF8 = string.Format("{0} {1}

{2}", response.StatusCode, request.Url, response.ReasonPhrase); } + } - WriteResponse(tcpClient, response); + WriteResponse(tcpClient, response); } // this formats the HTTP response... @@ -70,7 +71,8 @@ private static void WriteResponse(TcpClient client, HttpResponse response) { { sb.Append(header.Key + ": " + header.Value + "\r\n"); } - sb.Append("\r\n\r\n"); + sb.Append("\r\n"); + sb.Append(Encoding.ASCII.GetString(response.Content)); client.Send(Encoding.ASCII.GetBytes(sb.ToString())); } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs index 067664bb..1c3860f3 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs @@ -23,6 +23,7 @@ public class HttpServer #endregion #region Public Methods + public HttpServer(int port, List routes) { this.Port = port; @@ -36,12 +37,20 @@ public HttpServer(int port, List routes) public void Listen() { - this.Listener = new TcpListener((ushort)Port); - this.Listener.Start(); + Listener = new TcpListener((ushort)Port); + Listener.Start(); + while (this.IsActive) { - TcpClient s = this.Listener.AcceptTcpClient(); - this.Processor.HandleClient(s); + try + { + var s = Listener.AcceptTcpClient(); + Processor.HandleClient(s); + } + catch + { + + } } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs index 8a69d680..c9d869de 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs @@ -33,7 +33,7 @@ public override string ToString() { sb.Append(header.Key + ": " + header.Value + "\r\n"); } - sb.Append("\r\n\r\n"); + sb.Append("\r\n"); sb.Append(this.Content); return sb.ToString(); From 608febe7348836f76acbc3fad63840509220f259 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 22 Oct 2021 18:33:19 +0100 Subject: [PATCH 5/8] Clean code + Loop server --- .../Aura_OS/Properties/VersionInfo.cs | 2 +- .../Network/SimpleHttpServer/HttpBuilder.cs | 17 +++--- .../Network/SimpleHttpServer/HttpProcessor.cs | 54 +++++++++++++------ .../Network/SimpleHttpServer/HttpServer.cs | 22 ++++---- .../SimpleHttpServer/Models/HttpDiscussion.cs | 14 ----- .../SimpleHttpServer/Models/HttpRequest.cs | 24 ++++++--- .../SimpleHttpServer/Models/HttpResponse.cs | 13 +++-- .../Network/SimpleHttpServer/Models/Route.cs | 13 +++-- .../Shell/cmdIntr/Network/HttpServer.cs | 4 +- LICENSES/LICENSES.md | 5 ++ LICENSES/SimpleHttpServer/LICENSE.md | 22 ++++++++ 11 files changed, 125 insertions(+), 65 deletions(-) delete mode 100644 Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpDiscussion.cs create mode 100644 LICENSES/SimpleHttpServer/LICENSE.md diff --git a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs index 359021f3..b79a8a97 100644 --- a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs +++ b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs @@ -2,6 +2,6 @@ namespace Aura_OS { public class VersionInfo { - public static string revision = "221020211809"; + public static string revision = "221020211833"; } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs index b24e7a79..a91e402f 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs @@ -1,10 +1,13 @@ -using SimpleHttpServer.Models; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +/* +* PROJECT: Aura Operating System Development +* CONTENT: Http default responses +* PROGRAMMERS: Valentin Charbonnier +* David Jeske +* Barend Erasmus +* LICENSE: LICENSES\SimpleHttpServer\LICENSE.md +*/ + +using SimpleHttpServer.Models; namespace SimpleHttpServer { diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs index b0176a19..0c41ced6 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs @@ -1,20 +1,31 @@ -// Copyright (C) 2016 by David Jeske, Barend Erasmus and donated to the public domain +/* +* PROJECT: Aura Operating System Development +* CONTENT: HttpProcessor class +* PROGRAMMERS: Valentin Charbonnier +* David Jeske +* Barend Erasmus +* LICENSE: LICENSES\SimpleHttpServer\LICENSE.md +*/ using Cosmos.System.Network.IPv4; using Cosmos.System.Network.IPv4.TCP; using SimpleHttpServer.Models; using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Text; -using System.Text.RegularExpressions; namespace SimpleHttpServer { - public class HttpProcessor + //used because Event with return type is not supported by Cosmos. + public class HttpDiscussion { + public HttpRequest Request; + public HttpResponse Response; + } + public class HttpProcessor + { #region Fields private static int MAX_POST_SIZE = 10 * 1024 * 1024; // 10MB @@ -27,11 +38,13 @@ public class HttpProcessor public HttpProcessor() { + } #endregion #region Public Methods + public void HandleClient(TcpClient tcpClient) { HttpRequest request = GetRequest(tcpClient); @@ -42,8 +55,10 @@ public void HandleClient(TcpClient tcpClient) Console.WriteLine("{0} {1}", response.StatusCode, request.Url); // build a default response for errors - if (response.Content == null) { - if (response.StatusCode != "200") { + if (response.Content == null) + { + if (response.StatusCode != "200") + { response.ContentAsUTF8 = string.Format("{0} {1}

{2}", response.StatusCode, request.Url, response.ReasonPhrase); } } @@ -52,21 +67,24 @@ public void HandleClient(TcpClient tcpClient) } // this formats the HTTP response... - private static void WriteResponse(TcpClient client, HttpResponse response) { - if (response.Content == null) { + private static void WriteResponse(TcpClient client, HttpResponse response) + { + if (response.Content == null) + { response.Content = new byte[]{}; } // default to text/html content type - if (!response.Headers.ContainsKey("Content-Type")) { + if (!response.Headers.ContainsKey("Content-Type")) + { response.Headers["Content-Type"] = "text/html"; } response.Headers["Content-Length"] = response.Content.Length.ToString(); + //make response var sb = new StringBuilder(); sb.Append(string.Format("HTTP/1.0 {0} {1}\r\n", response.StatusCode, response.ReasonPhrase)); - foreach (var header in response.Headers) { sb.Append(header.Key + ": " + header.Value + "\r\n"); @@ -79,7 +97,7 @@ private static void WriteResponse(TcpClient client, HttpResponse response) { public void AddRoute(Route route) { - this.Routes.Add(route); + Routes.Add(route); } #endregion @@ -91,7 +109,9 @@ protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest reques Route route = null; if (!Routes.Any()) + { return HttpBuilder.NotFound(); + } foreach (var xroute in Routes) { @@ -112,11 +132,14 @@ protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest reques // trigger the route handler... request.Route = route; - try { + try + { var discussion = new HttpDiscussion() { Request = request, Response = null }; route.Callable(discussion); return discussion.Response; - } catch(Exception ex) { + } + catch (Exception ex) + { Console.WriteLine(ex); return HttpBuilder.InternalServerError(); } @@ -125,23 +148,25 @@ protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest reques private HttpRequest GetRequest(TcpClient client) { var ep = new EndPoint(Address.Zero, 0); + //Read Request Line string request = Encoding.ASCII.GetString(client.Receive(ref ep)); var lines = request.Split("\r\n"); string[] tokens = lines[0].Split(' '); + if (tokens.Length != 3) { throw new Exception("invalid http request line"); } + string method = tokens[0].ToUpper(); string url = tokens[1]; string protocolVersion = tokens[2]; //Read Headers Dictionary headers = new Dictionary(); - string line; for (int i = 1; i < lines.Length; i++) { @@ -198,6 +223,5 @@ private HttpRequest GetRequest(TcpClient client) #endregion - } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs index 1c3860f3..6d96adb3 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs @@ -1,16 +1,18 @@ -// Copyright (C) 2016 by David Jeske, Barend Erasmus and donated to the public domain +/* +* PROJECT: Aura Operating System Development +* CONTENT: HttpServer class +* PROGRAMMERS: Valentin Charbonnier +* David Jeske +* Barend Erasmus +* LICENSE: LICENSES\SimpleHttpServer\LICENSE.md +*/ using Cosmos.System.Network.IPv4.TCP; -using SimpleHttpServer; using SimpleHttpServer.Models; -using System; -using System.Collections; using System.Collections.Generic; -using System.IO; namespace SimpleHttpServer { - public class HttpServer { #region Fields @@ -26,12 +28,12 @@ public class HttpServer public HttpServer(int port, List routes) { - this.Port = port; - this.Processor = new HttpProcessor(); + Port = port; + Processor = new HttpProcessor(); foreach (var route in routes) { - this.Processor.AddRoute(route); + Processor.AddRoute(route); } } @@ -40,7 +42,7 @@ public void Listen() Listener = new TcpListener((ushort)Port); Listener.Start(); - while (this.IsActive) + while (IsActive) { try { diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpDiscussion.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpDiscussion.cs deleted file mode 100644 index d81f7bee..00000000 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpDiscussion.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace SimpleHttpServer.Models -{ - public class HttpDiscussion - { - public HttpRequest Request; - public HttpResponse Response; - } -} diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs index c9d869de..cfb8bcf3 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs @@ -1,8 +1,14 @@ -// Copyright (C) 2016 by Barend Erasmus and donated to the public domain +/* +* PROJECT: Aura Operating System Development +* CONTENT: HttpRequest class +* PROGRAMMERS: Valentin Charbonnier +* David Jeske +* Barend Erasmus +* LICENSE: LICENSES\SimpleHttpServer\LICENSE.md +*/ using System; using System.Collections.Generic; -using System.Linq; using System.Text; namespace SimpleHttpServer.Models @@ -24,17 +30,23 @@ public HttpRequest() public override string ToString() { if (!string.IsNullOrWhiteSpace(this.Content)) - if (!this.Headers.ContainsKey("Content-Length")) - this.Headers.Add("Content-Length", this.Content.Length.ToString()); + { + if (!Headers.ContainsKey("Content-Length")) + { + Headers.Add("Content-Length", Content.Length.ToString()); + } + + } + //make string from fields var sb = new StringBuilder(); - sb.Append(this.Method + " " + this.Url + " HTTP/1.0\r\n"); + sb.Append(Method + " " + Url + " HTTP/1.0\r\n"); foreach (var header in Headers) { sb.Append(header.Key + ": " + header.Value + "\r\n"); } sb.Append("\r\n"); - sb.Append(this.Content); + sb.Append(Content); return sb.ToString(); } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpResponse.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpResponse.cs index 3d963af2..d52f706a 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpResponse.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpResponse.cs @@ -1,8 +1,13 @@ -// Copyright (C) 2016 by Barend Erasmus and donated to the public domain +/* +* PROJECT: Aura Operating System Development +* CONTENT: HttpResponse class +* PROGRAMMERS: Valentin Charbonnier +* David Jeske +* Barend Erasmus +* LICENSE: LICENSES\SimpleHttpServer\LICENSE.md +*/ -using System; using System.Collections.Generic; -using System.Linq; using System.Text; // NOTE: two consequences of this simplified response model are: @@ -20,7 +25,6 @@ public enum HttpStatusCode // https://en.wikipedia.org/wiki/List_of_HTTP_status_codes Continue = 100, - Ok = 200, Created = 201, Accepted = 202, @@ -49,6 +53,7 @@ public string ContentAsUTF8 this.setContent(value, encoding: Encoding.UTF8); } } + public void setContent(string content, Encoding encoding = null) { if (encoding == null) diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs index 1fea557f..dda79414 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs @@ -1,9 +1,13 @@ -// Copyright (C) 2016 by Barend Erasmus and donated to the public domain +/* +* PROJECT: Aura Operating System Development +* CONTENT: Route class +* PROGRAMMERS: Valentin Charbonnier +* David Jeske +* Barend Erasmus +* LICENSE: LICENSES\SimpleHttpServer\LICENSE.md +*/ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace SimpleHttpServer.Models { @@ -11,7 +15,6 @@ public class Route { public string Name { get; set; } // descriptive name for debugging public string Method { get; set; } - public Action Callable; } } diff --git a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs index f3eef27a..3d5911fa 100644 --- a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs +++ b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs @@ -53,10 +53,8 @@ public override ReturnInfo Execute() }, }; - HttpServer httpServer = new HttpServer(80, route_config); - + var httpServer = new HttpServer(80, route_config); httpServer.Listen(); - } catch (Exception ex) { diff --git a/LICENSES/LICENSES.md b/LICENSES/LICENSES.md index 6031edb1..5fc2740e 100644 --- a/LICENSES/LICENSES.md +++ b/LICENSES/LICENSES.md @@ -32,3 +32,8 @@ Open source project used in Aura Operating System: - Description: Used in a big part of the network stack. - Location: [Aura Operating System/Aura OS/System/System/Network/] - License file: [LICENSE.MD](https://github.com/aura-systems/Aura-Operating-System/blob/master/LICENSES/COSMOS/LICENSE.md) + +### SimpleHttpServer +- Description: Simple Http Server. +- Location: [Aura Operating System/Aura OS/System/Network/SimpleHttpServer/] +- License file: [LICENSE.MD](https://github.com/aura-systems/Aura-Operating-System/blob/master/LICENSES/SimpleHttpServer/LICENSE.md) diff --git a/LICENSES/SimpleHttpServer/LICENSE.md b/LICENSES/SimpleHttpServer/LICENSE.md new file mode 100644 index 00000000..791be1f9 --- /dev/null +++ b/LICENSES/SimpleHttpServer/LICENSE.md @@ -0,0 +1,22 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file From 2bf106d1f35f5353e919c48b99ad4ba8da3e5ab8 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 22 Oct 2021 18:57:22 +0100 Subject: [PATCH 6/8] Route by URL --- Aura Operating System/Aura_OS/Properties/VersionInfo.cs | 2 +- .../System/Network/SimpleHttpServer/HttpProcessor.cs | 8 ++------ .../System/Network/SimpleHttpServer/Models/Route.cs | 3 ++- .../Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs | 1 + 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs index b79a8a97..29adc4f5 100644 --- a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs +++ b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs @@ -2,6 +2,6 @@ namespace Aura_OS { public class VersionInfo { - public static string revision = "221020211833"; + public static string revision = "221020211857"; } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs index 0c41ced6..716c75ec 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs @@ -115,7 +115,7 @@ protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest reques foreach (var xroute in Routes) { - if (xroute.Method == request.Method) + if (xroute.Url == request.Url) { route = xroute; } @@ -123,11 +123,7 @@ protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest reques if (route == null) { - return new HttpResponse() - { - ReasonPhrase = "Method Not Allowed", - StatusCode = "405", - }; + return HttpBuilder.NotFound(); } // trigger the route handler... diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs index dda79414..5d9827f8 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/Route.cs @@ -15,6 +15,7 @@ public class Route { public string Name { get; set; } // descriptive name for debugging public string Method { get; set; } - public Action Callable; + public string Url { get; set; } + public Action Callable { get; set; } } } diff --git a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs index 3d5911fa..375273c7 100644 --- a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs +++ b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs @@ -39,6 +39,7 @@ public override ReturnInfo Execute() new Route { Name = "Hello Handler", Method = "GET", + Url = "/", Callable = (HttpDiscussion result) => { result.Response = new HttpResponse() { From 43ab34a978c7935d4178c6a4e66d5300d85e6e49 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Thu, 28 Oct 2021 02:44:48 +0100 Subject: [PATCH 7/8] Update sample website. --- Aura Operating System/Aura_OS/Properties/VersionInfo.cs | 2 +- .../System/Network/SimpleHttpServer/HttpBuilder.cs | 8 ++------ .../Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs | 7 +++++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs index 29adc4f5..8605c276 100644 --- a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs +++ b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs @@ -2,6 +2,6 @@ namespace Aura_OS { public class VersionInfo { - public static string revision = "221020211857"; + public static string revision = "281020210235"; } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs index a91e402f..87fccf99 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpBuilder.cs @@ -15,9 +15,7 @@ class HttpBuilder { public static HttpResponse InternalServerError() { - string content = @"

Internal Server Error

- -by SimpleHtppServer"; + string content = "

500 Internal Server Error

Back to home page"; return new HttpResponse() { @@ -29,9 +27,7 @@ public static HttpResponse InternalServerError() public static HttpResponse NotFound() { - string content = @"

Not Found

- -by SimpleHtppServer"; + string content = "

404 Not Found

Back to home page"; return new HttpResponse() { diff --git a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs index 375273c7..e24e7c4e 100644 --- a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs +++ b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs @@ -44,8 +44,11 @@ public override ReturnInfo Execute() result.Response = new HttpResponse() { Content = Encoding.ASCII.GetBytes(@"" + - "\t

Hello from AuraOS!

" + - "\t

Server hour: " + DateTime.Now.ToString() + "

" + + "\t

Hello from AuraOS!

" + + "\t

Version: " + Kernel.version + "." + Kernel.revision + "

" + + "\t

Server Hour: " + DateTime.Now.ToString() + "

" + + "\t

Server Boot Time: " + Kernel.boottime + "

" + + "\t

Powered by Cosmos.

" + ""), ReasonPhrase = "OK", StatusCode = "200" From 500b0d8db76b6a2eb83be5ee5e0b19291e661eee Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Fri, 29 Oct 2021 05:21:56 +0100 Subject: [PATCH 8/8] Add filesystem routing --- .../Aura_OS/Properties/VersionInfo.cs | 2 +- .../Network/SimpleHttpServer/HttpProcessor.cs | 188 +++++++++--------- .../Network/SimpleHttpServer/HttpServer.cs | 3 + .../SimpleHttpServer/Models/HttpRequest.cs | 2 - .../RouteHandlers/FileSystemRouteHandler.cs | 147 ++++++++------ .../Shell/cmdIntr/Network/HttpServer.cs | 54 ++++- 6 files changed, 235 insertions(+), 161 deletions(-) diff --git a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs index 8605c276..d882455b 100644 --- a/Aura Operating System/Aura_OS/Properties/VersionInfo.cs +++ b/Aura Operating System/Aura_OS/Properties/VersionInfo.cs @@ -2,6 +2,6 @@ namespace Aura_OS { public class VersionInfo { - public static string revision = "281020210235"; + public static string revision = "291020210515"; } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs index 716c75ec..ffbc059c 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpProcessor.cs @@ -26,31 +26,20 @@ public class HttpDiscussion public class HttpProcessor { - #region Fields - - private static int MAX_POST_SIZE = 10 * 1024 * 1024; // 10MB - private List Routes = new List(); - #endregion - - #region Constructors - public HttpProcessor() { } - #endregion - - #region Public Methods - public void HandleClient(TcpClient tcpClient) { - HttpRequest request = GetRequest(tcpClient); + //get request from client + var request = GetRequest(tcpClient); // route and handle the request... - HttpResponse response = RouteRequest(tcpClient, request); + var response = RouteRequest(tcpClient, request); Console.WriteLine("{0} {1}", response.StatusCode, request.Url); @@ -64,81 +53,8 @@ public void HandleClient(TcpClient tcpClient) } WriteResponse(tcpClient, response); - } - - // this formats the HTTP response... - private static void WriteResponse(TcpClient client, HttpResponse response) - { - if (response.Content == null) - { - response.Content = new byte[]{}; - } - - // default to text/html content type - if (!response.Headers.ContainsKey("Content-Type")) - { - response.Headers["Content-Type"] = "text/html"; - } - - response.Headers["Content-Length"] = response.Content.Length.ToString(); - //make response - var sb = new StringBuilder(); - sb.Append(string.Format("HTTP/1.0 {0} {1}\r\n", response.StatusCode, response.ReasonPhrase)); - foreach (var header in response.Headers) - { - sb.Append(header.Key + ": " + header.Value + "\r\n"); - } - sb.Append("\r\n"); - sb.Append(Encoding.ASCII.GetString(response.Content)); - - client.Send(Encoding.ASCII.GetBytes(sb.ToString())); - } - - public void AddRoute(Route route) - { - Routes.Add(route); - } - - #endregion - - #region Private Methods - - protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest request) - { - Route route = null; - - if (!Routes.Any()) - { - return HttpBuilder.NotFound(); - } - - foreach (var xroute in Routes) - { - if (xroute.Url == request.Url) - { - route = xroute; - } - } - - if (route == null) - { - return HttpBuilder.NotFound(); - } - - // trigger the route handler... - request.Route = route; - try - { - var discussion = new HttpDiscussion() { Request = request, Response = null }; - route.Callable(discussion); - return discussion.Response; - } - catch (Exception ex) - { - Console.WriteLine(ex); - return HttpBuilder.InternalServerError(); - } + tcpClient.Close(); } private HttpRequest GetRequest(TcpClient client) @@ -206,7 +122,7 @@ private HttpRequest GetRequest(TcpClient client) } content = Encoding.ASCII.GetString(bytes); - }*/ + }*/ return new HttpRequest() { @@ -217,7 +133,99 @@ private HttpRequest GetRequest(TcpClient client) }; } - #endregion + protected virtual HttpResponse RouteRequest(TcpClient client, HttpRequest request) + { + if (!Routes.Any()) + { + return HttpBuilder.NotFound(); + } + + //Search Route + var route = GetRoute(request); + + if (route == null) + { + return HttpBuilder.NotFound(); + } + + // trigger the route handler... + try + { + var discussion = new HttpDiscussion() { Request = request, Response = null }; + + route.Callable(discussion); + + return discussion.Response; + } + catch (Exception ex) + { + Console.WriteLine(ex); + + return HttpBuilder.InternalServerError(); + } + } + + private Route GetRoute(HttpRequest request) + { + foreach (var route in Routes) + { + if (route.Url == request.Url) + { + return route; + } + } + + //no hardcoded route, get filesystem root + + if (request.Url == "/") + { + request.Url = "/index.html"; + } + + foreach (var route in Routes) + { + if (route.Url == "") + { + return route; + } + } + + return null; + } + + // this formats the HTTP response... + private static void WriteResponse(TcpClient client, HttpResponse response) + { + if (response.Content == null) + { + response.Content = new byte[] { }; + } + + // default to text/html content type + if (!response.Headers.ContainsKey("Content-Type")) + { + response.Headers["Content-Type"] = "text/html"; + } + + response.Headers["Content-Length"] = response.Content.Length.ToString(); + + //make response + var sb = new StringBuilder(); + sb.Append(string.Format("HTTP/1.0 {0} {1}\r\n", response.StatusCode, response.ReasonPhrase)); + foreach (var header in response.Headers) + { + sb.Append(header.Key + ": " + header.Value + "\r\n"); + } + sb.Append("\r\n"); + sb.Append(Encoding.ASCII.GetString(response.Content)); + + client.Send(Encoding.ASCII.GetBytes(sb.ToString())); + } + + public void AddRoute(Route route) + { + Routes.Add(route); + } } } diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs index 6d96adb3..edbe9b2d 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/HttpServer.cs @@ -9,6 +9,7 @@ using Cosmos.System.Network.IPv4.TCP; using SimpleHttpServer.Models; +using System; using System.Collections.Generic; namespace SimpleHttpServer @@ -42,6 +43,8 @@ public void Listen() Listener = new TcpListener((ushort)Port); Listener.Start(); + Console.WriteLine("HTTP Server Listening on port 80..."); + while (IsActive) { try diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs index cfb8bcf3..6578843c 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/Models/HttpRequest.cs @@ -17,9 +17,7 @@ public class HttpRequest { public string Method { get; set; } public string Url { get; set; } - public string Path { get; set; } // either the Url, or the first regex group public string Content { get; set; } - public Route Route { get; set; } public Dictionary Headers { get; set; } public HttpRequest() diff --git a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/RouteHandlers/FileSystemRouteHandler.cs b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/RouteHandlers/FileSystemRouteHandler.cs index 4c0f5c94..d2dc25fa 100644 --- a/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/RouteHandlers/FileSystemRouteHandler.cs +++ b/Aura Operating System/Aura_OS/System/Network/SimpleHttpServer/RouteHandlers/FileSystemRouteHandler.cs @@ -1,85 +1,98 @@ -// Copyright (C) 2016 by Barend Erasmus, David Jeske and donated to the public domain +/* +* PROJECT: Aura Operating System Development +* CONTENT: FileSystemRouteHandler class +* PROGRAMMERS: Valentin Charbonnier +* David Jeske +* Barend Erasmus +* LICENSE: LICENSES\SimpleHttpServer\LICENSE.md +*/ using SimpleHttpServer.Models; using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace SimpleHttpServer.RouteHandlers { - /* + public class LiveFile + { + public string Path { get; set; } + public byte[] Content { get; set; } + } + public class FileSystemRouteHandler { + public List FilesPath = new List(); + + public FileSystemRouteHandler(string basePath) + { + BasePath = basePath; + + Console.WriteLine("Scanning " + BasePath + "..."); + + DoTree(BasePath); + + foreach (var path in FilesPath) + { + Console.WriteLine(path.Path + " registered."); + } + + Console.WriteLine("Done."); + } + + private void DoTree(string directory) + { + var directories = Directory.GetDirectories(directory); + + foreach (string file in Directory.GetFiles(directory)) + { + FilesPath.Add(new LiveFile() { Path = directory.Remove(0, BasePath.Length - 1) + file, Content = File.ReadAllBytes(directory + file) }); + } + + for (int j = 0; j < directories.Length; j++) + { + DoTree(directory + directories[j].ToLower() + "\\"); + } + } public string BasePath { get; set; } public bool ShowDirectories { get; set; } - public HttpResponse Handle(HttpRequest request) { - var url_part = request.Path; + public void Handle(HttpDiscussion discussion) { + var url_part = discussion.Request.Url; // do some basic sanitization of the URL, attempting to make sure they can't read files outside the basepath // NOTE: this is probably not bulletproof/secure url_part = url_part.Replace("\\..\\", "\\"); url_part = url_part.Replace("/../", "/"); - url_part = url_part.Replace("//","/"); - url_part = url_part.Replace(@"\\",@"\"); - url_part = url_part.Replace(":",""); - url_part = url_part.Replace("/",Path.DirectorySeparatorChar.ToString()); - - // make sure the first part of the path is not - if (url_part.Length > 0) { - var first_char = url_part.ElementAt(0); - if (first_char == '/' || first_char == '\\') { - url_part = "." + url_part; - } - } - var local_path = Path.Combine(this.BasePath, url_part); - - if (ShowDirectories && Directory.Exists(local_path)) { - // Console.WriteLine("FileSystemRouteHandler Dir {0}",local_path); - return Handle_LocalDir(request, local_path); - } else if (File.Exists(local_path)) { - // Console.WriteLine("FileSystemRouteHandler File {0}", local_path); - return Handle_LocalFile(request, local_path); - } else { - return new HttpResponse { - StatusCode = "404", - ReasonPhrase = string.Format("Not Found ({0}) handler({1})",local_path,request.Route.Name), - }; - } - } + url_part = url_part.Replace("//", "/"); + url_part = url_part.Replace(@"\\", @"\"); + url_part = url_part.Replace(":", ""); + url_part = url_part.Replace("/", Path.DirectorySeparatorChar.ToString()); - HttpResponse Handle_LocalFile(HttpRequest request, string local_path) { - var file_extension = Path.GetExtension(local_path); + foreach (var file in FilesPath) + { + if (file.Path == url_part) + { + var file_extension = GetExtension(url_part); - var response = new HttpResponse(); - response.StatusCode = "200"; - response.ReasonPhrase = "Ok"; - response.Headers["Content-Type"] = QuickMimeTypeMapper.GetMimeType(file_extension); - response.Content = File.ReadAllBytes(local_path); + discussion.Response = new HttpResponse(); + discussion.Response.StatusCode = "200"; + discussion.Response.ReasonPhrase = "Ok"; + discussion.Response.Headers["Content-Type"] = QuickMimeTypeMapper.GetMimeType(file_extension); + discussion.Response.Content = file.Content; - return response; - } + return; + } + } - HttpResponse Handle_LocalDir(HttpRequest request, string local_path) { - var output = new StringBuilder(); - output.Append(string.Format("

Directory: {0}

",request.Url)); - - foreach (var entry in Directory.GetFiles(local_path)) { - var file_info = new System.IO.FileInfo(entry); - - var filename = file_info.Name; - output.Append(string.Format("{1}
",filename,filename)); - } - - return new HttpResponse() { - StatusCode = "200", - ReasonPhrase = "Ok", - ContentAsUTF8 = output.ToString(), - }; + discussion.Response = HttpBuilder.NotFound(); + } + + private string GetExtension(string attachment_name) + { + var index_point = attachment_name.IndexOf("."); + return attachment_name.Substring(index_point); } } @@ -96,17 +109,19 @@ public static string GetMimeType(string extension) { throw new ArgumentNullException("extension"); } - if (!extension.StartsWith(".")) { - extension = "." + extension; + foreach (var entry in _mappings) + { + if (entry.Key == extension) + { + return entry.Value; + } } - string mime; - - return _mappings.TryGetValue(extension, out mime) ? mime : "application/octet-stream"; + return "application/octet-stream"; } - private static IDictionary _mappings = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { + private static Dictionary _mappings = new Dictionary() { #region Big freaking list of mime types @@ -678,6 +693,6 @@ public static string GetMimeType(string extension) { }; - }*/ + } } diff --git a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs index e24e7c4e..cded28c8 100644 --- a/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs +++ b/Aura Operating System/Aura_OS/System/Shell/cmdIntr/Network/HttpServer.cs @@ -14,6 +14,8 @@ using Cosmos.System.Network.IPv4.TCP.FTP; using SimpleHttpServer; using SimpleHttpServer.Models; +using SimpleHttpServer.RouteHandlers; +using System.IO; namespace Aura_OS.System.Shell.cmdIntr.Network { @@ -24,7 +26,7 @@ class CommandHttpServer : ICommand /// public CommandHttpServer(string[] commandvalues) : base(commandvalues, CommandType.Network) { - Description = "to start an HTTP Server."; + Description = "to start an HTTP Server"; } /// @@ -54,7 +56,54 @@ public override ReturnInfo Execute() StatusCode = "200" }; } - }, + } + }; + + var httpServer = new HttpServer(80, route_config); + httpServer.Listen(); + } + catch (Exception ex) + { + return new ReturnInfo(this, ReturnCode.ERROR, ex.Message); + } + + return new ReturnInfo(this, ReturnCode.OK); + } + + /// + /// CommandTree + /// + /// Arguments + public override ReturnInfo Execute(List arguments) + { + if (arguments.Count != 1) + { + return new ReturnInfo(this, ReturnCode.ERROR_ARG); + } + + try + { + string path = arguments[0]; + + if (!path.EndsWith("/")) + { + path += "/"; + } + + path = path.Replace("/", "\\"); + + if (!Directory.Exists(path)) + { + return new ReturnInfo(this, ReturnCode.ERROR, "Directory does not exist."); + } + + var route_config = new List() { + new Route { + Name = "FileSystem Static Handler", + Method = "GET", + Url = "", + Callable = new FileSystemRouteHandler(path).Handle, + } }; var httpServer = new HttpServer(80, route_config); @@ -75,6 +124,7 @@ public override void PrintHelp() { Console.WriteLine("Usage:"); Console.WriteLine(" - httpserver"); + Console.WriteLine(" - httpserver {path}"); } } } \ No newline at end of file