Exemple #1
0
        public void GetString_should_work_with_Stream_Multiple_times()
        {
            var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(StringToTest));

            var ss = new StringStream(inputStream, EncodingEnum.UTF8);

            ss.GetString().Should().Be(StringToTest);
            ss.GetString().Should().Be(StringToTest);
            ss.GetString().Should().Be(StringToTest);
        }
Exemple #2
0
        public override string Execute()
        {
            if (_UserOptions.RemoveScripts)
            {
                // no script is required
                return(string.Empty);
            }
            else
            {
                Encoding _encoding;
                string   resultCodes = StringStream.GetString(
                    WebData.ResponseData,
                    WebData.ResponseInfo.ContentType,
                    _UserOptions.ForceEncoding,
                    false,
                    out _encoding);
                ContentEncoding = _encoding;

                IWebData webData = (IWebData)WebData;

                // Execute the result
                Execute(ref resultCodes,
                        webData.ResponseInfo.ResponseUrl,
                        null, null,
                        webData.ResponseInfo.ResponseRootUrl);

                // the result
                return(resultCodes);
            }
        }
        public void StringStream_GetString_WhenInvalidStringRequested_ReturnsEmptyString()
        {
            byte[]       contents = { 0 };
            StringStream stream   = new StringStream(contents, 0, 1);

            string returned = stream.GetString(260);

            Assert.AreEqual(string.Empty, returned);
        }
        public void StringStream_GetString_WhenValidStringRequested_ReturnsCorrectString()
        {
            const int    POSITION_OF_STRING = 260;
            const string REQUESTED          = "thisisateststring";

            StringStream stream = CreateStringStream();

            string returned = stream.GetString(POSITION_OF_STRING);

            Assert.AreEqual(REQUESTED, returned);
        }
Exemple #5
0
        /// <summary>
        /// Processes the html codes
        /// </summary>
        public override string Execute()
        {
            Encoding _encoding;
            string   resultHtml = StringStream.GetString(
                WebData.ResponseData,
                WebData.ResponseInfo.ContentType,
                _UserOptions.ForceEncoding,
                true,
                out _encoding);

            ContentEncoding = _encoding;

            if (_UserOptions.Frames)
            {
                IsFrameSet = HtmlTags.IsFramesetHtml(ref resultHtml);
            }

            if (_UserOptions.PageTitle)
            {
                PageTitle = HtmlParser.GetTagContent(ref resultHtml, "title");
            }

            if (_UserOptions.DocType)
            {
                DocType = HtmlTags.GetDocType(ref resultHtml);
            }


            // Page url. E.G. http://Site.com/users/profile.aspx?uid=90
            string pageUrl = WebData.ResponseInfo.ResponseUrl;

            // this is page path, used in processing relative paths in source html
            // for example the pageRootUrl for "http://Site.com/users/profile.aspx" will be "http://Site.com/users/"
            // gets page root Url
            string pagePath = UrlProvider.GetPagePath(pageUrl);

            // the page Url without any query parameter, used in processing relative query parameters
            // the pageUrlNoQuery for "http://Site.com/profile.aspx?uid=90" will be "http://Site.com/profile.aspx"
            // Gets page Url without any query parameter
            string pageUrlNoQuery = UrlProvider.GetPageAbsolutePath(pageUrl);

            // Execute the result
            Execute(ref resultHtml,
                    pageUrl,
                    pageUrlNoQuery,
                    pagePath,
                    WebData.ResponseInfo.ResponseRootUrl);

            // the result
            return(resultHtml);
        }
