Skip to content

Commit

Permalink
Bug fixes, add local network option, display all available urls
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanaobrien committed Aug 14, 2022
1 parent 441d6f0 commit cde1684
Show file tree
Hide file tree
Showing 5 changed files with 4,388 additions and 61 deletions.
73 changes: 43 additions & 30 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 64 additions & 20 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ namespace WebServer
{
public partial class WebServer : Form
{
private double version = 2.6;
private double version = 2.7;
Server mainServer;
private string pathToServe;
private int port = 8080;
private bool running = false;
private string localHostURL = "http://127.0.0.1:8080/";
private bool actionInProgress = false;
public WebServer()
{
Expand All @@ -27,35 +26,73 @@ public WebServer()
this.UpdateVersion.Visible = true;
this.UpdateVersion.Text = "Version " + version + " is out!";
});
this.running = true;
actionInProgress = true;
toggleServer();
actionInProgress = false;
}
private void toggleServer()
{
if (this.running)
if (!this.running)
{
this.MSG.Visible = false;
this.Status.Text = "Running";
this.running = true;
startServer();
SetURLText();
}
else if (mainServer != null)
{
mainServer.Terminate();
mainServer = null;
this.URL.Visible = false;
SetURLText();
this.running = false;
this.Status.Text = "Not Running";
this.MSG.Text = "Not Running";
this.MSG.Visible = true;
}
}
private void startServer()
{
this.MSG.Visible = false;
this.URL.Visible = true;
mainServer = new Server(pathToServe, port, PUT.Checked, DELETE.Checked, CORS.Checked, AutoIndex.Checked, ListDirectory.Checked);
this.localHostURL = "http://127.0.0.1:" + port + "/";
this.URL.Text = "Open " + this.localHostURL + " in your browser";
mainServer = new Server(pathToServe, port, PUT.Checked, DELETE.Checked, CORS.Checked, AutoIndex.Checked, ListDirectory.Checked, localNetwork.Checked);
}
private Label[] URLS = new Label[5] { null, null, null, null, null};
private void SetURLText()
{
for (int i = 0; i < URLS.Length; i++)
{
if (URLS[i] == null) break;
this.Controls.Remove(URLS[i]);
URLS[i] = null;
this.URL.Visible = false;
}
if (this.mainServer != null)
{
string[] urls = this.mainServer.GetURLs();
this.URL.Visible = true;
int location = 368;
for (int i = 0; i < urls.Length; i++)
{
if (urls[i] == null || urls[i].Length == 0) break;
if (5 < i) break;
URLS[i] = new System.Windows.Forms.Label();
URLS[i].AutoSize = true;
URLS[i].Cursor = System.Windows.Forms.Cursors.Hand;
URLS[i].Font = new System.Drawing.Font("Microsoft Sans Serif", 10F);
URLS[i].ForeColor = System.Drawing.SystemColors.Highlight;
URLS[i].Location = new System.Drawing.Point(80, location);
URLS[i].Name = "URL"+i;
URLS[i].Size = new System.Drawing.Size(276, 17);
URLS[i].TabIndex = 14;
URLS[i].Text = "http://"+urls[i]+":"+this.port+"/";
URLS[i].Click += new System.EventHandler(this.URL_Click);
this.Controls.Add(URLS[i]);
location += 30;
}
}
}
private void URL_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(((Label)sender).Text);
}
private void saveSettings()
{
Expand All @@ -72,7 +109,7 @@ private void saveSettings()
File.Delete(path);
}
FileStream fs = File.Create(path);
byte[] data = Encoding.UTF8.GetBytes(port + "\n" + pathToServe + "\n" + (PUT.Checked ? "1" : "0") + "\n" + (DELETE.Checked ? "1" : "0") + "\n" + (CORS.Checked ? "1" : "0") + "\n" + (AutoIndex.Checked ? "1" : "0") + "\n" + (ListDirectory.Checked ? "1" : "0") + "\n");
byte[] data = Encoding.UTF8.GetBytes(port + "\n" + pathToServe + "\n" + (PUT.Checked ? "1" : "0") + "\n" + (DELETE.Checked ? "1" : "0") + "\n" + (CORS.Checked ? "1" : "0") + "\n" + (AutoIndex.Checked ? "1" : "0") + "\n" + (ListDirectory.Checked ? "1" : "0") + "\n" + (localNetwork.Checked ? "1" : "0") + "\n");
fs.Write(data, 0, data.Length);
fs.Close();
}
Expand Down Expand Up @@ -125,20 +162,22 @@ private void loadSettings()
{
ListDirectory.Checked = (cl.Equals("1"));
}
else if (i == 7)
{
localNetwork.Checked = (cl.Equals("1"));
}
i++;
}
this.ServingPath.Text = "Currently Serving: " + pathToServe;
this.Port.Value = new decimal(new int[] { port, 0, 0, 0 });
this.localHostURL = "http://127.0.0.1:" + port + "/";
this.URL.Text = "Open " + this.localHostURL + " in your browser";
this.SetURLText();
}
catch (Exception e) { }
}
private void button1_Click(object sender, EventArgs e)
{
if (actionInProgress) return;
actionInProgress = true;
running = !running;
toggleServer();
actionInProgress = false;
}
Expand Down Expand Up @@ -175,9 +214,8 @@ private void ChooseDirectory_Click(object sender, EventArgs e)

private void Port_ValueChanged(object sender, EventArgs e)
{
if (this.running)
if (mainServer != null)
{
this.URL.Visible = false;
this.MSG.Text = "Restart server to init changes";
this.MSG.Visible = true;
}
Expand Down Expand Up @@ -233,14 +271,20 @@ private void viewOnGithub_Click(object sender, EventArgs e)
System.Diagnostics.Process.Start("https://github.com/ethanaobrien/C-server");
}

private void URL_Click(object sender, EventArgs e)
private void UpdateLink_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(this.localHostURL);
System.Diagnostics.Process.Start("https://github.com/ethanaobrien/C-server/releases/latest");
}

private void UpdateLink_Click(object sender, EventArgs e)
private void localNetwork_CheckStateChanged(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/ethanaobrien/C-server/releases/latest");
if (mainServer != null)
{
this.MSG.Text = "Restart server to init changes";
this.MSG.Visible = true;
mainServer.setLocalNetwork(localNetwork.Checked);
}
saveSettings();
}
}
}
Expand Down
Loading

0 comments on commit cde1684

Please sign in to comment.