Skip to content

Commit

Permalink
fix high cpu bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanaobrien committed Aug 15, 2022
1 parent cde1684 commit 1aa5c6d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace WebServer
{
public partial class WebServer : Form
{
private double version = 2.7;
private double version = 2.8;
Server mainServer;
private string pathToServe;
private int port = 8080;
Expand Down Expand Up @@ -55,7 +55,7 @@ private void startServer()
{
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 Label[] URLS = new Label[5] { null, null, null, null, null };
private void SetURLText()
{
for (int i = 0; i < URLS.Length; i++)
Expand All @@ -80,10 +80,10 @@ private void SetURLText()
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].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].Text = "http://" + urls[i] + ":" + this.port + "/";
URLS[i].Click += new System.EventHandler(this.URL_Click);
this.Controls.Add(URLS[i]);
location += 30;
Expand Down
12 changes: 8 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public string[] GetURLs()
rv[0] = "127.0.0.1";
for (int i = 0, j = 1; i < addr.Length; i++)
{
if (addr[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
if (addr[i].AddressFamily == AddressFamily.InterNetwork)
{
rv[j] = addr[i].ToString();
j++;
Expand All @@ -228,9 +228,12 @@ private void onRequest(Object obj)
{
byte[] bytes = new byte[1];
int bytesRec = handler.Receive(bytes);
if (bytesRec == 0) break;
data += Encoding.UTF8.GetString(bytes, 0, bytesRec);
consumed = (data.IndexOf("\r\n\r\n") != -1);
}
if (!consumed)
break;
//Console.WriteLine("Text received : {0}", data);
string url = Uri.UnescapeDataString(data.Split(' ')[1].Split('?')[0]);
string method = data.Split(' ')[0];
Expand Down Expand Up @@ -279,10 +282,10 @@ private void onRequest(Object obj)
}
catch (Exception e)
{
handler.Shutdown(SocketShutdown.Both);
handler.Close();
Console.WriteLine("Error: {0}", e.ToString());
}
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
private void writeHeader(Socket handler, int httpCode, string code, long cl, string ct, string extra)
{
Expand Down Expand Up @@ -502,7 +505,8 @@ private void put(Socket handler, string path, string data)
}
written += (long)a;
byte[] bytes = new byte[a];
handler.Receive(bytes);
int qwe = handler.Receive(bytes);
if (qwe == 0) break;
fs.Write(bytes, 0, a);
}
fs.Close();
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7
2.8

0 comments on commit 1aa5c6d

Please sign in to comment.