Exemplo n.º 1
0
        IEnumerator CoProcessFileGet(HttpListenerContext context, string path)
        {
            var request = context.Request;

            Debug.Assert(request.HttpMethod == "GET");
            var ret = new CoroutineReturnValue <string>();

            yield return(DebugServerUtil.CoLoad(ret, path, overrideEnabled: true));

            var response = context.Response;

            if (ret.Exception != null)
            {
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                response.Close();
            }
            else
            {
                var ext    = System.IO.Path.GetExtension(path);
                var isText = (ext == ".txt") ||
                             (ext == ".json") ||
                             (ext == ".html") ||
                             (ext == ".htm") ||
                             (ext == ".xml") ||
                             (ext == ".yaml") ||
                             (ext == ".csv");
                var sb     = new StringBuilder();
                var writer = HtmlUtil.CreateWriter(sb);
                var title  = path;
                HtmlUtil.WriteHeader(writer, title);
                writer.WriteStartElement("body");
                writer.WriteElementString("h1", title);
                if (isText)
                {
                    HtmlUtil.WriteTextarea(writer, "text", 20, 60, ret.Value);
                    HtmlUtil.WriteBr(writer);
                    HtmlUtil.WriteInput(writer, "update", "button", "submit");
                }

                HtmlUtil.WriteInput(writer, "delete", "button", "delete file");
                HtmlUtil.WriteBr(writer);

                HtmlUtil.WriteInput(writer, "file", "file");
                HtmlUtil.WriteInput(writer, "upload", "button", "upload file");
                HtmlUtil.WriteBr(writer);

                HtmlUtil.WriteOutput(writer, "log");

                writer.WriteStartElement("script");
                writer.WriteString(fileGetScript);
                writer.WriteEndElement();                 //script
                writer.WriteEndElement();                 //body
                writer.WriteEndElement();                 //html
                writer.Close();

                var html  = sb.ToString();
                var bytes = Encoding.UTF8.GetBytes(html);
                response.Close(bytes, willBlock: false);
            }
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
0
        IEnumerator CoProcessFilePut(HttpListenerContext context, string path)
        {
            var request = context.Request;

            UnityEngine.Debug.Assert(request.HttpMethod == "PUT");
            // 内容があるが?delete=trueがあれば、それは削除
            var deleteValue = request.QueryString["delete"];

            if (deleteValue == "true")
            {
                DebugServerUtil.DeleteOverride(path);
            }
            else
            {
                var  bodyData = request.InputStream;
                var  buffer   = new byte[1024 * 1024]; // 秒間60MBはありえない数字じゃないのでこれくらい用意しておく
                bool first    = true;
                while (true)
                {
                    var asyncResult = bodyData.BeginRead(buffer, 0, buffer.Length, null, null);
                    while (!asyncResult.IsCompleted)
                    {
                        yield return(null);
                    }
                    var size = bodyData.EndRead(asyncResult);
                    if (size == 0)
                    {
                        break;
                    }
                    if (first)
                    {
                        DebugServerUtil.SaveOverride(path, buffer, size);
                        first = false;
                    }
                    else
                    {
                        DebugServerUtil.AppendOverride(path, buffer, size);
                    }
                }
                bodyData.Close();
            }
            var response = context.Response;

            response.Close();
            OnChanged(path);
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
0
        void ProcessFilePut(HttpListenerContext context, string path)
        {
            var request = context.Request;

            Debug.Assert(request.HttpMethod == "PUT");
            // 内容があるが?delete=trueがあれば、それは削除
            var deleteValue = request.QueryString["delete"];

            if (deleteValue == "true")
            {
                DebugServerUtil.DeleteOverride(path);
            }
            else
            {
                var bodyData = request.InputStream;
                DebugServerUtil.SaveOverride(path, bodyData);                 // TODO: 非同期化
                bodyData.Close();
            }
            var response = context.Response;

            response.Close();
            OnChanged(path);
        }
Exemplo n.º 6
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();
                }
            }
        }
Exemplo n.º 7
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);
        }