/// <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> /// 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); }