Ejemplo n.º 1
0
        /// <summary>
        /// Process the web response and output corresponding objects.
        /// </summary>
        /// <param name="response"></param>
        internal override void ProcessResponse(HttpResponseMessage response)
        {
            if (null == response)
            {
                throw new ArgumentNullException("response");
            }

            using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response)))
            {
                if (ShouldWriteToPipeline)
                {
                    // First see if it is an RSS / ATOM feed, in which case we can
                    // stream it - unless the user has overridden it with a return type of "XML"
                    if (TryProcessFeedStream(responseStream))
                    {
                        // Do nothing, content has been processed.
                    }
                    else
                    {
                        // get the response encoding
                        Encoding       encoding   = ContentHelper.GetEncoding(response);
                        string         str        = StreamHelper.DecodeStream(responseStream, encoding);
                        RestReturnType returnType = CheckReturnType(response);
                        Object         obj        = ConvertOutput(str, returnType);
                        WriteObject(obj);
                    }
                }

                if (ShouldSaveToOutFile)
                {
                    StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Process the web reponse and output corresponding objects.
        /// </summary>
        /// <param name="response"></param>
        internal override void ProcessResponse(HttpResponseMessage response)
        {
            if (null == response)
            {
                throw new ArgumentNullException("response");
            }

            using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response)))
            {
                if (ShouldWriteToPipeline)
                {
                    // First see if it is an RSS / ATOM feed, in which case we can
                    // stream it - unless the user has overriden it with a return type of "XML"
                    if (TryProcessFeedStream(responseStream))
                    {
                        // Do nothing, content has been processed.
                    }
                    else
                    {
                        // determine the response type
                        RestReturnType returnType = CheckReturnType(response);
                        // get the response encoding
                        Encoding encoding = ContentHelper.GetEncoding(response);

                        object    obj = null;
                        Exception ex  = null;

                        string str            = StreamHelper.DecodeStream(responseStream, encoding);
                        bool   convertSuccess = false;

                        // On CoreCLR, we need to explicity load Json.NET
                        JsonObject.ImportJsonDotNetModule(this);
                        if (returnType == RestReturnType.Json)
                        {
                            convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex);
                        }
                        // default to try xml first since it's more common
                        else
                        {
                            convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex);
                        }

                        if (!convertSuccess)
                        {
                            // fallback to string
                            obj = str;
                        }

                        WriteObject(obj);
                    }
                }

                if (ShouldSaveToOutFile)
                {
                    StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this);
                }
            }
        }
        private bool TryProcessFeedStream(BufferingStreamReader responseStream)
        {
            bool isRssOrFeed = false;

            try
            {
                XmlReaderSettings readerSettings = GetSecureXmlReaderSettings();
                XmlReader         reader         = XmlReader.Create(responseStream, readerSettings);

                // See if the reader contained an "RSS" or "Feed" in the first 10 elements (RSS and Feed are normally 2 or 3)
                int readCount = 0;
                while ((readCount < 10) && reader.Read())
                {
                    if (string.Equals("rss", reader.Name, StringComparison.OrdinalIgnoreCase) ||
                        string.Equals("feed", reader.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        isRssOrFeed = true;
                        break;
                    }

                    readCount++;
                }

                if (isRssOrFeed)
                {
                    XmlDocument workingDocument = new XmlDocument();
                    // performing a Read() here to avoid rrechecking
                    // "rss" or "feed" items
                    reader.Read();
                    while (!reader.EOF)
                    {
                        // If node is Element and it's the 'Item' or 'Entry' node, emit that node.
                        if ((reader.NodeType == XmlNodeType.Element) &&
                            (string.Equals("Item", reader.Name, StringComparison.OrdinalIgnoreCase) ||
                             string.Equals("Entry", reader.Name, StringComparison.OrdinalIgnoreCase))
                            )
                        {
                            // this one will do reader.Read() internally
                            XmlNode result = workingDocument.ReadNode(reader);
                            WriteObject(result);
                        }
                        else
                        {
                            reader.Read();
                        }
                    }
                }
            }
            catch (XmlException) { }
            finally
            {
                responseStream.Seek(0, SeekOrigin.Begin);
            }

            return(isRssOrFeed);
        }
        private bool TryProcessFeedStream(BufferingStreamReader responseStream)
        {
            bool isRssOrFeed = false;

            try
            {
                XmlReaderSettings readerSettings = GetSecureXmlReaderSettings();
                XmlReader reader = XmlReader.Create(responseStream, readerSettings);

                // See if the reader contained an "RSS" or "Feed" in the first 10 elements (RSS and Feed are normally 2 or 3)
                int readCount = 0;
                while ((readCount < 10) && reader.Read())
                {
                    if (String.Equals("rss", reader.Name, StringComparison.OrdinalIgnoreCase) ||
                        String.Equals("feed", reader.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        isRssOrFeed = true;
                        break;
                    }

                    readCount++;
                }

                if (isRssOrFeed)
                {
                    XmlDocument workingDocument = new XmlDocument();
                    // performing a Read() here to avoid rrechecking
                    // "rss" or "feed" items
                    reader.Read();
                    while (!reader.EOF)
                    {
                        // If node is Element and it's the 'Item' or 'Entry' node, emit that node.
                        if ((reader.NodeType == XmlNodeType.Element) &&
                            (string.Equals("Item", reader.Name, StringComparison.OrdinalIgnoreCase) ||
                             string.Equals("Entry", reader.Name, StringComparison.OrdinalIgnoreCase))
                           )
                        {
                            // this one will do reader.Read() internally
                            XmlNode result = workingDocument.ReadNode(reader);
                            WriteObject(result);
                        }
                        else
                        {
                            reader.Read();
                        }
                    }
                }
            }
            catch (XmlException) { }
            finally
            {
                responseStream.Seek(0, SeekOrigin.Begin);
            }

            return isRssOrFeed;
        }
        /// <summary>
        /// Process the web response and output corresponding objects. 
        /// </summary>
        /// <param name="response"></param>
        internal override void ProcessResponse(HttpResponseMessage response)
        {
            if (null == response) { throw new ArgumentNullException("response"); }

            using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response)))
            {
                if (ShouldWriteToPipeline)
                {
                    // First see if it is an RSS / ATOM feed, in which case we can 
                    // stream it - unless the user has overridden it with a return type of "XML"
                    if (TryProcessFeedStream(responseStream))
                    {
                        // Do nothing, content has been processed.
                    }
                    else
                    {
                        // determine the response type
                        RestReturnType returnType = CheckReturnType(response);
                        // get the response encoding
                        Encoding encoding = ContentHelper.GetEncoding(response);

                        object obj = null;
                        Exception ex = null;

                        string str = StreamHelper.DecodeStream(responseStream, encoding);
                        bool convertSuccess = false;

                        // On CoreCLR, we need to explicitly load Json.NET
                        JsonObject.ImportJsonDotNetModule(this);
                        if (returnType == RestReturnType.Json)
                        {
                            convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex);
                        }
                        // default to try xml first since it's more common
                        else
                        {
                            convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex);
                        }

                        if (!convertSuccess)
                        {
                            // fallback to string
                            obj = str;
                        }

                        WriteObject(obj);
                    }
                }

                if (ShouldSaveToOutFile)
                {
                    StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this);
                }
            }
        }