Exemple #6
0
        public override string Execute()
        {
            Encoding _encoding;
            string   resultCodes = StringStream.GetString(
                WebData.ResponseData,
                WebData.ResponseInfo.ContentType,
                _UserOptions.ForceEncoding,
                false,
                out _encoding);

            ContentEncoding = _encoding;


            if (_UserOptions.Images)
            {
                // Page url. E.G. http://Site.com/users/profile.aspx?uid=90
                string pageUrl = WebData.ResponseInfo.ResponseUrl;

                // this is page path, used in processing relative paths in source html
                // for example the pageRootUrl for "http://Site.com/users/profile.aspx" will be "http://Site.com/users/"
                // gets page root Url
                string pagePath = UrlProvider.GetPagePath(pageUrl);

                // the page Url without any query parameter, used in processing relative query parameters
                // the pageUrlNoQuery for "http://Site.com/profile.aspx?uid=90" will be "http://Site.com/profile.aspx"
                // Gets page Url without any query parameter
                string pageUrlNoQuery = UrlProvider.GetPageAbsolutePath(pageUrl);


                // Execute the result
                Execute(ref resultCodes,
                        pageUrl,
                        pageUrlNoQuery,
                        pagePath,
                        WebData.ResponseInfo.ResponseRootUrl);
            }

            // the result
            return(resultCodes);
        }
Exemple #7
0
        public void GetString_should_close_original_Stream()
        {
            var inputStream = new MemoryStream(Encoding.UTF8.GetBytes(StringToTest));

            var ss = new StringStream(inputStream, EncodingEnum.UTF8);

            ss.GetString().Should().Be(StringToTest);

            var alreadyDisposed = false;

            try
            {
                var _ = inputStream.Read(Span <byte> .Empty);
            }
            catch (ObjectDisposedException)
            {
                alreadyDisposed = true;
            }

            if (!alreadyDisposed)
            {
                throw new XunitException("Stream had not yet been disposed");
            }
        }
Exemple #8
0
 public string GetStringFromStream()
 {
     return(_stream.GetString(23));
 }
