Esempio n. 1
0
 private static void ProcessLine(int i, ReadOnlySequence <byte> buffer)
 {
     if (cfg.SingleRun)
     {
         Console.WriteLine(i);
         Console.WriteLine(Encoding.ASCII.GetString(buffer.ToArray()));                 // !!! materialization, full copy
     }
 }
        protected virtual BinaryRequestInfo ProcessMatchedRequest(ref ReadOnlySequence <byte> buffer)
        {
            BinaryRequestInfo binaryRequestInfo = new BinaryRequestInfo();

            binaryRequestInfo.OriBuffer = buffer.ToArray();
            binaryRequestInfo.Key       = this.Key;
            return(binaryRequestInfo);
        }
Esempio n. 3
0
 public ValueTask <ReadOnlySequence <byte> > PrepareMessageToBeSent(ReadOnlySequence <byte> bytes)
 {
     byte[] byteList = new byte[_settings.StartDelimiter.Length + _settings.EndDelimiter.Length + bytes.Length];
     Buffer.BlockCopy(_settings.StartDelimiter, 0, byteList, 0, _settings.StartDelimiter.Length);
     Buffer.BlockCopy(bytes.ToArray(), 0, byteList, _settings.StartDelimiter.Length, (int)bytes.Length);
     Buffer.BlockCopy(_settings.EndDelimiter, 0, byteList, _settings.StartDelimiter.Length + (int)bytes.Length, _settings.EndDelimiter.Length);
     return(new ValueTask <ReadOnlySequence <byte> >(new ReadOnlySequence <byte>(byteList)));
 }
 private ReadOnlySpan <byte> ConvertBufferToSpan(ReadOnlySequence <byte> buffer)
 {
     if (buffer.IsSingleSegment)
     {
         return(buffer.First.Span);
     }
     return(buffer.ToArray());
 }
Esempio n. 5
0
        public void ToArrayIsCorrect(int length)
        {
            char[] data = Enumerable.Range(0, length).Select(i => (char)i).ToArray();
            ReadOnlySequence <char> buffer = Factory.CreateWithContent(data);

            Assert.Equal(length, buffer.Length);
            Assert.Equal(data, buffer.ToArray());
        }
Esempio n. 6
0
 public static ReadOnlyMemory <byte> ToMemory(this ReadOnlySequence <byte> source)
 {
     if (source.IsSingleSegment)
     {
         return(source.First);
     }
     return(source.ToArray());
 }
 public static ReadOnlySpan <byte> ToSpan(this ReadOnlySequence <byte> buffer)
 {
     if (buffer.IsSingleSegment)
     {
         return(buffer.First.Span);
     }
     return(buffer.ToArray());
 }
Esempio n. 8
0
        public bool ReadMessages(ReadOnlySequence <byte> buffer, IInvocationBinder binder, out IList <HubMessage> messages, out SequencePosition consumed, out SequencePosition examined)
        {
            // TODO: Fix this implementation to be incremental
            consumed = buffer.End;
            examined = consumed;

            return(ReadMessages(buffer.ToArray(), binder, out messages));
        }
Esempio n. 9
0
        internal async Task Handle_ContentBody(ReadOnlySequence <byte> payload)
        {
            pendingDelivery.Body = payload.ToArray();

            await consumers[pendingDelivery.ConsumerTag](pendingDelivery).ConfigureAwait(false);

            pendingDelivery = null;
        }
Esempio n. 10
0
        public MockMessage(IMessageTransaction transaction, ReadOnlySequence <byte> buffer)
        {
            Transaction = transaction;

            using var stream = new MemoryStream(buffer.ToArray());

            MimeMessage = MimeMessage.Load(stream);
        }
Esempio n. 11
0
        /// <summary>Parses and validates a JWT encoded as a JWS or JWE in compact serialized format.</summary>
        /// <param name="utf8Token">The JWT encoded as JWE or JWS.</param>
        /// <param name="policy">The validation policy.</param>
        /// <param name="jwt">The resulting <see cref="Jwt"/>.</param>
        public static bool TryParse(ReadOnlySequence <byte> utf8Token, TokenValidationPolicy policy, out Jwt jwt)
        {
            if (utf8Token.IsSingleSegment)
            {
                return(TryParse(utf8Token.First.Span, policy, out jwt));
            }

            return(TryParse(utf8Token.ToArray(), policy, out jwt));
        }
 public override AnalyzedPacket ResolvePackage(ReadOnlySequence <byte> buffer)
 {
     return(new AnalyzedPacket
     {
         SessionUniqueId = 0,
         PacketId = 0,
         Body = buffer.ToArray()
     });
 }
