protected override void Verify(IFullHttpResponse response)
        {
            if (!response.Status.Equals(HttpResponseStatus.SwitchingProtocols))
            {
                throw new WebSocketHandshakeException($"Invalid handshake response getStatus: {response.Status}");
            }

            HttpHeaders headers = response.Headers;

            if (!headers.TryGet(HttpHeaderNames.Upgrade, out ICharSequence upgrade) ||
                !Websocket.ContentEqualsIgnoreCase(upgrade))
            {
                throw new WebSocketHandshakeException($"Invalid handshake response upgrade: {upgrade}");
            }

            if (!headers.ContainsValue(HttpHeaderNames.Connection, HttpHeaderValues.Upgrade, true))
            {
                headers.TryGet(HttpHeaderNames.Connection, out upgrade);
                throw new WebSocketHandshakeException($"Invalid handshake response connection: {upgrade}");
            }

            IByteBuffer challenge = response.Content;

            if (!challenge.Equals(this.expectedChallengeResponseBytes))
            {
                throw new WebSocketHandshakeException("Invalid challenge");
            }
        }
Example #2
0
        protected override void Verify(IFullHttpResponse response)
        {
            if (!response.Status.Equals(HttpResponseStatus.SwitchingProtocols))
            {
                ThrowHelper.ThrowWebSocketHandshakeException_InvalidHandshakeResponseGS(response);
            }

            HttpHeaders headers = response.Headers;

            if (!headers.TryGet(HttpHeaderNames.Upgrade, out ICharSequence upgrade) ||
                !Websocket.ContentEqualsIgnoreCase(upgrade))
            {
                ThrowHelper.ThrowWebSocketHandshakeException_InvalidHandshakeResponseU(upgrade);
            }

            if (!headers.ContainsValue(HttpHeaderNames.Connection, HttpHeaderValues.Upgrade, true))
            {
                _ = headers.TryGet(HttpHeaderNames.Connection, out upgrade);
                ThrowHelper.ThrowWebSocketHandshakeException_InvalidHandshakeResponseConn(upgrade);
            }

            IByteBuffer challenge = response.Content;

            if (!challenge.Equals(_expectedChallengeResponseBytes))
            {
                ThrowHelper.ThrowWebSocketHandshakeException_InvalidChallenge();
            }
        }
Example #3
0
 protected override bool Equals0(IHttp2StreamFrame other)
 {
     return(other is DefaultHttp2DataFrame otherFrame &&
            base.Equals0(other) &&
            _content.Equals(otherFrame.Content) &&
            _endStream == otherFrame._endStream &&
            _padding == otherFrame._padding);
 }
        /// <summary>
        /// This implementation of the <see cref="Equals(object)"/> operation is restricted to
        /// work only with instances of the same class. The reason for that is that
        /// Netty library already has a number of classes that extend <see cref="DefaultByteBufferHolder"/> and
        /// override <see cref="Equals(object)"/> method with an additional comparison logic and we
        /// need the symmetric property of the <see cref="Equals(object)"/> operation to be preserved.
        /// </summary>
        /// <param name="obj">the reference object with which to compare.</param>
        /// <returns><c>true</c> if this object is the same as the <paramref name="obj"/>
        /// argument; <c>false</c> otherwise.</returns>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj))
            {
                return(true);
            }

            return(obj is object && GetType() == obj.GetType() && _data.Equals(((DefaultByteBufferHolder)obj)._data));
        }
Example #5
0
 private static bool ValueEquals(IByteBuffer a, IByteBuffer b)
 {
     if (a == null || b == null)
     {
         return(a == b);
     }
     else if (a == b)
     {
         return(true);
     }
     else if (a.IsMemory)
     {
         return(b.IsMemory && a.Data.SequenceEqual(b.Data));
     }
     else if (a is IBulkDataUriByteBuffer)
     {
         var buffer = b as IBulkDataUriByteBuffer;
         if (buffer != null)
         {
             return(((IBulkDataUriByteBuffer)a).BulkDataUri == buffer.BulkDataUri);
         }
         else
         {
             return(false);
         }
     }
     else if (a is EmptyBuffer && b is EmptyBuffer)
     {
         return(true);
     }
     else if (a is StreamByteBuffer && b is StreamByteBuffer)
     {
         var asbb = (StreamByteBuffer)a;
         var bsbb = (StreamByteBuffer)b;
         if (asbb.Stream == null || bsbb.Stream == null)
         {
             return(asbb.Stream == bsbb.Stream);
         }
         else
         {
             return(asbb.Position == bsbb.Position && asbb.Size == bsbb.Size && asbb.Stream.Equals(bsbb.Stream));
         }
     }
     else if (a is CompositeByteBuffer && b is CompositeByteBuffer)
     {
         return(((CompositeByteBuffer)a).Buffers.Zip(((CompositeByteBuffer)b).Buffers, ValueEquals).All(x => x));
     }
     else
     {
         return(a.Equals(b));
     }
 }
