Beispiel #1
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);
                    }
                }
            }
        }
Beispiel #2
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);
            }
        }