public HeaderField GetHeader(string headerName) { HeaderField header = headers.Find(x => x.IsName(headerName)); if (header == null) { string message = string.Format( CultureInfo.InvariantCulture, "Header '{0}' not found.", headerName); throw new KeyNotFoundException(message); } return(header); }
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); }
public static HeaderField Parse(string value) { Match match = headerRegex.Match(value); if (!match.Success) throw new FormatException(); string fieldName = match.Groups["name"].Value.Trim(); string fieldValue = match.Groups["value"].Value.Trim(); HeaderField field = new HeaderField(fieldName, fieldValue); for (int i = 0; i < match.Groups["pname"].Captures.Count; i++) { string parameterName = match.Groups["pname"].Captures[i].Value.Trim(); string parameterValue = match.Groups["pvalue"].Captures[i].Value.Trim(); field.AddParameter(parameterName, parameterValue); } return field; }
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 static HeaderField Parse(string value) { Match match = headerRegex.Match(value); if (!match.Success) { throw new FormatException(); } string fieldName = match.Groups["name"].Value.Trim(); string fieldValue = match.Groups["value"].Value.Trim(); HeaderField field = new HeaderField(fieldName, fieldValue); for (int i = 0; i < match.Groups["pname"].Captures.Count; i++) { string parameterName = match.Groups["pname"].Captures[i].Value.Trim(); string parameterValue = match.Groups["pvalue"].Captures[i].Value.Trim(); field.AddParameter(parameterName, parameterValue); } return(field); }
private static HeaderField ParseHeaderField(string value) { return(HeaderField.Parse(value)); }
public void AddHeader(HeaderField header) { headers.Add(header); }
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 MultipartMessage(HeaderField contentType) { this.contentType = contentType; }
public void HeaderNamesAreCaseInsensitive() { HeaderField field = new HeaderField(HttpConstants.ContentType, "value"); Assert.IsTrue(field.IsName("Content-type")); }