-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
WebServer.cs
188 lines (160 loc) · 6.65 KB
/
WebServer.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
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using nanoFramework.Runtime.Native;
namespace WifiAP
{
public class WebServer
{
HttpListener _listener;
Thread _serverThread;
public void Start()
{
if (_listener == null)
{
_listener = new HttpListener("http");
_serverThread = new Thread(RunServer);
_serverThread.Start();
}
}
public void Stop()
{
if (_listener != null)
_listener.Stop();
}
private void RunServer()
{
_listener.Start();
while (_listener.IsListening)
{
var context = _listener.GetContext();
if (context != null)
ProcessRequest(context);
}
_listener.Close();
_listener = null;
}
private void ProcessRequest(HttpListenerContext context)
{
var request = context.Request;
var response = context.Response;
string responseString;
string ssid = null;
string password = null;
bool isApSet = false;
switch (request.HttpMethod)
{
case "GET":
string[] url = request.RawUrl.Split('?');
if (url[0] == "/favicon.ico")
{
response.ContentType = "image/png";
byte[] responseBytes = Resources.GetBytes(Resources.BinaryResources.favicon);
OutPutByteResponse(response, responseBytes);
}
else
{
response.ContentType = "text/html";
responseString = ReplaceMessage(Resources.GetString(Resources.StringResources.main), "");
OutPutResponse(response, responseString);
}
break;
case "POST":
// Pick up POST parameters from Input Stream
Hashtable hashPars = ParseParamsFromStream(request.InputStream);
ssid = (string)hashPars["ssid"];
password = (string)hashPars["password"];
Debug.WriteLine($"Wireless parameters SSID:{ssid} PASSWORD:{password}");
string message = "<p>New settings saved.</p><p>Rebooting device to put into normal mode</p>";
bool res = Wireless80211.Configure(ssid, password);
if (res)
{
message += $"<p>And your new IP address should be {Wireless80211.GetCurrentIPAddress()}.</p>";
}
responseString = CreateMainPage(message);
OutPutResponse(response, responseString);
isApSet = true;
break;
}
response.Close();
if (isApSet && (!string.IsNullOrEmpty(ssid)) && (!string.IsNullOrEmpty(password)))
{
// Enable the Wireless station interface
Wireless80211.Configure(ssid, password);
// Disable the Soft AP
WirelessAP.Disable();
Thread.Sleep(200);
Power.RebootDevice();
}
}
static string ReplaceMessage(string page, string message)
{
int index = page.IndexOf("{message}");
if (index >= 0)
{
return page.Substring(0, index) + message + page.Substring(index + 9);
}
return page;
}
static void OutPutResponse(HttpListenerResponse response, string responseString)
{
var responseBytes = System.Text.Encoding.UTF8.GetBytes(responseString);
OutPutByteResponse(response, System.Text.Encoding.UTF8.GetBytes(responseString));
}
static void OutPutByteResponse(HttpListenerResponse response, Byte[] responseBytes)
{
response.ContentLength64 = responseBytes.Length;
response.OutputStream.Write(responseBytes, 0, responseBytes.Length);
}
static Hashtable ParseParamsFromStream(Stream inputStream)
{
byte[] buffer = new byte[inputStream.Length];
inputStream.Read(buffer, 0, (int)inputStream.Length);
return ParseParams(System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length));
}
static Hashtable ParseParams(string rawParams)
{
Hashtable hash = new Hashtable();
string[] parPairs = rawParams.Split('&');
foreach (string pair in parPairs)
{
string[] nameValue = pair.Split('=');
hash.Add(nameValue[0], nameValue[1]);
}
return hash;
}
static string CreateMainPage(string message)
{
return $"<!DOCTYPE html><html>{GetCss()}<body>" +
"<h1>NanoFramework</h1>" +
"<form method='POST'>" +
"<fieldset><legend>Wireless configuration</legend>" +
"Ssid:</br><input type='input' name='ssid' value='' ></br>" +
"Password:</br><input type='password' name='password' value='' >" +
"<br><br>" +
"<input type='submit' value='Save'>" +
"</fieldset>" +
"<b>" + message + "</b>" +
"</form></body></html>";
}
static string GetCss()
{
return "<head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><style>" +
"*{box-sizing: border-box}" +
"h1,legend {text-align:center;}" +
"form {max-width: 250px;margin: 10px auto 0 auto;}" +
"fieldset {border-radius: 5px;box-shadow: 3px 3px 15px hsl(0, 0%, 90%);font-size: large;}" +
"input {width: 100%;padding: 4px;margin-bottom: 8px;border: 1px solid hsl(0, 0%, 50%);border-radius: 3px;font-size: medium;}" +
"input[type=submit]:hover {cursor: pointer;background-color: hsl(0, 0%, 90%);transition: 0.5s;}" +
" @media only screen and (max-width: 768px) { form {max-width: 100%;}} " +
"</style><title>NanoFramework</title></head>";
}
}
}