예제 #1
0
 private static void WriteHeaders(HttpHeaders headers, ref WritableBuffer buffer)
 {
     foreach (var header in headers)
     {
         buffer.Append($"{header.Key}:{string.Join(",", header.Value)}\r\n", TextEncoding.Utf8);
     }
 }
예제 #2
0
        protected override void WriteRecords(ref ReadableBuffer buffer, ref WritableBuffer writer, RecordType recordType)
        {
            ReadableBuffer append;

            while (buffer.Length > 0)
            {
                append = buffer.Slice(0, Math.Min(_maxMessageSize, buffer.Length));
                buffer = buffer.Slice(append.End);
                var recordHeader = new RecordHeader()
                {
                    RecordType = recordType,
                    Length     = (ushort)append.Length,
                    Version    = _recordVersion
                };
                writer.Ensure(_minimumMessageSize);
                if (_connection?.WriteKey != null)
                {
                    recordHeader.Length += (ushort)(8 + _connection.WriteKey.Overhead);
                }
                writer.Buffer.Span.Write(recordHeader);
                writer.Advance(_minimumMessageSize);
                if (_connection?.WriteKey != null)
                {
                    _connection.WriteKey.Encrypt(ref writer, append, recordType, _recordVersion);
                }
                else
                {
                    writer.Append(append);
                }
            }
        }
 /// <summary>
 /// Writes a string as a RedisBulkString to the Stream.
 /// </summary>
 /// <param name="str">The string to write.</param>
 static void WriteRedisBulkString(WritableBuffer output, Utf8String str)
 {
     output.Write(RedisProtocol.Utf8BulkStringStart);
     output.Append(str.Length, TextEncoder.Utf8);
     output.Write(str);
     output.Write(RedisProtocol.Utf8CRLF);
 }
예제 #4
0
        void IMessageWriter.WritePayload(WritableBuffer destination)
        {
            var buffers = this._buffers;

            if (buffers == null)
            {
                ApplyMask();
                destination.Append(_buffer);
            }
            else
            {
                foreach (var buffer in buffers)
                {
                    destination.Append(buffer.Buffer);
                }
            }
        }
예제 #5
0
        private static void AppendPayloadWriter(WritableBuffer output, Span <byte> maskingKey, int payloadLength, ReadableBuffer payload)
        {
            if (maskingKey.Length > 0)
            {
                // Mask the payload in it's own buffer
                MaskingUtilities.ApplyMask(ref payload, maskingKey);
            }

            output.Append(payload);
        }
        public void CopyTo(bool chunk, WritableBuffer buffer)
        {
            foreach (var header in _headers)
            {
                buffer.Write(_headersStartBytes);
                buffer.Append(header.Key, SymbolTable.InvariantUtf8);
                buffer.Write(_headersSeperatorBytes);
                buffer.Append(header.Value.ToString(), SymbolTable.InvariantUtf8);
            }

            if (chunk)
            {
                buffer.Write(_chunkedHeaderBytes);
            }

            buffer.Write(_serverHeaderBytes);
            var date = _dateHeaderValueManager.GetDateHeaderValues().Bytes;

            buffer.Write(date);

            buffer.Write(_headersEndBytes);
        }
예제 #7
0
        void IMessageWriter.WritePayload(WritableBuffer destination)
        {
            var buffers = this._buffers;

            if (buffers == null)
            {
                ApplyMask();
                destination.Append(ref _buffer);
            }
            else
            {
                // all this because C# doesn't let you use "ref" with an iterator variable
                using (var iter = buffers.GetEnumerator())
                {
                    ReadableBuffer tmp;
                    while (iter.MoveNext())
                    {
                        tmp = iter.Current;
                        destination.Append(ref tmp);
                    }
                }
            }
        }
예제 #8
0
        public static void WriteRecord(ref WritableBuffer buffer, RecordType recordType, ReadableBuffer plainText, State.IConnectionState state)
        {
            buffer.Ensure(RecordHeaderLength);
            if (state.WriteKey == null)
            {
                buffer.WriteBigEndian(recordType);
                buffer.WriteBigEndian(TlsRecordVersion);
                buffer.WriteBigEndian((ushort)plainText.Length);
                buffer.Append(plainText);
                return;
            }
            buffer.WriteBigEndian(RecordType.Application);
            buffer.WriteBigEndian(TlsRecordVersion);
            var totalSize = plainText.Length + state.WriteKey.Overhead + sizeof(RecordType);

            buffer.WriteBigEndian((ushort)totalSize);
            state.WriteKey.Encrypt(ref buffer, plainText, recordType);
        }
        /// <summary>
        /// Sends a command and it's parameters to the Stream.
        /// </summary>
        /// <param name="connection">The connection to the Redis Server.</param>
        /// <param name="command">The command.</param>
        /// <param name="parameters">The paramaters for the command.</param>
        public static async Task WriteCommandAsync(this RedisConnection connection,
                                                   string command,
                                                   IEnumerable <object> parameters = null)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            var            sizeOfCommandArray = 1 + (parameters?.Count() ?? 0);
            WritableBuffer output             = connection.Output.Alloc();

            // output the command array start
            output.Write(RedisProtocol.Utf8ArrayStart);
            output.Append(sizeOfCommandArray, TextEncoder.Utf8);
            output.Write(RedisProtocol.Utf8CRLF);

            // output the command
            var commandData = (Utf8String)command;

            WriteRedisBulkString(output, commandData);

            if (sizeOfCommandArray > 1)
            {
                foreach (object obj in parameters)
                {
                    WriteObject(output, obj);
                }
            }

            await output.FlushAsync();

            // TODO: should I call this?
            // connection.Output.Complete();
        }
        internal static unsafe SecurityStatus Decrypt <T>(this T context, ReadableBuffer buffer, WritableBuffer decryptedData) where T : ISecureContext
        {
            void *pointer;

            if (buffer.IsSingleSpan)
            {
                buffer.First.TryGetPointer(out pointer);
            }
            else
            {
                if (buffer.Length > SecurityContext.MaxStackAllocSize)
                {
                    throw new OverflowException($"We need to create a buffer on the stack of size {buffer.Length} but the max is {SecurityContext.MaxStackAllocSize}");
                }
                byte *      tmpBuffer = stackalloc byte[buffer.Length];
                Span <byte> span      = new Span <byte>(tmpBuffer, buffer.Length);
                buffer.CopyTo(span);
                pointer = tmpBuffer;
            }

            int offset = 0;
            int count  = buffer.Length;

            var secStatus = DecryptMessage(pointer, ref offset, ref count, context.ContextHandle);

            if (buffer.IsSingleSpan)
            {
                buffer = buffer.Slice(offset, count);
                decryptedData.Append(ref buffer);
            }
            else
            {
                decryptedData.Ensure(buffer.Length);
                decryptedData.Write(new Span <byte>(pointer, buffer.Length));
            }
            return(secStatus);
        }
 private static void WriteHeaders(HttpHeaders headers, ref WritableBuffer buffer)
 {
     foreach (var header in headers)
     {
         buffer.Append($"{header.Key}:{string.Join(",", header.Value)}\r\n", TextEncoding.Utf8);
     }
 }