- Changes from manual reading of header content to Linq-approach (pro: no more chance of deadlocks in StreamReadLine)

- Flush output stream and close it
This commit is contained in:
Jürgen 2016-07-23 19:53:02 +02:00
parent 08137421ea
commit 961f25a487

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
@ -35,28 +37,13 @@ namespace SpotifyAPI.Web
_srv = srv; _srv = srv;
} }
private string StreamReadLine(Stream inputStream) private string[] GetIncomingRequest(Stream inputStream)
{ {
string data = ""; var buffer = new byte[4096];
while (_isActive) var read = inputStream.Read(buffer, 0, buffer.Length);
{
var nextChar = inputStream.ReadByte(); var inputData = Encoding.ASCII.GetString(buffer.Take(read).ToArray());
if (nextChar == '\n') return inputData.Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s)).ToArray();
{
break;
}
if (nextChar == '\r')
{
continue;
}
if (nextChar == -1)
{
Thread.Sleep(1);
continue;
}
data += Convert.ToChar(nextChar);
}
return data;
} }
public void Process(TcpClient socket) public void Process(TcpClient socket)
@ -69,8 +56,11 @@ namespace SpotifyAPI.Web
OutputStream = new StreamWriter(new BufferedStream(socket.GetStream())); OutputStream = new StreamWriter(new BufferedStream(socket.GetStream()));
try try
{ {
ParseRequest(); var requestLines = GetIncomingRequest(_inputStream);
ReadHeaders();
ParseRequest(requestLines.First());
ReadHeaders(requestLines.Skip(1));
if (HttpMethod.Equals("GET")) if (HttpMethod.Equals("GET"))
{ {
HandleGetRequest(); HandleGetRequest();
@ -89,9 +79,8 @@ namespace SpotifyAPI.Web
OutputStream = null; OutputStream = null;
} }
public void ParseRequest() public void ParseRequest(string request)
{ {
string request = StreamReadLine(_inputStream);
string[] tokens = request.Split(' '); string[] tokens = request.Split(' ');
if (tokens.Length < 2) if (tokens.Length < 2)
{ {
@ -101,10 +90,9 @@ namespace SpotifyAPI.Web
HttpUrl = tokens[1]; HttpUrl = tokens[1];
} }
public void ReadHeaders() public void ReadHeaders(IEnumerable<string> requestLines)
{ {
string line; foreach(var line in requestLines)
while ((line = StreamReadLine(_inputStream)) != null)
{ {
if (string.IsNullOrEmpty(line)) if (string.IsNullOrEmpty(line))
{ {
@ -328,6 +316,8 @@ namespace SpotifyAPI.Web
"window.location = hashes" + "window.location = hashes" +
"</script>" + "</script>" +
"<h1>Spotify Auth successful!<br>Please copy the URL and paste it into the application</h1></body></html>"); "<h1>Spotify Auth successful!<br>Please copy the URL and paste it into the application</h1></body></html>");
p.OutputStream.Flush();
p.OutputStream.Close();
return; return;
} }
string url = p.HttpUrl; string url = p.HttpUrl;
@ -358,8 +348,12 @@ namespace SpotifyAPI.Web
State = col.Get(3) State = col.Get(3)
}); });
}); });
p.OutputStream.Flush();
p.OutputStream.Close();
} }
} }
t.Start(); t.Start();
} }