mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-07 12:16:04 +00:00
Update ResourceServer.cs
This commit is contained in:
parent
1124dc5bf4
commit
2da5051c5b
|
@ -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<string> executables = _executablesCollector.GetExecutables();
|
||||
|
@ -143,61 +142,37 @@ namespace WelsonJS.Launcher
|
|||
))
|
||||
);
|
||||
|
||||
byte[] data = Encoding.UTF8.GetBytes(
|
||||
"<?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);
|
||||
}
|
||||
ServeResource(context, response.ToString(), "application/xml");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
byte[] errorData = Encoding.UTF8.GetBytes(
|
||||
"<?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);
|
||||
}
|
||||
ServeResource(context, $"<error>Failed to process completion request. {ex.Message}</error>", "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, $"<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) {
|
||||
data = "text/html".Equals(mimeType, StringComparison.OrdinalIgnoreCase) ?
|
||||
Encoding.UTF8.GetBytes("<html><body><h1>Could not find the resource.</h1></body></html>") :
|
||||
Encoding.UTF8.GetBytes("Could not find the resource.")
|
||||
;
|
||||
data = Encoding.UTF8.GetBytes(xmlHeader + "\r\n<error>Could not find the resource.</error>");
|
||||
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 = "<?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)
|
||||
{
|
||||
// Try to fetch embedded resource.
|
||||
|
|
Loading…
Reference in New Issue
Block a user