Exemple #9
0
        /// <summary>
        ///
        /// </summary>
        public override void ExecuteToStream(Stream stream)
        {
            // handshake should be ran before and there should not be error
            if (_webData == null || LastStatus == LastStatus.Error)
            {
                return;
            }

            // process type
            DataTypeToProcess processType = DataTypeToProcess;

            // gets mime type of content type
            MimeContentType contentMimeType = _webData.ResponseInfo.ContentTypeMime;

            // detecting processing method by response content type
            if (processType == DataTypeToProcess.AutoDetect)
            {
                // gets its process type
                processType = Common.MimeTypeToToProcessType(contentMimeType);
            }

            IDataProcessor dataProcessor = null;

            switch (processType)
            {
            case DataTypeToProcess.AutoDetect:
                break;

            case DataTypeToProcess.Html:
                dataProcessor = (IDataProcessor)Providers.GetProvider(ProviderType.IHtmlProcessor);
                break;

            case DataTypeToProcess.JavaScript:
                dataProcessor = (IDataProcessor)Providers.GetProvider(ProviderType.IJSProcessor);
                break;

            case DataTypeToProcess.Css:
                dataProcessor = (IDataProcessor)Providers.GetProvider(ProviderType.ICssProcessor);
                break;

            case DataTypeToProcess.AdobeFlash:
            // still nothing
            case DataTypeToProcess.None:
                break;

            default:
                break;
            }

            if (dataProcessor != null)
            {
                // 3- executing the plugins
                if (_isPluginAvailable)
                {
                    Plugins.CallPluginMethod(PluginHosts.IPluginEngine,
                                             PluginMethods.IPluginEngine.BeforeProcessor,
                                             this, dataProcessor);
                }

                // Web data instance
                dataProcessor.WebData = _webData;

                // executes the process
                string response = dataProcessor.Execute();

                // If execution occurred
                if (dataProcessor.LastStatus == LastStatus.Error)
                {
                    LastStatus       = LastStatus.Error;
                    LastErrorMessage = dataProcessor.LastErrorMessage;
                    LastException    = dataProcessor.LastException;
                    return;
                }
                else if (dataProcessor.LastStatus == LastStatus.ContinueWithError)
                {
                    LastStatus       = LastStatus.ContinueWithError;
                    LastErrorMessage = dataProcessor.LastErrorMessage;
                    LastException    = dataProcessor.LastException;
                }

                // processed content encoding
                ResponseInfo.ContentEncoding = dataProcessor.ContentEncoding;
                ResponseInfo.ContentLength   = response.Length;



                // Html specifies
                if (processType == DataTypeToProcess.Html && dataProcessor is IHtmlProcessor)
                {
                    IHtmlProcessor htmlProcessor = (IHtmlProcessor)dataProcessor;

                    ResponseInfo.HtmlPageTitle     = htmlProcessor.PageTitle;
                    ResponseInfo.HtmlIsFrameSet    = htmlProcessor.IsFrameSet;
                    ResponseInfo.ExtraCodesForPage = htmlProcessor.ExtraCodesForPage;
                    ResponseInfo.ExtraCodesForBody = htmlProcessor.ExtraCodesForBody;
                    ResponseInfo.HtmlDocType       = htmlProcessor.DocType;
                }

                // 4- executing the plugins
                if (_isPluginAvailable)
                {
                    Plugins.CallPluginMethod(PluginHosts.IPluginEngine,
                                             PluginMethods.IPluginEngine.AfterProcessor,
                                             this);
                }

                // the processed content
                byte[] streamBuff = ResponseInfo.ContentEncoding.GetBytes(response);
                stream.Write(streamBuff, 0, streamBuff.Length);
            }
            else
            {
                // used to resolvent mime processing conflict
                bool ContinueNonMime = true;

                // if response is a image
                if ((UserOptions.ImageCompressor) &&
                    (contentMimeType == MimeContentType.image_gif || contentMimeType == MimeContentType.image_jpeg))
                {
                    using (MemoryStream imgMem = ImageCompressor.CompressImage(
                               _webData.ResponseData))
                    {
                        // check if compression is decreased size of data
                        if (imgMem.Length < _webData.ResponseData.Length)
                        {
                            ContinueNonMime = false;

                            // write the image to result
                            imgMem.WriteTo(stream);
                        }
                        else
                        {
                            // Oops! the original image is smaller
                            ContinueNonMime = true;
                        }
                    }
                }

                // can process other types?
                if (ContinueNonMime)
                {
                    if (processType == DataTypeToProcess.None &&
                        _webData.ResponseData is MemoryStream)
                    {
                        MemoryStream mem = (MemoryStream)_webData.ResponseData;
                        if (mem.Length > 0)
                        {
                            mem.WriteTo(stream);
                        }
                    }
                    else if (processType == DataTypeToProcess.None)
                    {
                        int       readed    = -1;
                        const int blockSize = 1024 * 5;

                        byte[] buffer = new byte[blockSize];
                        while ((int)(readed = _webData.ResponseData.Read(buffer, 0, blockSize)) > 0)
                        {
                            stream.Write(buffer, 0, readed);
                        }
                    }
                    else
                    {
                        Encoding contentEncoding;
                        // Reads response stream to a string
                        string response = StringStream.GetString(
                            _webData.ResponseData,
                            WebData.ResponseInfo.ContentType,
                            UserOptions.ForceEncoding,
                            false,
                            out contentEncoding);

                        ResponseInfo.ContentEncoding = contentEncoding;
                        ResponseInfo.ContentLength   = response.Length;

                        byte[] streamBuff = ResponseInfo.ContentEncoding.GetBytes(response);
                        stream.Write(streamBuff, 0, streamBuff.Length);
                    }
                }
            }
        }
