コード例 #1
0
ファイル: Parser.cs プロジェクト: pengyancai/cs-util
        private static MimeHeaderCollection ParseHeader(LineOrientedStream stream)
        {
            var headers = new MimeHeaderCollection();
              MimeHeader current = null;

              for (;;) {
            var lineBytes = stream.ReadLine(false);

            if (lineBytes == null)
              break; // unexpected end of stream

            var line = new ByteString(lineBytes);

            if (line.IsEmpty)
              break; // end of headers

            if (line[0] == Octets.HT || line[0] == Octets.SP) { // LWSP-char
              // folding
              if (current == null)
            // ignore incorrect formed header
            continue;

              current.Value += Chars.SP;
              current.Value += line.TrimStart().ToString();
            }
            else {
              // field       =  field-name ":" [ field-body ] CRLF
              // field-name  =  1*<any CHAR, excluding CTLs, SPACE, and ":">
              var delim = line.IndexOf(MimeHeader.NameBodyDelimiter); // ':'

              if (delim < 0) {
            // ignore incorrect formed header
            current = null;
            continue;
              }

              var header = new MimeHeader(line.Substring(0, delim).TrimEnd().ToString(),
                                      line.Substring(delim + 1).TrimStart().ToString());

              headers.Add(header);

              current = header;
            }
              }

              return headers;
        }