private MultipartMessagePart ReadPart() { 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( buffer.ToArray(), buffer.Pattern.Length); if (log.IsDebugEnabled) { log.DebugFormat("Found new part (content length: '{0}')", part.Data.Length); } return(part); } }
public MultipartMessagePart ReadNextPart() { try { 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 == '-') { return(null); } if ((byte)markerByte1 == '\r' && (byte)markerByte2 == '\n') { MultipartMessagePart part = ReadPart(); return(part); } throw ParserException("Invalid multipart message: unexpected message part markers."); } catch (EndOfStreamException ex) { throw new MultipartMessageParserException("Unexpected end of stream.", ex); } }
private MultipartMessagePart ParsePart( byte[] data, int excludedLastBytes) { using (MemoryStream stream = new MemoryStream(data)) using (BinaryReader reader = new BinaryReader(stream)) { MultipartMessagePart part = ParsePartHeaders(reader); // 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); } }
private MultipartMessagePart ParsePartHeaders(BinaryReader reader) { 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); } }
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); } }
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 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); } }
public void AddPart(MultipartMessagePart part) { parts.Add(part); }
public void AddPart(MultipartMessagePart part) { parts.Add(part); }