Merge pull request #179 from gnh1201/dev
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

Add the favicon.ico response when try a request
This commit is contained in:
Namhyeon Go 2025-03-10 00:33:14 +09:00 committed by GitHub
commit 653c44dbf6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,7 +24,7 @@ namespace WelsonJS.Launcher
_prefix = prefix;
_listener = new HttpListener();
_listener.Prefixes.Add(prefix);
_resourceName = typeof(ResourceServer).Namespace + "." + resourceName;
_resourceName = resourceName;
}
public void Start()
@ -74,31 +74,80 @@ namespace WelsonJS.Launcher
private void ProcessRequest(HttpListenerContext context)
{
string responseString = GetEmbeddedResource();
string path = context.Request.Url.AbsolutePath.TrimStart('/');
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
context.Response.ContentType = "text/html";
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
if ("favicon.ico".Equals(path, StringComparison.OrdinalIgnoreCase))
{
ServeResource(context, GetResource("favicon"), "image/x-icon");
return;
}
ServeResource(context, GetResource(_resourceName), "text/html");
}
private void ServeResource(HttpListenerContext context, byte[] data, string mimeType = "text/html")
{
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.")
;
}
context.Response.ContentType = mimeType;
context.Response.ContentLength64 = data.Length;
context.Response.OutputStream.Write(data, 0, data.Length);
context.Response.OutputStream.Close();
}
private string GetEmbeddedResource()
private byte[] GetResource(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(_resourceName))
{
if (stream == null)
{
return "<html><body><h1>Could not find the resource.</h1></body></html>";
// Try to fetch embedded resource.
byte[] data = GetEmbeddedResource(typeof(ResourceServer).Namespace + "." + resourceName);
if (data != null) return data;
// Fallback: Try to fetch resource from ResourceManager.
return GetResourceFromManager(resourceName);
}
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
private byte[] GetEmbeddedResource(string fullResourceName)
{
return reader.ReadToEnd();
Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(fullResourceName))
{
if (stream != null)
{
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
}
return null;
}
private byte[] GetResourceFromManager(string resourceName)
{
object resourceObject = Properties.Resources.ResourceManager.GetObject(resourceName);
switch (resourceObject)
{
case byte[] resourceBytes:
return resourceBytes;
case System.Drawing.Icon icon:
return ConvertIconToBytes(icon);
default:
return null;
}
}
private byte[] ConvertIconToBytes(System.Drawing.Icon icon)
{
using (MemoryStream memoryStream = new MemoryStream())
{
icon.Save(memoryStream);
return memoryStream.ToArray();
}
}
}
}