/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> protected internal override void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { if (stream == null) { throw new ArgumentNullException("stream"); } m_pMessage.ToStream(stream, headerWordEncoder, headerParmetersCharset); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { if (m_ParseValue != null) { return(m_ParseValue); } else { if (wordEncoder != null) { return(m_Name + ": " + wordEncoder.Encode(m_Value) + "\r\n"); } else { return(m_Name + ": " + m_Value + "\r\n"); } } }
/// <summary> /// Stores MIME entity to the specified file. /// </summary> /// <param name="file">File name with path where to store MIME entity.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>file</b> is null.</exception> /// <exception cref="ArgumentException">Is raised when any of the arguments has invalid value.</exception> public void ToFile(string file, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { if (file == null) { throw new ArgumentNullException("file"); } if (file == "") { throw new ArgumentException("Argument 'file' value must be specified."); } using (FileStream fs = File.Create(file)) { ToStream(fs, headerWordEncoder, headerParmetersCharset); } }
/// <summary> /// Parses header field from the specified value. /// </summary> /// <param name="value">Header field value. Header field name must be included. For example: 'Content-Type: text/plain'.</param> /// <returns>Returns parsed header field.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>value</b> is null reference.</exception> /// <exception cref="ParseException">Is raised when header field parsing errors.</exception> public static MIME_h_Unstructured Parse(string value) { if (value == null) { throw new ArgumentNullException("value"); } MIME_h_Unstructured retVal = new MIME_h_Unstructured(); string[] name_value = value.Split(new[] { ':' }, 2); if (name_value[0].Trim() == string.Empty) { throw new ParseException("Invalid header field '" + value + "' syntax."); } retVal.m_Name = name_value[0]; retVal.m_Value = MIME_Encoding_EncodedWord.DecodeAll( MIME_Utils.UnfoldHeader(name_value.Length == 2 ? name_value[1].TrimStart() : "")); retVal.m_ParseValue = value; return(retVal); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { return m_ParseValue; }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded. /// If encoding needed, UTF-8 is strongly reccomended if not sure.</param> /// <returns>Returns header field as string.</returns> public abstract string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset);
/// <summary> /// Stores header to the specified file. /// </summary> /// <param name="fileName">File name with optional path.</param> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>fileName</b> is null reference.</exception> public void ToFile(string fileName, MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { if (fileName == null) { throw new ArgumentNullException("fileName"); } using (FileStream fs = File.Create(fileName)) { ToStream(fs, wordEncoder, parmetersCharset); } }
/// <summary> /// Reads RFC 822 'quoted-string' from source stream. /// </summary> /// <returns>Returns RFC 822 'quoted-string' or null if end of stream reached.</returns> /// <exception cref="InvalidOperationException">Is raised when source stream has no quoted-string at current position.</exception> /// <exception cref="ArgumentException">Is raised when not valid 'quoted-string'.</exception> public string QuotedString() { /* RFC 2822 3.2.5. * qtext = NO-WS-CTL / ; Non white space controls * %d33 / ; The rest of the US-ASCII * %d35-91 / ; characters not including "\" * %d93-126 ; or the quote character * qcontent = qtext / quoted-pair * quoted-string = [CFWS] DQUOTE *([FWS] qcontent) [FWS] DQUOTE [CFWS] */ ToFirstChar(); if (Peek(false) != '"') { throw new InvalidOperationException("No quoted-string available."); } // Read start DQUOTE. Char(false); string retVal = ""; bool escape = false; while (true) { int intC = Char(false); // We reached end of stream, invalid quoted string, end quote is missing. if (intC == -1) { throw new ArgumentException("Invalid quoted-string, end quote is missing."); } // This char is escaped. else if (escape) { escape = false; retVal += (char)intC; } // Closing DQUOTE. else if (intC == '"') { break; } // Next char is escaped. else if (intC == '\\') { escape = true; } // Skip folding chars. else if (intC == '\r' || intC == '\n') { } // Normal char in quoted-string. else { retVal += (char)intC; } } return(MIME_Encoding_EncodedWord.DecodeAll(retVal)); }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> protected internal abstract void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset);
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> protected internal override void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { if (stream == null) { throw new ArgumentNullException("stream"); } Net_Utils.StreamCopy(GetEncodedDataStream(), stream, Workaround.Definitions.MaxStreamLineLength); }
public byte[] ToByteArray(MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { using (MemoryStream ms = new MemoryStream()) { ToStream(ms, headerWordEncoder, headerParmetersCharset); return ms.GetBuffer(); } }
/// <summary> /// Store MIME enity to the specified stream. /// </summary> /// <param name="stream">Stream where to store MIME entity. Storing starts form stream current position.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null.</exception> public void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { if (stream == null) { throw new ArgumentNullException("stream"); } m_pHeader.ToStream(stream, headerWordEncoder, headerParmetersCharset); stream.Write(new byte[] {(int) '\r', (int) '\n'}, 0, 2); m_pBody.ToStream(stream, headerWordEncoder, headerParmetersCharset); }
/// <summary> /// Returns MIME header as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <returns>Returns MIME header as string.</returns> public string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { StringBuilder retVal = new StringBuilder(); foreach (MIME_h field in m_pFields) { retVal.Append(field.ToString(wordEncoder, parmetersCharset)); } return retVal.ToString(); }
/// <summary> /// Stores header to the specified stream. /// </summary> /// <param name="stream">Stream where to store header.</param> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> public void ToStream(Stream stream, MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { if (stream == null) { throw new ArgumentNullException("stream"); } byte[] header = Encoding.UTF8.GetBytes(ToString(wordEncoder, parmetersCharset)); stream.Write(header, 0, header.Length); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { if (m_ParseValue != null) { return m_ParseValue; } else { if (wordEncoder != null) { return m_Name + ": " + wordEncoder.Encode(m_Value) + "\r\n"; } else { return m_Name + ": " + m_Value + "\r\n"; } } }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { return(m_ParseValue); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { if (IsModified) { return "Content-Disposition: " + m_DispositionType + m_pParameters.ToString(parmetersCharset) + "\r\n"; } else { return m_ParseValue; } }
/// <summary> /// Stores MIME entity body to the specified stream. /// </summary> /// <param name="stream">Stream where to store body data.</param> /// <param name="headerWordEncoder">Header 8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="headerParmetersCharset">Charset to use to encode 8-bit header parameters. Value null means parameters not encoded.</param> /// <exception cref="ArgumentNullException">Is raised when <b>stream</b> is null reference.</exception> protected internal override void ToStream(Stream stream, MIME_Encoding_EncodedWord headerWordEncoder, Encoding headerParmetersCharset) { if (stream == null) { throw new ArgumentNullException("stream"); } // Set "preamble" text if any. if (!string.IsNullOrEmpty(m_TextPreamble)) { byte[] preableBytes = null; if (m_TextPreamble.EndsWith("\r\n")) { preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble); } else { preableBytes = Encoding.UTF8.GetBytes(m_TextPreamble + "\r\n"); } stream.Write(preableBytes, 0, preableBytes.Length); } for (int i = 0; i < m_pBodyParts.Count; i++) { MIME_Entity bodyPart = m_pBodyParts[i]; // Start new body part. byte[] bStart = Encoding.UTF8.GetBytes("--" + ContentType.Param_Boundary + "\r\n"); stream.Write(bStart, 0, bStart.Length); bodyPart.ToStream(stream, headerWordEncoder, headerParmetersCharset); // Last body part, close boundary. if (i == (m_pBodyParts.Count - 1)) { byte[] bEnd = Encoding.UTF8.GetBytes("--" + ContentType.Param_Boundary + "--\r\n"); stream.Write(bEnd, 0, bEnd.Length); } } // Set "epilogoue" text if any. if (!string.IsNullOrEmpty(m_TextEpilogue)) { byte[] epilogoueBytes = null; if (m_TextEpilogue.EndsWith("\r\n")) { epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue); } else { epilogoueBytes = Encoding.UTF8.GetBytes(m_TextEpilogue + "\r\n"); } stream.Write(epilogoueBytes, 0, epilogoueBytes.Length); } }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { if (!IsModified) { return m_ParseValue; } else { StringBuilder retVal = new StringBuilder(); retVal.Append("Content-Type: " + m_Type + "/" + m_SubType); retVal.Append(m_pParameters.ToString(parmetersCharset)); retVal.Append("\r\n"); return retVal.ToString(); } }