text:
using System.Diagnostics;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Security
{
    //
    // This is a wrapping stream that does data encryption/decryption based on a successfully authenticated SSPI context.
    //
    internal class SslStreamInternal
    {
        private static readonly AsyncCallback s_writeCallback = new AsyncCallback(WriteCallback);
        private static readonly AsyncProtocolCallback s_resumeAsyncWriteCallback = new AsyncProtocolCallback(ResumeAsyncWriteCallback);
        private static readonly AsyncProtocolCallback s_resumeAsyncReadCallback = new AsyncProtocolCallback(ResumeAsyncReadCallback);
        private static readonly AsyncProtocolCallback s_readHeaderCallback = new AsyncProtocolCallback(ReadHeaderCallback);
        private static readonly AsyncProtocolCallback s_readFrameCallback = new AsyncProtocolCallback(ReadFrameCallback);

        private const int PinnableReadBufferSize = 4096 * 4 + 32;         // We read in 16K chunks + headers.
        private static PinnableBufferCache s_PinnableReadBufferCache = new PinnableBufferCache("System.Net.SslStream", PinnableReadBufferSize);
        private const int PinnableWriteBufferSize = 4096 + 1024;        // We write in 4K chunks + encryption overhead.
        private static PinnableBufferCache s_PinnableWriteBufferCache = new PinnableBufferCache("System.Net.SslStream", PinnableWriteBufferSize);

        private SslState _sslState;
        private int _nestedWrite;
        private int _nestedRead;
        private AsyncProtocolRequest _readProtocolRequest; // cached, reusable AsyncProtocolRequest used for read operations
        private AsyncProtocolRequest _writeProtocolRequest; // cached, reusable AsyncProtocolRequest used for write operations

        // Never updated directly, special properties are used.  This is the read buffer.
        private byte[] _internalBuffer;
        private bool _internalBufferFromPinnableCache;

        private byte[] _pinnableOutputBuffer;                        // Used for writes when we can do it.
        private byte[] _pinnableOutputBufferInUse;                   // Remembers what UNENCRYPTED buffer is using _PinnableOutputBuffer.

        private int _internalOffset;
        private int _internalBufferCount;

        internal SslStreamInternal(SslState sslState)
        {
            if (PinnableBufferCacheEventSource.Log.IsEnabled())
            {
                PinnableBufferCacheEventSource.Log.DebugMessage1("CTOR: In System.Net._SslStream.SslStream", this.GetHashCode());
            }

            _sslState = sslState;
        }

        // If we have a read buffer from the pinnable cache, return it.
        private void FreeReadBuffer()
        {
            if (_internalBufferFromPinnableCache)
            {
                s_PinnableReadBufferCache.FreeBuffer(_internalBuffer);
                _internalBufferFromPinnableCache = false;
            }

            _internalBuffer = null;
        }

        ~SslStreamInternal()
        {
            if (_internalBufferFromPinnableCache)
            {
                if (PinnableBufferCacheEventSource.Log.IsEnabled())
                {
                    PinnableBufferCacheEventSource.Log.DebugMessage2("DTOR: In System.Net._SslStream.~SslStream Freeing Read Buffer", this.GetHashCode(), PinnableBufferCacheEventSource.AddressOfByteArray(_internalBuffer));
                }

                FreeReadBuffer();
            }
            if (_pinnableOutputBuffer != null)
            {
                if (PinnableBufferCacheEventSource.Log.IsEnabled())
                {
                    PinnableBufferCacheEventSource.Log.DebugMessage2("DTOR: In System.Net._SslStream.~SslStream Freeing Write Buffer", this.GetHashCode(), PinnableBufferCacheEventSource.AddressOfByteArray(_pinnableOutputBuffer));
                }

                s_PinnableWriteBufferCache.FreeBuffer(_pinnableOutputBuffer);
            }
        }

        internal int ReadByte()
        {
            if (Interlocked.Exchange(ref _nestedRead, 1) == 1)
            {
                throw new NotSupportedException(SR.Format(SR.net_io_invalidnestedcall, "ReadByte", "read"));
            }

            // If there's any data in the buffer, take one byte, and we're done.
            try
            {
                if (InternalBufferCount > 0)
                {
                    int b = InternalBuffer[InternalOffset];
                    SkipBytes(1);
                    return b;
                }
            }
            finally
            {
                // Regardless of whether we were able to read a byte from the buffer,
                // reset the read tracking.  If we weren't able to read a byte, the
                // subsequent call to Read will set the flag again.
                _nestedRead = 0;
            }

            // Otherwise, fall back to reading a byte via Read, the same way Stream.ReadByte does.
            // This allocation is unfortunate but should be relatively rare, as it'll only occur once
            // per buffer fill internally by Read.
            byte[] oneByte = new byte[1];
            int bytesRead = Read(oneByte, 0, 1);
            Debug.Assert(bytesRead == 0 || bytesRead == 1);
            return bytesRead == 1 ? oneByte[0] : -1;
        }

        internal int Read(byte[] buffer, int offset, int count)
        {
            return ProcessRead(buffer, offset, count, null);
        }

        internal void Write(byte[] buffer, int offset, int count)
        {
            ProcessWrite(buffer, offset, count, null);
        }

        internal IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
        {
            var bufferResult = new BufferAsyncResult(this, buffer, offset, count, asyncState, asyncCallback);
            ProcessRead(buffer, offset, count, bufferResult);
            return bufferResult;
        }

        internal int EndRead(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }

            BufferAsyncResult bufferResult = asyncResult as BufferAsyncResult;
            if (bufferResult == null)
            {
                throw new ArgumentException(SR.Format(SR.net_io_async_result, asyncResult.GetType().FullName), nameof(asyncResult));
            }

            if (Interlocked.Exchange(ref _nestedRead, 0) == 0)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndRead"));
            }

            // No "artificial" timeouts implemented so far, InnerStream controls timeout.
            bufferResult.InternalWaitForCompletion();

            if (bufferResult.Result is Exception)
            {
                if (bufferResult.Result is IOException)
                {
                    throw (Exception)bufferResult.Result;
                }

                throw new IOException(SR.net_io_read, (Exception)bufferResult.Result);
            }

            return bufferResult.Int32Result;
        }

        internal IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
        {
            var lazyResult = new LazyAsyncResult(this, asyncState, asyncCallback);
            ProcessWrite(buffer, offset, count, lazyResult);
            return lazyResult;
        }

        internal void EndWrite(IAsyncResult asyncResult)
        {
            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }

            LazyAsyncResult lazyResult = asyncResult as LazyAsyncResult;
            if (lazyResult == null)
            {
                throw new ArgumentException(SR.Format(SR.net_io_async_result, asyncResult.GetType().FullName), nameof(asyncResult));
            }

            if (Interlocked.Exchange(ref _nestedWrite, 0) == 0)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndWrite"));
            }

            // No "artificial" timeouts implemented so far, InnerStream controls timeout.
            lazyResult.InternalWaitForCompletion();

            if (lazyResult.Result is Exception e)
            {
                if (e is IOException)
                {
                    ExceptionDispatchInfo.Capture(e).Throw();
                }

                throw new IOException(SR.net_io_write, e);
            }
        }

        internal bool DataAvailable
        {
            get { return InternalBufferCount != 0; }
        }

        private byte[] InternalBuffer
        {
            get
            {
                return _internalBuffer;
            }
        }

        private int InternalOffset
        {
            get
            {
                return _internalOffset;
            }
        }

        private int InternalBufferCount
        {
            get
            {
                return _internalBufferCount;
            }
        }

        private void SkipBytes(int decrCount)
        {
            _internalOffset += decrCount;
            _internalBufferCount -= decrCount;
        }

        //
        // This will set the internal offset to "curOffset" and ensure internal buffer.
        // If not enough, reallocate and copy up to "curOffset".
        //
        private void EnsureInternalBufferSize(int curOffset, int addSize)
        {
            if (_internalBuffer == null || _internalBuffer.Length < addSize + curOffset)
            {
                bool wasPinnable = _internalBufferFromPinnableCache;
                byte[] saved = _internalBuffer;

                int newSize = addSize + curOffset;
                if (newSize <= PinnableReadBufferSize)
                {
                    if (PinnableBufferCacheEventSource.Log.IsEnabled())
                    {
                        PinnableBufferCacheEventSource.Log.DebugMessage2("In System.Net._SslStream.EnsureInternalBufferSize IS pinnable", this.GetHashCode(), newSize);
                    }

                    _internalBufferFromPinnableCache = true;
                    _internalBuffer = s_PinnableReadBufferCache.AllocateBuffer();
                }
                else
                {
                    if (PinnableBufferCacheEventSource.Log.IsEnabled())
                    {
                        PinnableBufferCacheEventSource.Log.DebugMessage2("In System.Net._SslStream.EnsureInternalBufferSize NOT pinnable", this.GetHashCode(), newSize);
                    }

                    _internalBufferFromPinnableCache = false;
                    _internalBuffer = new byte[newSize];
                }

                if (saved != null && curOffset != 0)
                {
                    Buffer.BlockCopy(saved, 0, _internalBuffer, 0, curOffset);
                }

                if (wasPinnable)
                {
                    s_PinnableReadBufferCache.FreeBuffer(saved);
                }
            }
            _internalOffset = curOffset;
            _internalBufferCount = curOffset + addSize;
        }

        //
        // Validates user parameters for all Read/Write methods.
        //
        private void ValidateParameters(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }

            if (count > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.net_offset_plus_count);
            }
        }

        private AsyncProtocolRequest GetOrCreateProtocolRequest(ref AsyncProtocolRequest aprField, LazyAsyncResult asyncResult)
        {
            AsyncProtocolRequest request = null;
            if (asyncResult != null)
            {
                // SslStreamInternal supports only a single read and a single write operation at a time.
                // As such, we can cache and reuse the AsyncProtocolRequest object that's used throughout
                // the implementation.
                request = aprField;
                if (request != null)
                {
                    request.Reset(asyncResult);
                }
                else
                {
                    aprField = request = new AsyncProtocolRequest(asyncResult);
                }
            }
            return request;
        }

        //
        // Sync write method.
        //
        private void ProcessWrite(byte[] buffer, int offset, int count, LazyAsyncResult asyncResult)
        {
            _sslState.CheckThrow(authSuccessCheck:true, shutdownCheck:true);
            ValidateParameters(buffer, offset, count);

            if (Interlocked.Exchange(ref _nestedWrite, 1) == 1)
            {
                throw new NotSupportedException(SR.Format(SR.net_io_invalidnestedcall, "Write", "write"));
            }

            // If this is an async operation, get the AsyncProtocolRequest to use.
            // We do this only after we verify we're the sole write operation in flight.
            AsyncProtocolRequest asyncRequest = GetOrCreateProtocolRequest(ref _writeProtocolRequest, asyncResult);

            bool failed = false;

            try
            {
                StartWriting(buffer, offset, count, asyncRequest);
            }
            catch (Exception e)
            {
                _sslState.FinishWrite();

                failed = true;
                if (e is IOException)
                {
                    throw;
                }

                throw new IOException(SR.net_io_write, e);
            }
            finally
            {
                if (asyncRequest == null || failed)
                {
                    _nestedWrite = 0;
                }
            }
        }

        private void StartWriting(byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
        {
            if (asyncRequest != null)
            {
                asyncRequest.SetNextRequest(buffer, offset, count, s_resumeAsyncWriteCallback);
            }

            // We loop to this method from the callback.
            // If the last chunk was just completed from async callback (count < 0), we complete user request.
            if (count >= 0 )
            {
                byte[] outBuffer = null;
                if (_pinnableOutputBufferInUse == null)
                {
                    if (_pinnableOutputBuffer == null)
                    {
                        _pinnableOutputBuffer = s_PinnableWriteBufferCache.AllocateBuffer();
                    }

                    _pinnableOutputBufferInUse = buffer;
                    outBuffer = _pinnableOutputBuffer;
                    if (PinnableBufferCacheEventSource.Log.IsEnabled())
                    {
                        PinnableBufferCacheEventSource.Log.DebugMessage3("In System.Net._SslStream.StartWriting Trying Pinnable", this.GetHashCode(), count, PinnableBufferCacheEventSource.AddressOfByteArray(outBuffer));
                    }
                }
                else
                {
                    if (PinnableBufferCacheEventSource.Log.IsEnabled())
                    {
                        PinnableBufferCacheEventSource.Log.DebugMessage2("In System.Net._SslStream.StartWriting BufferInUse", this.GetHashCode(), count);
                    }
                }

                do
                {
                    if (count == 0 && !SslStreamPal.CanEncryptEmptyMessage)
                    {
                        // If it's an empty message and the PAL doesn't support that,
                        // we're done.
                        break;
                    }

                    // Request a write IO slot.
                    if (_sslState.CheckEnqueueWrite(asyncRequest))
                    {
                        // Operation is async and has been queued, return.
                        return;
                    }

                    int chunkBytes = Math.Min(count, _sslState.MaxDataSize);
                    int encryptedBytes;
                    SecurityStatusPal status = _sslState.EncryptData(buffer, offset, chunkBytes, ref outBuffer, out encryptedBytes);
                    if (status.ErrorCode != SecurityStatusPalErrorCode.OK)
                    {
                        // Re-handshake status is not supported.
                        ProtocolToken message = new ProtocolToken(null, status);
                        throw new IOException(SR.net_io_encrypt, message.GetException());
                    }

                    if (PinnableBufferCacheEventSource.Log.IsEnabled())
                    {
                        PinnableBufferCacheEventSource.Log.DebugMessage3("In System.Net._SslStream.StartWriting Got Encrypted Buffer",
                            this.GetHashCode(), encryptedBytes, PinnableBufferCacheEventSource.AddressOfByteArray(outBuffer));
                    }

                    if (asyncRequest != null)
                    {
                        // Prepare for the next request.
                        asyncRequest.SetNextRequest(buffer, offset + chunkBytes, count - chunkBytes, s_resumeAsyncWriteCallback);
                        Task t = _sslState.InnerStream.WriteAsync(outBuffer, 0, encryptedBytes);
                        if (t.IsCompleted)
                        {
                            t.GetAwaiter().GetResult();
                        }
                        else
                        {
                            IAsyncResult ar = TaskToApm.Begin(t, s_writeCallback, asyncRequest);
                            if (!ar.CompletedSynchronously)
                            {
                                return;
                            }
                            TaskToApm.End(ar);
                        }
                    }
                    else
                    {
                        _sslState.InnerStream.Write(outBuffer, 0, encryptedBytes);
                    }

                    offset += chunkBytes;
                    count -= chunkBytes;

                    // Release write IO slot.
                    _sslState.FinishWrite();

                } while (count != 0);
            }

            if (asyncRequest != null)
            {
                asyncRequest.CompleteUser();
            }

            if (buffer == _pinnableOutputBufferInUse)
            {
                _pinnableOutputBufferInUse = null;
                if (PinnableBufferCacheEventSource.Log.IsEnabled())
                {
                    PinnableBufferCacheEventSource.Log.DebugMessage1("In System.Net._SslStream.StartWriting Freeing buffer.", this.GetHashCode());
                }
            }
        }

        //
        // Combined sync/async read method. For sync request asyncRequest==null.
        //
        private int ProcessRead(byte[] buffer, int offset, int count, BufferAsyncResult asyncResult)
        {
            ValidateParameters(buffer, offset, count);

            if (Interlocked.Exchange(ref _nestedRead, 1) == 1)
            {
                throw new NotSupportedException(SR.Format(SR.net_io_invalidnestedcall, (asyncResult!=null? "BeginRead":"Read"), "read"));
            }

            // If this is an async operation, get the AsyncProtocolRequest to use.
            // We do this only after we verify we're the sole write operation in flight.
            AsyncProtocolRequest asyncRequest = GetOrCreateProtocolRequest(ref _readProtocolRequest, asyncResult);

            bool failed = false;

            try
            {
                int copyBytes;
                if (InternalBufferCount != 0)
                {
                    copyBytes = InternalBufferCount > count ? count : InternalBufferCount;
                    if (copyBytes != 0)
                    {
                        Buffer.BlockCopy(InternalBuffer, InternalOffset, buffer, offset, copyBytes);
                        SkipBytes(copyBytes);
                    }

                    asyncRequest?.CompleteUser(copyBytes);

                    return copyBytes;
                }

                return StartReading(buffer, offset, count, asyncRequest);
            }
            catch (Exception e)
            {
                _sslState.FinishRead(null);
                failed = true;

                if (e is IOException)
                {
                    throw;
                }

                throw new IOException(SR.net_io_read, e);
            }
            finally
            {
                if (asyncRequest == null || failed)
                {
                    _nestedRead = 0;
                }
            }
        }

        //
        // To avoid recursion when decrypted 0 bytes this method will loop until a decrypted result at least 1 byte.
        //
        private int StartReading(byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
        {
            int result = 0;

            if (InternalBufferCount != 0)
            {
                NetEventSource.Fail(this, $"Previous frame was not consumed. InternalBufferCount: {InternalBufferCount}");
            }

            do
            {
                if (asyncRequest != null)
                {
                    asyncRequest.SetNextRequest(buffer, offset, count, s_resumeAsyncReadCallback);
                }

                int copyBytes = _sslState.CheckEnqueueRead(buffer, offset, count, asyncRequest);
                if (copyBytes == 0)
                {
                    // Queued but not completed!
                    return 0;
                }

                if (copyBytes != -1)
                {
                    asyncRequest?.CompleteUser(copyBytes);

                    return copyBytes;
                }
            }

            // When we read -1 bytes means we have decrypted 0 bytes or rehandshaking, need looping.
            while ((result = StartFrameHeader(buffer, offset, count, asyncRequest)) == -1);

            return result;
        }

        private int StartFrameHeader(byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
        {
            int readBytes = 0;

            //
            // Always pass InternalBuffer for SSPI "in place" decryption.
            // A user buffer can be shared by many threads in that case decryption/integrity check may fail cause of data corruption.
            //

            // Reset internal buffer for a new frame.
            EnsureInternalBufferSize(0, SecureChannel.ReadHeaderSize);

            if (asyncRequest != null)
            {
                asyncRequest.SetNextRequest(InternalBuffer, 0, SecureChannel.ReadHeaderSize, s_readHeaderCallback);
                FixedSizeReader.ReadPacketAsync(_sslState.InnerStream, asyncRequest);

                if (!asyncRequest.MustCompleteSynchronously)
                {
                    return 0;
                }

                readBytes = asyncRequest.Result;
            }
            else
            {
                readBytes = FixedSizeReader.ReadPacket(_sslState.InnerStream, InternalBuffer, 0, SecureChannel.ReadHeaderSize);
            }

            return StartFrameBody(readBytes, buffer, offset, count, asyncRequest);
        }

        private int StartFrameBody(int readBytes, byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
        {
            if (readBytes == 0)
            {
                //EOF : Reset the buffer as we did not read anything into it.
                SkipBytes(InternalBufferCount);
                asyncRequest?.CompleteUser(0);

                return 0;
            }

            // Now readBytes is a payload size.
            readBytes = _sslState.GetRemainingFrameSize(InternalBuffer, readBytes);

            if (readBytes < 0)
            {
                throw new IOException(SR.net_frame_read_size);
            }

            EnsureInternalBufferSize(SecureChannel.ReadHeaderSize, readBytes);

            if (asyncRequest != null)
            {
                asyncRequest.SetNextRequest(InternalBuffer, SecureChannel.ReadHeaderSize, readBytes, s_readFrameCallback);

                FixedSizeReader.ReadPacketAsync(_sslState.InnerStream, asyncRequest);

                if (!asyncRequest.MustCompleteSynchronously)
                {
                    return 0;
                }

                readBytes = asyncRequest.Result;
            }
            else
            {
                readBytes = FixedSizeReader.ReadPacket(_sslState.InnerStream, InternalBuffer, SecureChannel.ReadHeaderSize, readBytes);
            }

            return ProcessFrameBody(readBytes, buffer, offset, count, asyncRequest);
        }

        //
        // readBytes == SSL Data Payload size on input or 0 on EOF.
        //
        private int ProcessFrameBody(int readBytes, byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest)
        {
            if (readBytes == 0)
            {
                // EOF
                throw new IOException(SR.net_io_eof);
            }

            // Set readBytes to total number of received bytes.
            readBytes += SecureChannel.ReadHeaderSize;

            // Decrypt into internal buffer, change "readBytes" to count now _Decrypted Bytes_.
            int data_offset = 0;

            SecurityStatusPal status = _sslState.DecryptData(InternalBuffer, ref data_offset, ref readBytes);

            if (status.ErrorCode != SecurityStatusPalErrorCode.OK)
            {
                byte[] extraBuffer = null;
                if (readBytes != 0)
                {
                    extraBuffer = new byte[readBytes];
                    Buffer.BlockCopy(InternalBuffer, data_offset, extraBuffer, 0, readBytes);
                }

                // Reset internal buffer count.
                SkipBytes(InternalBufferCount);
                return ProcessReadErrorCode(status, buffer, offset, count, asyncRequest, extraBuffer);
            }

            if (readBytes == 0 && count != 0)
            {
                // Read again since remote side has sent encrypted 0 bytes.
                SkipBytes(InternalBufferCount);
                return -1;
            }

            // Decrypted data start from "data_offset" offset, the total count can be shrunk after decryption.
            EnsureInternalBufferSize(0, data_offset + readBytes);
            SkipBytes(data_offset);

            if (readBytes > count)
            {
                readBytes = count;
            }

            Buffer.BlockCopy(InternalBuffer, InternalOffset, buffer, offset, readBytes);

            // This will adjust both the remaining internal buffer count and the offset.
            SkipBytes(readBytes);

            _sslState.FinishRead(null);
            asyncRequest?.CompleteUser(readBytes);

            return readBytes;
        }

        private int ProcessReadErrorCode(SecurityStatusPal status, byte[] buffer, int offset, int count, AsyncProtocolRequest asyncRequest, byte[] extraBuffer)
        {
            ProtocolToken message = new ProtocolToken(null, status);
            if (NetEventSource.IsEnabled) NetEventSource.Info(null, $"***Processing an error Status = {message.Status}");

            if (message.Renegotiate)
            {
                _sslState.ReplyOnReAuthentication(extraBuffer);

                // Loop on read.
                return -1;
            }

            if (message.CloseConnection)
            {
                _sslState.FinishRead(null);
                asyncRequest?.CompleteUser(0);

                return 0;
            }

            throw new IOException(SR.net_io_decrypt, message.GetException());
        }

        private static void WriteCallback(IAsyncResult transportResult)
        {
            if (transportResult.CompletedSynchronously)
            {
                return;
            }

            if (!(transportResult.AsyncState is AsyncProtocolRequest))
            {
                NetEventSource.Fail(transportResult, "State type is wrong, expected AsyncProtocolRequest.");
            }

            AsyncProtocolRequest asyncRequest = (AsyncProtocolRequest)transportResult.AsyncState;

            var sslStream = (SslStreamInternal)asyncRequest.AsyncObject;

            try
            {
                TaskToApm.End(transportResult);
                sslStream._sslState.FinishWrite();

                if (asyncRequest.Count == 0)
                {
                    // This was the last chunk.
                    asyncRequest.Count = -1;
                }

                sslStream.StartWriting(asyncRequest.Buffer, asyncRequest.Offset, asyncRequest.Count, asyncRequest);
            }
            catch (Exception e)
            {
                if (asyncRequest.IsUserCompleted)
                {
                    // This will throw on a worker thread.
                    throw;
                }

                sslStream._sslState.FinishWrite();
                asyncRequest.CompleteUserWithError(e);
            }
        }

        //
        // This is used in a rare situation when async Read is resumed from completed handshake.
        //
        private static void ResumeAsyncReadCallback(AsyncProtocolRequest request)
        {
            try
            {
                ((SslStreamInternal)request.AsyncObject).StartReading(request.Buffer, request.Offset, request.Count, request);
            }
            catch (Exception e)
            {
                if (request.IsUserCompleted)
                {
                    // This will throw on a worker thread.
                    throw;
                }

                ((SslStreamInternal)request.AsyncObject)._sslState.FinishRead(null);
                request.CompleteUserWithError(e);
            }
        }

        //
        // This is used in a rare situation when async Write is resumed from completed handshake.
        //
        private static void ResumeAsyncWriteCallback(AsyncProtocolRequest asyncRequest)
        {
            try
            {
                ((SslStreamInternal)asyncRequest.AsyncObject).StartWriting(asyncRequest.Buffer, asyncRequest.Offset, asyncRequest.Count, asyncRequest);
            }
            catch (Exception e)
            {
                if (asyncRequest.IsUserCompleted)
                {
                    // This will throw on a worker thread.
                    throw;
                }

                ((SslStreamInternal)asyncRequest.AsyncObject)._sslState.FinishWrite();
                asyncRequest.CompleteUserWithError(e);
            }
        }

        private static void ReadHeaderCallback(AsyncProtocolRequest asyncRequest)
        {
            try
            {
                SslStreamInternal sslStream = (SslStreamInternal)asyncRequest.AsyncObject;
                BufferAsyncResult bufferResult = (BufferAsyncResult)asyncRequest.UserAsyncResult;
                if (-1 == sslStream.StartFrameBody(asyncRequest.Result, bufferResult.Buffer, bufferResult.Offset, bufferResult.Count, asyncRequest))
                {
                    // in case we decrypted 0 bytes start another reading.
                    sslStream.StartReading(bufferResult.Buffer, bufferResult.Offset, bufferResult.Count, asyncRequest);
                }
            }
            catch (Exception e)
            {
                if (asyncRequest.IsUserCompleted)
                {
                    // This will throw on a worker thread.
                    throw;
                }

                asyncRequest.CompleteUserWithError(e);
            }
        }

        private static void ReadFrameCallback(AsyncProtocolRequest asyncRequest)
        {
            try
            {
                SslStreamInternal sslStream = (SslStreamInternal)asyncRequest.AsyncObject;
                BufferAsyncResult bufferResult = (BufferAsyncResult)asyncRequest.UserAsyncResult;
                if (-1 == sslStream.ProcessFrameBody(asyncRequest.Result, bufferResult.Buffer, bufferResult.Offset, bufferResult.Count, asyncRequest))
                {
                    // in case we decrypted 0 bytes start another reading.
                    sslStream.StartReading(bufferResult.Buffer, bufferResult.Offset, bufferResult.Count, asyncRequest);
                }
            }
            catch (Exception e)
            {
                if (asyncRequest.IsUserCompleted)
                {
                    // This will throw on a worker thread.
                    throw;
                }

                asyncRequest.CompleteUserWithError(e);
            }
        }
    }
}


text:
package twitterapi

type DirectMessage struct {
	CreatedAt           string   `json:"created_at"`
	Entities            Entities `json:"entities"`
	Id                  int64    `json:"id"`
	IdStr               string   `json:"id_str"`
	Recipient           User     `json:"recipient"`
	RecipientId         int64    `json:"recipient_id"`
	RecipientScreenName string   `json:"recipient_screen_name"`
	Sender              User     `json:"sender"`
	SenderId            int64    `json:"sender_id"`
	SenderScreenName    string   `json:"sender_screen_name"`
	Text                string   `json:"text"`
}

redpajama_set_name: RedPajamaGithub


text:
package org.apache.velocity.test.provider;

/**
 * Used to confirm that a null to string is processed properly
 * @author Will Glass-Husain
 * @version $Id$
 */
public class NullToStringObject
{
    public String toString()
    {
        return null;
    }
}


text:
==============================
Travis-based Container Builder
==============================

This set of fabric tasks automate the task of building of a Docker container
provided a DVCS repository that contains a ``.travis.yml`` file. The resulting
container will be configured to run the main script as the Docker entrypoint.
It can then save the resulting container as a tarball, upload it to S3, etc.

Installing
==========

This script can run locally and use a local docker to build the container, or
provision a remote AWS instance and build the container there. It can also use
an existing remote instance that already has the necessary commands in place.

Requirements on the host running the fabric commands:

1. scp
2. sshd
3. docker
4. git

Requirements to run fabric:

1. Python
2. fabric

Usage
=====

To provision a remote machine to build the container on (You must have AWS
config where boto expects it):

.. code-block:: bash

    > fab provision

Create a container based on a repository and commit:

.. code-block:: bash

    > fab REPO_URL REPO_COMMIT


text:
require 'test_helper'

class Superadmin::UsersControllerTest < ActionController::TestCase

  def test_index_when_user_is_superadmin
    user = users :admin
    sign_in user
    get :index
    assert_response :success
  end

  def test_index_when_user_is_not_superadmin
    user = users :nancy
    sign_in user
    get :index
    assert_response :forbidden
  end

end


text:
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics.Contracts;
using System;

// Disable the "this variable is not used" warning as every field would imply it.
#pragma warning disable 0414
// Disable the "this variable is never assigned to".
#pragma warning disable 0067
// Disable the "this event is never assigned to".
#pragma warning disable 0649
// Disable the "this variable is never used".
#pragma warning disable 0169
// Disable the "new keyword not required" warning.
#pragma warning disable 0109
// Disable the "extern without DllImport" warning.
#pragma warning disable 0626
// Disable the "could hide other member" warning, can happen on certain properties.
#pragma warning disable 0108

namespace System.IO
{
  public partial class BinaryReader : IDisposable
  {
    #region Methods and constructors
    public BinaryReader(Stream input)
    {
      Contract.Ensures(input.CanRead == true);
    }

    public BinaryReader(Stream input, Encoding encoding)
    {
      Contract.Ensures(input.CanRead == true);
    }

    public virtual new void Close()
    {
    }

    public void Dispose()
    {
    }

    protected virtual new void Dispose(bool disposing)
    {
    }

    protected virtual new void FillBuffer(int numBytes)
    {
    }

    public virtual new int PeekChar()
    {
      return default(int);
    }

    public virtual new int Read(char[] buffer, int index, int count)
    {
      return default(int);
    }

    public virtual new int Read(byte[] buffer, int index, int count)
    {
      return default(int);
    }

    public virtual new int Read()
    {
      return default(int);
    }

    protected internal int Read7BitEncodedInt()
    {
      return default(int);
    }

    public virtual new bool ReadBoolean()
    {
      return default(bool);
    }

    public virtual new byte ReadByte()
    {
      return default(byte);
    }

    public virtual new byte[] ReadBytes(int count)
    {
      return default(byte[]);
    }

    public virtual new char ReadChar()
    {
      return default(char);
    }

    public virtual new char[] ReadChars(int count)
    {
      return default(char[]);
    }

    public virtual new Decimal ReadDecimal()
    {
      return default(Decimal);
    }

    public virtual new double ReadDouble()
    {
      return default(double);
    }

    public virtual new short ReadInt16()
    {
      return default(short);
    }

    public virtual new int ReadInt32()
    {
      return default(int);
    }

    public virtual new long ReadInt64()
    {
      return default(long);
    }

    public virtual new sbyte ReadSByte()
    {
      return default(sbyte);
    }

    public virtual new float ReadSingle()
    {
      return default(float);
    }

    public virtual new string ReadString()
    {
      return default(string);
    }

    public virtual new ushort ReadUInt16()
    {
      return default(ushort);
    }

    public virtual new uint ReadUInt32()
    {
      return default(uint);
    }

    public virtual new ulong ReadUInt64()
    {
      return default(ulong);
    }
    #endregion

    #region Properties and indexers
    public virtual new Stream BaseStream
    {
      get
      {
        return default(Stream);
      }
    }
    #endregion
  }
}


text:
package com.onlyoffice.integration.services.configurers.implementations;

import com.onlyoffice.integration.documentserver.models.filemodel.Document;
import com.onlyoffice.integration.documentserver.models.filemodel.Permission;
import com.onlyoffice.integration.documentserver.storage.FileStoragePathBuilder;
import com.onlyoffice.integration.services.configurers.DocumentConfigurer;
import com.onlyoffice.integration.services.configurers.wrappers.DefaultDocumentWrapper;
import com.onlyoffice.integration.documentserver.managers.document.DocumentManager;
import com.onlyoffice.integration.documentserver.util.file.FileUtility;
import com.onlyoffice.integration.documentserver.util.service.ServiceConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
@Primary
public class DefaultDocumentConfigurer implements DocumentConfigurer<DefaultDocumentWrapper> {

    @Autowired
    private DocumentManager documentManager;

    @Autowired
    private FileStoragePathBuilder storagePathBuilder;

    @Autowired
    private FileUtility fileUtility;

    @Autowired
    private ServiceConverter serviceConverter;

    public void configure(Document document, DefaultDocumentWrapper wrapper){  // define the document configurer
        String fileName = wrapper.getFileName();  // get the fileName parameter from the document wrapper
        Permission permission = wrapper.getPermission();  // get the permission parameter from the document wrapper

        document.setTitle(fileName);  // set the title to the document config
        document.setUrl(documentManager.getDownloadUrl(fileName, true));  // set the URL to download a file to the document config
        document.setUrlUser(documentManager.getFileUri(fileName, false));  // set the file URL to the document config
        document.setDirectUrl(documentManager.getDownloadUrl(fileName, false));
        document.setFileType(fileUtility.getFileExtension(fileName).replace(".",""));  // set the file type to the document config
        document.getInfo().setFavorite(wrapper.getFavorite());  // set the favorite parameter to the document config

        String key =  serviceConverter.  // get the document key
                        generateRevisionId(storagePathBuilder.getStorageLocation()
                        + "/" + fileName + "/"
                        + new File(storagePathBuilder.getFileLocation(fileName)).lastModified());

        document.setKey(key);  // set the key to the document config
        document.setPermissions(permission);  // set the permission parameters to the document config
    }
}


text:
"""Tests for compile_db."""

import sys
import unittest

import compile_db

# Input compile DB.
_TEST_COMPILE_DB = [
    # Verifies that gomacc.exe is removed.
    {
        'command': r'C:\gomacc.exe C:\clang-cl.exe /blah',
    },
    # Verifies a goma path containing a space.
    {
        'command': r'"C:\Program Files\gomacc.exe" C:\clang-cl.exe /blah',
    },
    # Includes a string define.
    {
        'command': r'clang-cl.exe /blah "-DCR_CLANG_REVISION=\"346388-1\""',
    },
    # Includes a string define with a space in it.
    {
        'command': r'clang-cl.exe /blah -D"MY_DEFINE=\"MY VALUE\""',
    },
]

# Expected compile DB after processing for windows.
_EXPECTED_COMPILE_DB = [
    {
        'command': r'C:\clang-cl.exe --driver-mode=cl /blah',
    },
    {
        'command': r'C:\clang-cl.exe --driver-mode=cl /blah',
    },
    {
        'command': r'clang-cl.exe --driver-mode=cl /blah '
                   r'"-DCR_CLANG_REVISION=\"346388-1\""',
    },
    {
        'command': r'clang-cl.exe --driver-mode=cl /blah '
                   r'-D"MY_DEFINE=\"MY VALUE\""',
    },
]

class CompileDbTest(unittest.TestCase):

  def setUp(self):
    self.maxDiff = None

  def testProcessNotOnWindows(self):
    sys.platform = 'linux2'
    processed_compile_db = compile_db.ProcessCompileDatabase(
        _TEST_COMPILE_DB, [])

    # Assert no changes were made.
    try:
      # assertItemsEqual is renamed assertCountEqual in Python3.
      self.assertCountEqual(processed_compile_db, _TEST_COMPILE_DB)
    except AttributeError:
      self.assertItemsEqual(processed_compile_db, _TEST_COMPILE_DB)

  def testProcessForWindows_HostPlatformBased(self):
    sys.platform = 'win32'
    processed_compile_db = compile_db.ProcessCompileDatabase(
        _TEST_COMPILE_DB, [])

    # Check each entry individually to improve readability of the output.
    for actual, expected in zip(processed_compile_db, _EXPECTED_COMPILE_DB):
      self.assertDictEqual(actual, expected)

  def testProcessForWindows_TargetOsBased(self):
    sys.platform = 'linux2'
    processed_compile_db = compile_db.ProcessCompileDatabase(_TEST_COMPILE_DB,
                                                             [],
                                                             target_os='win')

    # Check each entry individually to improve readability of the output.
    for actual, expected in zip(processed_compile_db, _EXPECTED_COMPILE_DB):
      self.assertDictEqual(actual, expected)

  def testFrontendArgsFiltered(self):
    sys.platform = 'linux2'
    input_db = [{
        'command':
        r'clang -g -Xclang -fuse-ctor-homing -funroll-loops test.cc'
    }]
    self.assertEquals(compile_db.ProcessCompileDatabase(input_db, []),
                      [{
                          'command': r'clang -g -funroll-loops test.cc'
                      }])

  def testFilterArgs(self):
    sys.platform = 'linux2'
    input_db = [{'command': r'clang -g -ffile-compilation-dir=. -O3 test.cc'}]
    self.assertEquals(
        compile_db.ProcessCompileDatabase(
            input_db,
            ['-ffile-compilation-dir=.', '-frandom-flag-that-does-not-exist']),
        [{
            'command': r'clang -g -O3 test.cc'
        }])

if __name__ == '__main__':
  unittest.main()


text:
import Ember from 'ember';
import C from 'ui/utils/constants';

export default Ember.Route.extend({
  model: function(params/*, transition*/) {
    var store = this.get('store');

    var dependencies = [
      store.findAll('host'), // Need inactive ones in case a link points to an inactive host
    ];

    if ( params.containerId )
    {
      dependencies.pushObject(store.find('container', params.containerId, {include: ['ports','instanceLinks']}));
    }

    return Ember.RSVP.all(dependencies, 'Load container dependencies').then(function(results) {
      var allHosts = results[0];

      var data, healthCheckData;
      if ( params.containerId )
      {
        data = results[1].serializeForNew();
        data.ports = (data.ports||[]).map((port) => {
          delete port.id;
          return port;
        });

        if ( Ember.isArray(data.instanceLinks) )
        {
          data.instanceLinks = (data.instanceLinks||[]).map((link) => {
            delete link.id;
            return link;
          });
        }

        healthCheckData = data.healthCheck;
        delete  data.healthCheck;
      }
      else
      {
        data = {
          type: 'container',
          requestedHostId: params.hostId,
          tty: true,
          stdinOpen: true,
          labels: { [C.LABEL.PULL_IMAGE]: C.LABEL.PULL_IMAGE_VALUE },
        };
      }

      var instance = store.createRecord(data);
      if ( healthCheckData )
      {
        // The type isn't set on an existing one
        healthCheckData.type = 'instanceHealthCheck';
        instance.set('healthCheck', store.createRecord(healthCheckData));
      }

      return Ember.Object.create({
        instance: instance,
        allHosts: allHosts,
      });
    });
  },

  resetController: function (controller, isExiting/*, transition*/) {
    if (isExiting)
    {
      controller.set('hostId', null);
      controller.set('environmentId', null);
      controller.set('containerId', null);
    }
  }
});


text:
<?php
$this->load->view('Genericas/mensajes');
?>

<?php
if (!empty($listanotificaciones)) {
    ?>

Notificaciones

                <?php
                foreach ($listanotificaciones->result() as $notificacion) {
                    ?>
                    <button type="button" class="list-group-item"><?= $notificacion->mensaje; ?></button>
                    <?php
                }
                ?>

    <?php
}
?>

<?php
if (!empty($listarevision)) {
    ?>

Revisiones pendientes

                <?php
                foreach ($listarevision->result() as $revision) {
                    ?>
                    <a href="<?= base_url() ?>banco/bancoIdeas/<?= $revision->id_grupo; ?>" class="list-group-item">Tiene ideas pendientes por aprobar del grupo <?= $revision->id_grupo; ?>
                    <?php
                }
                ?>

    <?php
}
?>
<?php
if (!empty($devueltos)) {
    ?>

Devoluciones

    <?php
    foreach ($devueltos->result() as $notificacion) {
        ?>
                    <a href="<?= base_url() ?>ideas/gestionVersion/<?= $notificacion->id_idea; ?>/<?= $notificacion->id_entregable; ?>/<?= $notificacion->id_version; ?>" class="list-group-item">Fue devuelta una version generada
                    <?php
                }
                ?>

    <?php
}
if (!empty($pendientesrevision)) {
    ?>

Notificaciones

    <?php
    foreach ($pendientesrevision->result() as $pendiente) {
        ?>
                <a href="<?= base_url() ?>revision/misPendientes/<?= $pendiente->grupo; ?>" class="list-group-item">Tiene versiones pendientes por revisar en el Grupo <?= $pendiente->grupo; ?>
                    <?php
                }
                ?>

                <?php
            }
            ?>

<?php
if (!empty($paratrabajar)) {
    ?>

Idea Aprobada

    <?php
    foreach ($paratrabajar->result() as $notificacion) {
        ?>
                    <a href="<?= base_url() ?>ideas/mostrarMarco/<?= $notificacion->id_idea; ?>" class="list-group-item">Fue aprobada su idea. Puede empezar a desarrollarla
                    <?php
                }
                ?>

    <?php
}
?>
<img src="<?= base_url() ?>application/asset/img/baner1.png" class="img-fluid">

<fieldset>
    <legend>¿Tiene una Idea ?</legend>
    Si usted tiene una idea para ser utilizada le recomendamos  agregar una nueva idea a nuestro banco de ideas

<a href="<?= base_url() ?>ideas/registrobanco" class="btn btn-warning">REGISTRAR UNA IDEA PARA EL BANCO
</fieldset>


text:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ro.pub.cs.systems.pdsd.examples.multimediasamples"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />

    <uses-permission
        android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MultimediaSamplesActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


text:
namespace password_manager {

// The sync- and GAIA- aware implementation of the filter.
// TODO(vabr): Rename this to match the interface.
class SyncStoreResultFilter : public CredentialsFilter {
 public:
  typedef base::Callback<const sync_driver::SyncService*(void)>
      SyncServiceFactoryFunction;
  typedef base::Callback<const SigninManagerBase*(void)>
      SigninManagerFactoryFunction;

  // Implements protection of sync credentials. Uses |client| to get the last
  // commited entry URL for a check against GAIA reauth site. Uses the factory
  // functions repeatedly to get the sync service and signin manager to pass
  // them to sync_util methods.
  // TODO(vabr): Could we safely just get a pointer to the services for the
  // lifetime of the filter?
  SyncStoreResultFilter(
      const PasswordManagerClient* client,
      SyncServiceFactoryFunction sync_service_factory_function,
      SigninManagerFactoryFunction signin_manager_factory_function);
  ~SyncStoreResultFilter() override;

  // CredentialsFilter
  ScopedVector<autofill::PasswordForm> FilterResults(
      ScopedVector<autofill::PasswordForm> results) const override;
  bool ShouldSave(const autofill::PasswordForm& form) const override;
  void ReportFormUsed(const autofill::PasswordForm& form) const override;

 private:
  enum AutofillForSyncCredentialsState {
    ALLOW_SYNC_CREDENTIALS,
    DISALLOW_SYNC_CREDENTIALS_FOR_REAUTH,
    DISALLOW_SYNC_CREDENTIALS,
  };

  // Determines autofill state based on experiment and flag values.
  static AutofillForSyncCredentialsState GetAutofillForSyncCredentialsState();

  const PasswordManagerClient* const client_;

  const SyncServiceFactoryFunction sync_service_factory_function_;

  const SigninManagerFactoryFunction signin_manager_factory_function_;

  DISALLOW_COPY_AND_ASSIGN(SyncStoreResultFilter);
};

}  // namespace password_manager

#endif  // COMPONENTS_PASSWORD_MANAGER_SYNC_BROWSER_SYNC_STORE_RESULT_FILTER_H_


text:
<?php
class Pedido extends CI_Controller
{
    public function index()
    {
        $this->load->model("pedido_model");
        $this->pedido_model->creaPedidosTest();
    }
}
?>


text:
README
======

**Django Daguerre** makes it easy to adjust images on-the-fly without
slowing down your templates and without needing to generate everything
ahead of time with a cron job. You don't need to make any changes to
your models; it **Just Works**.

.. code-block:: html+django

    {% load daguerre %}

    {% adjust_bulk my_queryset "method.image" "fill" width=200 height=400 as adjusted_list %}
    {% for my_model, image in adjusted_list %}

    {% endfor %}

:code:         http://github.com/melinath/django-daguerre
:docs:         http://readthedocs.org/docs/django-daguerre/
:build status: |build-image|

.. |build-image| image:: https://secure.travis-ci.org/melinath/django-daguerre.png?branch=master
                 :target: http://travis-ci.org/melinath/django-daguerre/branches

Requirements
------------

* Python 3.5+
* Pillow
* Django 1.11 – 3.0

Daguerre *may* work with earlier or later versions of these packages, but they are not officially supported.

Installation
------------

You can install the latest version of Daguerre using ``pip``::

    $ pip install django-daguerre

You can clone the repository yourself at https://github.com/melinath/django-daguerre.

Setup
-----

Ensure that ``'daguerre'`` is in your project's ``INSTALLED_APPS``::

   INSTALLED_APPS = (
       'daguerre',
       ...
   )

Add the following or similar anywhere in your URLconf::

   urlpatterns = patterns('',
       url(r'^daguerre/', include('daguerre.urls')),
       ...
   )

Run the migration command to create the database models::

    python manage.py migrate daguerre

Testing
-------

You can run the tests with ``test_project/manage.py test daguerre``.


text: rbac for deluxcms


text:
* A class implements select, insert, update and delete operations to postgresql.
* Dependent: psycopg2

## REQUESTMETHOD.py

* A class implements sending GET/POST/PUT/DELETE requests and receiving the server responses.
* Collaboration with PHP (REQUESTMETHOD.php)
* Dependent : urllib, urllib2, json

## REQUESTMETHOD2.py

* A class implements sending GET/POST/PUT/DELETE requests and receiving the server responses.
* Collaboration with PHP (REQUESTMETHOD.php)
* Dependent : urllib2, requests, TEXTCODING(also in seed project)

## TEXTCODING.py

* Several methods transform text among different encoding.

## py2mysql.py

* A class implements select, insert, update and delete operations to mysql.
* Dependent: mysql.connector

## GoogleSheetsApiByOAuth.py

* related to Google Sheet API (v4) by OAuth

##  LineNotify.py

* Send the message, the sticker, and the image to the specific LINE group.

## TF1_FrozenModel.py

*   We provided several useful tools for using TF frozen model, including listing operations, listing nodes, transforming the model into tflite one, etc. We hope this script can bring you easier way access to the portable model.
*   Dependency: tensorflow (>= 1.13.2)


text:
class CreateFlags < ActiveRecord::Migration
  def change
    create_table :flags do |t|
      t.string :name

      t.timestamps
    end
  end
end


text:
angular.module( "starter.directives", [] )
.directive("maxlenstr", function () {
    return {
        restrict: "A",
        link: function (scope, elem, attrs) {
            var limit = parseInt(attrs.maxlenstr);
            elem.bind('keypress', function (e) {
                if (elem[0].value.length >= limit) {
                    e.preventDefault();
                    return false;
                }
            });
        }
    }
});


text:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Devoffice.GettingStarted.Utils
{
    public static class AppRegistration
    {
        public static Dictionary<string, string> UpdateWebAppRegistration(string token, string tenantId, string appName,
    string signOnUri, string appIdUri, string redirectUri, bool multiTenant, bool twoYearSecret, List<Scopes> appScopes,
    string existingAppId)
        {
            Dictionary<string, string> results = new Dictionary<string, string>();

            var registrationManifest = new JObject(
                                            new JProperty("availableToOtherTenants", multiTenant ? "True" : "False"),
                                            new JProperty("displayName", appName),
                                            new JProperty("homePage", signOnUri),
                                            new JProperty("identifierUris",
                                                new JArray(
                                                    new JValue(appIdUri)
                                                    )
                                                ),
                                            new JProperty("replyUrls",
                                                new JArray(
                                                    new JValue(redirectUri)
                                                    )
                                                ),
                                            new JProperty("requiredResourceAccess", GenerateRequiredAccess(appScopes.ToArray()))
                                            );

            string payload = JsonConvert.SerializeObject(registrationManifest);

            string result = UpdateAppRegistration(token, tenantId, payload, existingAppId);
            if (result.StartsWith("ERROR"))
            {
                // Check for expired token and handle specially
                // Implementing refresh can come later.
                if (result.Contains("Authentication_ExpiredToken"))
                {
                    results.Add(Utils.Constants.errorMessageTagStr, "Your session has expired. Please logout and log in again.");
                }
                else
                {
                    results.Add(Utils.Constants.errorMessageTagStr, result);
                }
            }

            return results;
        }
        public static Dictionary<string, string> CreateWebAppRegistration(string token, string tenantId, string appName,
            string signOnUri, string appIdUri, string redirectUri, bool multiTenant, bool twoYearSecret, List<Scopes> appScopes)
        {
            Dictionary<string, string> registrationInfo = new Dictionary<string, string>()
            {
                { "client_id", "" },
                { "client_secret", GenerateAppSecret() },
                { "error_message", "" },
            };

            var registrationManifest = new JObject(
                                            new JProperty("objectType", "Application"),
                                            new JProperty("availableToOtherTenants", multiTenant ? "True" : "False"),
                                            new JProperty("displayName", appName),
                                            new JProperty("homePage", signOnUri),
                                            new JProperty("identifierUris",
                                                new JArray(
                                                    new JValue(appIdUri)
                                                    )
                                                ),
                                            new JProperty("passwordCredentials",
                                                new JArray(
                                                    GeneratePasswordCredential(registrationInfo["client_secret"], twoYearSecret)
                                                    )
                                                ),
                                            new JProperty("replyUrls",
                                                new JArray(
                                                    new JValue(redirectUri)
                                                    )
                                                ),
                                            new JProperty("requiredResourceAccess", GenerateRequiredAccess(appScopes.ToArray()))
                                            );

            string payload = JsonConvert.SerializeObject(registrationManifest);

            string result = CreateAppRegistration(token, tenantId, payload);
            if (result.StartsWith("ERROR"))
            {
                // Check for expired token and handle specially
                // Implementing refresh can come later.
                if (result.Contains("Authentication_ExpiredToken"))
                {
                    registrationInfo["error_message"] = "Your session has been expired. Please logout and log in again.";
                }
                else if (result.Contains("Authorization_RequestDenied"))
                {
                    registrationInfo["error_message"] = "Request Denied !! Please sign in again using an account that has access to resources.";
                }
                else
                {
                    registrationInfo["error_message"] = result;
                }
            }
            else
            {
                registrationInfo["client_id"] = result;
            }

            return registrationInfo;
        }

        private static string CreateAppRegistration(string token, string tenantId, string payload)
        {
            string graphEndpoint = string.Format("https://graph.windows.net/{0}/applications?api-version=1.5", tenantId);
            using (var client = new HttpClient())
            {
                var content = new StringContent(payload);
                content.Headers.ContentType.MediaType = "application/json";
                var request = new HttpRequestMessage(HttpMethod.Post, graphEndpoint);
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
                request.Headers.UserAgent.Add(new ProductInfoHeaderValue("OutlookDevPortal", "1.0"));
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.Content = content;

                var result = client.SendAsync(request).Result;
                var resultContent = result.Content.ReadAsStringAsync().Result;

                if (result.IsSuccessStatusCode)
                {
                    try
                    {
                        dynamic appRegistration = JsonConvert.DeserializeObject(resultContent);

                        string appId = appRegistration["appId"];
                        return appId;
                    }
                    catch (JsonException ex)
                    {
                        return string.Format("ERROR: {0}", ex.Message);
                    }
                }
                else
                {
                    try
                    {
                        dynamic errorDetails = JsonConvert.DeserializeObject(resultContent);
                        return string.Format("ERROR: {0} - {1}", errorDetails["odata.error"]["code"],
                            errorDetails["odata.error"]["message"]["value"]);

                    }
                    catch (JsonException)
                    {
                        return string.Format("ERROR: Request returned {0}", result.StatusCode);
                    }
                }
            }
        }

        private static string GetErrorDetails(string response, System.Net.HttpStatusCode code)
        {
            try
            {
                dynamic errorDetails = JsonConvert.DeserializeObject(response);
                return string.Format("ERROR: {0} - {1}", errorDetails["odata.error"]["code"],
                     errorDetails["odata.error"]["message"]["value"]);
            }
            catch (JsonException)
            {
                return string.Format("ERROR: Request returned {0}", code);
            }
        }
        private static string UpdateAppRegistration(string token, string tenantId, string payload, string appId)
        {
            #region Get applicaiton object
            string appEndPoint = string.Format("https://graph.windows.net/{0}/applications?$filter=appId eq '{1}' &api-version=1.5", tenantId, appId);
            string appidObject = null;
            using (var client = new HttpClient())
            {
                var request = new HttpRequestMessage(HttpMethod.Get, appEndPoint);
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
                request.Headers.UserAgent.Add(new ProductInfoHeaderValue("OutlookDevPortal", "1.0"));
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var result = client.SendAsync(request).Result;
                var resultContent = result.Content.ReadAsStringAsync().Result;

                if(!result.IsSuccessStatusCode) {
                    return GetErrorDetails(resultContent, result.StatusCode);
                }
                dynamic appObject = (JObject.Parse(resultContent))["value"];
                appidObject = appObject[0].objectId;
            }
	#endregion
            #region Update the app info

            string graphEndpoint = string.Format("https://graph.windows.net/{0}/applications/{1}?api-version=1.5", tenantId, appidObject);
            using (var client = new HttpClient())
            {
                var content = new StringContent(payload);
                var method = new HttpMethod("PATCH");
                content.Headers.ContentType.MediaType = "application/json";
                var request = new HttpRequestMessage(method, graphEndpoint);
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
                request.Headers.UserAgent.Add(new ProductInfoHeaderValue("OutlookDevPortal", "1.0"));
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.Content = content;

                var result = client.SendAsync(request).Result;
                var resultContent = result.Content.ReadAsStringAsync().Result;

                if (result.IsSuccessStatusCode)
                {
                    return resultContent; /* on success result content is empty string*/
                }
                else
                {
                    return GetErrorDetails(resultContent, result.StatusCode);
                }
            }
            #endregion
        }

        private static string GenerateAppSecret()
        {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            byte[] buff = new byte[32];
            rng.GetBytes(buff);

            return Convert.ToBase64String(buff);
        }

        private static JObject GeneratePasswordCredential(string secret, bool twoYear)
        {
            DateTime validStart = DateTime.UtcNow;
            DateTime validEnd = validStart.AddYears(twoYear ? 2 : 1);
            var credential = new JObject(
                                new JProperty("startDate", validStart.ToString("o")),
                                new JProperty("endDate", validEnd.ToString("o")),
                                new JProperty("value", secret)
                                );
            //{
            //    startDate = validStart.ToString("o"),
            //    endDate = validEnd.ToString("o"),
            //    value = secret
            //};

            return credential;
        }

        private static JArray GenerateRequiredAccess(Scopes[] appScopes)
        {
            var requiredAccess = new JArray();

            var graphAccess = GetResourceNode(Resources.Graph, appScopes);
            if (graphAccess != null)
                requiredAccess.Add(graphAccess);

            var exchangeAccess = GetResourceNode(Resources.Exchange, appScopes);
            if (exchangeAccess != null)
                requiredAccess.Add(exchangeAccess);

            var sharepointAccess = GetResourceNode(Resources.SharePoint, appScopes);
            if (sharepointAccess != null)
                requiredAccess.Add(sharepointAccess);

            return requiredAccess;
        }

        private static JObject GetResourceNode(Resources resource, Scopes[] appScopes)
        {
            var entries = new JArray ();

            foreach (Scopes scope in appScopes)
            {
                if (AppScopes.GetScopeResource(scope) == resource)
                {
                    var entry = new JObject(
                                    new JProperty("id", AppScopes.GetScopeId(scope)),
                                    new JProperty("type", "Scope")
                                    );

                    entries.Add(entry);
                }
            }

            if (entries.Count > 0)
            {
                var node = new JObject(
                                new JProperty("resourceAppId", AppScopes.GetResourceId(resource)),
                                new JProperty("resourceAccess", entries)
                                );

                return node;
            }

            return null;
        }

        public static Dictionary<string, string> CreateNativeAppRegistration(string token, string tenantId,
            string appName,string redirectUri, List<Scopes> appScopes)
        {
            Dictionary<string, string> registrationInfo = new Dictionary<string, string>();

            var registrationManifest = new JObject(
                                            new JProperty("objectType", "Application"),
                                            new JProperty("availableToOtherTenants", "True"),
                                            new JProperty("publicClient", "True"),
                                            new JProperty("displayName", appName),
                                            new JProperty("replyUrls",
                                                new JArray(
                                                    new JValue(redirectUri)
                                                    )
                                                ),
                                            new JProperty("requiredResourceAccess", GenerateRequiredAccess(appScopes.ToArray()))
                                            );

            string payload = JsonConvert.SerializeObject(registrationManifest);

            string result = CreateAppRegistration(token, tenantId, payload);
            if (result.StartsWith("ERROR"))
            {
                // Check for expired token and handle specially
                // Implementing refresh can come later.
                if (result.Contains("Authentication_ExpiredToken"))
                {
                    registrationInfo.Add(Constants.errorMessageTagStr, "Your session has expired. Please logout and log in again.");
                }
                else
                {
                    registrationInfo.Add(Constants.errorMessageTagStr, result);
                }
            }
            else
            {
                    registrationInfo.Add(Constants.clientIdTagStr, result);
            }

            return registrationInfo;
        }

        public static Dictionary<string, string> UpdateNativeAppRegistration(string token, string tenantId,
    string appName, string redirectUri, List<Scopes> appScopes, string existingAppId)
        {
            Dictionary<string, string> registrationInfo = new Dictionary<string, string>();

            var registrationManifest = new JObject(
                                            new JProperty("displayName", appName),
                                            new JProperty("replyUrls",
                                                new JArray(
                                                    new JValue(redirectUri)
                                                    )
                                                ),
                                            new JProperty("requiredResourceAccess", GenerateRequiredAccess(appScopes.ToArray()))
                                            );

            string payload = JsonConvert.SerializeObject(registrationManifest);

            string result = UpdateAppRegistration(token, tenantId, payload, existingAppId);
            if (result.StartsWith("ERROR"))
            {
                // Check for expired token and handle specially
                // Implementing refresh can come later.
                if (result.Contains("Authentication_ExpiredToken"))
                {
                    registrationInfo.Add(Constants.errorMessageTagStr, "Your session has expired. Please logout and log in again.");
                }
                else
                {
                    registrationInfo.Add(Constants.errorMessageTagStr, result);
                }
            }
            return registrationInfo;
        }
    }
}


text:
function openWindow(id, src, srcArray) {
   var winClose = $('.frame-window-header .close-icon');
	// var current = $('.image-container').children('img').attr('src');
	// var number = $.inArray(current, srcArray);

    $('.frame-window').css('display', 'block');
    show(id, src);

    winClose.bind('click', function() {
        close();
    });

	$('.nextButton').bind('click', function() {
        var pager = new FramePager(srcArray);
        pager.nextStep();
  });

	$('.backButton').bind('click', function() {
        var pager = new FramePager(srcArray);
        pager.prevStep();
	});
}

var show = function(id, src) {
	var container = $('.image-container'),
			title = $('.frame-window-header .title');

	title.html(src).attr('href',src);
	container.html('<img src = "' + src + '" id = "' + id + '"/>');
	return this;
};

var close = function() {
	$('.frame-window').css('display', 'none');
	var container = $('.image-container');

	container.empty();
	return this;
};


text:
package com.telefonica.euro_iaas.paasmanager.exception;

public class InvalidOVFException extends Exception {

    public InvalidOVFException(String msg) {
        super(msg);
    }

    public InvalidOVFException(Throwable e) {
        super(e);
    }
}


text:
package market

import (
	"testing"

	"github.com/stretchr/testify/require"

	"openpitrix.io/openpitrix/pkg/constants"
	"openpitrix.io/openpitrix/test/client/market_manager"
	"openpitrix.io/openpitrix/test/models"
	"openpitrix.io/openpitrix/test/testutil"
)

var client = testutil.GetClient(testutil.GetClientConfig())
var userId = constants.UserSystem

const Service = "hyperpitrix"

func deleteMarketUser(t *testing.T, marketId string) {
	userLeaveMarketParams := market_manager.NewUserLeaveMarketParams()
	userLeaveMarketParams.SetBody(
		&models.OpenpitrixUserLeaveMarketRequest{
			MarketID: []string{marketId},
			UserID:   []string{userId},
		})
	leaveResp, err := client.MarketManager.UserLeaveMarket(userLeaveMarketParams, nil)
	testutil.NoError(t, err, []string{Service})
	t.Log(leaveResp)
}

// temporary comment
func testMarket(t *testing.T) {

	testMarketName := "test_market"
	testVisibility1 := constants.VisibilityPublic
	testVisibility2 := constants.VisibilityPrivate
	testStatus := constants.StatusEnabled
	testDescription := "test_create_market"

	// create market
	createMarketParams := market_manager.NewCreateMarketParams()
	createMarketParams.SetBody(
		&models.OpenpitrixCreateMarketRequest{
			Name:        testMarketName,
			Visibility:  testVisibility1,
			Description: testDescription,
		})
	createMarketResp, err := client.MarketManager.CreateMarket(createMarketParams, nil)

	testutil.NoError(t, err, []string{Service})
	t.Log(createMarketResp)

	marketId := createMarketResp.Payload.MarketID

	deleteMarketUser(t, marketId)
	// modify market
	modifyMarketParams := market_manager.NewModifyMarketParams()
	modifyMarketParams.SetBody(
		&models.OpenpitrixModifyMarketRequest{
			MarketID:    marketId,
			Name:        testMarketName,
			Status:      testStatus,
			Visibility:  testVisibility2,
			Description: testDescription,
		})
	modifyMarketResp, err := client.MarketManager.ModifyMarket(modifyMarketParams, nil)

	testutil.NoError(t, err, []string{Service})

	t.Log(modifyMarketResp)

	// describe market
	describeMarketsParams := market_manager.NewDescribeMarketsParams()
	describeMarketsParams.SetMarketID([]string{marketId})
	describeMarketsResp, err := client.MarketManager.DescribeMarkets(describeMarketsParams, nil)
	testutil.NoError(t, err, []string{Service})

	markets := describeMarketsResp.Payload.MarketSet
	require.Equal(t, 1, len(markets))

	market := markets[0]

	require.Equal(t, testMarketName, market.Name)
	require.Equal(t, testVisibility2, market.Visibility)
	require.Equal(t, testStatus, market.Status)
	require.Equal(t, testDescription, market.Description)
	t.Log(describeMarketsResp)

	// delete market
	deleteMarketParams := market_manager.NewDeleteMarketsParams()
	deleteMarketParams.WithBody(&models.OpenpitrixDeleteMarketsRequest{
		MarketID: []string{marketId},
	})
	deleteMarketResp, err := client.MarketManager.DeleteMarkets(deleteMarketParams, nil)
	testutil.NoError(t, err, []string{Service})
	t.Log(deleteMarketResp)

	// delete market_user

}

// temporary comment
func testMarketUser(t *testing.T) {
	testMarketName := "test_marketUser"

	// create a market for market_user
	testVisibility := constants.VisibilityPublic

	createMarektParams := market_manager.NewCreateMarketParams()
	createMarektParams.SetBody(
		&models.OpenpitrixCreateMarketRequest{
			Name:        testMarketName,
			Visibility:  testVisibility,
			Description: "test_market3",
		})

	createResp, err := client.MarketManager.CreateMarket(createMarektParams, nil)
	testutil.NoError(t, err, []string{Service})
	marketId := createResp.Payload.MarketID

	// describe marker_user
	describMarketUserParams := market_manager.NewDescribeMarketUsersParams()
	describMarketUserParams.SetMarketID([]string{marketId})
	describMarketUserParams.SetUserID([]string{userId})

	describMarketUserResp, err := client.MarketManager.DescribeMarketUsers(describMarketUserParams, nil)
	testutil.NoError(t, err, []string{Service})

	marketUsers := describMarketUserResp.Payload.MarketUserSet
	marketUser := marketUsers[0]

	require.Equal(t, marketId, marketUser.MarketID)
	require.Equal(t, userId, marketUser.UserID)
	//require.Equal(t, userId, marketUser.Owner)
	t.Log(describMarketUserResp)

	// user leave market
	userLeaveMarketParams := market_manager.NewUserLeaveMarketParams()
	userLeaveMarketParams.SetBody(
		&models.OpenpitrixUserLeaveMarketRequest{
			MarketID: []string{marketId},
			UserID:   []string{userId},
		})
	leaveResp, err := client.MarketManager.UserLeaveMarket(userLeaveMarketParams, nil)
	testutil.NoError(t, err, []string{Service})
	t.Log(leaveResp)

	// user join market
	userJoinMarketParams := market_manager.NewUserJoinMarketParams()
	userJoinMarketParams.SetBody(
		&models.OpenpitrixUserJoinMarketRequest{
			MarketID: []string{marketId},
			UserID:   []string{userId},
		})
	joinResp, err := client.MarketManager.UserJoinMarket(userJoinMarketParams, nil)
	testutil.NoError(t, err, []string{Service})
	t.Log(joinResp)

	// delete old date
	deleteMarketUser(t, marketId)

	deleteParams := market_manager.NewDeleteMarketsParams()
	deleteParams.SetBody(&models.OpenpitrixDeleteMarketsRequest{
		MarketID: []string{marketId},
	})
	_, err = client.MarketManager.DeleteMarkets(deleteParams, nil)
	testutil.NoError(t, err, []string{Service})

}


text:
package com.foc.helpBook;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.foc.Globals;
import com.foc.IFocEnvironment;
import com.vaadin.server.ClassResource;

public class FocHelpPage {

	private String pageCode = null;
	private String fileName = null;

	public FocHelpPage(String pageCode, String fileName) {
		this.fileName = fileName;
		this.pageCode = pageCode;
	}

	public void setFileName(String fileName){
		this.fileName = fileName;
	}

	public String getFileName(){
		return fileName;
	}

	public String getPageCode() {
    return pageCode;
  }

  public void setPageCode(String pageCode) {
  	this.pageCode = pageCode;
  }

	public String getHelpMessage() {
		String message = null;
		if(message == null){
			try{
				InputStream xmlFileStream = getXMLStream();
				if(xmlFileStream != null){
			    ByteArrayOutputStream baos = new ByteArrayOutputStream();
			    byte[] buffer = new byte[1024];
			    int length = 0;
			    while ((length = xmlFileStream.read(buffer)) != -1) {
		        baos.write(buffer, 0, length);
			    }
			    message = new String(baos.toByteArray());

			    xmlFileStream.close();
			    xmlFileStream = null;
				}
			}catch (Exception e){
				Globals.logException(e);
			}
		}
		return message;
	}

  private InputStream getXMLStream(){
  	InputStream inputStream = null;
		ClassResource resource = null;
		try{
			if(fileName != null && !fileName.isEmpty()){
				resource = new ClassResource(fileName);
				inputStream = resource.getStream().getStream();
			}
		}catch(Exception e){
      Globals.logString("Could not load file : "+fileName);
      Globals.logException(e);
		}

    if(inputStream == null){
    	Globals.showNotification("Could not load help page "+fileName, " for page code"+pageCode, IFocEnvironment.TYPE_ERROR_MESSAGE);
    }

    return inputStream;
  }
}


text:
<?php

namespace jeus\QuickstrikeBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class jeusQuickstrikeBundle extends Bundle
{
}


text:
#pragma once
#include <aws/mediaconvert/MediaConvert_EXPORTS.h>
#include <aws/core/utils/memory/stl/AWSString.h>

namespace Aws
{
namespace MediaConvert
{
namespace Model
{
  enum class CmafIntervalCadence
  {
    NOT_SET,
    FOLLOW_IFRAME,
    FOLLOW_CUSTOM
  };

namespace CmafIntervalCadenceMapper
{
AWS_MEDIACONVERT_API CmafIntervalCadence GetCmafIntervalCadenceForName(const Aws::String& name);

AWS_MEDIACONVERT_API Aws::String GetNameForCmafIntervalCadence(CmafIntervalCadence value);
} // namespace CmafIntervalCadenceMapper
} // namespace Model
} // namespace MediaConvert
} // namespace Aws


text:
package org.teavm.backend.c.intrinsic;

import org.teavm.ast.InvocationExpr;
import org.teavm.model.MethodReference;

public class PlatformObjectIntrinsic implements Intrinsic {
    private static final String PLATFORM_OBJECT = "org.teavm.platform.PlatformObject";

    @Override
    public boolean canHandle(MethodReference method) {
        if (!method.getClassName().equals(PLATFORM_OBJECT)) {
            return false;
        }

        return method.getName().equals("getPlatformClass");
    }

    @Override
    public void apply(IntrinsicContext context, InvocationExpr invocation) {
        switch (invocation.getMethod().getName()) {
            case "getPlatformClass":
                context.writer().print("TEAVM_CLASS_OF(");
                context.emit(invocation.getArguments().get(0));
                context.writer().print(")");
                break;
        }
    }
}


text:
<?xml version='1.0' encoding='UTF-8'?>
<flow-build plugin="workflow-job@2.10">
  <actions>
    <hudson.model.CauseAction>
      <causeBag class="linked-hash-map">
        <entry>
          <hudson.model.Cause_-UserIdCause/>
          <int>1</int>
        </entry>
      </causeBag>
    </hudson.model.CauseAction>
  </actions>
  <queueId>1</queueId>
  <timestamp>1506103764137</timestamp>
  <startTime>1506103764139</startTime>
  <result>FAILURE</result>
  <duration>758</duration>
  <keepLog>false</keepLog>
  <execution class="org.jenkinsci.plugins.workflow.cps.CpsFlowExecution">
    <result>FAILURE</result>
    <script>node(""){

    // test echo step
    dhecho("hehe my first echo")

    //test pwd step
    def x = dhpwd()
    dhecho "Current working directory: "+x
}</script>
    <loadedScripts class="map"/>
    <timings class="map">
      <entry>
        <string>flowNode</string>
        <long>38000028</long>
      </entry>
      <entry>
        <string>classLoad</string>
        <long>206885745</long>
      </entry>
      <entry>
        <string>run</string>
        <long>291815665</long>
      </entry>
      <entry>
        <string>parse</string>
        <long>280191855</long>
      </entry>
      <entry>
        <string>saveProgram</string>
        <long>100254889</long>
      </entry>
    </timings>
    <sandbox>true</sandbox>
    <iota>9</iota>
    <head>1:9</head>
  </execution>
  <checkouts class="hudson.util.PersistedList"/>
</flow-build>


text:
package com.kryger.bottlenose.support;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.lang3.ArrayUtils;

import com.kryger.bottlenose.Operation;
import com.kryger.bottlenose.Region;

public class UrlUtils {
	/*-
	 * Adapted from
	 *
	 * http://www.jokecamp.com/blog/examples-of-creating-base64-hashes-using-hmac-sha256-in-different-languages/#java
	 *
	 */
	public static String makeSignature(String paramsPart, Region region,
			String awsSecretAccessKey) {
		String data = "GET\n" + region.getUrl() + "\n/onca/xml\n" + paramsPart;

		try {
			Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
			SecretKeySpec secret_key = new SecretKeySpec(
					awsSecretAccessKey.getBytes(), "HmacSHA256");
			sha256_HMAC.init(secret_key);

			String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(data
					.getBytes()));
			return hash;
		} catch (NoSuchAlgorithmException | InvalidKeyException e) {
			throw new RuntimeException(e);
		}
	}

	static String makeUrl(Object... params) {
		if (params.length % 2 != 0) {
			throw new IllegalArgumentException(
					String.format(
							"List of input paramter key/value must be even to allow pairing. Was: %s",
							Arrays.toString(params)));
		}
		List<Tuple> kvPairs = new ArrayList<Tuple>();

		for (int i = 0; i < params.length; i += 2) {
			String key = params[i].toString();
			String value = safeEncode(params[i + 1].toString());
			Tuple t = new Tuple(key, value);
			kvPairs.add(t);
		}
		Collections.sort(kvPairs, (e1, e2) -> e1.key.compareTo(e2.key));

		String result = kvPairs.stream().map(e1 -> e1.key + "=" + e1.value)
				.collect(Collectors.joining("&"));
		return result;
	}

	public static String makeUrlString(Operation operation, Region region,
			AwsCredentials awsCredentials, String... extraParams) {
		String version = "2011-08-01";

		String baseUrl = String.format("http://%s/onca/xml?", region.getUrl());

		Object[] params = new Object[] { "AWSAccessKeyId",
				awsCredentials.getAwsAccessKey(), "AssociateTag",
				awsCredentials.getAwsAssociateTag(), "Operation",
				operation.getOperationToken(), "Service",
				"AWSECommerceService", "Timestamp", Instant.now(), "Version",
				version };
		String paramsJoined = makeUrl(ArrayUtils.addAll(params,
				(Object[]) extraParams));
		final String requestSignature = makeSignature(paramsJoined, region,
				awsCredentials.getAwsSecretAccessKey());
		String url = baseUrl + paramsJoined + "&Signature="
				+ safeEncode(requestSignature);
		return url;
	}

	private static String safeEncode(String in) {
		try {
			return new URLCodec().encode(in);
		} catch (EncoderException e) {
			throw new RuntimeException(e);
		}
	}

}


text:
import json
import select
import socket

class TestServer(object):
    """ A test Marionette server which can be used to test the Marionette
        protocol.  Each request will trigger a canned response; see
        process_command().
    """

    TEST_URL = 'http://www.mozilla.org'
    TEST_CURRENT_WINDOW = 'window1'
    TEST_WINDOW_LIST = ['window1', 'window2', 'window3']
    TEST_EXECUTE_RETURN_VALUE = 10
    TEST_EXECUTE_SCRIPT = 'return 2 * 5;'
    TEST_EXECUTE_SCRIPT_ARGS = 'testing'
    TEST_FIND_ELEMENT = 'element1'
    TEST_FIND_ELEMENTS = ['element1', 'element2', 'element3']
    TEST_GET_TEXT = 'first name'
    TEST_GET_VALUE = 'Mozilla Firefox'

    # canned responses for test messages
    test_responses = {
        'newSession': { 'value': 'a65bef90b145' },
        'getMarionetteID': { 'id': 'conn0.marionette' },
        'deleteSession': { 'ok': True },
        'setScriptTimeout': { 'ok': True },
        'setSearchTimeout': { 'ok': True },
        'getWindow': { 'value': TEST_CURRENT_WINDOW },
        'getWindows': { 'values': TEST_WINDOW_LIST },
        'closeWindow': { 'ok': True },
        'switchToWindow': { 'ok': True },
        'switchToFrame': { 'ok': True },
        'setContext': { 'ok': True },
        'getUrl' : { 'value': TEST_URL },
        'goUrl': { 'ok': True },
        'goBack': { 'ok': True },
        'goForward': { 'ok': True },
        'refresh': { 'ok': True },
        'executeScript': { 'value': TEST_EXECUTE_RETURN_VALUE },
        'executeAsyncScript': { 'value': TEST_EXECUTE_RETURN_VALUE },
        'executeJSScript': { 'value': TEST_EXECUTE_RETURN_VALUE },
        'findElement': { 'value': TEST_FIND_ELEMENT },
        'findElements': { 'values': TEST_FIND_ELEMENTS },
        'clickElement': { 'ok': True },
        'getElementText': { 'value': TEST_GET_TEXT },
        'sendKeysToElement': { 'ok': True },
        'clearElement': { 'ok': True },
        'isElementSelected': { 'value': True },
        'elementsEqual': { 'value': True },
        'isElementEnabled': { 'value': True },
        'isElementDisplayed': { 'value': True },
        'getElementAttribute': { 'value': TEST_GET_VALUE },
        'getSessionCapabilities': { 'value': {
            "cssSelectorsEnabled": True,
            "browserName": "firefox",
            "handlesAlerts": True,
            "javascriptEnabled": True,
            "nativeEvents": True,
            "platform": 'linux',
            "takeScreenshot": False,
            "version": "10.1"
            }
        },
        'getStatus': { 'value': {
            "os": {
                "arch": "x86",
                "name": "linux",
                "version": "unknown"
                },
            "build": {
                "revision": "unknown",
                "time": "unknown",
                "version": "unknown"
                }
            }
        }
    }

    # canned error responses for test messages
    error_responses = {
        'executeScript': { 'error': { 'message': 'JavaScript error', 'status': 17 } },
        'executeAsyncScript': { 'error': { 'message': 'Script timed out', 'status': 28 } },
        'findElement': { 'error': { 'message': 'Element not found', 'status': 7 } },
        'findElements': { 'error': { 'message': 'XPath is invalid', 'status': 19 } },
        'closeWindow': { 'error': { 'message': 'No such window', 'status': 23 } },
        'getWindow': { 'error': { 'message': 'No such window', 'status': 23 } },
        'clickElement': { 'error': { 'message': 'Element no longer exists', 'status': 10 } },
        'sendKeysToElement': { 'error': { 'message': 'Element is not visible on the page', 'status': 11 } },
        'switchToFrame': { 'error': { 'message': 'No such frame', 'status': 8 } }
    }

    def __init__(self, port):
        self.port = port

        self.srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.srvsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.srvsock.bind(("", port))
        self.srvsock.listen(5)
        self.descriptors = [self.srvsock]
        self.responses = self.test_responses
        print 'TestServer started on port %s' % port

    def _recv_n_bytes(self, sock, n):
        """ Convenience method for receiving exactly n bytes from
            self.sock (assuming it's open and connected).
        """
        data = ''
        while len(data) < n:
            chunk = sock.recv(n - len(data))
            if chunk == '':
                break
            data += chunk
        return data

    def receive(self, sock):
        """ Receive the next complete response from the server, and return
            it as a dict.  Each response from the server is prepended by
            len(message) + ':'.
        """
        assert(sock)
        response = sock.recv(10)
        sep = response.find(':')
        if sep == -1:
            return None
        length = response[0:sep]
        response = response[sep + 1:]
        response += self._recv_n_bytes(sock, int(length) + 1 + len(length) - 10)
        print 'received', response
        return json.loads(response)

    def send(self, sock, msg):
        print 'msg', msg
        data = json.dumps(msg)
        print 'sending %s' % data
        sock.send('%s:%s' % (len(data), data))

    def accept_new_connection(self):
        newsock, (remhost, remport) = self.srvsock.accept()
        self.descriptors.append( newsock )
        str = 'Client connected %s:%s\r\n' % (remhost, remport)
        print str
        self.send(newsock, {'from': 'root',
                            'applicationType': 'gecko',
                            'traits': []})

    def process_command(self, data):
        command = data['type']

        if command == 'use_test_responses':
            self.responses = self.test_responses
            return { 'ok': True }
        elif command == 'use_error_responses':
            self.responses = self.error_responses
            return { 'ok': True }

        if command in self.responses:
            response = self.responses[command]
        else:
            response = { 'error': { 'message': 'unknown command: %s' % command, 'status': 500} }

        if command not in ('newSession', 'getStatus', 'getMarionetteID') and 'session' not in data:
            response = { 'error': { 'message': 'no session specified', 'status': 500 } }

        return response

    def run(self):
        while 1:
            # Await an event on a readable socket descriptor
            (sread, swrite, sexc) = select.select( self.descriptors, [], [] )
            # Iterate through the tagged read descriptors
            for sock in sread:
                # Received a connect to the server (listening) socket
                if sock == self.srvsock:
                    self.accept_new_connection()
                else:
                    # Received something on a client socket
                    try:
                        data = self.receive(sock)
                    except:
                        data = None
                    # Check to see if the peer socket closed
                    if data is None:
                        host,port = sock.getpeername()
                        str = 'Client disconnected %s:%s\r\n' % (host, port)
                        print str
                        sock.close
                        self.descriptors.remove(sock)
                    else:
                        if 'type' in data:
                            msg = self.process_command(data)
                        else:
                            msg = 'command: %s' % json.dumps(data)
                        self.send(sock, msg)

if __name__ == "__main__":
    server = TestServer(2626)
    server.run()


text:
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;

namespace DataAnnotationsValidator
{
    public class DataAnnotationsValidator : IDataAnnotationsValidator
    {
        public bool TryValidateObject(object obj, ICollection<ValidationResult> results, IDictionary<object, object> validationContextItems = null)
        {
            return Validator.TryValidateObject(obj, new ValidationContext(obj, null, validationContextItems), results, true);
        }

        public bool TryValidateObjectRecursive<T>(T obj, List<ValidationResult> results, IDictionary<object, object> validationContextItems = null)
        {
            return TryValidateObjectRecursive(obj, results, new HashSet<object>(), validationContextItems);
        }

        private bool TryValidateObjectRecursive<T>(T obj, List<ValidationResult> results, ISet<object> validatedObjects, IDictionary<object, object> validationContextItems = null)
        {
            //short-circuit to avoid infinit loops on cyclical object graphs
            if (validatedObjects.Contains(obj))
            {
                return true;
            }

            validatedObjects.Add(obj);
            bool result = TryValidateObject(obj, results, validationContextItems);

            var properties = obj.GetType().GetProperties().Where(prop => prop.CanRead
                && !prop.GetCustomAttributes(typeof(SkipRecursiveValidation), false).Any()
                && prop.GetIndexParameters().Length == 0).ToList();

            foreach (var property in properties)
            {
                if (property.PropertyType == typeof(string) || property.PropertyType.IsValueType) continue;

                var value = obj.GetPropertyValue(property.Name);

                if (value == null) continue;

                var asEnumerable = value as IEnumerable;
                if (asEnumerable != null)
                {
                    foreach (var enumObj in asEnumerable)
                    {
                        if ( enumObj != null) {
                           var nestedResults = new List<ValidationResult>();
                           if (!TryValidateObjectRecursive(enumObj, nestedResults, validatedObjects, validationContextItems))
                           {
                               result = false;
                               foreach (var validationResult in nestedResults)
                               {
                                   PropertyInfo property1 = property;
                                   results.Add(new ValidationResult(validationResult.ErrorMessage, validationResult.MemberNames.Select(x => property1.Name + '.' + x)));
                               }
                           };
                        }
                    }
                }
                else
                {
                    var nestedResults = new List<ValidationResult>();
                    if (!TryValidateObjectRecursive(value, nestedResults, validatedObjects, validationContextItems))
                    {
                        result = false;
                        foreach (var validationResult in nestedResults)
                        {
                            PropertyInfo property1 = property;
                            results.Add(new ValidationResult(validationResult.ErrorMessage, validationResult.MemberNames.Select(x => property1.Name + '.' + x)));
                        }
                    };
                }
            }

            return result;
        }
    }
}