Exemplo n.º 1
0
 /// <summary>
 /// Converts content type enum value to HTTP content-type header value
 /// </summary>
 public static string ContentTypeToString(MimeContentType type)
 {
     if (type == MimeContentType.application)
     {
         return("application/octet-stream");
     }
     return(type.ToString().Replace('_', '/'));
 }
Exemplo n.º 2
0
        private void ValidateContentForType(MimeContentType contentType, string content, List <ValidationError> detectedErrors, bool validateJsonSchema = false)
        {
            if (contentType.MimeType.Equals(MimeTypeJson, StringComparison.OrdinalIgnoreCase))
            {
                // Verify that the request is valid JSON
                try
                {
                    JsonConvert.DeserializeObject(content);

                    if (validateJsonSchema)
                    {
                        ValidationError[] schemaErrors;
                        if (!ContentMatchesResourceSchema(content, RequestMetadata.ResourceType, SourceFile.Parent.ResourceCollection, out schemaErrors))
                        {
                            detectedErrors.AddRange(schemaErrors);
                        }
                    }
                }
                catch (Exception ex)
                {
                    detectedErrors.Add(new ValidationError(ValidationErrorCode.JsonParserException, null, "Invalid JSON format: {0}", ex.Message));
                }
            }
            else if (contentType.MimeType.Equals(MimeTypeMultipartRelated, StringComparison.OrdinalIgnoreCase))
            {
                // Parse the multipart/form-data body to ensure it's properly formatted
                try
                {
                    MultipartMimeContent multipartContent = new MultipartMime.MultipartMimeContent(contentType, content);
                    var part = multipartContent.PartWithId("<metadata>");
                    ValidateContentForType(part.ContentType, part.Body, detectedErrors, validateJsonSchema: true);
                }
                catch (Exception ex)
                {
                    detectedErrors.Add(new ValidationError(ValidationErrorCode.ContentFormatException, null, "Invalid Multipart MIME content format: {0}", ex.Message));
                }
            }
            else if (contentType.MimeType.Equals(MimeTypePlainText, StringComparison.OrdinalIgnoreCase))
            {
                // Ignore this, because it isn't something we can verify
            }
            else
            {
                detectedErrors.Add(new ValidationWarning(ValidationErrorCode.UnsupportedContentType, null, "Unvalidated request content type: {0}", contentType.MimeType));
            }
        }
Exemplo n.º 3
0
		public static void ApplyCompression(MimeContentType contentType)
		{
			// only text contents
			switch (contentType)
			{
				case MimeContentType.text_html:
				case MimeContentType.text_plain:
				case MimeContentType.text_css:
				case MimeContentType.text_javascript:
					ApplyCompression(HttpContext.Current.Request, HttpContext.Current.Response);
					break;

				case MimeContentType.image_jpeg:
				case MimeContentType.image_gif:
				case MimeContentType.application:
				default:
					break;
			}
		}
Exemplo n.º 4
0
        public static void ApplyCompression(MimeContentType contentType)
        {
            // only text contents
            switch (contentType)
            {
            case MimeContentType.text_html:
            case MimeContentType.text_plain:
            case MimeContentType.text_css:
            case MimeContentType.text_javascript:
                ApplyCompression(HttpContext.Current.Request, HttpContext.Current.Response);
                break;

            case MimeContentType.image_jpeg:
            case MimeContentType.image_gif:
            case MimeContentType.application:
            default:
                break;
            }
        }
Exemplo n.º 5
0
        private void ValidateContentForType(MimeContentType contentType, string content, IssueLogger issues, bool validateJsonSchema = false)
        {
            if (contentType.MimeType.Equals(MimeTypeJson, StringComparison.OrdinalIgnoreCase))
            {
                // Verify that the request is valid JSON
                try
                {
                    JsonConvert.DeserializeObject(content);

                    if (validateJsonSchema)
                    {
                        ContentMatchesResourceSchema(content, RequestMetadata.ResourceType, SourceFile.Parent.ResourceCollection, issues);
                    }
                }
                catch (Exception ex)
                {
                    issues.Error(ValidationErrorCode.JsonParserException, "Invalid JSON format.", ex);
                }
            }
            else if (contentType.MimeType.StartsWith(MimeTypeMultipartRelated, StringComparison.OrdinalIgnoreCase))
            {
                // Parse the multipart/form-data body to ensure it's properly formatted
                try
                {
                    MultipartMimeContent multipartContent = new MultipartMime.MultipartMimeContent(contentType, content);
                    var part = multipartContent.PartWithId("<metadata>");
                    ValidateContentForType(part.ContentType, part.Body, issues, validateJsonSchema: true);
                }
                catch (Exception ex)
                {
                    issues.Error(ValidationErrorCode.ContentFormatException, "Invalid Multipart MIME content format.", ex);
                }
            }
            else if (ContentTypesNotToValidate.Contains(contentType.MimeType))
            {
                // Ignore this, because it isn't something we can verify
            }
            else
            {
                issues.Warning(ValidationErrorCode.UnsupportedContentType, $"Unvalidated request content type: {contentType.MimeType}");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Disables http compression for non-text contents
        /// </summary>
        /// <reason>BUGFIX v5.5b2:  HttpCompression increases images size</reason>
        private void VerifyHttpCompressionByMimeType(HttpResponse httpResponse, MimeContentType responseMimeType)
        {
            switch (responseMimeType)
            {
            case MimeContentType.text_html:
            case MimeContentType.text_plain:
            case MimeContentType.text_css:
            case MimeContentType.text_javascript:
                // nothing
                // the compression remains if it is there
                break;

            case MimeContentType.image_jpeg:
            case MimeContentType.image_gif:
            case MimeContentType.application:
            default:
                // Disabling HttpCompression for this request
                //httpResponse.Headers.Remove("Content-Encoding");
                httpResponse.ClearHeaders();

                if (httpResponse.Filter is GZipStream)
                {
                    using (GZipStream compress = (GZipStream)httpResponse.Filter)
                    {
                        // reassign the original filter
                        httpResponse.Filter = compress.BaseStream;
                    }
                }
                else if (httpResponse.Filter is DeflateStream)
                {
                    using (DeflateStream compress = (DeflateStream)httpResponse.Filter)
                    {
                        // reassign the original filter
                        httpResponse.Filter = compress.BaseStream;
                    }
                }
                break;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Converts contentType to DataTypeToProcess
        /// </summary>
        public static DataTypeToProcess MimeTypeToToProcessType(MimeContentType mime)
        {
            switch (mime)
            {
            case MimeContentType.text_html:
                return(DataTypeToProcess.Html);

            case MimeContentType.text_css:
                return(DataTypeToProcess.Css);

            case MimeContentType.text_javascript:
                return(DataTypeToProcess.JavaScript);

            case MimeContentType.text_plain:
            case MimeContentType.image_jpeg:
            case MimeContentType.image_gif:
            case MimeContentType.application:
                return(DataTypeToProcess.None);

            default:
                return(DataTypeToProcess.None);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Disables http compression for non-text contents
        /// </summary>
        /// <reason>BUGFIX v5.5b2:  HttpCompression increases images size</reason>
        private void VerifyHttpCompressionByMimeType(HttpResponse httpResponse, MimeContentType responseMimeType)
        {
            switch (responseMimeType)
            {
                case MimeContentType.text_html:
                case MimeContentType.text_plain:
                case MimeContentType.text_css:
                case MimeContentType.text_javascript:
                    // nothing
                    // the compression remains if it is there
                    break;
                case MimeContentType.image_jpeg:
                case MimeContentType.image_gif:
                case MimeContentType.application:
                default:
                    // Disabling HttpCompression for this request
                    //httpResponse.Headers.Remove("Content-Encoding");
                    httpResponse.ClearHeaders();

                    if (httpResponse.Filter is GZipStream)
                    {
                        using (GZipStream compress = (GZipStream)httpResponse.Filter)
                        {
                            // reassign the original filter
                            httpResponse.Filter = compress.BaseStream;
                        }
                    }
                    else if (httpResponse.Filter is DeflateStream)
                    {
                        using (DeflateStream compress = (DeflateStream)httpResponse.Filter)
                        {
                            // reassign the original filter
                            httpResponse.Filter = compress.BaseStream;
                        }
                    }
                    break;
            }
        }
Exemplo n.º 9
0
 public void SetContentType(MimeContentType contentType)
 {
     _contentTypeMime = contentType;
     _contentType = Common.ContentTypeToString(contentType);
 }
Exemplo n.º 10
0
 public void SetContentType(string contentType)
 {
     _contentType = contentType;
     _contentTypeMime = Common.StringToContentType(contentType);
 }
Exemplo n.º 11
0
 public void SetContentType(MimeContentType contentType)
 {
     _contentTypeMime = contentType;
     _contentType     = Common.ContentTypeToString(contentType);
 }
Exemplo n.º 12
0
 public void SetContentType(string contentType)
 {
     _contentType     = contentType;
     _contentTypeMime = Common.StringToContentType(contentType);
 }
Exemplo n.º 13
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);
                    }
                }
            }
        }
Exemplo n.º 14
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);
            }
        }
Exemplo n.º 15
0
		/// <summary>
		/// Converts contentType to DataTypeToProcess
		/// </summary>
		public static DataTypeToProcess MimeTypeToToProcessType(MimeContentType mime)
		{
			switch (mime)
			{
				case MimeContentType.text_html:
					return DataTypeToProcess.Html;

				case MimeContentType.text_css:
					return DataTypeToProcess.Css;

				case MimeContentType.text_javascript:
					return DataTypeToProcess.JavaScript;

				case MimeContentType.text_plain:
				case MimeContentType.image_jpeg:
				case MimeContentType.image_gif:
				case MimeContentType.application:
					return DataTypeToProcess.None;
				default:
					return DataTypeToProcess.None;
			}
		}
Exemplo n.º 16
0
		/// <summary>
		/// Converts content type enum value to HTTP content-type header value
		/// </summary>
		public static string ContentTypeToString(MimeContentType type)
		{
			if (type == MimeContentType.application)
				return "application/octet-stream";
			return type.ToString().Replace('_', '/');
		}
Exemplo n.º 17
0
 protected string FileExt2ContentType(string fileExt)
 {
     return(MimeContentType.GetContentType(fileExt));
 }