-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWebLoggerHttpServer.cs
131 lines (115 loc) · 4.39 KB
/
WebLoggerHttpServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#pragma warning disable IDE1006
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO;
using Crestron.SimplSharp.Net.Http;
using System;
using System.Collections.Generic;
namespace WebLogger.Crestron
{
/// <summary>
/// Serves the files in the provided directory
/// Server is unsecured and will provide a valid html location to run the web page hosted on a secured server.
/// Browsers will block unsecured websocket connections from html locations severed securely
/// </summary>
public sealed class WebLoggerHttpServer : IDisposable
{
/// <summary>
/// The extension content types
/// </summary>
private static Dictionary<string, string> _extensionContentTypes;
/// <summary>
/// Gets the type of the content.
/// </summary>
/// <param name="extension">The extension.</param>
/// <returns>System.String.</returns>
public static string GetContentType(string extension)
{
var type = _extensionContentTypes.TryGetValue(extension, out var contentType) ? contentType : "text/plain";
return type;
}
private readonly HttpServer _server;
private readonly string _directory;
private bool _disposedValue;
/// <summary>
/// Creates a new instance of the Logo Server and starts server
/// </summary>
/// <param name="port">HTTP Port</param>
/// <param name="directory">File Directory to Serve</param>
public WebLoggerHttpServer(int port, string directory)
{
_extensionContentTypes = new Dictionary<string, string>
{
{ ".html", "text/html" },
{ ".json", "application/json" },
{ ".js", "text/javascript" },
{ ".css", "text/css" },
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".pdf", "application/pdf" },
{ ".png", "image/png" },
};
_directory = directory;
_server = new HttpServer() { Port = port };
_server.OnHttpRequest += Server_OnHttpRequest;
_server.Open();
CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler;
}
private void Server_OnHttpRequest(object sender, OnHttpRequestArgs args)
{
var path = args.Request.Path;
try
{
if (File.Exists(_directory + path))
{
var filePath = path.Replace('/', '\\');
var localPath = $@"{_directory}{filePath}";
if (File.Exists(localPath))
{
args.Response.Header.ContentType = GetContentType(new FileInfo(localPath).Extension);
args.Response.ContentStream = new FileStream(localPath, FileMode.Open, FileAccess.Read);
}
else
{
args.Response.ContentString = $"Not found: '{filePath}'";
args.Response.Code = 404;
}
}
else
{
args.Response.ContentString = $"Not found: '{_directory + path}'";
args.Response.Code = 404;
}
}
catch (Exception)
{
args.Response.Code = 400;
args.Response.ContentString = string.Format("invalid request");
}
}
private void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType)
{
if (programEventType == eProgramStatusEventType.Stopping)
Dispose();
}
private void Dispose(bool disposing)
{
if (_disposedValue) return;
if (disposing)
{
_server.OnHttpRequest -= Server_OnHttpRequest;
_server.Close();
_server.Dispose();
}
_disposedValue = true;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}