예제 #1
0
        void ProcessRequest(HttpListenerContext context)
        {
            UnityEngine.Profiling.Profiler.BeginSample("DebugServer.ProcessRequest");
            var request = context.Request;
            var path    = DebugServerUtil.RemoveQueryString(request.RawUrl);          // QueryStringいらない

            if (path.StartsWith(fileService.pathPrefix))
            {
                Debug.Log("FileService.ProcessReq");
                fileService.ProcessRequest(context);
            }
            else
            {
                OnRequest callback = null;
                if (callbacks.TryGetValue(path, out callback))
                {
                    ProcessRequest(context, callback);
                }
                else
                {
                    var response = context.Response;
                    response.StatusCode = 404;
                    response.Close();
                }
            }
            UnityEngine.Profiling.Profiler.EndSample();
        }
예제 #2
0
        public void ProcessRequest(HttpListenerContext context)
        {
            UnityEngine.Profiling.Profiler.BeginSample("DebugFileService.ProcessRequest");
            var response = context.Response;
            // index.html処理
            var path = DebugServerUtil.RemoveQueryString(context.Request.RawUrl);

            // prefixチェック
            if (path.StartsWith(pathPrefix))
            {
                path = path.Remove(0, pathPrefix.Length);
                // 空になったか、末尾が/か、その名前のディレクトリが存在すれば
                if ((path == "") || (path[path.Length - 1] == '/') || DebugServerUtil.DirectoryExists(path))
                {
                    ProcessDirectory(context, path);
                }
                else                 // そうでなければファイル
                {
                    var request = context.Request;
                    if (request.HttpMethod == "PUT")                     // Putなら即時処理
                    {
                        ProcessFilePut(context, path);
                    }
                    else if (request.HttpMethod == "GET")                     // GETはロードが必要なのでコルーチンで処理
                    {
                        coroutineRunner.Start(CoProcessFileGet(context, path));
                    }
                    else
                    {
                        response.StatusCode = (int)HttpStatusCode.InternalServerError;
                        response.Close();
                    }
                }
            }
            else
            {
                Debug.Assert(false, "url does not contain prefix: " + pathPrefix);
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                response.Close();
            }
            UnityEngine.Profiling.Profiler.EndSample();
        }
예제 #3
0
        void ProcessRequest(HttpListenerContext context)
        {
            var request = context.Request;
            var path    = DebugServerUtil.RemoveQueryString(request.RawUrl);          // QueryStringいらない

            if (path.StartsWith(fileService.pathPrefix))
            {
                fileService.ProcessRequest(context);
            }
            else
            {
                OnRequest callback = null;
                if (callbacks.TryGetValue(path, out callback))
                {
                    Process(context, callback);
                }
                else
                {
                    var response = context.Response;
                    response.StatusCode = 404;
                    response.Close();
                }
            }
        }
예제 #4
0
        void ProcessDirectory(HttpListenerContext context, string path)
        {
            var  response   = context.Response;
            bool redirected = false;

            if (path.Length > 0)
            {
                if (path[path.Length - 1] == '/')                 // 末尾スラッシュ消し
                {
                    path = path.Remove(path.Length - 1, 1);
                }
                else
                {
                    redirected = true;                     // スラッシュ付きにリダイレクトする
                }
            }
            var sb     = new StringBuilder();
            var writer = HtmlUtil.CreateWriter(sb);
            var title  = "index of /" + path;

            HtmlUtil.WriteHeader(writer, title);
            writer.WriteStartElement("body");
            writer.WriteElementString("h1", title);

            var parentDirectory = FindDirectory(path);

            if (parentDirectory == null)
            {
                writer.WriteElementString("p", "not found in map. use MakeStreamingAssetsMap tool in Editor.");
            }
            else
            {
                writer.WriteStartElement("ul");
                var directoryIndex = parentDirectory.firstChild;
                while (directoryIndex >= 0)
                {
                    var directory = streamingAssetsMap.directories[directoryIndex];
                    writer.WriteStartElement("li");
                    HtmlUtil.WriteA(writer, directory.name + "/", directory.name);
                    writer.WriteEndElement();                     //li
                    directoryIndex = directory.nextBrother;
                }

                var fileIndex = parentDirectory.firstFile;
                while (fileIndex >= 0)
                {
                    var file = streamingAssetsMap.files[fileIndex];
                    writer.WriteStartElement("li");
                    HtmlUtil.WriteA(writer, file.name, file.name);
                    writer.WriteEndElement();                     //li
                    fileIndex = file.nextFile;
                }
                writer.WriteEndElement();         //ul
            }
            writer.WriteEndElement();             //body
            writer.WriteEndElement();             //html
            writer.Close();

            var bytes = System.Text.Encoding.UTF8.GetBytes(sb.ToString());

            if (redirected)
            {
                var url = DebugServerUtil.RemoveQueryString(context.Request.Url.ToString());
                url += "/";
                response.Redirect(url);
            }
            else
            {
                response.StatusCode = (int)HttpStatusCode.OK;
            }
            response.Close(bytes, willBlock: false);
        }