예제 #1
0
        /// <summary>
        /// Deserialize the content from the stream.
        /// </summary>
        /// <param name="contentType">Used to identify the object which is about to be deserialized. Specified by the <c>Serialize()</c> method when invoked in the other end point.</param>
        /// <param name="source">Stream that contains the object to deserialize.</param>
        /// <returns>Created object</returns>
        /// <exception cref="SerializationException">Deserialization failed</exception>
        public object Deserialize(string contentType, Stream source)
        {
            if (contentType == null)
            {
                throw new ArgumentNullException("contentType");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (!contentType.StartsWith(MimeType, StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            try
            {
                var result = new FormAndFilesResult()
                {
                    Form = new ParameterCollection()
                };
                var decoder = new UrlDecoder();
                decoder.Parse(new StreamReader(source), result.Form);
                source.Position = 0;
                return(result);
            }
            catch (ArgumentException err)
            {
                throw new DecoderFailureException(err.Message, err);
            }
        }
        /// <summary>
        /// Deserialize the content from the stream.
        /// </summary>
        /// <param name="contentType">Used to identify the object which is about to be deserialized. Specified by the <c>Serialize()</c> method when invoked in the other end point.</param>
        /// <param name="source">Stream that contains the object to deserialize.</param>
        /// <returns>Created object</returns>
        /// <exception cref="SerializationException">Deserialization failed</exception>
        public object Deserialize(string contentType, Stream source)
        {
            if (contentType == null) throw new ArgumentNullException("contentType");
            if (source == null) throw new ArgumentNullException("source");

            if (!contentType.StartsWith(MimeType, StringComparison.OrdinalIgnoreCase))
                return null;

            try
            {
                var result = new FormAndFilesResult()
                {
                    Form = new ParameterCollection()
                };
                var decoder = new UrlDecoder();
                decoder.Parse(new StreamReader(source), result.Form);
                source.Position = 0;
                return result;
            }
            catch (ArgumentException err)
            {
                throw new DecoderFailureException(err.Message, err);
            }
        }
예제 #3
0
        /// <summary>
        /// Deserialize the content from the stream.
        /// </summary>
        /// <param name="contentType">Used to identify the object which is about to be deserialized. Specified by the <c>Serialize()</c> method when invoked in the other end point.</param>
        /// <param name="source">Stream that contains the object to deserialize.</param>
        /// <returns>Created object</returns>
        /// <exception cref="SerializationException">Deserialization failed</exception>
        public object Deserialize(string contentType, Stream source)
        {
            if (contentType == null)
            {
                throw new ArgumentNullException("contentType");
            }
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (!contentType.StartsWith(MimeType, StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            var result = new FormAndFilesResult()
            {
                Form  = new ParameterCollection(),
                Files = new HttpFileCollection()
            };
            var contentTypeHeader = new HttpHeaderValue(contentType);
            var encodingStr       = contentTypeHeader.Parameters["charset"];
            var encoding          = encodingStr != null?Encoding.GetEncoding(encodingStr) : Encoding.UTF8;

            //multipart/form-data, boundary=AaB03x
            var boundary = contentTypeHeader.Parameters.Get("boundary");

            if (boundary == null)
            {
                throw new DecoderFailureException("Missing boundary in content type: " + contentType);
            }

            var multipart = new HttpMultipart(source, boundary.Value, encoding);

            HttpMultipart.Element element;

            while ((element = multipart.ReadNextElement()) != null)
            {
                if (string.IsNullOrEmpty(element.Name))
                {
                    throw new DecoderFailureException(string.Format("Missing value name.\nElement: {0}", element));
                }

                if (!string.IsNullOrEmpty(element.Filename))
                {
                    if (string.IsNullOrEmpty(element.ContentType))
                    {
                        throw new DecoderFailureException(string.Format("Value '{0}' lacks a content type.",
                                                                        element.Name));
                    }

                    // Read the file data
                    var buffer = new byte[element.Length];
                    source.Seek(element.Start, SeekOrigin.Begin);
                    source.Read(buffer, 0, (int)element.Length);

                    // Generate a filename

                    var applicationData = Windows.Storage.ApplicationData.Current;
                    var temporaryFolder = applicationData.LocalCacheFolder;

                    var originalFileName = element.Filename;

                    // if the internet path doesn't exist, assume mono and /var/tmp
                    var path = string.IsNullOrEmpty(temporaryFolder.Path)
                        ? Path.Combine("var", "tmp")
                        : Path.Combine(temporaryFolder.Path.Replace("\\\\", "\\"), "tmp");

                    element.Filename = Path.Combine(path, Math.Abs(element.Filename.GetHashCode()) + ".tmp");

                    // If the file exists generate a new filename
                    while (File.Exists(element.Filename))
                    {
                        element.Filename = Path.Combine(path, Math.Abs(element.Filename.GetHashCode() + 1) + ".tmp");
                    }

                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    File.WriteAllBytes(element.Filename, buffer);

                    var file = new HttpFile
                    {
                        Name             = element.Name,
                        OriginalFileName = originalFileName,
                        ContentType      = element.ContentType,
                        TempFileName     = element.Filename
                    };
                    result.Files.Add(file);
                }
                else
                {
                    var buffer = new byte[element.Length];
                    source.Seek(element.Start, SeekOrigin.Begin);
                    source.Read(buffer, 0, (int)element.Length);

                    result.Form.Add(Uri.UnescapeDataString(element.Name), encoding.GetString(buffer));
                }
            }

            return(result);
        }
        /// <summary>
        /// Deserialize the content from the stream.
        /// </summary>
        /// <param name="contentType">Used to identify the object which is about to be deserialized. Specified by the <c>Serialize()</c> method when invoked in the other end point.</param>
        /// <param name="source">Stream that contains the object to deserialize.</param>
        /// <returns>Created object</returns>
        /// <exception cref="SerializationException">Deserialization failed</exception>
        public object Deserialize(string contentType, Stream source)
        {
            if (contentType == null) throw new ArgumentNullException("contentType");
            if (source == null) throw new ArgumentNullException("source");

            if (!contentType.StartsWith(MimeType, StringComparison.OrdinalIgnoreCase))
                return null;

            var result = new FormAndFilesResult()
            {
                Form = new ParameterCollection(),
                Files = new HttpFileCollection()
            };
            var contentTypeHeader = new HttpHeaderValue(contentType);
            var encodingStr = contentTypeHeader.Parameters["charset"];
            var encoding = encodingStr != null ? Encoding.GetEncoding(encodingStr) : Encoding.UTF8;

            //multipart/form-data, boundary=AaB03x
            var boundary = contentTypeHeader.Parameters.Get("boundary");
            if (boundary == null)
                throw new DecoderFailureException("Missing boundary in content type: " + contentType);

            var multipart = new HttpMultipart(source, boundary.Value, encoding);
            HttpMultipart.Element element;
            while ((element = multipart.ReadNextElement()) != null)
            {
                if (string.IsNullOrEmpty(element.Name))
                    throw new DecoderFailureException(string.Format("Missing value name.\nElement: {0}", element));

                if (!string.IsNullOrEmpty(element.Filename))
                {
                    if (string.IsNullOrEmpty(element.ContentType))
                        throw new DecoderFailureException(string.Format("Value '{0}' lacks a content type.",
                            element.Name));

                    // Read the file data
                    var buffer = new byte[element.Length];
                    source.Seek(element.Start, SeekOrigin.Begin);
                    source.Read(buffer, 0, (int)element.Length);

                    // Generate a filename
                    var originalFileName = element.Filename;
                    var internetCache = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);

                    // if the internet path doesn't exist, assume mono and /var/tmp
                    var path = string.IsNullOrEmpty(internetCache)
                        ? Path.Combine("var", "tmp")
                        : Path.Combine(internetCache.Replace("\\\\", "\\"), "tmp");

                    element.Filename = Path.Combine(path, Math.Abs(element.Filename.GetHashCode()) + ".tmp");

                    // If the file exists generate a new filename
                    while (File.Exists(element.Filename))
                        element.Filename = Path.Combine(path, Math.Abs(element.Filename.GetHashCode() + 1) + ".tmp");

                    if (!Directory.Exists(path))
                        Directory.CreateDirectory(path);

                    File.WriteAllBytes(element.Filename, buffer);

                    var file = new HttpFile
                    {
                        Name = element.Name,
                        OriginalFileName = originalFileName,
                        ContentType = element.ContentType,
                        TempFileName = element.Filename
                    };
                    result.Files.Add(file);
                }
                else
                {
                    var buffer = new byte[element.Length];
                    source.Seek(element.Start, SeekOrigin.Begin);
                    source.Read(buffer, 0, (int)element.Length);

                    result.Form.Add(Uri.UnescapeDataString(element.Name), encoding.GetString(buffer));
                }
            }


            return result;
        }