Exemple #10
0
        /// <summary>
        ///
        /// </summary>
        public override string ExecuteToString()
        {
            // hanshake should be ran before and there should not be error
            if (_webData == null || LastStatus == LastStatus.Error)
            {
                return(String.Empty);
            }

            // process type
            DataTypeToProcess processType = DataTypeToProcess;

            // gets mime type of content type
            MimeContentType contentMimeType = _webData.ResponseInfo.ContentTypeMime;

            // detecting processing method by response content type
            if (processType == DataTypeToProcess.AutoDetect)
            {
                // gets its process type
                processType = Common.MimeTypeToToProcessType(contentMimeType);
            }

            if (processType == DataTypeToProcess.Html && Systems.LogSystem.ActivityLogEnabled)
            {
                Systems.LogSystem.Log(LogEntity.UrlRequested, ResponseInfo.ResponseUrl);
            }

            IDataProcessor dataProcessor = null;

            switch (processType)
            {
            case DataTypeToProcess.AutoDetect:
                break;

            case DataTypeToProcess.Html:
                dataProcessor = (IDataProcessor)Providers.GetProvider(ProviderType.IHtmlProcessor);
                break;

            case DataTypeToProcess.JavaScript:
                dataProcessor = (IDataProcessor)Providers.GetProvider(ProviderType.IJSProcessor);
                break;

            case DataTypeToProcess.Css:
                dataProcessor = (IDataProcessor)Providers.GetProvider(ProviderType.ICssProcessor);
                break;

            case DataTypeToProcess.AdobeFlash:
            // still nothing
            default:
                break;
            }

            if (dataProcessor != null)
            {
                // 3- executing the plugins
                if (_isPluginAvailable)
                {
                    Plugins.CallPluginMethod(PluginHosts.IPluginEngine,
                                             PluginMethods.IPluginEngine.BeforeProcessor,
                                             this, dataProcessor);
                }

                // Web data instance
                dataProcessor.WebData = _webData;

                // executes the process
                string response = dataProcessor.Execute();

                // If execution occurred
                if (dataProcessor.LastStatus == LastStatus.Error)
                {
                    LastStatus       = LastStatus.Error;
                    LastErrorMessage = dataProcessor.LastErrorMessage;
                    LastException    = dataProcessor.LastException;
                    return(response);
                }
                else if (dataProcessor.LastStatus == LastStatus.ContinueWithError)
                {
                    LastStatus       = LastStatus.ContinueWithError;
                    LastErrorMessage = dataProcessor.LastErrorMessage;
                    LastException    = dataProcessor.LastException;
                }

                // processed content encoding
                ResponseInfo.ContentEncoding = dataProcessor.ContentEncoding;
                ResponseInfo.ContentLength   = response.Length;



                // Html specifies
                if (processType == DataTypeToProcess.Html && dataProcessor is IHtmlProcessor)
                {
                    IHtmlProcessor htmlProcessor = (IHtmlProcessor)dataProcessor;

                    ResponseInfo.HtmlPageTitle     = htmlProcessor.PageTitle;
                    ResponseInfo.HtmlIsFrameSet    = htmlProcessor.IsFrameSet;
                    ResponseInfo.ExtraCodesForPage = htmlProcessor.ExtraCodesForPage;
                    ResponseInfo.ExtraCodesForBody = htmlProcessor.ExtraCodesForBody;
                    ResponseInfo.HtmlDocType       = htmlProcessor.DocType;
                }

                // 4- executing the plugins
                if (_isPluginAvailable)
                {
                    Plugins.CallPluginMethod(PluginHosts.IPluginEngine,
                                             PluginMethods.IPluginEngine.AfterProcessor,
                                             this);
                }

                // the processed content
                return(response);
            }
            else
            {
                Encoding contentEncoding;

                // Reads response stream to a string
                string response = StringStream.GetString(
                    _webData.ResponseData,
                    WebData.ResponseInfo.ContentType,
                    UserOptions.ForceEncoding,
                    false,
                    out contentEncoding);

                ResponseInfo.ContentEncoding = contentEncoding;
                ResponseInfo.ContentLength   = response.Length;

                return(response);
            }
        }
Exemple #11
0
        public void GetString_should_work_with_basic_String()
        {
            StringStream ss = StringToTest;

            ss.GetString().Should().Be(StringToTest);
        }