-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
368 lines (294 loc) · 11.8 KB
/
MainForm.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace SimpleWebServer
{
/// <summary>
/// Simple WebServer is a basic static file web server program written by GitHub user Obeliskos.
/// Uses web server class gist written by github user aksakalli.
/// This program uses core .net functionality to ensure it runs well on the Windows 8.1 RT (Surface RT) .NET framework.
/// </summary>
public partial class MainForm : Form
{
private SimpleHTTPServer simpleServer;
public string version = "0.6";
public Settings settings = new Settings();
string settingsPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\ServerSettings.xml";
public static MainForm formInstance = null;
public bool isAdmin = false;
public MainForm()
{
InitializeComponent();
}
public static bool IsAdministrator()
{
return (new WindowsPrincipal(WindowsIdentity.GetCurrent()))
.IsInRole(WindowsBuiltInRole.Administrator);
}
#region Form level methods
private void MainForm_Load(object sender, EventArgs e)
{
formInstance = this;
isAdmin = IsAdministrator();
// Detect if multiple instances are running and show System tray notification if so.
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("SimpleWebServer");
if (processes.Length > 1)
{
showNotification("Multiple servers are running", ToolTipIcon.Info, 3000);
}
// Load SystemSettings.xml if it exists
if (System.IO.File.Exists(settingsPath))
{
try
{
Settings newSettings;
XmlSerializer s = new XmlSerializer(typeof(Settings));
TextReader r = new StreamReader(settingsPath);
newSettings = (Settings)s.Deserialize(r);
r.Close();
settings = newSettings;
this.txtRootDirectory.Text = settings.RootDirectory;
this.numPort.Value = settings.ListenPort;
this.chkStartMinimized.Checked = settings.MinimizeOnStartup;
this.chkStartupServer.Checked = settings.StartServerOnStartup;
toolStripMainStatus.Text = "Settings loaded.";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
toolStripMainStatus.Text = "No settings file yet... using defaults.";
}
// populate mime type listbox
this.listBoxMimeExtensions.DataSource = settings._mimeTypeMappings.OrderBy(kp => kp.Key)
.Select(kp => kp.Key)
.ToList();
// Default root directory to exe location
if (txtRootDirectory.Text == "")
{
txtRootDirectory.Text = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
}
this.Text = "Simple Web Server v" + version;
this.notifyIcon.Text = "Simple Web Server v" + version;
}
private void MainForm_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
{
this.Hide();
showNotification("Minimized to System tray.", ToolTipIcon.Info, 3000);
}
}
private void MainForm_Shown(object sender, EventArgs e)
{
if (settings.MinimizeOnStartup)
{
this.WindowState = FormWindowState.Minimized;
}
if (settings.StartServerOnStartup)
{
startServer();
}
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
simpleServer.Stop();
}
catch(Exception ex)
{
}
}
#endregion
#region Toolbar Handlers
private void saveDefaults()
{
settings.RootDirectory = txtRootDirectory.Text;
settings.ListenPort = (int)numPort.Value;
settings.MinimizeOnStartup = this.chkStartMinimized.Checked;
settings.StartServerOnStartup = this.chkStartupServer.Checked;
XmlSerializer s = new XmlSerializer(typeof(Settings));
TextWriter w = new StreamWriter(settingsPath);
s.Serialize(w, settings);
w.Close();
}
private void startServer()
{
simpleServer = new SimpleHTTPServer(txtRootDirectory.Text, (int)numPort.Value, !isAdmin, settings.UseCustomPrefixes, settings.CustomPrefixes);
showNotification("WebServer listening " + (isAdmin?"":"locally") + " on port " + numPort.Value.ToString(), ToolTipIcon.Info, 3000);
toolStripServerStatus.Text = "Server Status : Listening " + (isAdmin ? "" : "locally") + " on port " + numPort.Value.ToString();
}
private void toolStripButtonStart_Click(object sender, EventArgs e)
{
startServer();
}
private void toolStripButtonStop_Click(object sender, EventArgs e)
{
if (simpleServer != null)
{
simpleServer.Stop();
showNotification("WebServer stopped.", ToolTipIcon.Info, 3000);
toolStripServerStatus.Text = "Server Status : Stopped.";
}
}
private void toolStripButtonSaveSettings_Click(object sender, EventArgs e)
{
saveDefaults();
toolStripMainStatus.Text = "Settings saved.";
}
private void toolStripButtonAbout_Click(object sender, EventArgs e)
{
AboutForm af = new AboutForm();
af.Show();
}
#endregion
#region Main Settings Handlers
private void btnPickRoot_Click(object sender, EventArgs e)
{
DialogResult dr = folderBrowserDialog1.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
txtRootDirectory.Text = folderBrowserDialog1.SelectedPath;
}
}
private void btnLocationExe_Click(object sender, EventArgs e)
{
txtRootDirectory.Text = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
}
private void btnLocationWorking_Click(object sender, EventArgs e)
{
txtRootDirectory.Text = Directory.GetCurrentDirectory();
}
#endregion
#region System Tray Icon
private void showNotification(string notifyText, ToolTipIcon icon, int timeout)
{
notifyIcon.ShowBalloonTip(timeout, "Simple Webserver", notifyText, icon);
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
#endregion
#region Mimetype management
private void btnUpdateMimeMapping_Click(object sender, EventArgs e)
{
textBoxMimeExtension.Text = textBoxMimeExtension.Text.ToLower();
textBoxMimeMapping.Text = textBoxMimeMapping.Text.ToLower();
if (textBoxMimeExtension.Text == ".newmimetype")
{
MessageBox.Show("You need to set the mime extension", "Fix errors before saving mime type", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (textBoxMimeExtension.Text == "")
{
MessageBox.Show("You need to enter a mime extension", "Fix errors before saving mime type", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (textBoxMimeMapping.Text == "")
{
MessageBox.Show("You need to enter a mime mapping", "Fix errors before saving mime type", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
string ext = textBoxMimeExtension.Text;
string selMime = (string) listBoxMimeExtensions.SelectedItem;
string mval = "";
bool result = settings._mimeTypeMappings.TryGetValue(selMime, out mval);
if (result)
{
settings._mimeTypeMappings[ext] = textBoxMimeMapping.Text;
if (selMime == ".newmimetype")
{
settings._mimeTypeMappings.Remove(selMime);
}
refreshMimeList();
}
}
private void selectMimeType()
{
string newitem = listBoxMimeExtensions.SelectedItem.ToString();
string mapping = "";
bool result = settings._mimeTypeMappings.TryGetValue(newitem, out mapping);
if (result)
{
textBoxMimeExtension.Text = newitem;
textBoxMimeMapping.Text = mapping;
}
if (newitem == ".newmimetype")
{
textBoxMimeExtension.Enabled = true;
}
else
{
textBoxMimeExtension.Enabled = false;
}
groupBoxEditMimeType.Visible = true;
}
private void listBoxMimeExtensions_Click(object sender, EventArgs e)
{
selectMimeType();
}
private void btnCancelMimeEdit_Click(object sender, EventArgs e)
{
string selectedExt = listBoxMimeExtensions.SelectedValue.ToString();
if (selectedExt == ".newmimetype")
{
settings._mimeTypeMappings.Remove(".newmimetype");
refreshMimeList();
}
else
{
groupBoxEditMimeType.Visible = false;
}
}
private void refreshMimeList()
{
// refresh listbox
this.listBoxMimeExtensions.DataSource = settings._mimeTypeMappings.OrderBy(kp => kp.Key)
.Select(kp => kp.Key)
.ToList();
// unselect listbox
this.listBoxMimeExtensions.SelectedItem = null;
// re-hide groupbox
this.groupBoxEditMimeType.Visible = false;
}
private void btnRemoveMimeType_Click(object sender, EventArgs e)
{
string ext = listBoxMimeExtensions.SelectedValue.ToString();
// remove from dictionary
settings._mimeTypeMappings.Remove(ext);
refreshMimeList();
}
private void btnAddMimeType_Click(object sender, EventArgs e)
{
// add mime type and lookup up the dictionary item
settings._mimeTypeMappings.Add(".newmimetype", "application/octet-stream");
object newDictItem = settings._mimeTypeMappings.FirstOrDefault(di => di.Key == ".newmimetype");
refreshMimeList();
// rough mechanism to highlight recently added mime type and ensure visible
listBoxMimeExtensions.Focus();
listBoxMimeExtensions.SelectedItem = ".newmimetype";
// now display group box editor for selected (new) mime type
selectMimeType();
}
#endregion
}
}