Update ResourceServer.cs

This commit is contained in:
Namhyeon Go 2025-03-16 15:08:20 +09:00
parent 1124dc5bf4
commit 2da5051c5b

View File

@ -9,6 +9,7 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq; using System.Xml.Linq;
namespace WelsonJS.Launcher namespace WelsonJS.Launcher
@ -116,8 +117,6 @@ namespace WelsonJS.Launcher
private void ServeCompletion(HttpListenerContext context, string word) private void ServeCompletion(HttpListenerContext context, string word)
{ {
int statusCode = 200;
try try
{ {
List<string> executables = _executablesCollector.GetExecutables(); List<string> executables = _executablesCollector.GetExecutables();
@ -143,61 +142,37 @@ namespace WelsonJS.Launcher
)) ))
); );
byte[] data = Encoding.UTF8.GetBytes( ServeResource(context, response.ToString(), "application/xml");
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
byte[] errorData = Encoding.UTF8.GetBytes( ServeResource(context, $"<error>Failed to process completion request. {ex.Message}</error>", "application/xml", 500);
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" +
$"<error>Failed to process completion request. {ex.Message}</error>"
);
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);
}
} }
} }
private void ServeDevTools(HttpListenerContext context, string endpoint) private void ServeDevTools(HttpListenerContext context, string endpoint)
{ {
int statusCode = 200; try
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)
{ {
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, $"<error>Failed to process completion request. {ex.Message}</error>", "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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
if (data == null) { if (data == null) {
data = "text/html".Equals(mimeType, StringComparison.OrdinalIgnoreCase) ? data = Encoding.UTF8.GetBytes(xmlHeader + "\r\n<error>Could not find the resource.</error>");
Encoding.UTF8.GetBytes("<html><body><h1>Could not find the resource.</h1></body></html>") : mimeType = "application/xml";
Encoding.UTF8.GetBytes("Could not find the resource.")
;
statusCode = 404; 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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
if (data == null)
{
data = xmlHeader + "\r\n<error>Could not find the resource.</error>";
mimeType = "application/xml";
statusCode = 404;
}
else if (mimeType == "application/xml" && !data.StartsWith("<?xml"))
{
data = xmlHeader + "\r\n" + data;
}
ServeResource(context, Encoding.UTF8.GetBytes(data), mimeType, statusCode);
}
private byte[] GetResource(string resourceName) private byte[] GetResource(string resourceName)
{ {
// Try to fetch embedded resource. // Try to fetch embedded resource.