Exemplo n.º 1
0
		static byte[] EncodeReceivedHeader (ParserOptions options, FormatOptions format, Encoding charset, string field, string value)
		{
			var tokens = new List<ReceivedTokenValue> ();
			var rawValue = charset.GetBytes (value);
			var encoded = new StringBuilder ();
			int lineLength = field.Length + 1;
			bool date = false;
			int index = 0;
			int count = 0;

			while (index < rawValue.Length) {
				ReceivedTokenValue token = null;
				int startIndex = index;

				if (!ParseUtils.SkipCommentsAndWhiteSpace (rawValue, ref index, rawValue.Length, false) || index >= rawValue.Length) {
					tokens.Add (new ReceivedTokenValue (startIndex, index - startIndex));
					break;
				}

				while (index < rawValue.Length && !rawValue[index].IsWhitespace ())
					index++;

				var atom = charset.GetString (rawValue, startIndex, index - startIndex);

				for (int i = 0; i < ReceivedTokens.Length; i++) {
					if (atom == ReceivedTokens[i].Atom) {
						ReceivedTokens[i].Skip (rawValue, ref index);

						if (ParseUtils.SkipCommentsAndWhiteSpace (rawValue, ref index, rawValue.Length, false)) {
							if (index < rawValue.Length && rawValue[index] == (byte) ';') {
								date = true;
								index++;
							}
						}

						token = new ReceivedTokenValue (startIndex, index - startIndex);
						break;
					}
				}

				if (token == null) {
					if (ParseUtils.SkipCommentsAndWhiteSpace (rawValue, ref index, rawValue.Length, false)) {
						while (index < rawValue.Length && !rawValue[index].IsWhitespace ())
							index++;
					}

					token = new ReceivedTokenValue (startIndex, index - startIndex);
				}

				tokens.Add (token);

				ParseUtils.SkipWhiteSpace (rawValue, ref index, rawValue.Length);

				if (date && index < rawValue.Length) {
					// slurp up the date (the final token)
					tokens.Add (new ReceivedTokenValue (index, rawValue.Length - index));
					break;
				}
			}

			foreach (var token in tokens) {
				var text = charset.GetString (rawValue, token.StartIndex, token.Length).TrimEnd ();

				if (count > 0 && lineLength + text.Length + 1 > format.MaxLineLength) {
					encoded.Append (format.NewLine);
					encoded.Append ('\t');
					lineLength = 1;
					count = 0;
				} else {
					encoded.Append (' ');
					lineLength++;
				}

				lineLength += text.Length;
				encoded.Append (text);
				count++;
			}

			encoded.Append (format.NewLine);

			return charset.GetBytes (encoded.ToString ());
		}
Exemplo n.º 2
0
        static byte[] EncodeReceivedHeader(ParserOptions options, FormatOptions format, Encoding charset, string field, string value)
        {
            var  tokens     = new List <ReceivedTokenValue> ();
            var  rawValue   = charset.GetBytes(value);
            var  encoded    = new StringBuilder();
            int  lineLength = field.Length + 1;
            bool date       = false;
            int  index      = 0;
            int  count      = 0;

            while (index < rawValue.Length)
            {
                ReceivedTokenValue token = null;
                int startIndex           = index;

                if (!ParseUtils.SkipCommentsAndWhiteSpace(rawValue, ref index, rawValue.Length, false) || index >= rawValue.Length)
                {
                    tokens.Add(new ReceivedTokenValue(startIndex, index - startIndex));
                    break;
                }

                while (index < rawValue.Length && !rawValue[index].IsWhitespace())
                {
                    index++;
                }

                var atom = charset.GetString(rawValue, startIndex, index - startIndex);

                for (int i = 0; i < ReceivedTokens.Length; i++)
                {
                    if (atom == ReceivedTokens[i].Atom)
                    {
                        ReceivedTokens[i].Skip(rawValue, ref index);

                        if (ParseUtils.SkipCommentsAndWhiteSpace(rawValue, ref index, rawValue.Length, false))
                        {
                            if (index < rawValue.Length && rawValue[index] == (byte)';')
                            {
                                date = true;
                                index++;
                            }
                        }

                        token = new ReceivedTokenValue(startIndex, index - startIndex);
                        break;
                    }
                }

                if (token == null)
                {
                    if (ParseUtils.SkipCommentsAndWhiteSpace(rawValue, ref index, rawValue.Length, false))
                    {
                        while (index < rawValue.Length && !rawValue[index].IsWhitespace())
                        {
                            index++;
                        }
                    }

                    token = new ReceivedTokenValue(startIndex, index - startIndex);
                }

                tokens.Add(token);

                ParseUtils.SkipWhiteSpace(rawValue, ref index, rawValue.Length);

                if (date && index < rawValue.Length)
                {
                    // slurp up the date (the final token)
                    tokens.Add(new ReceivedTokenValue(index, rawValue.Length - index));
                    break;
                }
            }

            foreach (var token in tokens)
            {
                var text = charset.GetString(rawValue, token.StartIndex, token.Length).TrimEnd();

                if (count > 0 && lineLength + text.Length + 1 > format.MaxLineLength)
                {
                    encoded.Append(format.NewLine);
                    encoded.Append('\t');
                    lineLength = 1;
                    count      = 0;
                }
                else
                {
                    encoded.Append(' ');
                    lineLength++;
                }

                lineLength += text.Length;
                encoded.Append(text);
                count++;
            }

            encoded.Append(format.NewLine);

            return(charset.GetBytes(encoded.ToString()));
        }