Mercurial > hg > ucis.core
comparison Net/HTTP.cs @ 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 | ebdff34b9e4f |
children | 04c56f31db37 |
comparison
equal
deleted
inserted
replaced
98:c9bc014bdbe8 | 100:2b5e7bb9b979 |
---|---|
4 using System.Net; | 4 using System.Net; |
5 using System.Net.Security; | 5 using System.Net.Security; |
6 using System.Net.Sockets; | 6 using System.Net.Sockets; |
7 using System.Security.Cryptography.X509Certificates; | 7 using System.Security.Cryptography.X509Certificates; |
8 using System.Text; | 8 using System.Text; |
9 using UCIS.Net; | |
10 using UCIS.Util; | 9 using UCIS.Util; |
11 using HTTPHeader = System.Collections.Generic.KeyValuePair<string, string>; | 10 using HTTPHeader = System.Collections.Generic.KeyValuePair<string, string>; |
12 | 11 |
13 namespace UCIS.Net.HTTP { | 12 namespace UCIS.Net.HTTP { |
14 public class HTTPServer : TCPServer.IModule, IDisposable { | 13 public class HTTPServer : TCPServer.IModule, IDisposable { |
30 Listener.Listen(5); | 29 Listener.Listen(5); |
31 Listener.BeginAccept(AcceptCallback, null); | 30 Listener.BeginAccept(AcceptCallback, null); |
32 } | 31 } |
33 | 32 |
34 private void AcceptCallback(IAsyncResult ar) { | 33 private void AcceptCallback(IAsyncResult ar) { |
34 Socket socket = null; | |
35 try { | 35 try { |
36 Socket socket = Listener.EndAccept(ar); | 36 socket = Listener.EndAccept(ar); |
37 if (SSLCertificate != null) { | 37 HandleClient(socket); |
38 SslStream ssl = new SslStream(new NetworkStream(socket, true)); | 38 } catch { |
39 ssl.BeginAuthenticateAsServer(SSLCertificate, SslAuthenticationCallback, new Object[] { socket, ssl }); | 39 if (socket != null) socket.Close(); |
40 } else { | 40 } |
41 new HTTPContext(this, socket); | |
42 } | |
43 } catch (Exception) { } | |
44 try { | 41 try { |
45 Listener.BeginAccept(AcceptCallback, null); | 42 Listener.BeginAccept(AcceptCallback, null); |
46 } catch (Exception) { } | 43 } catch { } |
47 } | 44 } |
48 | 45 |
49 private void SslAuthenticationCallback(IAsyncResult ar) { | 46 private void SslAuthenticationCallback(IAsyncResult ar) { |
50 Object[] args = (Object[])ar.AsyncState; | 47 Object[] args = (Object[])ar.AsyncState; |
51 Socket socket = (Socket)args[0]; | 48 Socket socket = (Socket)args[0]; |
52 SslStream ssl = (SslStream)args[1]; | 49 SslStream ssl = (SslStream)args[1]; |
53 try { | 50 try { |
54 ssl.EndAuthenticateAsServer(ar); | 51 ssl.EndAuthenticateAsServer(ar); |
55 new HTTPContext(this, ssl, socket); | 52 new HTTPContext(this, ssl, socket); |
56 } catch (Exception) { } | 53 } catch { |
54 socket.Close(); | |
55 } | |
57 } | 56 } |
58 | 57 |
59 public void Dispose() { | 58 public void Dispose() { |
60 if (Listener != null) Listener.Close(); | 59 if (Listener != null) Listener.Close(); |
61 } | 60 } |
62 | 61 |
62 public void HandleClient(Socket socket, Stream streamwrapper) { | |
63 if (streamwrapper == null) streamwrapper = new NetworkStream(socket, true); | |
64 if (SSLCertificate != null) { | |
65 SslStream ssl = new SslStream(streamwrapper); | |
66 ssl.BeginAuthenticateAsServer(SSLCertificate, SslAuthenticationCallback, new Object[] { socket, ssl }); | |
67 } else { | |
68 new HTTPContext(this, streamwrapper, socket); | |
69 } | |
70 } | |
71 | |
63 public void HandleClient(Socket client) { | 72 public void HandleClient(Socket client) { |
64 new HTTPContext(this, client); | 73 HandleClient(client, null); |
65 } | 74 } |
66 | 75 |
67 bool TCPServer.IModule.Accept(TCPStream stream) { | 76 bool TCPServer.IModule.Accept(TCPStream stream) { |
68 new HTTPContext(this, stream); | 77 HandleClient(stream.Socket, stream); |
69 return false; | 78 return false; |
70 } | 79 } |
71 } | 80 } |
72 | 81 |
73 public enum HTTPResponseStreamMode { | 82 public enum HTTPResponseStreamMode { |
92 public Boolean SuppressStandardHeaders { get; set; } | 101 public Boolean SuppressStandardHeaders { get; set; } |
93 public TCPStream TCPStream { get; private set; } | 102 public TCPStream TCPStream { get; private set; } |
94 | 103 |
95 private StreamWriter Writer; | 104 private StreamWriter Writer; |
96 private PrebufferingStream Reader; | 105 private PrebufferingStream Reader; |
97 private List<HTTPHeader> RequestHeaders; | 106 private List<HTTPHeader> RequestHeaders = null; |
98 private HTTPConnectionState State = HTTPConnectionState.Starting; | 107 private HTTPConnectionState State = HTTPConnectionState.Starting; |
99 private KeyValuePair<String, String>[] QueryParameters = null, PostParameters = null, Cookies = null; | 108 private KeyValuePair<String, String>[] QueryParameters = null, PostParameters = null, Cookies = null; |
100 private HTTPOutputStream ResponseStream = null; | 109 private HTTPOutputStream ResponseStream = null; |
101 private HTTPInputStream RequestStream = null; | 110 private HTTPInputStream RequestStream = null; |
102 | 111 |
499 return; | 508 return; |
500 } | 509 } |
501 | 510 |
502 public String GetRequestHeader(String name) { | 511 public String GetRequestHeader(String name) { |
503 if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException(); | 512 if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException(); |
513 if (RequestHeaders == null) return null; | |
504 foreach (HTTPHeader h in RequestHeaders) { | 514 foreach (HTTPHeader h in RequestHeaders) { |
505 if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) return h.Value; | 515 if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) return h.Value; |
506 } | 516 } |
507 return null; | 517 return null; |
508 } | 518 } |
509 public String[] GetRequestHeaders(String name) { | 519 public String[] GetRequestHeaders(String name) { |
510 if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException(); | 520 if (State != HTTPConnectionState.ProcessingRequest && State != HTTPConnectionState.SendingHeaders && State != HTTPConnectionState.SendingContent) throw new InvalidOperationException(); |
511 String[] items = new String[0]; | 521 String[] items = new String[0]; |
522 if (RequestHeaders == null) return items; | |
512 foreach (HTTPHeader h in RequestHeaders) { | 523 foreach (HTTPHeader h in RequestHeaders) { |
513 if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) ArrayUtil.Add(ref items, h.Value); | 524 if (name.Equals(h.Key, StringComparison.OrdinalIgnoreCase)) ArrayUtil.Add(ref items, h.Value); |
514 } | 525 } |
515 return items; | 526 return items; |
516 } | 527 } |