Skip to content

Commit 961f25a

Browse files
committed
- Changes from manual reading of header content to Linq-approach (pro: no more chance of deadlocks in StreamReadLine)
- Flush output stream and close it
1 parent 0813742 commit 961f25a

1 file changed

Lines changed: 22 additions & 28 deletions

File tree

‎SpotifyAPI/Web/SimpleHttpServer.cs‎

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections;
3+
using System.Collections.Generic;
34
using System.Collections.Specialized;
45
using System.IO;
6+
using System.Linq;
57
using System.Net;
68
using System.Net.Sockets;
79
using System.Text;
@@ -35,28 +37,13 @@ public HttpProcessor(HttpServer srv)
3537
_srv = srv;
3638
}
3739

38-
private string StreamReadLine(Stream inputStream)
40+
private string[] GetIncomingRequest(Stream inputStream)
3941
{
40-
string data = "";
41-
while (_isActive)
42-
{
43-
var nextChar = inputStream.ReadByte();
44-
if (nextChar == '\n')
45-
{
46-
break;
47-
}
48-
if (nextChar == '\r')
49-
{
50-
continue;
51-
}
52-
if (nextChar == -1)
53-
{
54-
Thread.Sleep(1);
55-
continue;
56-
}
57-
data += Convert.ToChar(nextChar);
58-
}
59-
return data;
42+
var buffer = new byte[4096];
43+
var read = inputStream.Read(buffer, 0, buffer.Length);
44+
45+
var inputData = Encoding.ASCII.GetString(buffer.Take(read).ToArray());
46+
return inputData.Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToArray();
6047
}
6148

6249
public void Process(TcpClient socket)
@@ -69,8 +56,11 @@ public void Process(TcpClient socket)
6956
OutputStream = new StreamWriter(new BufferedStream(socket.GetStream()));
7057
try
7158
{
72-
ParseRequest();
73-
ReadHeaders();
59+
var requestLines = GetIncomingRequest(_inputStream);
60+
61+
ParseRequest(requestLines.First());
62+
ReadHeaders(requestLines.Skip(1));
63+
7464
if (HttpMethod.Equals("GET"))
7565
{
7666
HandleGetRequest();
@@ -89,9 +79,8 @@ public void Process(TcpClient socket)
8979
OutputStream = null;
9080
}
9181

92-
public void ParseRequest()
82+
public void ParseRequest(string request)
9383
{
94-
string request = StreamReadLine(_inputStream);
9584
string[] tokens = request.Split(' ');
9685
if (tokens.Length < 2)
9786
{
@@ -101,10 +90,9 @@ public void ParseRequest()
10190
HttpUrl = tokens[1];
10291
}
10392

104-
public void ReadHeaders()
93+
public void ReadHeaders(IEnumerable<string> requestLines)
10594
{
106-
string line;
107-
while ((line = StreamReadLine(_inputStream)) != null)
95+
foreach(var line in requestLines)
10896
{
10997
if (string.IsNullOrEmpty(line))
11098
{
@@ -328,6 +316,8 @@ public override void HandleGetRequest(HttpProcessor p)
328316
"window.location = hashes" +
329317
"</script>" +
330318
"<h1>Spotify Auth successful!<br>Please copy the URL and paste it into the application</h1></body></html>");
319+
p.OutputStream.Flush();
320+
p.OutputStream.Close();
331321
return;
332322
}
333323
string url = p.HttpUrl;
@@ -358,8 +348,12 @@ public override void HandleGetRequest(HttpProcessor p)
358348
State = col.Get(3)
359349
});
360350
});
351+
p.OutputStream.Flush();
352+
p.OutputStream.Close();
361353
}
362354
}
355+
356+
363357
t.Start();
364358
}
365359

0 commit comments

Comments
 (0)