Ejemplo n.º 1
0
        private bool ProcessMultipartData(ref IDictionary <string, string> postVars, ref IDictionary <string, IFile> files)
        {
            bool?result = null;

            try
            {
                var match = _REGEX_MULTIPART_BOUNDARY.Match(this.ContentType ?? string.Empty);
                if (match.Success)
                {
                    postVars = new ConcurrentDictionary <string, string>(EqualityComparerFactory.CreateHttpKeyComparer());
                    files    = new ConcurrentDictionary <string, IFile>(EqualityComparerFactory.CreateHttpKeyComparer());

                    var parser = new HttpMultipartContentParser(this.GetBodyData(),
                                                                Encoding.ASCII.GetBytes("--" + match.Groups[9].Value));

                    foreach (var part in parser.PARTS
                             .Where(p => !string.IsNullOrWhiteSpace(p.NAME)))
                    {
                        try
                        {
                            var data = part.DATA ?? new byte[0];

                            if (string.IsNullOrWhiteSpace(part.FILE_NAME))
                            {
                                // POST data

                                postVars[part.NAME] = Encoding.ASCII.GetString(data);
                            }
                            else
                            {
                                // uploaded file
                                var newFile = new SimpleFile();
                                newFile.SetContentType(string.IsNullOrWhiteSpace(part.CONTENT_TYPE) ? "application/octet-stream" : part.CONTENT_TYPE.ToLower().Trim());
                                newFile.Name = part.FILE_NAME.Trim();
                                newFile.SetData(data);

                                files[part.NAME] = newFile;
                            }
                        }
                        catch
                        {
                            // ignore
                        }
                    }

                    parser = null;
                    result = true;
                }
            }
            catch
            {
                // ignore
            }

            return(result ?? false);
        }