33// See the LICENSE file in the project root for more information.
44
55using Microsoft . AspNetCore . Connections ;
6- using Microsoft . AspNetCore . Http . Connections . Features ;
6+ using Microsoft . AspNetCore . Http ;
77using Microsoft . AspNetCore . Http . Features ;
88using MQTTnet . Adapter ;
99using MQTTnet . Exceptions ;
@@ -29,7 +29,7 @@ class MqttChannel : IDisposable
2929 readonly PipeReader _input ;
3030 readonly PipeWriter _output ;
3131 readonly MqttPacketInspector ? _packetInspector ;
32- readonly bool _allowPacketFragmentation ;
32+ bool _allowPacketFragmentation = false ;
3333
3434 public MqttPacketFormatterAdapter PacketFormatterAdapter { get ; }
3535
@@ -43,69 +43,64 @@ class MqttChannel : IDisposable
4343
4444 public bool IsSecureConnection { get ; }
4545
46+ public bool IsWebSocketConnection { get ; }
47+
4648
4749 public MqttChannel (
4850 MqttPacketFormatterAdapter packetFormatterAdapter ,
4951 ConnectionContext connection ,
50- MqttPacketInspector ? packetInspector = null ,
51- bool ? allowPacketFragmentation = null )
52+ HttpContext ? httpContext ,
53+ MqttPacketInspector ? packetInspector )
5254 {
5355 PacketFormatterAdapter = packetFormatterAdapter ;
54- _packetInspector = packetInspector ;
5556
56- var httpContextFeature = connection . Features . Get < IHttpContextFeature > ( ) ;
5757 var tlsConnectionFeature = connection . Features . Get < ITlsConnectionFeature > ( ) ;
58- RemoteEndPoint = GetRemoteEndPoint ( httpContextFeature , connection . RemoteEndPoint ) ;
59- IsSecureConnection = IsTlsConnection ( httpContextFeature , tlsConnectionFeature ) ;
60- ClientCertificate = GetClientCertificate ( httpContextFeature , tlsConnectionFeature ) ;
58+ RemoteEndPoint = GetRemoteEndPoint ( connection . RemoteEndPoint , httpContext ) ;
59+ ClientCertificate = GetClientCertificate ( tlsConnectionFeature , httpContext ) ;
60+ IsSecureConnection = IsTlsConnection ( tlsConnectionFeature , httpContext ) ;
61+ IsWebSocketConnection = connection . Features . Get < WebSocketConnectionFeature > ( ) != null ;
6162
63+ _packetInspector = packetInspector ;
6264 _input = connection . Transport . Input ;
6365 _output = connection . Transport . Output ;
64-
65- _allowPacketFragmentation = allowPacketFragmentation == null
66- ? AllowPacketFragmentation ( httpContextFeature )
67- : allowPacketFragmentation . Value ;
6866 }
6967
70- private static bool AllowPacketFragmentation ( IHttpContextFeature ? _httpContextFeature )
68+ private static EndPoint ? GetRemoteEndPoint ( EndPoint ? remoteEndPoint , HttpContext ? httpContext )
7169 {
72- var serverModeWebSocket = _httpContextFeature != null &&
73- _httpContextFeature . HttpContext != null &&
74- _httpContextFeature . HttpContext . WebSockets . IsWebSocketRequest ;
75-
76- return ! serverModeWebSocket ;
77- }
78-
70+ if ( remoteEndPoint != null )
71+ {
72+ return remoteEndPoint ;
73+ }
7974
80- private static EndPoint ? GetRemoteEndPoint ( IHttpContextFeature ? _httpContextFeature , EndPoint ? remoteEndPoint )
81- {
82- if ( _httpContextFeature != null && _httpContextFeature . HttpContext != null )
75+ if ( httpContext != null )
8376 {
84- var httpConnection = _httpContextFeature . HttpContext . Connection ;
77+ var httpConnection = httpContext . Connection ;
8578 var remoteAddress = httpConnection . RemoteIpAddress ;
8679 if ( remoteAddress != null )
8780 {
8881 return new IPEndPoint ( remoteAddress , httpConnection . RemotePort ) ;
8982 }
9083 }
9184
92- return remoteEndPoint ;
85+ return null ;
9386 }
9487
95- private static bool IsTlsConnection ( IHttpContextFeature ? _httpContextFeature , ITlsConnectionFeature ? tlsConnectionFeature )
88+ private static bool IsTlsConnection ( ITlsConnectionFeature ? tlsConnectionFeature , HttpContext ? httpContext )
9689 {
97- return _httpContextFeature != null && _httpContextFeature . HttpContext != null
98- ? _httpContextFeature . HttpContext . Request . IsHttps
99- : tlsConnectionFeature != null ;
90+ return tlsConnectionFeature != null || ( httpContext != null && httpContext . Request . IsHttps ) ;
10091 }
10192
102- private static X509Certificate2 ? GetClientCertificate ( IHttpContextFeature ? _httpContextFeature , ITlsConnectionFeature ? tlsConnectionFeature )
93+ private static X509Certificate2 ? GetClientCertificate ( ITlsConnectionFeature ? tlsConnectionFeature , HttpContext ? httpContext )
10394 {
104- return _httpContextFeature != null && _httpContextFeature . HttpContext != null
105- ? _httpContextFeature . HttpContext . Connection . ClientCertificate
106- : tlsConnectionFeature ? . ClientCertificate ;
95+ return tlsConnectionFeature != null
96+ ? tlsConnectionFeature . ClientCertificate
97+ : httpContext ? . Connection . ClientCertificate ;
10798 }
10899
100+ public void SetAllowPacketFragmentation ( bool value )
101+ {
102+ _allowPacketFragmentation = value ;
103+ }
109104
110105 public async Task DisconnectAsync ( )
111106 {
0 commit comments