/// <summary> /// Add a header with the specified name and value to the header list. /// /// The current implementation knows about the preferred order of most /// well-known headers and will insert headers in that order.In /// addition, it knows that<code> Received</code> headers should be /// inserted in reverse order (newest before oldest), and that they /// should appear at the beginning of the headers, preceeded only by /// a possible<code> Return-Path</code> header. /// /// Note that RFC822 headers can only contain US-ASCII characters. /// </summary> /// <param name="name">header name</param> /// <param name="value">header value</param> public void AddHeader(string name, string value) { int pos = headers.Count; bool addReverse = name.EqualsIgnoreCase("Received") || name.EqualsIgnoreCase("Return-Path"); if (addReverse) { pos = 0; } for (int i = headers.Count - 1; i >= 0; i--) { InternetHeader h = headers[i]; if (name.EqualsIgnoreCase(h.Name)) { if (addReverse) { pos = i; } else { headers.Insert(i + 1, new InternetHeader(name, value)); return; } } // marker for default place to add new headers if (!addReverse && h.Name.Equals(":")) { pos = i; } } headers.Insert(pos, new InternetHeader(name, value)); }
/// <summary> /// Advances the enumerator to the next element in this enumeration? /// </summary> public bool MoveNext() { if (next_Header.IsNull()) { next_Header = nextMatch(); } return(next_Header != null); }
private InternetHeader next_Header; // the next header to be returned /// <summary> /// Constructor. Initialize the enumeration for the entire /// List of headers, the set of headers, whether to return /// matching or non-matching headers, and whether to return /// header lines or Header objects. /// </summary> public MatchEnumerator(IEnumerable <InternetHeader> v, string[] n, bool m, bool l) { e = v.GetEnumerator(); names = n; match = m; want_line = l; next_Header = null; }
/// <summary> /// Remove all header entries that match the given name /// </summary> /// <param name="name">header name</param> public void RemoveHeader(string name) { for (int i = 0; i < headers.Count; i++) { InternetHeader h = headers[i]; if (name.EqualsIgnoreCase(h.Name)) { h.Line = null; } } }
/// <summary> /// Add an RFC822 header line to the header store. /// If the line starts with a space or tab (a continuation line), /// add it to the last header line in the list. Otherwise, /// append the new header line to the list. /// /// Note that RFC822 headers can only contain US-ASCII characters /// /// </summary> /// <param name="line">raw RFC822 header line</param> public void AddHeaderLine(string line) { try { char c = line.CharAt(0); if (c == ' ' || c == '\t') { InternetHeader h = headers[headers.Count - 1]; h.Line = string.Concat(h.Line, Environment.NewLine, line); } else { headers.Add(new InternetHeader(line)); } } catch { } }
private InternetHeader nextMatch() { next: while (e.MoveNext()) { InternetHeader h = e.Current; // skip "place holder" headers if (string.IsNullOrEmpty(h.Line)) { continue; } // if no names to match against, return appropriately if (names.IsNull() || names.Length == 0) { return(match ? null : h); } // check whether this header matches any of the names for (int i = 0; i < names.Length; i++) { if (names[i].EqualsIgnoreCase(h.Name)) { if (match) { return(h); } else { // found a match, but we're // looking for non-matches. // try next header. goto next; } } } // found no matches. if that's what we wanted, return it. if (!match) { return(h); } } return(null); }
/// <summary> /// Return all the values for the specified header. The /// values are String objects. Returns <code>null</code> /// if no headers with the specified name exist. /// /// </summary> /// <param name="name">header name</param> /// <returns>array of header values, or null if none</returns> public string[] GetHeader(string name) { IEnumerator <InternetHeader> e = headers.GetEnumerator(); IList <string> v = new List <string>(); while (e.MoveNext()) { InternetHeader h = e.Current; if (name.EqualsIgnoreCase(h.Name) && !string.IsNullOrEmpty(h.Line)) { v.Add(h.Value); } } if (v.Count == 0) { return(null); } return(v.ToArray()); }
/// <summary> /// Change the first header line that matches name /// to have value, adding a new header if no existing header /// matches. Remove all matching headers but the first. /// /// Note that RFC822 headers can only contain US-ASCII characters /// /// </summary> /// <param name="name">header name</param> /// <param name="value">header value</param> public void SetHeader(string name, string value) { bool found = false; for (int i = 0; i < headers.Count; i++) { InternetHeader h = headers[i]; if (name.EqualsIgnoreCase(h.Name)) { if (!found) { int j; if (!string.IsNullOrEmpty(h.Line) && (j = h.Line.IndexOf(':')) >= 0) { h.Line = string.Concat(h.Line.Substring(0, j + 1), " ", value); // preserves capitalization, spacing } else { h.Line = string.Concat(name, ": ", value); } found = true; } else { headers.RemoveAt(i); i--; } } } if (!found) { AddHeader(name, value); } }