Beispiel #1
0
        static void ProcessRequest(HttpListenerContext context)
        {
            if (String.IsNullOrEmpty(WebServer.Root))
            {
                WebServer.Root = "/";
            }
            if (String.IsNullOrEmpty(WebServer.Unity))
            {
                WebServer.Unity = "/";
            }

            var request = context.Request;
            var url     = request.Url;

            try
            {
                string path = String.Empty;
                context.Response.AddHeader("Cache-Control", "no-cache");
                if (string.IsNullOrEmpty(url.LocalPath) || url.LocalPath == "/")
                {
                    path = Path.Combine(WebServer.Root, "index.html");
                    if (!File.Exists(path))
                    {
                        path = Path.Combine(WebServer.Root, "index.htm");
                        if (!File.Exists(path))
                        {
                            path = Path.Combine(WebServer.Root, "default.html");
                            if (!File.Exists(path))
                            {
                                path = Path.Combine(WebServer.Root, "default.htm");
                                if (!File.Exists(path))
                                {
                                    path = Path.Combine(WebServer.Root, "index.html");
                                }
                            }
                        }
                    }
                }
                else if (url.LocalPath.ToLower().StartsWith("/assets/") && url.LocalPath.ToLower().EndsWith(".ts"))
                {
                    path = Path.Combine(WebServer.Unity, UnityEngine.WWW.UnEscapeURL(url.PathAndQuery.Substring(1)));
                }
                else
                {
                    path = Path.Combine(WebServer.Root, UnityEngine.WWW.UnEscapeURL(url.PathAndQuery.Substring(1)));
                }
                if (!String.IsNullOrEmpty(path))
                {
                    var questionMarkIndex = path.IndexOf("?");
                    if (questionMarkIndex != -1)
                    {
                        path = path.Substring(0, questionMarkIndex);
                    }
                    var hashIndex = path.IndexOf("#");
                    if (hashIndex != -1)
                    {
                        path = path.Substring(0, hashIndex);
                    }
                    if (File.Exists(path))
                    {
                        WebServer.WriteResponse(context, new FileInfo(path));
                    }
                    else
                    {
                        context.Response.StatusCode        = 404;
                        context.Response.StatusDescription = "File Not Found";
                        context.Response.Close();
                    }
                }
                else
                {
                    throw new Exception("Failed to format valid request path.");
                }
            }
            catch (Exception ex)
            {
                context.Response.StatusCode        = 500;
                context.Response.StatusDescription = ex.Message;
                context.Response.Close();
                UnityEngine.Debug.LogException(ex);
            }
        }
Beispiel #2
0
 static void WriteResponse(HttpListenerContext context, byte[] buffer)
 {
     WebServer.WriteResponse(context, new MemoryStream(buffer));
 }
Beispiel #3
0
 static string GetMimeType(string filename)
 {
     return(WebServer.GetMimeType(new FileInfo(filename)));
 }
Beispiel #4
0
 static void WriteResponse(HttpListenerContext context, string data)
 {
     byte[] buffer = Encoding.UTF8.GetBytes(data);
     WebServer.WriteResponse(context, buffer);
 }