Example #1
0
 public void WriteWholeMessage(MultipartMessage message)
 {
     foreach (MultipartMessagePart part in message.Parts)
     {
         WritePart(part.Headers, part.Data);
     }
     WriteCloseDelimiter();
 }
Example #2
0
        public MultipartMessage ParseMessage(string contentType, Stream inputStream)
        {
            if (log.IsDebugEnabled)
                log.DebugFormat("Message content-type: '{0}'", contentType);

            HeaderField contentTypeField = ParseHeaderField(
                HttpConstants.ContentType,
                contentType);

            MultipartMessage message = new MultipartMessage(contentTypeField);

            if (!message.IsMultipart)
                throw ParserException("This is not a MIME multipart message. The content type '{0}' is not supported", contentType);

            if (log.IsDebugEnabled)
                log.DebugFormat("Multipart message boundary: '{0}'", message.Boundary);

            BinaryBufferWithPatternWatching buffer = new BinaryBufferWithPatternWatching(
                ConstructDelimiterBuffer(message));

            try
            {
                SkipToFirstDelimiter(inputStream, buffer);

                while (true)
                {
                    int markerByte1 = inputStream.ReadByte();
                    int markerByte2 = inputStream.ReadByte();

                    if (markerByte1 == -1 || markerByte2 == -1)
                        throw ParserException("Unexpected end of stream (1).");

                    if ((byte)markerByte1 == '-' && (byte)markerByte2 == '-')
                        break;

                    if ((byte)markerByte1 == '\r' && (byte)markerByte2 == '\n')
                    {
                        MultipartMessagePart part = ReadPart(message, inputStream, buffer);
                        message.AddPart(part);
                    }
                    else
                        throw ParserException("Invalid multipart message: unexpected message part markers.");
                }

                return message;
            }
            catch (EndOfStreamException ex)
            {
                throw new MultipartMessageParserException("Unexpected end of stream.", ex);
            }
        }
Example #3
0
        public MultipartMessage ReadHeader(string contentType)
        {
            HeaderField contentTypeField = ParseHeaderField(
                HttpConstants.ContentType,
                contentType);

            message = new MultipartMessage(contentTypeField);

            if (!message.IsMultipart)
                throw ParserException("This is not a MIME multipart message. The content type '{0}' is not supported", contentType);

            buffer = new BinaryBufferWithPatternWatching(ConstructDelimiterBuffer(message));
            SkipToFirstDelimiter();

            return message;
        }
Example #4
0
        private static MultipartMessagePart ParsePart(
            MultipartMessage message,
            byte[] data,
            int excludedLastBytes)
        {
            using (MemoryStream stream = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    MultipartMessagePart part = ParsePartHeaders(reader, message);

                    // copy the part data into a separate array
                    byte[] partData = new byte[data.Length - excludedLastBytes - stream.Position];
                    Array.Copy(data, stream.Position, partData, 0, partData.Length);
                    part.Data = partData;
                    return(part);
                }
        }
Example #5
0
        public MultipartMessage ReadHeader(string contentType)
        {
            HeaderField contentTypeField = ParseHeaderField(
                HttpConstants.ContentType,
                contentType);

            message = new MultipartMessage(contentTypeField);

            if (!message.IsMultipart)
            {
                throw ParserException("This is not a MIME multipart message. The content type '{0}' is not supported", contentType);
            }

            buffer = new BinaryBufferWithPatternWatching(ConstructDelimiterBuffer(message));
            SkipToFirstDelimiter();

            return(message);
        }
Example #6
0
        private static MultipartMessagePart ParsePartHeaders(BinaryReader reader, MultipartMessage message)
        {
            MultipartMessagePart part = new MultipartMessagePart();

            while (true)
            {
                byte[] bytes = reader.ReadUntilCRLF();

                // did we reach the end of the headers list?
                if (bytes.Length == 0)
                {
                    break;
                }

                HeaderField header = HeaderField.Parse(bytes, message.BaseEncoding);
                part.AddHeader(header);
            }

            return(part);
        }
        public void WriteMessageAsAWhole()
        {
            string boundary = "-----Test";

            MultipartMessage message = new MultipartMessage(HeaderField.Parse("Content-type: multipart/mixed; boundary=-----Test"));

            MultipartMessagePart part;

            part = new MultipartMessagePart();
            part.AddHeader(new HeaderField("Content-Type", "text/plain"));
            part.Data = Encoding.UTF8.GetBytes("my text");
            message.AddPart(part);

            part = new MultipartMessagePart();
            part.AddHeader(new HeaderField("Content-Type", "application/xml"));
            part.Data = Encoding.UTF8.GetBytes("<tag/>");
            message.AddPart(part);

            using (MemoryStream stream = new MemoryStream())
            {
                long writtenBytes;
                using (MultipartStreamWriter writer = new MultipartStreamWriter(
                    stream,
                    boundary,
                    Encoding.UTF8))
                {
                    writer.WriteWholeMessage(message);
                    writtenBytes = writer.BytesWritten;
                }

                byte[] bytes = stream.ToArray();
                Assert.AreEqual(bytes.Length, writtenBytes);
                string result = Encoding.UTF8.GetString(bytes);
                Assert.AreEqual("\r\n-------Test\r\nContent-Type: text/plain\r\n\r\nmy text\r\n-------Test\r\nContent-Type: application/xml\r\n\r\n<tag/>\r\n-------Test--", result);
            }
        }
