public object ReadFrom(IHttpEntity request, IType destinationType, string parameterName)
        {
            if (destinationType.IsAssignableFrom<IEnumerable<IMultipartHttpEntity>>())
            {
                var multipartReader = new MultipartReader(request.ContentType.Boundary, request.Stream);
                return multipartReader.GetParts();
            }

            if (destinationType.IsAssignableFrom<IDictionary<string, IList<IMultipartHttpEntity>>>())
            {
                return FormData(request);
            }

            var binder = BinderLocator.GetBinder(destinationType);
            
            if (binder == null)
            {
                throw new InvalidOperationException("Cannot find a binder to create the object");
            }

            binder.Prefixes.Add(parameterName);
            bool wasAnyKeyUsed = ReadKeyValues(request).Aggregate(false, (wasUsed, kv) => kv.SetProperty(binder) || wasUsed);
            var result = binder.BuildObject();

            return wasAnyKeyUsed && result.Successful ? result.Instance : Missing.Value;
        }
            parsing_a_message_with_three_parts_and_skipping_the_first_one_gets_the_content_of_the_second_properly()
        {
            MultipartReader reader = new MultipartReader("boundary42",
                                                         new MemoryStream(
                                                             Encoding.ASCII.GetBytes(TEXT_RFC1521_MULTIPART_MANY)));

            var enumerator = reader.GetParts().GetEnumerator();
            enumerator.MoveNext();
            enumerator.MoveNext();
            CheckEntity(enumerator.Current, new MediaType("text/richtext"), "text");
            enumerator.Dispose();
        }
        public void a_part_with_no_headers_has_its_content_parsed_correctly_and_no_header()
        {
            MultipartReader reader = new MultipartReader("boundary42",
                                                         new MemoryStream(Encoding.ASCII.GetBytes(TEXT_MULTIPART_NOHEAD)));

            var enumerator = reader.GetParts().GetEnumerator();
            enumerator.MoveNext()
                .ShouldBeTrue();

            enumerator.Current.Headers.Count.ShouldBe(0);
            TheTextIn(enumerator.Current.Stream).ShouldBe("text");
        }
 IDictionary<string, IList<IMultipartHttpEntity>> PreLoadAllParts(IHttpEntity source)
 {
     var multipartReader = new MultipartReader(source.ContentType.Boundary, source.Stream)
     {
         Log = Log
     };
     var formData =
         new NullBehaviorDictionary<string, IList<IMultipartHttpEntity>>(StringComparer.OrdinalIgnoreCase);
     foreach (var requestPart in multipartReader.GetParts())
     {
         if (requestPart.Headers.ContentDisposition != null &&
             requestPart.Headers.ContentDisposition.Disposition.EqualsOrdinalIgnoreCase("form-data"))
         {
             var memoryStream = new MemoryStream();
             int totalRead = 0, lastRead;
             while ((lastRead = requestPart.Stream.Read(_buffer, 0, _buffer.Length)) > 0)
             {
                 totalRead += lastRead;
                 if (totalRead > REQUEST_LENGTH_TRESHOLD)
                 {
                     string filePath;
                     using (var fileStream = CreateTempFile(out filePath))
                     {
                         memoryStream.Position = 0;
                         var memoryStreamLength = memoryStream.CopyTo(fileStream);
                         fileStream.Write(_buffer, 0, lastRead);
                         var leftoverLength = requestPart.Stream.CopyTo(fileStream);
                     }
                     memoryStream = null;
                     requestPart.SwapStream(filePath);
                     break;
                 }
                 memoryStream.Write(_buffer, 0, lastRead);
             }
             if (memoryStream != null)
             {
                 memoryStream.Position = 0;
                 requestPart.SwapStream(memoryStream);
             }
             var listOfEntities = formData[requestPart.Headers.ContentDisposition.Name]
                                  ??
                                  (formData[requestPart.Headers.ContentDisposition.Name] = new List<IMultipartHttpEntity>());
             if (requestPart.ContentType == null)
                 requestPart.ContentType = MediaType.TextPlain;
             listOfEntities.Add(requestPart);
         }
     }
     return formData;
 }
        public void parsing_an_entity_with_three_messages_returns_three_objects()
        {
            MultipartReader reader = new MultipartReader("boundary42",
                                                         new MemoryStream(
                                                             Encoding.ASCII.GetBytes(TEXT_RFC1521_MULTIPART_MANY)));

            var enumerator = reader.GetParts().GetEnumerator();
            enumerator.MoveNext();

            CheckEntity(enumerator.Current, new MediaType("text/plain") {CharSet = "us-ascii"}, "text\r\n");

            enumerator.MoveNext();
            CheckEntity(enumerator.Current, new MediaType("text/richtext"), "text");

            enumerator.MoveNext();
            CheckEntity(enumerator.Current, new MediaType("text/x-whatever"), "text\r\n...");

            enumerator.MoveNext().ShouldBeFalse();
            enumerator.Dispose();
        }