From 2da5051c5bef0965fb2f87c5e2f0bf88b90f7dce Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Sun, 16 Mar 2025 15:08:20 +0900 Subject: [PATCH] Update ResourceServer.cs --- .../WelsonJS.Launcher/ResourceServer.cs | 77 +++++++++---------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/WelsonJS.Toolkit/WelsonJS.Launcher/ResourceServer.cs b/WelsonJS.Toolkit/WelsonJS.Launcher/ResourceServer.cs index b1edfd8..4e48675 100644 --- a/WelsonJS.Toolkit/WelsonJS.Launcher/ResourceServer.cs +++ b/WelsonJS.Toolkit/WelsonJS.Launcher/ResourceServer.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; +using System.Xml; using System.Xml.Linq; namespace WelsonJS.Launcher @@ -116,8 +117,6 @@ namespace WelsonJS.Launcher private void ServeCompletion(HttpListenerContext context, string word) { - int statusCode = 200; - try { List executables = _executablesCollector.GetExecutables(); @@ -143,61 +142,37 @@ namespace WelsonJS.Launcher )) ); - byte[] data = Encoding.UTF8.GetBytes( - "\r\n" + - response.ToString() - ); - - context.Response.StatusCode = statusCode; - context.Response.ContentType = "application/xml"; - context.Response.ContentLength64 = data.Length; - using (Stream outputStream = context.Response.OutputStream) - { - outputStream.Write(data, 0, data.Length); - } + ServeResource(context, response.ToString(), "application/xml"); } catch (Exception ex) { - byte[] errorData = Encoding.UTF8.GetBytes( - "\r\n" + - $"Failed to process completion request. {ex.Message}" - ); - context.Response.StatusCode = 500; - context.Response.ContentType = "application/xml"; - context.Response.ContentLength64 = errorData.Length; - using (Stream outputStream = context.Response.OutputStream) - { - outputStream.Write(errorData, 0, errorData.Length); - } + ServeResource(context, $"Failed to process completion request. {ex.Message}", "application/xml", 500); } } private void ServeDevTools(HttpListenerContext context, string endpoint) { - int statusCode = 200; - - HttpClient client = new HttpClient(); - string url = "http://localhost:9222" + endpoint; - byte[] data = Task.Run(async () => await client.GetByteArrayAsync(url)).Result; - - context.Response.StatusCode = statusCode; - context.Response.ContentType = "application/json"; - context.Response.ContentLength64 = data.Length; - using (Stream outputStream = context.Response.OutputStream) + try { - outputStream.Write(data, 0, data.Length); + HttpClient client = new HttpClient(); + string url = "http://localhost:9222" + endpoint; + string data = Task.Run(async () => await client.GetStringAsync(url)).Result; + + ServeResource(context, data, "application/json"); + } + catch (Exception ex) + { + ServeResource(context, $"Failed to process completion request. {ex.Message}", "application/xml", 500); } } - private void ServeResource(HttpListenerContext context, byte[] data, string mimeType = "text/html") + private void ServeResource(HttpListenerContext context, byte[] data, string mimeType = "text/html", int statusCode = 200) { - int statusCode = 200; + string xmlHeader = ""; if (data == null) { - data = "text/html".Equals(mimeType, StringComparison.OrdinalIgnoreCase) ? - Encoding.UTF8.GetBytes("

Could not find the resource.

") : - Encoding.UTF8.GetBytes("Could not find the resource.") - ; + data = Encoding.UTF8.GetBytes(xmlHeader + "\r\nCould not find the resource."); + mimeType = "application/xml"; statusCode = 404; } @@ -210,6 +185,24 @@ namespace WelsonJS.Launcher } } + private void ServeResource(HttpListenerContext context, string data, string mimeType = "text/html", int statusCode = 200) + { + string xmlHeader = ""; + + if (data == null) + { + data = xmlHeader + "\r\nCould not find the resource."; + mimeType = "application/xml"; + statusCode = 404; + } + else if (mimeType == "application/xml" && !data.StartsWith("