Ejemplo n.º 6
0
        internal override void ProcessResponse(WebResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }
            MemoryStream stream = new MemoryStream();

            using (BufferingStreamReader reader = new BufferingStreamReader(StreamHelper.GetResponseStream(response)))
            {
                if (base.ShouldWriteToPipeline && !this.TryProcessFeedStream(reader))
                {
                    RestReturnType type = this.CheckReturnType(response);
                    stream = StreamHelper.ReadStream(reader, response.ContentLength, this);
                    Encoding  encoding = ContentHelper.GetEncoding(response);
                    object    obj2     = null;
                    Exception exRef    = null;
                    string    json     = StreamHelper.DecodeStream(stream, encoding);
                    bool      flag     = false;
                    if (type == RestReturnType.Json)
                    {
                        flag = this.TryConvertToJson(json, out obj2, ref exRef) || this.TryConvertToXml(json, out obj2, ref exRef);
                    }
                    else
                    {
                        flag = this.TryConvertToXml(json, out obj2, ref exRef) || this.TryConvertToJson(json, out obj2, ref exRef);
                    }
                    if (!flag)
                    {
                        obj2 = json;
                    }
                    base.WriteObject(obj2);
                }
                if (base.ShouldSaveToOutFile)
                {
                    StreamHelper.SaveStreamToFile(StreamHelper.ReadStream(reader, response.ContentLength, this), base.QualifiedOutFile, this);
                }
            }
        }
