Exemplo n.º 1
0
        public AMF0ObjectReader(AMFDataInput input)
        {
            this.input = input;

            amf3ObjectReader     = new AMF3ObjectReader(input);
            objectReferenceCache = new List <IASValue>();
        }
Exemplo n.º 2
0
        public AMF3ObjectReader(AMFDataInput input)
        {
            this.input = input;

            objectReferenceCache = new List <CacheItem>();
            stringReferenceCache = new List <string>();
        }
Exemplo n.º 3
0
        public AMF0ObjectReader(AMFDataInput input)
        {
            this.input = input;

            amf3ObjectReader = new AMF3ObjectReader(input);
            objectReferenceCache = new List<IASValue>();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads an AMF message from an input stream.
        /// </summary>
        /// <param name="input">The input stream</param>
        /// <returns>The AMF message that was read, or null on end of stream</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="input"/> is null</exception>
        /// <exception cref="AMFException">Thrown if an exception occurred while writing the message</exception>
        public static AMFMessage ReadAMFMessage(AMFDataInput input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            try
            {
                // Check for end of stream.
                // If we catch an EndOfStreamException here, we gracefully unwind and
                // return null to the caller.  If it happens later on then it's an error
                // because it means the message is incomplete.
                byte firstByte;
                try
                {
                    firstByte = input.ReadByte();
                }
                catch (EndOfStreamException)
                {
                    // Return gracefully.
                    return(null);
                }

                // Okay, there's data.  From now on, if we see EndOfStreamException it's an error!
                return(UncheckedReadAMFMessage(input, firstByte));
            }
            catch (Exception ex)
            {
                throw new AMFException("An exception occurred while reading an AMF message from the stream.", ex);
            }
        }
Exemplo n.º 5
0
        public AMF3ObjectReader(AMFDataInput input)
        {
            this.input = input;

            objectReferenceCache = new List<CacheItem>();
            stringReferenceCache = new List<string>();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Reads an AMF message from an input stream.
        /// </summary>
        /// <param name="input">The input stream</param>
        /// <returns>The AMF message that was read, or null on end of stream</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="input"/> is null</exception>
        /// <exception cref="AMFException">Thrown if an exception occurred while writing the message</exception>
        public static AMFMessage ReadAMFMessage(AMFDataInput input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            try
            {
                // Check for end of stream.
                // If we catch an EndOfStreamException here, we gracefully unwind and
                // return null to the caller.  If it happens later on then it's an error
                // because it means the message is incomplete.
                byte firstByte;
                try
                {
                    firstByte = input.ReadByte();
                }
                catch (EndOfStreamException)
                {
                    // Return gracefully.
                    return null;
                }

                // Okay, there's data.  From now on, if we see EndOfStreamException it's an error!
                return UncheckedReadAMFMessage(input, firstByte);
            }
            catch (Exception ex)
            {
                throw new AMFException("An exception occurred while reading an AMF message from the stream.", ex);
            }
        }
Exemplo n.º 7
0
        public override void SetUp()
        {
            base.SetUp();

            stream = new MemoryStream();
            serializer = Mocks.CreateMock<IActionScriptSerializer>();
            input = new AMFDataInput(stream, serializer);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Reads an AMF body from an input stream.
        /// </summary>
        /// <param name="input">The input stream</param>
        /// <returns>The AMF body that was read</returns>
        private static AMFBody ReadAMFBody(AMFDataInput input)
        {
            string requestTarget  = input.ReadShortString();
            string responseTarget = input.ReadShortString();
            int    bodyLength     = input.ReadInt();

            IASValue content = ReadAMFContent(input, bodyLength);

            return(new AMFBody(requestTarget, responseTarget, content));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Reads an AMF header from an input stream.
        /// </summary>
        /// <param name="input">The input stream</param>
        /// <returns>The AMF header that was read</returns>
        private static AMFHeader ReadAMFHeader(AMFDataInput input)
        {
            string name           = input.ReadShortString();
            bool   mustUnderstand = input.ReadBoolean();
            int    headerLength   = input.ReadInt();

            IASValue content = ReadAMFContent(input, headerLength);

            return(new AMFHeader(name, mustUnderstand, content));
        }
Exemplo n.º 10
0
 /// <summary>
 /// Reads AMF content from an input stream.
 /// </summary>
 /// <param name="input">The input stream</param>
 /// <param name="contentLength">The length of the content to read in bytes</param>
 /// <returns>The content object</returns>
 private static IASValue ReadAMFContent(AMFDataInput input, int contentLength)
 {
     input.BeginObjectStream();
     try
     {
         // TODO: validate for content length
         return(input.ReadObject());
     }
     finally
     {
         input.EndObjectStream();
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// Reads an AMF message from an input stream and bubbles up exceptions.
        /// </summary>
        /// <param name="input">The input stream</param>
        /// <param name="firstByte">The first byte of the message</param>
        /// <returns>The AMF message that was read</returns>
        private static AMFMessage UncheckedReadAMFMessage(AMFDataInput input, byte firstByte)
        {
            byte secondVersionByte = input.ReadByte();
            ushort version = (ushort) ((firstByte << 8) | secondVersionByte);

            int headerCount = input.ReadUnsignedShort();
            AMFHeader[] headers = new AMFHeader[headerCount];
            for (int i = 0; i < headerCount; i++)
                headers[i] = ReadAMFHeader(input);

            int bodyCount = input.ReadUnsignedShort();
            AMFBody[] bodies = new AMFBody[bodyCount];
            for (int i = 0; i < bodyCount; i++)
                bodies[i] = ReadAMFBody(input);

            return new AMFMessage(version, headers, bodies);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Reads an AMF message from an input stream and bubbles up exceptions.
        /// </summary>
        /// <param name="input">The input stream</param>
        /// <param name="firstByte">The first byte of the message</param>
        /// <returns>The AMF message that was read</returns>
        private static AMFMessage UncheckedReadAMFMessage(AMFDataInput input, byte firstByte)
        {
            byte   secondVersionByte = input.ReadByte();
            ushort version           = (ushort)((firstByte << 8) | secondVersionByte);

            int headerCount = input.ReadUnsignedShort();

            AMFHeader[] headers = new AMFHeader[headerCount];
            for (int i = 0; i < headerCount; i++)
            {
                headers[i] = ReadAMFHeader(input);
            }

            int bodyCount = input.ReadUnsignedShort();

            AMFBody[] bodies = new AMFBody[bodyCount];
            for (int i = 0; i < bodyCount; i++)
            {
                bodies[i] = ReadAMFBody(input);
            }

            return(new AMFMessage(version, headers, bodies));
        }
Exemplo n.º 13
0
            protected override IEnumerable<Step> GetSteps()
            {
                // Set properties from context.
                context.Request.IsSecureConnection = context.HttpContext.Request.IsSecureConnection;

                // Read the AMF message and prepare the response context.
                AMFDataInput dataInput = new AMFDataInput(context.HttpContext.Request.InputStream, context.Serializer);
                AMFMessage amfRequestMessage = AMFMessageReader.ReadAMFMessage(dataInput);
                context.Request.SetMessage(amfRequestMessage);
                context.Response.PrepareResponseMessage(amfRequestMessage);

                // Process all message bodies.
                foreach (AMFBody requestBody in amfRequestMessage.Bodies)
                {
                    object nativeContent;
                    try
                    {
                        nativeContent = context.Serializer.ToNative(requestBody.Content, null);
                    }
                    catch (Exception ex)
                    {
                        context.Response.AddErrorResponse(requestBody.ResponseTarget,
                            "An error occurred while deserializing the request body.", ex);
                        continue;
                    }

                    if (requestBody.RequestTarget == "null")
                    {
                        IMessage requestMessage = nativeContent as IMessage;
                        if (requestMessage == null)
                        {
                            object[] array = nativeContent as object[];
                            if (array != null)
                                requestMessage = array[0] as IMessage;

                            if (requestMessage != null)
                            {
                                IAsyncResult result;
                                try
                                {
                                    result = messageBroker.BeginProcessRequest(context, requestMessage, OnAsyncResultCallbackResume, null);
                                }
                                catch (Exception ex)
                                {
                                    ErrorMessage errorMessage = ErrorMessage.CreateErrorResponse(requestMessage,
                                        "An error occurred while the message broker was processing the message.",
                                        "Gateway.MessageBroker.BeginProcessRequestFailed",
                                        ex);

                                    context.Response.AddErrorResponse(requestBody.ResponseTarget, errorMessage);
                                    continue;
                                }

                                yield return new Step("Waiting for message broker.");

                                try
                                {
                                    IMessage responseMessage = messageBroker.EndProcessRequest(result);
                                    context.Response.AddResultResponse(requestBody.ResponseTarget, responseMessage);
                                }
                                catch (Exception ex)
                                {
                                    ErrorMessage errorMessage = ErrorMessage.CreateErrorResponse(requestMessage,
                                        "An error occurred while the message broker was processing the message.",
                                        "Gateway.MessageBroker.EndProcessRequestFailed",
                                        ex);

                                    context.Response.AddErrorResponse(requestBody.ResponseTarget, errorMessage);
                                }

                                continue;
                            }
                        }
                    }

                    // Don't know what to do with this message.
                    context.Response.AddErrorResponse(requestBody.ResponseTarget,
                        String.Format(CultureInfo.CurrentCulture,
                        "Received a message for unsupported request target '{0}'.", requestBody.RequestTarget), null);
                }

                // Generate the response.
                AMFDataOutput dataOutput = new AMFDataOutput(context.HttpContext.Response.OutputStream, context.Serializer);

                AMFMessageWriter.WriteAMFMessage(dataOutput, context.Response.Message);

                context.HttpContext.Response.ContentType = AMFContentType;
                context.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                yield break;
            }
Exemplo n.º 14
0
        private object FromAMF(byte[] bytes)
        {
            IActionScriptSerializer serializer = serializerFactory.CreateSerializer();
            MemoryStream stream = new MemoryStream(bytes);
            AMFDataInput dataInput = new AMFDataInput(stream, serializer);
            dataInput.BeginObjectStream();
            IASValue asValue = dataInput.ReadObject();
            dataInput.EndObjectStream();
            object nativeValue = serializer.ToNative(asValue, null);

            return nativeValue;
        }
Exemplo n.º 15
0
        /// <summary>
        /// Reads an AMF header from an input stream.
        /// </summary>
        /// <param name="input">The input stream</param>
        /// <returns>The AMF header that was read</returns>
        private static AMFHeader ReadAMFHeader(AMFDataInput input)
        {
            string name = input.ReadShortString();
            bool mustUnderstand = input.ReadBoolean();
            int headerLength = input.ReadInt();

            IASValue content = ReadAMFContent(input, headerLength);

            return new AMFHeader(name, mustUnderstand, content);
        }
Exemplo n.º 16
0
 /// <summary>
 /// Reads AMF content from an input stream.
 /// </summary>
 /// <param name="input">The input stream</param>
 /// <param name="contentLength">The length of the content to read in bytes</param>
 /// <returns>The content object</returns>
 private static IASValue ReadAMFContent(AMFDataInput input, int contentLength)
 {
     input.BeginObjectStream();
     try
     {
         // TODO: validate for content length
         return input.ReadObject();
     }
     finally
     {
         input.EndObjectStream();
     }
 }
Exemplo n.º 17
0
        /// <summary>
        /// Reads an AMF body from an input stream.
        /// </summary>
        /// <param name="input">The input stream</param>
        /// <returns>The AMF body that was read</returns>
        private static AMFBody ReadAMFBody(AMFDataInput input)
        {
            string requestTarget = input.ReadShortString();
            string responseTarget = input.ReadShortString();
            int bodyLength = input.ReadInt();

            IASValue content = ReadAMFContent(input, bodyLength);

            return new AMFBody(requestTarget, responseTarget, content);
        }