changeset 100:2b5e7bb9b979

HTTP: Small fixes in server SSL support
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 03 Sep 2014 21:44:26 +0200
parents c9bc014bdbe8
children 04c56f31db37 5e717aac4c1d
files Net/HTTP.cs
diffstat 1 files changed, 25 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Net/HTTP.cs	Tue Jul 08 16:52:10 2014 +0200
+++ b/Net/HTTP.cs	Wed Sep 03 21:44:26 2014 +0200
@@ -6,7 +6,6 @@
 using System.Net.Sockets;
 using System.Security.Cryptography.X509Certificates;
 using System.Text;
-using UCIS.Net;
 using UCIS.Util;
 using HTTPHeader = System.Collections.Generic.KeyValuePair<string, string>;
 
@@ -32,18 +31,16 @@
 		}
 
 		private void AcceptCallback(IAsyncResult ar) {
+			Socket socket = null;
 			try {
-				Socket socket = Listener.EndAccept(ar);
-				if (SSLCertificate != null) {
-					SslStream ssl = new SslStream(new NetworkStream(socket, true));
-					ssl.BeginAuthenticateAsServer(SSLCertificate, SslAuthenticationCallback, new Object[] { socket, ssl });
-				} else {
-					new HTTPContext(this, socket);
-				}
-			} catch (Exception) { }
+				socket = Listener.EndAccept(ar);
+				HandleClient(socket);
+			} catch {
+				if (socket != null) socket.Close();
+			}
 			try {
 				Listener.BeginAccept(AcceptCallback, null);
-			} catch (Exception) { }
+			} catch { }
 		}
 
 		private void SslAuthenticationCallback(IAsyncResult ar) {
@@ -53,19 +50,31 @@
 			try {
 				ssl.EndAuthenticateAsServer(ar);
 				new HTTPContext(this, ssl, socket);
-			} catch (Exception) { }
+			} catch {
+				socket.Close();
+			}
 		}
 
 		public void Dispose() {
 			if (Listener != null) Listener.Close();
 		}
 
+		public void HandleClient(Socket socket, Stream streamwrapper) {
+			if (streamwrapper == null) streamwrapper = new NetworkStream(socket, true);
+			if (SSLCertificate != null) {
+				SslStream ssl = new SslStream(streamwrapper);
+				ssl.BeginAuthenticateAsServer(SSLCertificate, SslAuthenticationCallback, new Object[] { socket, ssl });
+			} else {
+				new HTTPContext(this, streamwrapper, socket);
+			}
+		}
+
 		public void HandleClient(Socket client) {
-			new HTTPContext(this, client);
+			HandleClient(client, null);
 		}
 
 		bool TCPServer.IModule.Accept(TCPStream stream) {
-			new HTTPContext(this, stream);
+			HandleClient(stream.Socket, stream);
 			return false;
 		}
 	}
@@ -94,7 +103,7 @@
 
 		private StreamWriter Writer;
 		private PrebufferingStream Reader;
-		private List<HTTPHeader> RequestHeaders;
+		private List<HTTPHeader> RequestHeaders = null;
 		private HTTPConnectionState State = HTTPConnectionState.Starting;
 		private KeyValuePair<String, String>[] QueryParameters = null, PostParameters = null, Cookies = null;
 		private HTTPOutputStream ResponseStream = null;
@@ -501,6 +510,7 @@
 
 		public String GetRequestHeader(String name) {
 			if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException();
+			if (RequestHeaders == null) return null;
 			foreach (HTTPHeader h in RequestHeaders) {
 				if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) return h.Value;
 			}
@@ -509,6 +519,7 @@
 		public String[] GetRequestHeaders(String name) {
 			if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException();
 			String[] items = new String[0];
+			if (RequestHeaders == null) return items;
 			foreach (HTTPHeader h in RequestHeaders) {
 				if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) ArrayUtil.Add(ref items, h.Value);
 			}