mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-27 16:52:40 +08:00
Fix encoding and decoding of AUTH paket according to RFC. (#2076)
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
* Core: Fixed issue when parsing AUTH packet with 0 length body (#2039).
|
||||||
* nuget: Changed code signing and nuget certificate (**BREAKING CHANGE**).
|
* nuget: Changed code signing and nuget certificate (**BREAKING CHANGE**).
|
||||||
* TopicTemplates: Updated samples, parameter validation (#2022).
|
* TopicTemplates: Updated samples, parameter validation (#2022).
|
||||||
* ManagedClient: Switch SubscribeAsync/UnsubscribeAsync to IEnumerable<string> (#2026).
|
* ManagedClient: Switch SubscribeAsync/UnsubscribeAsync to IEnumerable<string> (#2026).
|
||||||
|
|||||||
@@ -68,12 +68,12 @@ namespace MQTTnet.Formatter.V5
|
|||||||
|
|
||||||
MqttPacket DecodeAuthPacket(ArraySegment<byte> body)
|
MqttPacket DecodeAuthPacket(ArraySegment<byte> body)
|
||||||
{
|
{
|
||||||
ThrowIfBodyIsEmpty(body);
|
|
||||||
|
|
||||||
_bufferReader.SetBuffer(body.Array, body.Offset, body.Count);
|
_bufferReader.SetBuffer(body.Array, body.Offset, body.Count);
|
||||||
|
|
||||||
var packet = new MqttAuthPacket();
|
var packet = new MqttAuthPacket();
|
||||||
|
|
||||||
|
// MQTT spec: The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success) and there are no Properties.
|
||||||
|
// In this case the AUTH has a Remaining Length of 0.
|
||||||
if (_bufferReader.EndOfStream)
|
if (_bufferReader.EndOfStream)
|
||||||
{
|
{
|
||||||
packet.ReasonCode = MqttAuthenticateReasonCode.Success;
|
packet.ReasonCode = MqttAuthenticateReasonCode.Success;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace MQTTnet.Formatter.V5
|
|||||||
public sealed class MqttV5PacketEncoder
|
public sealed class MqttV5PacketEncoder
|
||||||
{
|
{
|
||||||
const int FixedHeaderSize = 1;
|
const int FixedHeaderSize = 1;
|
||||||
|
|
||||||
readonly MqttBufferWriter _bufferWriter;
|
readonly MqttBufferWriter _bufferWriter;
|
||||||
readonly MqttV5PropertiesWriter _propertiesWriter = new MqttV5PropertiesWriter(new MqttBufferWriter(1024, 4096));
|
readonly MqttV5PropertiesWriter _propertiesWriter = new MqttV5PropertiesWriter(new MqttBufferWriter(1024, 4096));
|
||||||
|
|
||||||
@@ -64,13 +64,20 @@ namespace MQTTnet.Formatter.V5
|
|||||||
|
|
||||||
byte EncodeAuthPacket(MqttAuthPacket packet)
|
byte EncodeAuthPacket(MqttAuthPacket packet)
|
||||||
{
|
{
|
||||||
_bufferWriter.WriteByte((byte)packet.ReasonCode);
|
|
||||||
|
|
||||||
_propertiesWriter.WriteAuthenticationMethod(packet.AuthenticationMethod);
|
_propertiesWriter.WriteAuthenticationMethod(packet.AuthenticationMethod);
|
||||||
_propertiesWriter.WriteAuthenticationData(packet.AuthenticationData);
|
_propertiesWriter.WriteAuthenticationData(packet.AuthenticationData);
|
||||||
_propertiesWriter.WriteReasonString(packet.ReasonString);
|
_propertiesWriter.WriteReasonString(packet.ReasonString);
|
||||||
_propertiesWriter.WriteUserProperties(packet.UserProperties);
|
_propertiesWriter.WriteUserProperties(packet.UserProperties);
|
||||||
|
|
||||||
|
// MQTT spec: The Reason Code and Property Length can be omitted if the Reason Code is 0x00 (Success) and there are no Properties.
|
||||||
|
// In this case the AUTH has a Remaining Length of 0.
|
||||||
|
if (packet.ReasonCode == MqttAuthenticateReasonCode.Success && _propertiesWriter.Length == 0)
|
||||||
|
{
|
||||||
|
return MqttBufferWriter.BuildFixedHeader(MqttControlPacketType.Auth);
|
||||||
|
}
|
||||||
|
|
||||||
|
_bufferWriter.WriteByte((byte)packet.ReasonCode);
|
||||||
|
|
||||||
_propertiesWriter.WriteTo(_bufferWriter);
|
_propertiesWriter.WriteTo(_bufferWriter);
|
||||||
_propertiesWriter.Reset();
|
_propertiesWriter.Reset();
|
||||||
|
|
||||||
@@ -526,7 +533,7 @@ namespace MQTTnet.Formatter.V5
|
|||||||
|
|
||||||
static void ThrowIfPacketIdentifierIsInvalid(ushort packetIdentifier, MqttPacket packet)
|
static void ThrowIfPacketIdentifierIsInvalid(ushort packetIdentifier, MqttPacket packet)
|
||||||
{
|
{
|
||||||
// SUBSCRIBE, UNSUBSCRIBE, and PUBLISH(in cases where QoS > 0) Control Packets MUST contain a non-zero 16 - bit Packet Identifier[MQTT - 2.3.1 - 1].
|
// SUBSCRIBE, UNSUBSCRIBE, and PUBLISH(in cases where QoS > 0) Control Packets MUST contain a non-zero 16 - bit Packet Identifier[MQTT - 2.3.1 - 1].
|
||||||
|
|
||||||
if (packetIdentifier == 0)
|
if (packetIdentifier == 0)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user