Example #6
0
 public override bool Equals(object obj) => Buf.Equals(obj);
 private static bool ValueEquals(IByteBuffer a, IByteBuffer b)
 {
   if (a == null || b == null)
     return a == b;
   else if (a == b)
     return true;
   else if (a.IsMemory)
   {
     return b.IsMemory && a.Data.SequenceEqual(b.Data);
   }
   else if (a is BulkUriByteBuffer)
   {
     if (b is BulkUriByteBuffer)
       return ((BulkUriByteBuffer)a).BulkDataUri == ((BulkUriByteBuffer)b).BulkDataUri;
     else
       return false;
   }
   else if (a is EmptyBuffer && b is EmptyBuffer)
     return true;
   else if (a is StreamByteBuffer && b is StreamByteBuffer)
   {
     var asbb = (StreamByteBuffer)a;
     var bsbb = (StreamByteBuffer)b;
     if (asbb.Stream == null || bsbb.Stream == null)
     {
       return asbb.Stream == bsbb.Stream;
     }
     else
     {
       return asbb.Position == bsbb.Position && asbb.Size == bsbb.Size && asbb.Stream.Equals(bsbb.Stream);
     }
   }
   else if (a is CompositeByteBuffer && b is CompositeByteBuffer)
   {
     return ((CompositeByteBuffer)a).Buffers.Zip(((CompositeByteBuffer)b).Buffers, ValueEquals).All(x => x);
   }
   else
   {
     return a.Equals(b);
   }
 }
Example #8
0
        private IByteBuffer DecompressContent(IChannelHandlerContext ctx, WebSocketFrame msg)
        {
            if (_decoder is null)
            {
                switch (msg.Opcode)
                {
                case Opcode.Text:
                case Opcode.Binary:
                    break;

                default:
                    ThrowHelper.ThrowCodecException_UnexpectedInitialFrameType(msg);
                    break;
                }
                _decoder = new EmbeddedChannel(ZlibCodecFactory.NewZlibDecoder(ZlibWrapper.None));
            }

            var readable          = msg.Content.IsReadable();
            var emptyDeflateBlock = EmptyDeflateBlock.Equals(msg.Content);

            _ = _decoder.WriteInbound(msg.Content.Retain());
            if (AppendFrameTail(msg))
            {
                _ = _decoder.WriteInbound(FrameTail.Duplicate());
            }

            var compositeDecompressedContent = ctx.Allocator.CompositeBuffer();

            for (; ;)
            {
                var partUncompressedContent = _decoder.ReadInbound <IByteBuffer>();
                if (partUncompressedContent is null)
                {
                    break;
                }
                if (!partUncompressedContent.IsReadable())
                {
                    _ = partUncompressedContent.Release();
                    continue;
                }
                _ = compositeDecompressedContent.AddComponent(true, partUncompressedContent);
            }
            // Correctly handle empty frames
            // See https://github.com/netty/netty/issues/4348
            if (!emptyDeflateBlock && readable && compositeDecompressedContent.NumComponents <= 0)
            {
                // Sometimes after fragmentation the last frame
                // May contain left-over data that doesn't affect decompression
                if (!(msg is ContinuationWebSocketFrame))
                {
                    _ = compositeDecompressedContent.Release();
                    ThrowHelper.ThrowCodecException_CannotReadUncompressedBuf();
                }
            }

            if (msg.IsFinalFragment && _noContext)
            {
                Cleanup();
            }

            return(compositeDecompressedContent);
        }