Esempio n. 13
0
        public void SetHeader(ref ReadOnlySequence <byte> key, ref ReadOnlySequence <byte> value)
        {
            string headerKey = GetHeaderKey(ref key);

            _headers[headerKey] = new HeaderValue
            {
                Raw = value.ToArray()
            };
        }
        public async Task CanDoMultipleAsyncWritesToStream()
        {
            var        pipe   = new Pipe();
            PipeWriter writer = PipeWriter.Create(pipe.Writer.AsStream());
            // This needs to run inline to synchronize the reader and writer
            TaskCompletionSource <object> waitForRead = null;

            async Task DoWritesAsync(PipeWriter writer, byte[][] writes)
            {
                for (int i = 0; i < writes.Length; i++)
                {
                    waitForRead = new TaskCompletionSource <object>();
                    await writer.WriteAsync(writes[i]);

                    await waitForRead.Task;
                }

                writer.Complete();
            }

            async Task DoReadsAsync(PipeReader reader, byte[][] reads)
            {
                int index = 0;

                while (true)
                {
                    ReadResult readResult = await reader.ReadAsync();

                    ReadOnlySequence <byte> buffer = readResult.Buffer;
                    if (readResult.IsCompleted)
                    {
                        break;
                    }
                    Assert.Equal(reads[index], buffer.ToArray());
                    reader.AdvanceTo(buffer.End);
                    index++;
                    waitForRead.TrySetResult(null);
                }

                reader.Complete();
            }

            var data = new List <byte[]>
            {
                Encoding.ASCII.GetBytes("Hello"),
                Encoding.ASCII.GetBytes("World"),
                Encoding.ASCII.GetBytes("This"),
                Encoding.ASCII.GetBytes("Works"),
            }.
            ToArray();

            Task readsTask  = DoReadsAsync(pipe.Reader, data);
            Task writesTask = DoWritesAsync(writer, data);

            await writesTask;
            await readsTask;
        }
Esempio n. 15
0
        private string GetUtf8String(ReadOnlySequence <byte> buffer)
        {
            if (buffer.IsSingleSegment)
            {
                return(Encoding.UTF8.GetString(buffer.First.Span));
            }

            return(Encoding.UTF8.GetString(buffer.ToArray()));
        }
        internal static ReadOnlyMemory <byte> ToMemory(this ReadOnlySequence <byte> buffer)
        {
            if (buffer.IsSingleSegment)
            {
                return(buffer.First);
            }

            return(buffer.ToArray());
        }
Esempio n. 17
0
        public static ReadOnlySpan <byte> GetSpanFromSequence(ReadOnlySequence <byte> sequence)
        {
            if (sequence.IsSingleSegment)
            {
                return(sequence.First.Span);
            }

            return(sequence.ToArray());
        }
        public static ReadOnlySpan <T> GetSpan <T>(this ReadOnlySequence <T> sequence)
        {
            if (sequence.IsSingleSegment)
            {
                return(sequence.First.Span);
            }

            return(sequence.ToArray());
        }
        public Task <bool> TryStoreAuditTrail(ReadOnlySequence <byte> buffer, [NotNullWhen(false)] out AuditTrailError?error, CancellationToken cancellationToken = default)
        {
            Jwt?jwt = null;

            try
            {
                if (Jwt.TryParse(buffer, _options.Policy, out jwt))
                {
                    var record = new AuditTrailRecord(buffer.ToArray(), jwt, jwt.Payload !.TryGetClaim("iss", out var iss) ? iss.GetString() ! : "");
                    if (_sink.TryWrite(record))
                    {
                        error = null;
                        return(_trueTask);
                    }

                    error = AuditTrailError.TooManyRequest();
                    return(_falseTask);
                }
                else
                {
                    var jwtError = jwt.Error !;
                    if ((jwtError.Status & TokenValidationStatus.KeyError) == TokenValidationStatus.KeyError)
                    {
                        error = AuditTrailError.InvalidKey();
                        return(_falseTask);
                    }
                    else
                    {
                        var description = jwt.Error !.Status switch
                        {
                            TokenValidationStatus.MalformedToken => "Malformed token.",
                            TokenValidationStatus.TokenReplayed => "Duplicated token.",
                            TokenValidationStatus.Expired => "Expired token.",
                            TokenValidationStatus.MissingEncryptionAlgorithm => "Missing encryption algorithm in the header.",
                            TokenValidationStatus.DecryptionFailed => "Unable to decrypt the token.",
                            TokenValidationStatus.NotYetValid => "The token is not yet valid.",
                            TokenValidationStatus.DecompressionFailed => "Unable to decompress the token.",
                            TokenValidationStatus.CriticalHeaderMissing => $"The critical header '{jwtError.ErrorHeader}' is missing.",
                            TokenValidationStatus.CriticalHeaderUnsupported => $"The critical header '{jwtError.ErrorHeader}' is not supported.",
                            TokenValidationStatus.InvalidClaim => $"The claim '{jwtError.ErrorClaim}' is invalid.",
                            TokenValidationStatus.MissingClaim => $"The claim '{jwtError.ErrorClaim}' is missing.",
                            TokenValidationStatus.InvalidHeader => $"The header '{jwtError.ErrorHeader}' is invalid.",
                            TokenValidationStatus.MissingHeader => $"The header '{jwtError.ErrorHeader}' is missing.",
                            _ => null
                        };

                        error = AuditTrailError.InvalidRequest(description);
                        return(_falseTask);
                    }
                }
            }
            finally
            {
                jwt?.Dispose();
            }
        }