Ejemplo n.º 7
0
        private bool TryProcessFeedStream(BufferingStreamReader responseStream)
        {
            bool flag = false;

            try
            {
                XmlReaderSettings secureXmlReaderSettings = this.GetSecureXmlReaderSettings();
                XmlReader         reader = XmlReader.Create(responseStream, secureXmlReaderSettings);
                for (int i = 0; (i < 10) && reader.Read(); i++)
                {
                    if (string.Equals("rss", reader.Name, StringComparison.OrdinalIgnoreCase) || string.Equals("feed", reader.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        flag = true;
                        break;
                    }
                }
                if (flag)
                {
                    XmlDocument document = new XmlDocument();
                    while (reader.Read())
                    {
                        if ((reader.NodeType == XmlNodeType.Element) && (string.Equals("Item", reader.Name, StringComparison.OrdinalIgnoreCase) || string.Equals("Entry", reader.Name, StringComparison.OrdinalIgnoreCase)))
                        {
                            XmlNode sendToPipeline = document.ReadNode(reader);
                            base.WriteObject(sendToPipeline);
                        }
                    }
                }
                return(flag);
            }
            catch (XmlException)
            {
            }
            finally
            {
                responseStream.Seek(0L, SeekOrigin.Begin);
            }
            return(flag);
        }
Ejemplo n.º 8
0
 internal override void ProcessResponse(WebResponse response)
 {
     if (response == null)
     {
         throw new ArgumentNullException("response");
     }
     MemoryStream stream = new MemoryStream();
     using (BufferingStreamReader reader = new BufferingStreamReader(StreamHelper.GetResponseStream(response)))
     {
         if (base.ShouldWriteToPipeline && !this.TryProcessFeedStream(reader))
         {
             RestReturnType type = this.CheckReturnType(response);
             stream = StreamHelper.ReadStream(reader, response.ContentLength, this);
             Encoding encoding = ContentHelper.GetEncoding(response);
             object obj2 = null;
             Exception exRef = null;
             string json = StreamHelper.DecodeStream(stream, encoding);
             bool flag = false;
             if (type == RestReturnType.Json)
             {
                 flag = this.TryConvertToJson(json, out obj2, ref exRef) || this.TryConvertToXml(json, out obj2, ref exRef);
             }
             else
             {
                 flag = this.TryConvertToXml(json, out obj2, ref exRef) || this.TryConvertToJson(json, out obj2, ref exRef);
             }
             if (!flag)
             {
                 obj2 = json;
             }
             base.WriteObject(obj2);
         }
         if (base.ShouldSaveToOutFile)
         {
             StreamHelper.SaveStreamToFile(StreamHelper.ReadStream(reader, response.ContentLength, this), base.QualifiedOutFile, this);
         }
     }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Process the web response and output corresponding objects.
        /// </summary>
        /// <param name="response"></param>
        internal override void ProcessResponse(HttpResponseMessage response)
        {
            if (null == response)
            {
                throw new ArgumentNullException("response");
            }

            using (BufferingStreamReader responseStream = new BufferingStreamReader(StreamHelper.GetResponseStream(response)))
            {
                if (ShouldWriteToPipeline)
                {
                    // First see if it is an RSS / ATOM feed, in which case we can
                    // stream it - unless the user has overridden it with a return type of "XML"
                    if (TryProcessFeedStream(responseStream))
                    {
                        // Do nothing, content has been processed.
                    }
                    else
                    {
                        // determine the response type
                        RestReturnType returnType = CheckReturnType(response);

                        // Try to get the response encoding from the ContentType header.
                        Encoding encoding = null;
                        string   charSet  = response.Content.Headers.ContentType?.CharSet;
                        if (!string.IsNullOrEmpty(charSet))
                        {
                            // NOTE: Don't use ContentHelper.GetEncoding; it returns a
                            // default which bypasses checking for a meta charset value.
                            StreamHelper.TryGetEncoding(charSet, out encoding);
                        }

                        object    obj = null;
                        Exception ex  = null;

                        string str = StreamHelper.DecodeStream(responseStream, ref encoding);
                        // NOTE: Tests use this verbose output to verify the encoding.
                        WriteVerbose(string.Format
                                     (
                                         System.Globalization.CultureInfo.InvariantCulture,
                                         "Content encoding: {0}",
                                         string.IsNullOrEmpty(encoding.HeaderName) ? encoding.EncodingName : encoding.HeaderName)
                                     );
                        bool convertSuccess = false;

                        // On CoreCLR, we need to explicitly load Json.NET
                        JsonObject.ImportJsonDotNetModule(this);
                        if (returnType == RestReturnType.Json)
                        {
                            convertSuccess = TryConvertToJson(str, out obj, ref ex) || TryConvertToXml(str, out obj, ref ex);
                        }
                        // default to try xml first since it's more common
                        else
                        {
                            convertSuccess = TryConvertToXml(str, out obj, ref ex) || TryConvertToJson(str, out obj, ref ex);
                        }

                        if (!convertSuccess)
                        {
                            // fallback to string
                            obj = str;
                        }

                        WriteObject(obj);
                    }
                }

                if (ShouldSaveToOutFile)
                {
                    StreamHelper.SaveStreamToFile(responseStream, QualifiedOutFile, this);
                }
            }
        }
Ejemplo n.º 10
0
 private bool TryProcessFeedStream(BufferingStreamReader responseStream)
 {
     bool flag = false;
     try
     {
         XmlReaderSettings secureXmlReaderSettings = this.GetSecureXmlReaderSettings();
         XmlReader reader = XmlReader.Create(responseStream, secureXmlReaderSettings);
         for (int i = 0; (i < 10) && reader.Read(); i++)
         {
             if (string.Equals("rss", reader.Name, StringComparison.OrdinalIgnoreCase) || string.Equals("feed", reader.Name, StringComparison.OrdinalIgnoreCase))
             {
                 flag = true;
                 break;
             }
         }
         if (flag)
         {
             XmlDocument document = new XmlDocument();
             while (reader.Read())
             {
                 if ((reader.NodeType == XmlNodeType.Element) && (string.Equals("Item", reader.Name, StringComparison.OrdinalIgnoreCase) || string.Equals("Entry", reader.Name, StringComparison.OrdinalIgnoreCase)))
                 {
                     XmlNode sendToPipeline = document.ReadNode(reader);
                     base.WriteObject(sendToPipeline);
                 }
             }
         }
         return flag;
     }
     catch (XmlException)
     {
     }
     finally
     {
         responseStream.Seek(0L, SeekOrigin.Begin);
     }
     return flag;
 }