< Summary

Class:SimpleHTTPServer
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Debugging/SimpleWebServer.cs
Covered lines:0
Uncovered lines:61
Coverable lines:61
Total lines:246
Line coverage:0% (0 of 61)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SimpleHTTPServer(...)0%2100%
SimpleHTTPServer(...)0%2100%
SimpleHTTPServer()0%2100%
Stop()0%2100%
Listen()0%2100%
Process(...)0%72800%
Initialize(...)0%2100%
Restart(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Debugging/SimpleWebServer.cs

#LineLine coverage
 1// MIT License - Copyright (c) 2016 Can Güney Aksakalli
 2
 3using System;
 4using System.Collections.Generic;
 5using System.IO;
 6using System.Net;
 7using System.Net.Sockets;
 8using System.Threading;
 9
 10class SimpleHTTPServer
 11{
 012    private readonly string[] _indexFiles =
 13    {
 14        "index.html",
 15        "index.htm",
 16        "default.html",
 17        "default.htm"
 18    };
 19
 20#if !WINDOWS_UWP
 021    private static IDictionary<string, string> _mimeTypeMappings =
 22        new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
 23        {
 24#else
 25private static IDictionary<string, string> _mimeTypeMappings =
 26new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
 27#endif
 28
 29            #region extension to MIME type list
 30
 31            {".asf", "video/x-ms-asf"},
 32            {".asx", "video/x-ms-asf"},
 33            {".avi", "video/x-msvideo"},
 34            {".bin", "application/octet-stream"},
 35            {".cco", "application/x-cocoa"},
 36            {".crt", "application/x-x509-ca-cert"},
 37            {".css", "text/css"},
 38            {".deb", "application/octet-stream"},
 39            {".der", "application/x-x509-ca-cert"},
 40            {".dll", "application/octet-stream"},
 41            {".dmg", "application/octet-stream"},
 42            {".ear", "application/java-archive"},
 43            {".eot", "application/octet-stream"},
 44            {".exe", "application/octet-stream"},
 45            {".flv", "video/x-flv"},
 46            {".gif", "image/gif"},
 47            {".hqx", "application/mac-binhex40"},
 48            {".htc", "text/x-component"},
 49            {".htm", "text/html"},
 50            {".html", "text/html"},
 51            {".ico", "image/x-icon"},
 52            {".img", "application/octet-stream"},
 53            {".iso", "application/octet-stream"},
 54            {".jar", "application/java-archive"},
 55            {".jardiff", "application/x-java-archive-diff"},
 56            {".jng", "image/x-jng"},
 57            {".jnlp", "application/x-java-jnlp-file"},
 58            {".jpeg", "image/jpeg"},
 59            {".jpg", "image/jpeg"},
 60            {".js", "application/x-javascript"},
 61            {".mml", "text/mathml"},
 62            {".mng", "video/x-mng"},
 63            {".mov", "video/quicktime"},
 64            {".mp3", "audio/mpeg"},
 65            {".mpeg", "video/mpeg"},
 66            {".mpg", "video/mpeg"},
 67            {".msi", "application/octet-stream"},
 68            {".msm", "application/octet-stream"},
 69            {".msp", "application/octet-stream"},
 70            {".pdb", "application/x-pilot"},
 71            {".pdf", "application/pdf"},
 72            {".pem", "application/x-x509-ca-cert"},
 73            {".pl", "application/x-perl"},
 74            {".pm", "application/x-perl"},
 75            {".png", "image/png"},
 76            {".prc", "application/x-pilot"},
 77            {".ra", "audio/x-realaudio"},
 78            {".rar", "application/x-rar-compressed"},
 79            {".rpm", "application/x-redhat-package-manager"},
 80            {".rss", "text/xml"},
 81            {".run", "application/x-makeself"},
 82            {".sea", "application/x-sea"},
 83            {".shtml", "text/html"},
 84            {".sit", "application/x-stuffit"},
 85            {".swf", "application/x-shockwave-flash"},
 86            {".tcl", "application/x-tcl"},
 87            {".tk", "application/x-tcl"},
 88            {".txt", "text/plain"},
 89            {".war", "application/java-archive"},
 90            {".wbmp", "image/vnd.wap.wbmp"},
 91            {".wmv", "video/x-ms-wmv"},
 92            {".xml", "text/xml"},
 93            {".xpi", "application/x-xpinstall"},
 94            {".zip", "application/zip"},
 95
 96            #endregion
 97        };
 98
 99    private Thread _serverThread;
 100    private string _rootDirectory;
 101#if !WINDOWS_UWP
 102    private HttpListener _listener;
 103#endif
 104    private int _port;
 105
 106    public int Port
 107    {
 0108        get { return _port; }
 0109        private set { }
 110    }
 111
 112    /// <summary>
 113    /// Construct server with given port.
 114    /// </summary>
 115    /// <param name="path">Directory path to serve.</param>
 116    /// <param name="port">Port of the server.</param>
 0117    public SimpleHTTPServer(string path, int port)
 118    {
 0119        this.Initialize(path, port);
 0120    }
 121
 122    /// <summary>
 123    /// Construct server with suitable port.
 124    /// </summary>
 125    /// <param name="path">Directory path to serve.</param>
 0126    public SimpleHTTPServer(string path)
 127    {
 128#if !WINDOWS_UWP
 129        //get an empty port
 0130        TcpListener l = new TcpListener(IPAddress.Loopback, 0);
 0131        l.Start();
 0132        int port = ((IPEndPoint) l.LocalEndpoint).Port;
 0133        l.Stop();
 0134        this.Initialize(path, port);
 135#endif
 0136    }
 137
 138    /// <summary>
 139    /// Stop server and dispose all functions.
 140    /// </summary>
 141    public void Stop()
 142    {
 143#if !WINDOWS_UWP
 0144        _serverThread.Abort();
 0145        _listener.Stop();
 146#endif
 0147    }
 148
 149    private void Listen()
 150    {
 151#if !WINDOWS_UWP
 0152        _listener = new HttpListener();
 0153        _listener.Prefixes.Add("http://*:" + _port.ToString() + "/");
 0154        _listener.Start();
 155        while (true)
 156        {
 157            try
 158            {
 0159                HttpListenerContext context = _listener.GetContext();
 0160                Process(context);
 0161            }
 0162            catch (Exception)
 163            {
 0164            }
 165        }
 166#endif
 167    }
 168
 169#if !WINDOWS_UWP
 170    private void Process(HttpListenerContext context)
 171    {
 0172        string filename = context.Request.Url.AbsolutePath;
 0173        Console.WriteLine(filename);
 0174        filename = filename.Substring(1);
 175
 0176        if (string.IsNullOrEmpty(filename))
 177        {
 0178            foreach (string indexFile in _indexFiles)
 179            {
 0180                if (File.Exists(Path.Combine(_rootDirectory, indexFile)))
 181                {
 0182                    filename = indexFile;
 0183                    break;
 184                }
 185            }
 186        }
 187
 0188        filename = Path.Combine(_rootDirectory, filename);
 189
 0190        if (File.Exists(filename))
 191        {
 192            try
 193            {
 0194                Stream input = new FileStream(filename, FileMode.Open);
 195
 196                //Adding permanent http response headers
 197                string mime;
 0198                context.Response.ContentType = _mimeTypeMappings.TryGetValue(Path.GetExtension(filename), out mime)
 199                    ? mime
 200                    : "application/octet-stream";
 0201                context.Response.ContentLength64 = input.Length;
 0202                context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
 0203                context.Response.AddHeader("Last-Modified", System.IO.File.GetLastWriteTime(filename).ToString("r"));
 204
 0205                byte[] buffer = new byte[1024 * 16];
 206                int nbytes;
 0207                while ((nbytes = input.Read(buffer, 0, buffer.Length)) > 0)
 208                {
 0209                    context.Response.OutputStream.Write(buffer, 0, nbytes);
 210                }
 211
 0212                input.Close();
 213
 0214                context.Response.StatusCode = (int) HttpStatusCode.OK;
 0215                context.Response.OutputStream.Flush();
 0216            }
 0217            catch (Exception)
 218            {
 0219                context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
 0220            }
 221        }
 222        else
 223        {
 0224            context.Response.StatusCode = (int) HttpStatusCode.NotFound;
 225        }
 226
 0227        context.Response.OutputStream.Close();
 0228    }
 229#endif
 230
 231    private void Initialize(string path, int port)
 232    {
 233#if !WINDOWS_UWP
 0234        this._rootDirectory = path;
 0235        this._port = port;
 0236        _serverThread = new Thread(this.Listen);
 0237        _serverThread.Start();
 238#endif
 0239    }
 240
 241    public void Restart(string path, int port)
 242    {
 0243        Stop();
 0244        Initialize(path, port);
 0245    }
 246}