Esempio n. 20
0
        private static string GetString(ReadOnlySequence <byte> buffer)
        {
            if (buffer.IsSingleSegment)
            {
                MemoryMarshal.TryGetArray(buffer.First, out var segment);
                return(Encoding.UTF8.GetString(segment.Array, segment.Offset, segment.Count));
            }

            return(Encoding.UTF8.GetString(buffer.ToArray()));
        }
Esempio n. 21
0
        private bool TryReadFrame(ref ReadOnlySequence <byte> buffer, out Frame frame)
        {
            var bufferString = String.Join(",", buffer.ToArray().Select(p => p.ToString()).ToArray());

            Console.Error.WriteLine($"Trying to read buffer ({bufferString})");
            if (frameReader.TryReadFrame(ref buffer, out var frame2))
            {
                var positionOfNextPossibleFrame = buffer.GetPosition(frame2.Header.StartPosition - 1 + frame2.Header.FrameSize + 2);
                buffer = buffer.Slice(positionOfNextPossibleFrame);
            }
Esempio n. 22
0
 private static EventPipeMessage ParseMessage(ReadOnlySequence <byte> input)
 {
     if (input.IsSingleSegment)
     {
         return(ParseMessage(input.First.Span));
     }
     else
     {
         return(ParseMessage(input.ToArray()));
     }
 }
Esempio n. 23
0
        public static void TestRead(ReadOnlySequence <char> charData)
        {
            using var tr = charData.AsTextReader();
            var expectedData = charData.ToArray();

            for (var count = 0; count < expectedData.Length; ++count)
            {
                var tmp = tr.Read();
                Equal(expectedData[count], tmp);
            }
        }
    public static void Main()
    {
        const string newLine = "\r\n";

        string requestString =
            "POST /resource/?query_id=0 HTTP/1.1" + newLine +
            "Host: localhost" + newLine +
            "User-Agent: custom" + newLine +
            "Accept: */*" + newLine +
            "Connection: close" + newLine +
            "Content-Length: 20" + newLine +
            "Content-Type: application/json" + newLine +

            "key1=value&key2=value2&key3=value3";

        byte[] requestRaw = Encoding.UTF8.GetBytes(requestString);
        ReadOnlySequence <byte> buffer = new ReadOnlySequence <byte>(requestRaw);

        HttpParser <Program> parser = new HttpParser <Program>();
        Program app = new Program();

        Console.WriteLine("Start line:");

        parser.ParseRequestLine(app, buffer, out var consumed, out var examined);
        buffer = buffer.Slice(consumed);

        Console.WriteLine("\r\nHeaders:");

        parser.ParseHeaders(app, buffer, out consumed, out examined, out var b);
        buffer = buffer.Slice(consumed);

        Console.WriteLine("\r\nBody:");

        string body = Encoding.UTF8.GetString(buffer.ToArray());

        string[] kvpArray = body.Split('&').ToArray();

        Dictionary <string, string> bodyKvps = new Dictionary <string, string>();

        #region Deserialize JSON body
        //Dictionary<string, int> bodyObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, int>>(body);
        #endregion

        foreach (var item in kvpArray)
        {
            string[] kvp   = item.Split('=').ToArray();
            string   key   = kvp[0];
            string   value = kvp[1];
            bodyKvps[key] = value;
            Console.WriteLine($"key: {key}, value: {value}");
        }

        Console.ReadKey();
    }
Esempio n. 25
0
        public bool TryParseMessage(ref ReadOnlySequence <byte> input, IInvocationBinder binder, out HubMessage message)
        {
            using var inStream  = new MemoryStream(input.ToArray());
            using var outStream = new MemoryStream();

            GZip.Decompress(inStream, outStream, false);

            var decompressedInput = new ReadOnlySequence <byte>(outStream.ToArray());

            return(_jsonHubProtocol.TryParseMessage(ref decompressedInput, binder, out message));
        }
Esempio n. 26
0
        //public SequencePosition Decode(ReadOnlySequence<byte> input, out IEnumerable<ReadOnlyMemory<byte>> rawMessage) {
        //   IAsyncEnumerable
        //}

        public static bool TryParseMessage(ref ReadOnlySequence <byte> buffer, out Message message)
        {
            if (buffer.Length == 0)
            {
                message = null;
                return(false);
            }

            using (MemoryStream ms = new MemoryStream(buffer.ToArray())) {
                try {
                    message = Message.ReadNext(ms, NBitcoin.Network.TestNet, 70015, default, out _);
Esempio n. 27
0
        public static byte[] Serialize(BaseCommand command, MessageMetadata metadata, ReadOnlySequence <byte> payload)
        {
            var stream = MemoryManager.GetStream();
            var writer = new BinaryWriter(stream);

            // write fake totalLength
            for (var i = 0; i < 4; i++)
            {
                stream.WriteByte(0);
            }

            // write commandPayload
            ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, command, PrefixStyle.Fixed32BigEndian);

            var stream1Size = (int)stream.Length;

            // write magic number 0x0e01 0x0e, 0x01
            stream.WriteByte(14);
            stream.WriteByte(1);

            for (var i = 0; i < 4; i++)
            {
                stream.WriteByte(0);
            }
            // write metadata
            ProtoBuf.Serializer.SerializeWithLengthPrefix(stream, metadata, PrefixStyle.Fixed32BigEndian);

            var stream2Size       = (int)stream.Length;
            var totalMetadataSize = stream2Size - stream1Size - 6;

            // write payload
            stream.Write(payload.ToArray(), 0, (int)payload.Length);

            var frameSize   = (int)stream.Length;
            var totalSize   = frameSize - 4;
            var payloadSize = frameSize - stream2Size;

            var crcStart        = stream1Size + 2;
            var crcPayloadStart = crcStart + 4;

            //write CRC
            stream.Seek(crcPayloadStart, SeekOrigin.Begin);
            var crc = (int)CRC32C.Get(0u, stream, totalMetadataSize + payloadSize);

            stream.Seek(crcStart, SeekOrigin.Begin);
            writer.Write(crc.IntToBigEndian());

            //write total size and command size
            stream.Seek(0L, SeekOrigin.Begin);
            writer.Write(totalSize.IntToBigEndian());

            stream.Seek(0L, SeekOrigin.Begin);
            return(stream.ToArray());
        }
        public override async Task <SmtpResponse> SaveAsync(
            ISessionContext context,
            IMessageTransaction transaction,
            ReadOnlySequence <byte> buffer,
            CancellationToken cancellationToken)
        {
            await this._receivedDataHandler.HandleReceivedAsync(
                buffer.ToArray(),
                transaction.To.Select(s => s.AsAddress()).ToArray());

            return(SmtpResponse.Ok);
        }
        public override AnalyzedPacket Filter(ref ReadOnlySequence <byte> buffer)
        {
            try
            {
                if (buffer.Length < HEADER_SIZE)
                {
                    return(null);
                }

                var woringSpan = buffer.First.Span;

                var packetTotalSize = C3SockNetUtil.FastBinaryRead.UInt16(woringSpan, 0);
                var BodySize        = (UInt16)(packetTotalSize - HEADER_SIZE);
                var ProtocolId      = C3SockNetUtil.FastBinaryRead.UInt16(woringSpan, 2);
                var PacketType      = C3SockNetUtil.FastBinaryRead.SByte(woringSpan, 4);

                if (BodySize == 0)
                {
                    var packet = new AnalyzedPacket
                    {
                        SessionUniqueId = 0,
                        PacketId        = ProtocolId,
                        Head            = null,
                        Body            = buffer.ToArray()
                    };

                    buffer = buffer.Slice(packetTotalSize);
                    return(packet);
                }
                else if (BodySize > 0 && (packetTotalSize >= buffer.Length))
                {
                    var packet = new AnalyzedPacket
                    {
                        SessionUniqueId = 0,
                        PacketId        = ProtocolId,
                        Head            = null,
                        Body            = buffer.Slice(HEADER_SIZE, BodySize).ToArray()
                    };

                    buffer = buffer.Slice(packetTotalSize);
                    return(packet);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex)
            {
                GLogging.Logger().LogError($"Failed to Filter {ex.ToString()}");
                return(null);
            }
        }
Esempio n. 30
0
        private async Task <ReadOnlyMemory <byte> > ProcessProxyMessage(ReadOnlySequence <byte> request)
        {
            var proxyMessage = KdcProxyMessage.Decode(request.ToArray());

            var unwrapped = proxyMessage.UnwrapMessage();

            var tag = PeekTag(unwrapped);

            var response = await ProcessMessageCore(new ReadOnlySequence <byte>(unwrapped), tag);

            return(EncodeProxyResponse(response));
        }