Example #8
0
        private static MultipartMessagePart ReadPart(
            MultipartMessage message,
            Stream inputStream,
            BinaryBufferWithPatternWatching buffer)
        {
            buffer.Start();

            while (true)
            {
                int value = inputStream.ReadByte();
                if (value == -1)
                {
                    throw ParserException("Unexpected end of stream (3).");
                }

                byte byteValue      = (byte)value;
                bool patternMatched = buffer.WriteByte(byteValue);

                if (!patternMatched)
                {
                    continue;
                }

                MultipartMessagePart part = ParsePart(
                    message,
                    buffer.ToArray(),
                    buffer.Pattern.Length);

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Found part '{0}'", message.BaseEncoding.GetString(part.Data));
                }

                return(part);
            }
        }
Example #9
0
        private static MultipartMessagePart ParsePart(
            MultipartMessage message,
            byte[] data,
            int excludedLastBytes)
        {
            using (MemoryStream stream = new MemoryStream(data))
            using (BinaryReader reader = new BinaryReader(stream))
            {
                MultipartMessagePart part = ParsePartHeaders(reader, message);

                // copy the part data into a separate array
                byte[] partData = new byte[data.Length - excludedLastBytes - stream.Position];
                Array.Copy(data, stream.Position, partData, 0, partData.Length);
                part.Data = partData;
                return part;
            }
        }
Example #10
0
 private static byte[] ConstructDelimiterBuffer(MultipartMessage message)
 {
     string value = "\r\n--" + message.Boundary;
     return message.BaseEncoding.GetBytes(value);
 }
Example #11
0
        private static MultipartMessagePart ReadPart(
            MultipartMessage message,
            Stream inputStream,
            BinaryBufferWithPatternWatching buffer)
        {
            buffer.Start();

            while (true)
            {
                int value = inputStream.ReadByte();
                if (value == -1)
                    throw ParserException("Unexpected end of stream (3).");

                byte byteValue = (byte)value;
                bool patternMatched = buffer.WriteByte(byteValue);

                if (!patternMatched)
                    continue;

                MultipartMessagePart part = ParsePart(
                    message,
                    buffer.ToArray(),
                    buffer.Pattern.Length);

                if (log.IsDebugEnabled)
                    log.DebugFormat("Found part '{0}'", message.BaseEncoding.GetString(part.Data));

                return part;
            }
        }
Example #12
0
        private static MultipartMessagePart ParsePartHeaders(BinaryReader reader, MultipartMessage message)
        {
            MultipartMessagePart part = new MultipartMessagePart();

            while (true)
            {
                byte[] bytes = reader.ReadUntilCRLF();

                // did we reach the end of the headers list?
                if (bytes.Length == 0)
                    break;

                HeaderField header = HeaderField.Parse(bytes, message.BaseEncoding);
                part.AddHeader(header);
            }

            return part;
        }
Example #13
0
        private static byte[] ConstructDelimiterBuffer(MultipartMessage message)
        {
            string value = "\r\n--" + message.Boundary;

            return(message.BaseEncoding.GetBytes(value));
        }
Example #14
0
 public void WriteWholeMessage(MultipartMessage message)
 {
     foreach (MultipartMessagePart part in message.Parts)
         WritePart(part.Headers, part.Data);
     WriteCloseDelimiter();
 }
Example #15
0
        public MultipartMessage ParseMessage(string contentType, Stream inputStream)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Message content-type: '{0}'", contentType);
            }

            HeaderField contentTypeField = ParseHeaderField(
                HttpConstants.ContentType,
                contentType);

            MultipartMessage message = new MultipartMessage(contentTypeField);

            if (!message.IsMultipart)
            {
                throw ParserException("This is not a MIME multipart message. The content type '{0}' is not supported", contentType);
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Multipart message boundary: '{0}'", message.Boundary);
            }

            BinaryBufferWithPatternWatching buffer = new BinaryBufferWithPatternWatching(
                ConstructDelimiterBuffer(message));

            try
            {
                SkipToFirstDelimiter(inputStream, buffer);

                while (true)
                {
                    int markerByte1 = inputStream.ReadByte();
                    int markerByte2 = inputStream.ReadByte();

                    if (markerByte1 == -1 || markerByte2 == -1)
                    {
                        throw ParserException("Unexpected end of stream (1).");
                    }

                    if ((byte)markerByte1 == '-' && (byte)markerByte2 == '-')
                    {
                        break;
                    }

                    if ((byte)markerByte1 == '\r' && (byte)markerByte2 == '\n')
                    {
                        MultipartMessagePart part = ReadPart(message, inputStream, buffer);
                        message.AddPart(part);
                    }
                    else
                    {
                        throw ParserException("Invalid multipart message: unexpected message part markers.");
                    }
                }

                return(message);
            }
            catch (EndOfStreamException ex)
            {
                throw new MultipartMessageParserException("Unexpected end of stream.", ex);
            }
        }