Exemplo n.º 1
0
        static void WriteResponse(HttpListenerContext context, FileInfo info)
        {
            bool compress = false;

            context.Response.ContentType = WebServer.GetMimeType(info);
            if (WebServer.CompressExtentions.IndexOf(info.Extension, StringComparison.OrdinalIgnoreCase) >= 0)
            {
                string contentEncoding = context.Request.Headers["Accept-Encoding"] ?? "none";
                compress = contentEncoding.Contains("gzip");
            }
            FileStream file = new FileStream(info.FullName, FileMode.Open, FileAccess.Read);

            WebServer.WriteResponse(context, file, compress);
            file.Close();
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
 static void WriteResponse(HttpListenerContext context, byte[] buffer)
 {
     WebServer.WriteResponse(context, new MemoryStream(buffer));
 }
Exemplo n.º 4
0
 static void WriteResponse(HttpListenerContext context, string data)
 {
     byte[] buffer = Encoding.UTF8.GetBytes(data);
     WebServer.WriteResponse(context, buffer);
 }