|
7 | 7 | // File name: HttpHost.cs |
8 | 8 | // Repository: https://github.com/sisk-http/core |
9 | 9 |
|
| 10 | +using System.ComponentModel; |
10 | 11 | using System.Net; |
11 | 12 | using System.Net.Security; |
12 | 13 | using System.Net.Sockets; |
13 | 14 | using System.Runtime.CompilerServices; |
| 15 | +using System.Text; |
14 | 16 |
|
15 | 17 | namespace Sisk.Cadente; |
16 | 18 |
|
@@ -125,6 +127,32 @@ private async void ReceiveClient ( IAsyncResult result ) { |
125 | 127 | await HandleTcpClient ( client ); |
126 | 128 | } |
127 | 129 |
|
| 130 | + private byte[] GetBadRequestMessage ( string message ) { |
| 131 | + string content = $""" |
| 132 | + <HTML> |
| 133 | + <HEAD> |
| 134 | + <TITLE>400 - Bad Request</TITLE> |
| 135 | + </HEAD> |
| 136 | + <BODY> |
| 137 | + <H1>400 - Bad Request</H1> |
| 138 | + <P>{message}</P> |
| 139 | + <HR> |
| 140 | + <P><EM>Cadente</EM></P> |
| 141 | + </BODY> |
| 142 | + </HTML> |
| 143 | + """; |
| 144 | + |
| 145 | + string html = |
| 146 | + $"HTTP/1.1 400 Bad Request\r\n" + |
| 147 | + $"Content-Type: text/html\r\n" + |
| 148 | + $"Content-Length: {content.Length}\r\n" + |
| 149 | + $"Connection: close\r\n" + |
| 150 | + $"\r\n" + |
| 151 | + content; |
| 152 | + |
| 153 | + return Encoding.ASCII.GetBytes ( html ); |
| 154 | + } |
| 155 | + |
128 | 156 | private async Task HandleTcpClient ( TcpClient client ) { |
129 | 157 |
|
130 | 158 | try { |
@@ -166,12 +194,15 @@ await sslStream.AuthenticateAsServerAsync ( |
166 | 194 | hostClient.ClientCertificate = sslStream.RemoteCertificate; |
167 | 195 | } |
168 | 196 | catch (Exception) { |
| 197 | + |
| 198 | + var message = GetBadRequestMessage ( "SSL/TLS Handshake failed." ); |
| 199 | + await clientStream.WriteAsync ( message, 0, message.Length ); |
169 | 200 | return; |
170 | 201 | } |
171 | 202 | } |
172 | 203 |
|
173 | 204 | await Handler.OnClientConnectedAsync ( this, hostClient ); |
174 | | - await connection.HandleConnectionEvents (); |
| 205 | + await connection.HandleConnectionEventsAsync (); |
175 | 206 | await Handler.OnClientDisconnectedAsync ( this, hostClient ); |
176 | 207 | } |
177 | 208 | } |
|
0 commit comments