-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppServer.cs
196 lines (175 loc) · 7.49 KB
/
AppServer.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using IRWA.Resources;
using nanoFramework.Runtime.Native;
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
#nullable enable
namespace IRWA
{
public class AppServer : HttpServiceBase
{
private const string ApiRequestPathPrefix = "api/";
private const string ApiRequestSendRawSuffix = "sendraw";
public const string ContentTypeText = "text/plain";
public const string ContentTypeHtml = "text/html";
public const string ContentTypeJson = "application/json";
private readonly IrSenderNec irSender;
public AppServer(int port, IrSenderNec irSender) : base(port)
{
this.irSender = irSender;
}
public AppServer(int port, X509Certificate httpsCertificate, IrSenderNec irSender)
: base(port, httpsCertificate)
{
this.irSender = irSender;
}
protected override void HandleClient(HttpListenerContext context)
{
var path = context.Request.RawUrl.ToLower().TrimStart('/').TrimStart() ?? "";
if (path.Length == 0)
{
path = WebResourceProvider.DefaultFileName;
}
if (path.StartsWith(ApiRequestPathPrefix))
{
if (path == (ApiRequestPathPrefix + ApiRequestSendRawSuffix))
{
HandleRequestAsApiSendRaw(context);
}
else
{
SendErrorResponse(context.Response, 404, "API endpoint not found.");
}
}
else if (!TrySendResourceResponse(context.Response, path))
{
SendErrorResponse(context.Response, 404, "File not found.");
}
}
public bool TrySendResourceResponse(HttpListenerResponse response,
string path)
{
if (WebResourceProvider.TryResolveResource(path, out var resource))
{
response.StatusCode = 200;
response.Headers.Add("Cache-Control", "max-age=86400");
response.ContentType = GetContentType(path);
CopyResourceToStream(resource, response.OutputStream);
response.Close();
return true;
}
else
{
return false;
}
}
private void HandleRequestAsApiSendRaw(HttpListenerContext context)
{
if (context.Request.HttpMethod != "POST")
{
SendErrorResponse(context.Response, 405, "Only POST mode allowed.");
return;
}
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
byte[] requestContent = new byte[Math.Min((int)context.Request.ContentLength64, 1024)];
context.Request.InputStream.Read(requestContent, 0, requestContent.Length);
string requestContentString = Encoding.UTF8.GetString(requestContent, 0, requestContent.Length);
uint code;
if (requestContentString.Contains("x"))
{
code = Convert.ToUInt32(requestContentString, 16);
}
else if (!uint.TryParse(requestContentString, out code))
{
code = default;
}
irSender.SendRaw(code, 2);
SendEmptyResponse(context.Response);
}
public static void SendEmptyResponse(HttpListenerResponse targetResponse,
int statusCode = 204)
{
targetResponse.StatusCode = statusCode;
targetResponse.Close();
}
public static void SendErrorResponse(HttpListenerResponse targetResponse, int statusCode,
string description)
{
SendStringResponse(targetResponse, statusCode,
BuildErrorPage("Error", ">﹏<", description));
}
public static void SendStringResponse(HttpListenerResponse targetResponse, int statusCode,
string content, string contentType = ContentTypeHtml, bool closeAfterwards = true)
{
targetResponse.ContentType = contentType;
targetResponse.StatusCode = statusCode;
byte[] contentBytes = Encoding.UTF8.GetBytes(content);
targetResponse.ContentLength64 = contentBytes.Length;
targetResponse.OutputStream.Write(contentBytes, 0, contentBytes.Length);
if (closeAfterwards)
{
targetResponse.Close();
}
}
public static string GetContentType(string uri)
{
int fileExtensionSeparatorIndex = uri.LastIndexOf('.') + 1;
if (fileExtensionSeparatorIndex > 0)
{
string fileExtension = uri.Substring(fileExtensionSeparatorIndex).ToLower();
return fileExtension switch
{
"txt" => "text/plain",
"css" => "text/css",
"js" => "text/javascript",
"htm" => ContentTypeHtml,
"html" => ContentTypeHtml,
"ico" => "image/vnd.microsoft.icon",
"svg" => "image/svg+xml",
"png" => "image/png",
_ => "application/octet-stream",
};
}
else return "application/octet-stream";
}
public static string BuildErrorPage(string title, string heading, string subtext)
{
return $"<!DOCTYPE html><html><head><title>{title}</title><meta name='viewport' " +
"content='width=device-width,initial-scale=1,maximum-scale=1," +
"user-scalable=no'></head><body style=\"background-color:#11191F; " +
"color: #EDF0F3; margin: 0; height: 100vh; display: flex; flex-direction: " +
"column; justify-content: center; text-align: center; font-family: system-ui," +
"-apple-system,'Segoe UI','Roboto','Ubuntu','Cantarell','Noto Sans'," +
$"sans-serif\"><h1 style='font-size: 5em'>{heading}</h1>" +
$"<sub>{subtext}</sub></body></html>";
}
internal static void CopyResourceToStream(WebResources.BinaryResources resourceName,
Stream targetStream)
{
byte[] buffer;
int offset = 0;
const int count = 1024;
try
{
do
{
buffer = (byte[])ResourceUtility.GetObject(
WebResources.ResourceManager, resourceName, offset, count);
targetStream.Write(buffer, 0, buffer.Length);
offset += buffer.Length;
} while (buffer.Length == count);
}
catch (Exception)
{
// If the size of the requested resource is divisible by the specified count,
// an exception will be thrown after getting the last chunk (as no way to retrieve
// the resource file size was found while writing this).
// So, only if the offset is 0 (no data has been transferred), treat an exception
// as an error.
if (offset == 0) throw;
}
}
}
}