Last active
June 14, 2020 08:42
-
-
Save SradnickDev/25778641a29d1c539866e69a66ae9244 to your computer and use it in GitHub Desktop.
HttpServer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Net; | |
using System.Threading; | |
namespace Websocket | |
{ | |
public class HttpServer | |
{ | |
public bool IsRunning => m_listener.IsListening; | |
private HttpListener m_listener; | |
private Thread m_thread; | |
private List<string> m_indexFiles = new List<string>() | |
{ | |
"index.html", "index.htm", "default.html", "default.htm" | |
}; | |
private string m_rootDirectory = ""; | |
public void Setup(string path, string adress, int port) | |
{ | |
m_listener = new HttpListener(); | |
m_rootDirectory = path; | |
m_listener.Prefixes.Add($"http://{adress}:{port}/"); | |
m_thread = new Thread(Job); | |
m_listener.Start(); | |
m_thread.Start(); | |
} | |
public void AddCustomIndexFile(string indexFile) | |
{ | |
m_indexFiles.Add(indexFile); | |
} | |
private void Job() | |
{ | |
while (true) | |
{ | |
try | |
{ | |
var context = m_listener.GetContext(); | |
Process(context); | |
} | |
catch (Exception ex) | |
{ | |
Console.Write(ex); | |
} | |
} | |
} | |
private void Process(HttpListenerContext context) | |
{ | |
string file = context.Request.Url.AbsolutePath; | |
file = file.Substring(1); | |
if (string.IsNullOrEmpty(file)) | |
{ | |
foreach (var indexFile in m_indexFiles) | |
{ | |
if (File.Exists(Path.Combine(m_rootDirectory, indexFile))) | |
{ | |
file = indexFile; | |
break; | |
} | |
} | |
} | |
file = Path.Combine(m_rootDirectory, file); | |
if (File.Exists(file)) | |
{ | |
try | |
{ | |
using (var input = new FileStream(file, FileMode.Open)) | |
{ | |
context.Response.ContentType = | |
MimeTypes.Mappings.TryGetValue(Path.GetExtension(file), out var mime) | |
? mime | |
: "application/octet-stream"; | |
context.Response.ContentLength64 = input.Length; | |
context.Response.AddHeader("Date", DateTime.Now.ToString("r")); | |
context.Response.AddHeader("Last-Modified", File.GetLastWriteTime(file).ToString("r")); | |
byte[] buffer = new byte[1024 * 16]; | |
int nBytes; | |
while ((nBytes = input.Read(buffer, 0, buffer.Length)) > 0) | |
{ | |
context.Response.OutputStream.Write(buffer, 0, nBytes); | |
} | |
input.Close(); | |
context.Response.StatusCode = (int) HttpStatusCode.OK; | |
context.Response.OutputStream.Flush(); | |
} | |
} | |
catch (Exception) | |
{ | |
context.Response.StatusCode = (int) HttpStatusCode.InternalServerError; | |
} | |
context.Response.OutputStream.Close(); | |
} | |
else | |
{ | |
context.Response.StatusCode = (int) HttpStatusCode.NotFound; | |
} | |
} | |
public void Shutdown() | |
{ | |
m_thread.Abort(); | |
m_listener.Stop(); | |
m_listener.Close(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Websocket | |
{ | |
public static class MimeTypes | |
{ | |
public static IDictionary<string, string> Mappings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase) | |
{{".asf", "video/x-ms-asf"}, | |
{".asx", "video/x-ms-asf"}, | |
{".apk", "application/vnd.android.package-archive"}, | |
{".avi", "video/x-msvideo"}, | |
{".bin", "application/octet-stream"}, | |
{".cco", "application/x-cocoa"}, | |
{".crt", "application/x-x509-ca-cert"}, | |
{".css", "text/css"}, | |
{".deb", "application/octet-stream"}, | |
{".der", "application/x-x509-ca-cert"}, | |
{".dll", "application/octet-stream"}, | |
{".dmg", "application/octet-stream"}, | |
{".ear", "application/java-archive"}, | |
{".eot", "application/octet-stream"}, | |
{".exe", "application/octet-stream"}, | |
{".flv", "video/x-flv"}, | |
{".gif", "image/gif"}, | |
{".hqx", "application/mac-binhex40"}, | |
{".htc", "text/x-component"}, | |
{".htm", "text/html"}, | |
{".html", "text/html"}, | |
{".ico", "image/x-icon"}, | |
{".img", "application/octet-stream"}, | |
{".iso", "application/octet-stream"}, | |
{".jar", "application/java-archive"}, | |
{".jardiff", "application/x-java-archive-diff"}, | |
{".jng", "image/x-jng"}, | |
{".jnlp", "application/x-java-jnlp-file"}, | |
{".jpeg", "image/jpeg"}, | |
{".jpg", "image/jpeg"}, | |
{".js", "application/x-javascript"}, | |
{".mml", "text/mathml"}, | |
{".mng", "video/x-mng"}, | |
{".mov", "video/quicktime"}, | |
{".mp3", "audio/mpeg"}, | |
{".mpeg", "video/mpeg"}, | |
{".mpg", "video/mpeg"}, | |
{".msi", "application/octet-stream"}, | |
{".msm", "application/octet-stream"}, | |
{".msp", "application/octet-stream"}, | |
{".pdb", "application/x-pilot"}, | |
{".pdf", "application/pdf"}, | |
{".pem", "application/x-x509-ca-cert"}, | |
{".pl", "application/x-perl"}, | |
{".pm", "application/x-perl"}, | |
{".png", "image/png"}, | |
{".prc", "application/x-pilot"}, | |
{".ra", "audio/x-realaudio"}, | |
{".rar", "application/x-rar-compressed"}, | |
{".rpm", "application/x-redhat-package-manager"}, | |
{".rss", "text/xml"}, | |
{".run", "application/x-makeself"}, | |
{".sea", "application/x-sea"}, | |
{".shtml", "text/html"}, | |
{".sit", "application/x-stuffit"}, | |
{".swf", "application/x-shockwave-flash"}, | |
{".tcl", "application/x-tcl"}, | |
{".tk", "application/x-tcl"}, | |
{".txt", "text/plain"}, | |
{".war", "application/java-archive"}, | |
{".wbmp", "image/vnd.wap.wbmp"}, | |
{".wmv", "video/x-ms-wmv"}, | |
{".xml", "text/xml"}, | |
{".xpi", "application/x-xpinstall"}, | |
{".zip", "application/zip"}, | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment