/// <summary>
 /// Creates a new instance of <see cref="MultipartFormXmlDeserializer"/> using specified form data item <paramref name="multipartFormDataItem"/>
 /// </summary>
 /// <param name="multipartFormDataItem">Multi-part form data item</param>
 public MultipartFormXmlDeserializer(MultipartFormDataItem multipartFormDataItem)
 {
     if (multipartFormDataItem == null)
     {
         throw new ArgumentNullException("multipartFormDataItem");
     }
     _decoder = ContentDecoderFactory.CreateTextDecoder(multipartFormDataItem);
 }
 /// <summary>
 /// Finds and creates instance of a type which implements <see cref="IContentDecoder{TClrType}"/> interface for specific Content-Type in <paramref name="multipartFormDataItem"/>
 /// </summary>
 /// <typeparam name="TClrType">Managed type into which decoding should be perfomed</typeparam>
 /// <param name="multipartFormDataItem">Multi-part form data item</param>
 /// <returns>Instance of a type which decodes form content into type <see cref="TClrType"/> or null, if such not found</returns>
 public static IContentDecoder <TClrType> FindAndCreateInstance <TClrType>(MultipartFormDataItem multipartFormDataItem)
 {
     lock (_lock)
     {
         var type = FindType <TClrType>(multipartFormDataItem.ContentType);
         if (type == null)
         {
             return(null);
         }
         var ctors = ReflectionHelper.GetConstructors(type);
         if (ctors.Any(c => c.Parameters.Any(p => p.ParameterType == typeof(MultipartFormDataItem))))
         {
             return((IContentDecoder <TClrType>)Activator.CreateInstance(type, multipartFormDataItem));
         }
         else
         {
             var inst = (IContentDecoder <TClrType>)Activator.CreateInstance(type);
             inst.MultipartFormDataItem = multipartFormDataItem;
             return(inst);
         }
     }
 }
 /// <summary>
 /// Creates image decoder for the form data with content base type "image" and returns <see cref="System.Drawing.Image"/>
 /// </summary>
 /// <param name="multipartFormDataItem">Multi-part form data item</param>
 /// <returns><see cref="ImageContentDecoder"/> decoder</returns>
 public static ImageContentDecoder CreateImageContentDecoder(MultipartFormDataItem multipartFormDataItem)
 {
     return(new ImageContentDecoder(multipartFormDataItem));
 }
 /// <summary>
 /// Creates common text decoder for any content, which can be represented as text data and returns string
 /// </summary>
 /// <param name="multipartFormDataItem">Multi-part form data item</param>
 /// <returns>String content decoder</returns>
 public static IContentDecoder <string> CreateTextDecoder(MultipartFormDataItem multipartFormDataItem)
 {
     return(new TextContentDecoder(multipartFormDataItem));
 }
 /// <summary>
 /// Creates common binary decoder for any content, which is binary by nature and returns byte array
 /// </summary>
 /// <param name="multipartFormDataItem">Multi-part form data item</param>
 /// <returns>Binary content decoder</returns>
 public static IContentDecoder <byte[]> CreateBinaryDecoder(MultipartFormDataItem multipartFormDataItem)
 {
     return(new BinaryContentDecoder(multipartFormDataItem));
 }
 /// <summary>
 /// Creates a new instance of <see cref="MultipartFormXmlDeserializer"/> using specified form data item <paramref name="multipartFormDataItem"/> and text content decoder <paramref name="textContentDecoder"/> (instead of default)
 /// </summary>
 /// <param name="multipartFormDataItem"></param>
 /// <param name="textContentDecoder"></param>
 public MultipartFormXmlDeserializer(MultipartFormDataItem multipartFormDataItem, IContentDecoder <string> textContentDecoder)
 {
     _multipartFormDataItem = multipartFormDataItem;
     _decoder = textContentDecoder;
 }
Esempio n. 7
0
 public abstract IEnumerable <T> Decode(MultipartFormDataItem multipartFormDataItem);
        private MultipartFormData ParseWithBoundary(string boundary, StreamLineReader reader)
        {
            IList <MultipartFormDataItem> content = new List <MultipartFormDataItem>(10);
            MultipartFormDataItem         item    = new MultipartFormDataItem();
            bool found = false;

            reader.Read((c, bytes) =>
            {
                string line = Encoding.ASCII.GetString(bytes);
                if (line.StartsWith("Content-Disposition:"))
                {
                    if (!line.Contains("form-data"))
                    {
                        throw new MultiPartFormParsingException("The stream is not multi-part form data");
                    }
                    var nameMatch     = _nameRegex.Match(line);
                    var filenameMatch = _filenameRegex.Match(line);
                    if (nameMatch.Success)
                    {
                        item.Name = nameMatch.Value;
                    }
                    if (filenameMatch.Success)
                    {
                        item.Filename = filenameMatch.Value;
                    }
                    found = true;
                }
                else if (line.StartsWith("Content-Type:"))
                {
                    var contentTypeMatch = _contentTypeRegex.Match(line);
                    if (contentTypeMatch.Success)
                    {
                        item.ContentType = contentTypeMatch.Value.Trim();
                    }
                    var charsetMatch = _charsetRegex.Match(line);
                    if (charsetMatch.Success)
                    {
                        item.Charset = charsetMatch.Value.Trim();
                    }
                }
                else if (line.StartsWith("Content-Transfer-Encoding:"))
                {
                    var contentTransferEncodingMatch = _contentTransferEncoding.Match(line);
                    if (contentTransferEncodingMatch.Success)
                    {
                        item.ContentTransferEncoding = contentTransferEncodingMatch.Value.Trim();
                    }
                }
                else if (found && string.IsNullOrWhiteSpace(line))
                {
                    item.Content = ReadToNextBoundary(boundary, reader);
                    content.Add(item);
                    item  = new MultipartFormDataItem();
                    found = false;
                }
                return(true);
            });
            return(new MultipartFormData()
            {
                Content = content.ToArray()
            });
        }
Esempio n. 9
0
        /// <summary>
        /// Finds and creates instance of a type which implements <see cref="IContentTransferDecoder{TEnumeration}"/> interface for specific Content-Transfer-Encoding in <paramref name="multipartFormDataItem"/>
        /// </summary>
        /// <typeparam name="TEnumeration">Enumeration of type into which decoding should be perfomed</typeparam>
        /// <param name="multipartFormDataItem">Multi-part form data item</param>
        /// <returns>Instance of a type which decodes form content into enumerable of <see cref="TEnumeration"/> or null, if such not found</returns>
        public static IContentTransferDecoder <TEnumeration> FindAndCreateInstance <TEnumeration>(MultipartFormDataItem multipartFormDataItem)
        {
            if (multipartFormDataItem == null)
            {
                throw new ArgumentNullException("multipartFormDataItem");
            }
            var type = FindType <TEnumeration>(multipartFormDataItem.ContentTransferEncoding);

            if (type == null)
            {
                return(null);
            }
            //if (type != typeof(IContentTransferDecoder<TEnumeration>)) throw new Exception();
            return((IContentTransferDecoder <TEnumeration>)Activator.CreateInstance(type));
        }