Пример #1
0
        public async Task WriteLargeDataTextAscii(int length)
        {
            string data = new string('#', length);

            FillRandomStringData(data, length);
            using (var memoryPool = new MemoryPool())
            {
                var pipe = new Pipe(memoryPool);

                var output = pipe.Writer.Alloc();
                output.Append(data, TextEncoder.Utf8);
                var foo = output.Buffer.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();

                pipe.Writer.Complete();

                int offset = 0;
                while (true)
                {
                    var result = await pipe.Reader.ReadAsync();

                    var input = result.Buffer;
                    if (input.Length == 0)
                    {
                        break;
                    }

                    string s = ReadableBufferExtensions.GetAsciiString(input);
                    Assert.Equal(data.Substring(offset, input.Length), s);
                    offset += input.Length;
                    pipe.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }
Пример #2
0
        public async Task WriteLargeDataTextUtf8(int length)
        {
            string data = new string('#', length);

            FillRandomStringData(data, length);
            using (var memoryPool = new MemoryPool())
            {
                var channel = new Channel(memoryPool);

                var output = channel.Alloc();
                output.WriteUtf8String(data);
                var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();

                channel.CompleteWriter();

                int offset = 0;
                while (true)
                {
                    var result = await channel.ReadAsync();

                    var input = result.Buffer;
                    if (input.Length == 0)
                    {
                        break;
                    }

                    string s = ReadableBufferExtensions.GetUtf8String(input);
                    Assert.Equal(data.Substring(offset, input.Length), s);
                    offset += input.Length;
                    channel.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }
        internal static string ComputeReply(ReadableBuffer key, BufferSpan buffer)
        {
            //To prove that the handshake was received, the server has to take two
            //pieces of information and combine them to form a response.  The first
            //piece of information comes from the |Sec-WebSocket-Key| header field
            //in the client handshake:

            //     Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==

            //For this header field, the server has to take the value (as present
            //in the header field, e.g., the base64-encoded [RFC4648] version minus
            //any leading and trailing whitespace) and concatenate this with the
            //Globally Unique Identifier (GUID, [RFC4122]) "258EAFA5-E914-47DA-
            //95CA-C5AB0DC85B11" in string form, which is unlikely to be used by
            //network endpoints that do not understand the WebSocket Protocol.  A
            //SHA-1 hash (160 bits) [FIPS.180-3], base64-encoded (see Section 4 of
            //[RFC4648]), of this concatenation is then returned in the server's
            //handshake.

            const int ExpectedKeyLength = 24;

            key = ReadableBufferExtensions.TrimStart(key);
            int len = key.Length, baseOffset = buffer.Offset;

            if (len != ExpectedKeyLength)
            {
                throw new ArgumentException("Invalid key length", nameof(key));
            }

            if (buffer.Length < (ExpectedKeyLength + WebSocketKeySuffixBytes.Length))
            {
                throw new ArgumentException("Insufficient buffer space", nameof(buffer));
            }

            // use the output buffer as a scratch pad to compute the hash
            byte[] arr = buffer.Array;
            key.CopyTo(arr, baseOffset);
            Buffer.BlockCopy( // append the magic number from RFC6455
                WebSocketKeySuffixBytes, 0,
                arr, baseOffset + ExpectedKeyLength,
                WebSocketKeySuffixBytes.Length);

            // compute the hash
            using (var sha = SHA1.Create())
            {
                var hash = sha.ComputeHash(arr, baseOffset,
                                           ExpectedKeyLength + WebSocketKeySuffixBytes.Length);
                return(Convert.ToBase64String(hash));
            }
        }
Пример #4
0
        public async Task WriteLargeDataTextAscii(int length)
        {
            string data = new string('#', length);

            FillRandomStringData(data, length);
            var pipe = new Pipe();

            var output = pipe.Writer;

            output.Append(data, SymbolTable.InvariantUtf8);
            var foo = output.GetMemory().IsEmpty; // trying to see if .Memory breaks
            await output.FlushAsync();

            pipe.Writer.Complete();

            long offset = 0;

            while (true)
            {
                var result = await pipe.Reader.ReadAsync();

                var input = result.Buffer;
                if (input.Length == 0)
                {
                    break;
                }

                string s = ReadableBufferExtensions.GetAsciiString(input);
                // We are able to cast because test arguments are in range of int
                Assert.Equal(data.Substring((int)offset, (int)input.Length), s);
                offset += input.Length;
                pipe.Reader.AdvanceTo(input.End);
            }

            Assert.Equal(data.Length, offset);
        }