Fixed Issue 368. Fixed Issue 369. Fixed Issue 370.
This commit is contained in:
Generated
+5
-5
@@ -84,7 +84,7 @@ namespace OpenHardwareMonitor.GUI {
|
||||
this.plotWindowMenuItem = new System.Windows.Forms.MenuItem();
|
||||
this.plotBottomMenuItem = new System.Windows.Forms.MenuItem();
|
||||
this.plotRightMenuItem = new System.Windows.Forms.MenuItem();
|
||||
this.MenuItem4 = new System.Windows.Forms.MenuItem();
|
||||
this.webMenuItemSeparator = new System.Windows.Forms.MenuItem();
|
||||
this.webMenuItem = new System.Windows.Forms.MenuItem();
|
||||
this.runWebServerMenuItem = new System.Windows.Forms.MenuItem();
|
||||
this.serverPortMenuItem = new System.Windows.Forms.MenuItem();
|
||||
@@ -354,7 +354,7 @@ namespace OpenHardwareMonitor.GUI {
|
||||
this.separatorMenuItem,
|
||||
this.temperatureUnitsMenuItem,
|
||||
this.plotLocationMenuItem,
|
||||
this.MenuItem4,
|
||||
this.webMenuItemSeparator,
|
||||
this.webMenuItem});
|
||||
this.optionsMenuItem.Text = "Options";
|
||||
//
|
||||
@@ -434,8 +434,8 @@ namespace OpenHardwareMonitor.GUI {
|
||||
//
|
||||
// MenuItem4
|
||||
//
|
||||
this.MenuItem4.Index = 7;
|
||||
this.MenuItem4.Text = "-";
|
||||
this.webMenuItemSeparator.Index = 7;
|
||||
this.webMenuItemSeparator.Text = "-";
|
||||
//
|
||||
// webMenuItem
|
||||
//
|
||||
@@ -596,7 +596,7 @@ namespace OpenHardwareMonitor.GUI {
|
||||
private System.Windows.Forms.MenuItem minMenuItem;
|
||||
private System.Windows.Forms.MenuItem maxMenuItem;
|
||||
private System.Windows.Forms.MenuItem temperatureUnitsMenuItem;
|
||||
private System.Windows.Forms.MenuItem MenuItem4;
|
||||
private System.Windows.Forms.MenuItem webMenuItemSeparator;
|
||||
private System.Windows.Forms.MenuItem celsiusMenuItem;
|
||||
private System.Windows.Forms.MenuItem fahrenheitMenuItem;
|
||||
private System.Windows.Forms.MenuItem sumbitReportMenuItem;
|
||||
|
||||
+7
-3
@@ -263,11 +263,16 @@ namespace OpenHardwareMonitor.GUI {
|
||||
fahrenheitMenuItem.Checked = !celsiusMenuItem.Checked;
|
||||
|
||||
server = new HttpServer(root, this.settings.GetValue("listenerPort", 8085));
|
||||
if (server.PlatformNotSupported) {
|
||||
webMenuItemSeparator.Visible = false;
|
||||
webMenuItem.Visible = false;
|
||||
}
|
||||
|
||||
runWebServer = new UserOption("runWebServerMenuItem", false,
|
||||
runWebServerMenuItem, settings);
|
||||
runWebServer.Changed += delegate(object sender, EventArgs e) {
|
||||
if (runWebServer.Value)
|
||||
runWebServer.Value = server.StartHTTPListener();
|
||||
server.StartHTTPListener();
|
||||
else
|
||||
server.StopHTTPListener();
|
||||
};
|
||||
@@ -293,8 +298,7 @@ namespace OpenHardwareMonitor.GUI {
|
||||
computer.Close();
|
||||
SaveConfiguration();
|
||||
if (runWebServer.Value)
|
||||
server.Quit();
|
||||
|
||||
server.Quit();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+84
-37
@@ -28,16 +28,30 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
private Thread listenerThread;
|
||||
private Node root;
|
||||
|
||||
public HttpServer(Node r, int p) {
|
||||
root = r;
|
||||
listenerPort = p;
|
||||
public HttpServer(Node node, int port) {
|
||||
root = node;
|
||||
listenerPort = port;
|
||||
|
||||
//JSON node count.
|
||||
nodeCount = 0;
|
||||
listener = new HttpListener();
|
||||
|
||||
try {
|
||||
listener = new HttpListener();
|
||||
} catch (PlatformNotSupportedException) {
|
||||
listener = null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool PlatformNotSupported {
|
||||
get {
|
||||
return listener == null;
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean StartHTTPListener() {
|
||||
if (PlatformNotSupported)
|
||||
return false;
|
||||
|
||||
try {
|
||||
if (listener.IsListening)
|
||||
return true;
|
||||
@@ -59,6 +73,9 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
}
|
||||
|
||||
public Boolean StopHTTPListener() {
|
||||
if (PlatformNotSupported)
|
||||
return false;
|
||||
|
||||
try {
|
||||
listenerThread.Abort();
|
||||
listener.Stop();
|
||||
@@ -71,7 +88,7 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void HandleRequests() {
|
||||
private void HandleRequests() {
|
||||
|
||||
while (listener.IsListening) {
|
||||
var context = listener.BeginGetContext(
|
||||
@@ -80,22 +97,30 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
}
|
||||
}
|
||||
|
||||
public void ListenerCallback(IAsyncResult result) {
|
||||
private void ListenerCallback(IAsyncResult result) {
|
||||
HttpListener listener = (HttpListener)result.AsyncState;
|
||||
if (listener == null || !listener.IsListening)
|
||||
return;
|
||||
|
||||
// Call EndGetContext to complete the asynchronous operation.
|
||||
HttpListenerContext context = listener.EndGetContext(result);
|
||||
HttpListenerContext context;
|
||||
try {
|
||||
context = listener.EndGetContext(result);
|
||||
} catch (Exception) {
|
||||
return;
|
||||
}
|
||||
|
||||
HttpListenerRequest request = context.Request;
|
||||
|
||||
var requestedFile = request.RawUrl.Substring(1);
|
||||
if (requestedFile == "data.json") {
|
||||
SendJSON(context);
|
||||
SendJSON(context.Response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (requestedFile.Contains("images_icon")) {
|
||||
ServeResourceImage(context, requestedFile.Replace("images_icon/", ""));
|
||||
ServeResourceImage(context.Response,
|
||||
requestedFile.Replace("images_icon/", ""));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -105,13 +130,13 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
|
||||
string[] splits = requestedFile.Split('.');
|
||||
string ext = splits[splits.Length - 1];
|
||||
ServeResourceFile(context, "Web." + requestedFile.Replace('/', '.'), ext);
|
||||
ServeResourceFile(context.Response,
|
||||
"Web." + requestedFile.Replace('/', '.'), ext);
|
||||
}
|
||||
|
||||
private void ServeResourceFile(HttpListenerContext context, string name,
|
||||
private void ServeResourceFile(HttpListenerResponse response, string name,
|
||||
string ext)
|
||||
{
|
||||
|
||||
// resource names do not support the hyphen
|
||||
name = "OpenHardwareMonitor.Resources." +
|
||||
name.Replace("custom-theme", "custom_theme");
|
||||
@@ -122,24 +147,29 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
if (names[i].Replace('\\', '.') == name) {
|
||||
using (Stream stream = Assembly.GetExecutingAssembly().
|
||||
GetManifestResourceStream(names[i])) {
|
||||
context.Response.ContentType = GetcontentType("." + ext);
|
||||
context.Response.ContentLength64 = stream.Length;
|
||||
response.ContentType = GetcontentType("." + ext);
|
||||
response.ContentLength64 = stream.Length;
|
||||
byte[] buffer = new byte[512 * 1024];
|
||||
int len;
|
||||
while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) {
|
||||
context.Response.OutputStream.Write(buffer, 0, len);
|
||||
try {
|
||||
Stream output = response.OutputStream;
|
||||
while ((len = stream.Read(buffer, 0, buffer.Length)) > 0) {
|
||||
output.Write(buffer, 0, len);
|
||||
}
|
||||
output.Close();
|
||||
} catch (HttpListenerException) {
|
||||
}
|
||||
context.Response.OutputStream.Close();
|
||||
}
|
||||
return;
|
||||
response.Close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
context.Response.OutputStream.Close();
|
||||
context.Response.StatusCode = 404;
|
||||
context.Response.Close();
|
||||
|
||||
response.StatusCode = 404;
|
||||
response.Close();
|
||||
}
|
||||
|
||||
private void ServeResourceImage(HttpListenerContext context, string name) {
|
||||
private void ServeResourceImage(HttpListenerResponse response, string name) {
|
||||
name = "OpenHardwareMonitor.Resources." + name;
|
||||
|
||||
string[] names =
|
||||
@@ -150,23 +180,28 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
GetManifestResourceStream(names[i])) {
|
||||
|
||||
Image image = Image.FromStream(stream);
|
||||
context.Response.ContentType = "image/png";
|
||||
using (MemoryStream ms = new MemoryStream()) {
|
||||
image.Save(ms, ImageFormat.Png);
|
||||
ms.WriteTo(context.Response.OutputStream);
|
||||
response.ContentType = "image/png";
|
||||
try {
|
||||
Stream output = response.OutputStream;
|
||||
using (MemoryStream ms = new MemoryStream()) {
|
||||
image.Save(ms, ImageFormat.Png);
|
||||
ms.WriteTo(output);
|
||||
}
|
||||
output.Close();
|
||||
} catch (HttpListenerException) {
|
||||
}
|
||||
context.Response.OutputStream.Close();
|
||||
image.Dispose();
|
||||
response.Close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
context.Response.OutputStream.Close();
|
||||
context.Response.StatusCode = 404;
|
||||
context.Response.Close();
|
||||
|
||||
response.StatusCode = 404;
|
||||
response.Close();
|
||||
}
|
||||
|
||||
private void SendJSON(HttpListenerContext context) {
|
||||
private void SendJSON(HttpListenerResponse response) {
|
||||
|
||||
string JSON = "{\"id\": 0, \"Text\": \"Sensor\", \"Children\": [";
|
||||
nodeCount = 1;
|
||||
@@ -181,13 +216,19 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
var responseContent = JSON;
|
||||
byte[] buffer = Encoding.UTF8.GetBytes(responseContent);
|
||||
|
||||
context.Response.ContentLength64 = buffer.Length;
|
||||
context.Response.ContentType = "application/json";
|
||||
response.AddHeader("Cache-Control", "no-cache");
|
||||
|
||||
Stream outputStream = context.Response.OutputStream;
|
||||
outputStream.Write(buffer, 0, buffer.Length);
|
||||
outputStream.Close();
|
||||
response.ContentLength64 = buffer.Length;
|
||||
response.ContentType = "application/json";
|
||||
|
||||
try {
|
||||
Stream output = response.OutputStream;
|
||||
output.Write(buffer, 0, buffer.Length);
|
||||
output.Close();
|
||||
} catch (HttpListenerException) {
|
||||
}
|
||||
|
||||
response.Close();
|
||||
}
|
||||
|
||||
private string GenerateJSON(Node n) {
|
||||
@@ -327,11 +368,17 @@ namespace OpenHardwareMonitor.Utilities {
|
||||
}
|
||||
|
||||
~HttpServer() {
|
||||
if (PlatformNotSupported)
|
||||
return;
|
||||
|
||||
StopHTTPListener();
|
||||
listener.Abort();
|
||||
}
|
||||
|
||||
public void Quit() {
|
||||
if (PlatformNotSupported)
|
||||
return;
|
||||
|
||||
StopHTTPListener();
|
||||
listener.Abort();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user