Example #1
0
        /// <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_ContentDisposition Parse(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // We should not have encoded words here, but some email clients do this, so encoded them if any.
            string valueDecoded = MIME_Encoding_EncodedWord.DecodeS(value);

            MIME_h_ContentDisposition retVal = new MIME_h_ContentDisposition();

            string[] name_value = valueDecoded.Split(new char[] { ':' }, 2);
            if (name_value.Length != 2)
            {
                throw new ParseException("Invalid Content-Type: header field value '" + value + "'.");
            }

            MIME_Reader r    = new MIME_Reader(name_value[1]);
            string      type = r.Token();

            if (type == null)
            {
                throw new ParseException("Invalid Content-Disposition: header field value '" + value + "'.");
            }
            retVal.m_DispositionType = type.Trim();

            retVal.m_pParameters.Parse(r);

            retVal.m_ParseValue = value;

            return(retVal);
        }
Example #2
0
        /// <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_ContentType Parse(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // We should not have encoded words here, but some email clients do this, so encoded them if any.
            string valueDecoded = MIME_Encoding_EncodedWord.DecodeS(value);

            MIME_h_ContentType retVal = new MIME_h_ContentType();

            string[] name_value = valueDecoded.Split(new char[] { ':' }, 2);
            if (name_value.Length != 2)
            {
                throw new ParseException("Invalid Content-Type: header field value '" + value + "'.");
            }

            MIME_Reader r    = new MIME_Reader(name_value[1]);
            string      type = r.Token();

            if (type == null)
            {
                throw new ParseException("Invalid Content-Type: header field value '" + value + "'.");
            }
            retVal.m_Type = type;

            if (r.Char(false) != '/')
            {
                throw new ParseException("Invalid Content-Type: header field value '" + value + "'.");
            }

            string subtype = r.Token();

            if (subtype == null)
            {
                throw new ParseException("Invalid Content-Type: header field value '" + value + "'.");
            }
            retVal.m_SubType = subtype;

            if (r.Available > 0)
            {
                retVal.m_pParameters.Parse(r);
            }

            retVal.m_ParseValue = value;
            retVal.m_IsModified = false;

            return(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 char[] { ':' }, 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.DecodeS(MIME_Utils.UnfoldHeader(name_value.Length == 2 ? name_value[1].TrimStart() : ""));

            retVal.m_ParseValue = value;

            return(retVal);
        }
        /// <summary>
        /// Parses parameters from the specified reader.
        /// </summary>
        /// <param name="reader">MIME reader.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>reader</b> is null reference.</exception>
        public void Parse(MIME_Reader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            /* RFC 2231.
             */

            while (true)
            {
                // End os stream reached.
                if (reader.Peek(true) == -1)
                {
                    break;
                }
                // Next parameter start, just eat that char.
                else if (reader.Peek(true) == ';')
                {
                    reader.Char(false);
                }
                else
                {
                    string name = reader.Token();

                    string value = "";
                    // Parameter value specified.
                    if (reader.Peek(true) == '=')
                    {
                        reader.Char(false);

                        string v = reader.Word();
                        // Normally value may not be null, but following case: paramName=EOS.
                        if (v != null)
                        {
                            value = v;
                        }
                    }

                    // RFC 2231 encoded/splitted parameter.
                    if (name.IndexOf('*') > -1)
                    {
                        string[] name_x_no_x = name.Split('*');
                        name = name_x_no_x[0];

                        Encoding      charset     = Encoding.ASCII;
                        StringBuilder valueBuffer = new StringBuilder();
                        // We must have charset'language'value.
                        // Examples:
                        //      URL*=utf-8''test;
                        //      URL*0*=utf-8''"test";
                        if ((name_x_no_x.Length == 2 && name_x_no_x[1] == "") || name_x_no_x.Length == 3)
                        {
                            string[] charset_language_value = value.Split('\'');
                            charset = Encoding.GetEncoding(charset_language_value[0]);
                            valueBuffer.Append(DecodeExtOctet(charset_language_value[2], charset));
                        }
                        // No encoding, probably just splitted ASCII value.
                        // Example:
                        //     URL*0="value1";
                        //     URL*1="value2";
                        else
                        {
                            valueBuffer.Append(value);
                        }

                        // Read while value continues.
                        while (true)
                        {
                            // End os stream reached.
                            if (reader.Peek(true) == -1)
                            {
                                break;
                            }
                            // Next parameter start, just eat that char.
                            else if (reader.Peek(true) == ';')
                            {
                                reader.Char(false);
                            }
                            else
                            {
                                if (!reader.StartsWith(name + "*"))
                                {
                                    break;
                                }
                                reader.Token();

                                // Parameter value specified.
                                if (reader.Peek(true) == '=')
                                {
                                    reader.Char(false);

                                    string v = reader.Word();
                                    // Normally value may not be null, but following case: paramName=EOS.
                                    if (v != null)
                                    {
                                        valueBuffer.Append(DecodeExtOctet(v, charset));
                                    }
                                }
                            }
                        }

                        this[name] = valueBuffer.ToString();
                    }
                    // Regular parameter.
                    else
                    {
                        this[name] = MIME_Encoding_EncodedWord.DecodeS(value);
                    }
                }
            }

            m_IsModified = false;
        }