private static void EncodeToBuffer(string value, StringBuilder builder, bool allowUnicode) { Encoding encoding = MimeBasePart.DecodeEncoding(value); if (encoding != null) // Manually encoded elsewhere, pass through { builder.Append('"').Append(value).Append('"'); } else if ((allowUnicode && !MailBnfHelper.HasCROrLF(value)) || // Unicode without CL or LF's MimeBasePart.IsAscii(value, false)) // Ascii { MailBnfHelper.GetTokenOrQuotedString(value, builder, allowUnicode); } else { // MIME Encoding required encoding = Encoding.GetEncoding(MimeBasePart.DefaultCharSet); builder.Append('"').Append(MimeBasePart.EncodeHeaderValue(value, encoding, MimeBasePart.ShouldUseBase64Encoding(encoding))).Append('"'); } }
public override void Add(string name, string value) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (value == null) { throw new ArgumentNullException(nameof(value)); } if (name == string.Empty) { throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(name)), nameof(name)); } if (value == string.Empty) { throw new ArgumentException(SR.Format(SR.net_emptystringcall, nameof(value)), nameof(value)); } MailBnfHelper.ValidateHeaderName(name); // normalize the case of well known headers name = MailHeaderInfo.NormalizeCase(name); MailHeaderID id = MailHeaderInfo.GetID(name); value = value.Normalize(NormalizationForm.FormC); if (id == MailHeaderID.ContentType && _part != null) { _part.ContentType.Set(value.ToLowerInvariant(), this); } else if (id == MailHeaderID.ContentDisposition && _part is MimePart) { ((MimePart)_part).ContentDisposition.Set(value.ToLowerInvariant(), this); } else { InternalAdd(name, value); } }
public override string ToString() { if ((this.type == null) || this.IsChanged) { StringBuilder builder = new StringBuilder(); builder.Append(this.mediaType); builder.Append('/'); builder.Append(this.subType); foreach (string str in this.Parameters.Keys) { builder.Append("; "); builder.Append(str); builder.Append('='); MailBnfHelper.GetTokenOrQuotedString(this.parameters[str], builder); } this.type = builder.ToString(); this.isChanged = false; this.parameters.IsChanged = false; this.isPersisted = false; } return(this.type); }
public override void Add(string name, string value) { if (Logging.On) { Logging.PrintInfo(Logging.Web, this, "Add", name.ToString() + "=" + value.ToString()); } if (name == null) { throw new ArgumentNullException("name"); } if (value == null) { throw new ArgumentNullException("value"); } if (name == string.Empty) { throw new ArgumentException(SR.GetString("net_emptystringcall", new object[] { "name" }), "name"); } if (value == string.Empty) { throw new ArgumentException(SR.GetString("net_emptystringcall", new object[] { "value" }), "name"); } MailBnfHelper.ValidateHeaderName(name); name = MailHeaderInfo.NormalizeCase(name); MailHeaderID iD = MailHeaderInfo.GetID(name); if ((iD == MailHeaderID.ContentType) && (this.part != null)) { this.part.ContentType.Set(value.ToLower(CultureInfo.InvariantCulture), this); } else if ((iD == MailHeaderID.ContentDisposition) && (this.part is MimePart)) { ((MimePart)this.part).ContentDisposition.Set(value.ToLower(CultureInfo.InvariantCulture), this); } else { this.InternalAdd(name, value); } }
private void WriteAndFold(string value, int charsAlreadyOnLine, bool allowUnicode) { int lastSpace = 0, startOfLine = 0; for (int index = 0; index < value.Length; index++) { // When we find a FWS (CRLF) copy it as is. if (MailBnfHelper.IsFWSAt(value, index)) // At the first char of "\r\n " or "\r\n\t" { index += 2; // Skip the FWS this.bufferBuilder.Append(value, startOfLine, index - startOfLine, allowUnicode); // Reset for the next line startOfLine = index; lastSpace = index; charsAlreadyOnLine = 0; } // When we pass the line length limit, and know where there was a space to fold at, fold there else if (((index - startOfLine) > (this.lineLength - charsAlreadyOnLine)) && lastSpace != startOfLine) { this.bufferBuilder.Append(value, startOfLine, lastSpace - startOfLine, allowUnicode); this.bufferBuilder.Append(CRLF); startOfLine = lastSpace; charsAlreadyOnLine = 0; } // Mark a foldable space. If we go over the line length limit, fold here. else if (value[index] == MailBnfHelper.Space || value[index] == MailBnfHelper.Tab) { lastSpace = index; } } // Write any remaining data to the buffer. if (value.Length - startOfLine > 0) { this.bufferBuilder.Append(value, startOfLine, value.Length - startOfLine, allowUnicode); } }
private void ParseValue() { int offset = 0; try { this.dispositionType = MailBnfHelper.ReadToken(this.disposition, ref offset, null); if (string.IsNullOrEmpty(this.dispositionType)) { throw new FormatException(SR.GetString("MailHeaderFieldMalformedHeader")); } if (this.parameters == null) { this.parameters = new TrackingValidationObjectDictionary(validators); } else { this.parameters.Clear(); } while (MailBnfHelper.SkipCFWS(this.disposition, ref offset)) { string str2; if (this.disposition[offset++] != ';') { throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[] { this.disposition[offset - 1] })); } if (!MailBnfHelper.SkipCFWS(this.disposition, ref offset)) { goto Label_018C; } string str = MailBnfHelper.ReadParameterAttribute(this.disposition, ref offset, null); if (this.disposition[offset++] != '=') { throw new FormatException(SR.GetString("MailHeaderFieldMalformedHeader")); } if (!MailBnfHelper.SkipCFWS(this.disposition, ref offset)) { throw new FormatException(SR.GetString("ContentDispositionInvalid")); } if (this.disposition[offset] == '"') { str2 = MailBnfHelper.ReadQuotedString(this.disposition, ref offset, null); } else { str2 = MailBnfHelper.ReadToken(this.disposition, ref offset, null); } if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(str2)) { throw new FormatException(SR.GetString("ContentDispositionInvalid")); } this.Parameters.Add(str, str2); } } catch (FormatException exception) { throw new FormatException(SR.GetString("ContentDispositionInvalid"), exception); } Label_018C: this.parameters.IsChanged = false; }
private void ParseValue() { try { int offset = 0; _mediaType = MailBnfHelper.ReadToken(_type, ref offset, null); if (_mediaType == null || _mediaType.Length == 0 || offset >= _type.Length || _type[offset++] != '/') { throw new FormatException(SR.ContentTypeInvalid); } _subType = MailBnfHelper.ReadToken(_type, ref offset, null); if (_subType == null || _subType.Length == 0) { throw new FormatException(SR.ContentTypeInvalid); } while (MailBnfHelper.SkipCFWS(_type, ref offset)) { if (_type[offset++] != ';') { throw new FormatException(SR.ContentTypeInvalid); } if (!MailBnfHelper.SkipCFWS(_type, ref offset)) { break; } string?paramAttribute = MailBnfHelper.ReadParameterAttribute(_type, ref offset, null); if (paramAttribute == null || paramAttribute.Length == 0) { throw new FormatException(SR.ContentTypeInvalid); } string?paramValue; if (offset >= _type.Length || _type[offset++] != '=') { throw new FormatException(SR.ContentTypeInvalid); } if (!MailBnfHelper.SkipCFWS(_type, ref offset)) { throw new FormatException(SR.ContentTypeInvalid); } paramValue = _type[offset] == '"' ? MailBnfHelper.ReadQuotedString(_type, ref offset, null) : MailBnfHelper.ReadToken(_type, ref offset, null); if (paramValue == null) { throw new FormatException(SR.ContentTypeInvalid); } _parameters.Add(paramAttribute, paramValue); } _parameters.IsChanged = false; } catch (FormatException fe) when(fe.Message != SR.ContentTypeInvalid) { throw new FormatException(SR.ContentTypeInvalid); } }
private void ParseValue() { int num = 0; Exception ex = null; _parameters = new Dictionary <string, string>(); try { _mediaType = MailBnfHelper.ReadToken(_type, ref num, null); if (string.IsNullOrEmpty(_mediaType) || num >= _type.Length || _type[num++] != '/') { ex = new FormatException(); } if (ex == null) { _subType = MailBnfHelper.ReadToken(_type, ref num, null); if (string.IsNullOrEmpty(_subType)) { ex = new FormatException(); } } if (ex == null) { while (MailBnfHelper.SkipCfWs(_type, ref num)) { if (_type[num++] != ';') { ex = new FormatException(); break; } if (!MailBnfHelper.SkipCfWs(_type, ref num)) { break; } string text = MailBnfHelper.ReadParameterAttribute(_type, ref num, null); if (string.IsNullOrEmpty(text)) { ex = new FormatException(); break; } if (num >= _type.Length || _type[num++] != '=') { ex = new FormatException(); break; } if (!MailBnfHelper.SkipCfWs(_type, ref num)) { ex = new FormatException(); break; } string text2 = _type[num] == '"' ? MailBnfHelper.ReadQuotedString(_type, ref num, null) : MailBnfHelper.ReadToken(_type, ref num, null); if (text2 == null) { ex = new FormatException(); break; } _parameters.Add(text, text2); } } } catch (FormatException) { throw new FormatException(); } if (ex != null) { throw new FormatException(); } }
private void ParseValue() { int offset = 0; try { // the disposition MUST be the first parameter in the string _dispositionType = MailBnfHelper.ReadToken(_disposition, ref offset, null); // disposition MUST not be empty if (string.IsNullOrEmpty(_dispositionType)) { throw new FormatException(SR.MailHeaderFieldMalformedHeader); } // now we know that there are parameters so we must initialize or clear // and parse if (_parameters == null) { _parameters = new TrackingValidationObjectDictionary(s_validators); } else { _parameters.Clear(); } while (MailBnfHelper.SkipCFWS(_disposition, ref offset)) { // ensure that the separator charactor is present if (_disposition[offset++] != ';') { throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, _disposition[offset - 1])); } // skip whitespace and see if there's anything left to parse or if we're done if (!MailBnfHelper.SkipCFWS(_disposition, ref offset)) { break; } string paramAttribute = MailBnfHelper.ReadParameterAttribute(_disposition, ref offset, null); string paramValue; // verify the next character after the parameter is correct if (_disposition[offset++] != '=') { throw new FormatException(SR.MailHeaderFieldMalformedHeader); } if (!MailBnfHelper.SkipCFWS(_disposition, ref offset)) { // parameter was at end of string and has no value // this is not valid throw new FormatException(SR.ContentDispositionInvalid); } paramValue = _disposition[offset] == '"' ? MailBnfHelper.ReadQuotedString(_disposition, ref offset, null) : MailBnfHelper.ReadToken(_disposition, ref offset, null); // paramValue could potentially still be empty if it was a valid quoted string that // contained no inner value. this is invalid if (string.IsNullOrEmpty(paramAttribute) || string.IsNullOrEmpty(paramValue)) { throw new FormatException(SR.ContentDispositionInvalid); } // if validation is needed, the parameters dictionary will have a validator registered // for the parameter that is being set so no additional formatting checks are needed here Parameters.Add(paramAttribute, paramValue); } } catch (FormatException exception) { // it's possible that something in MailBNFHelper could throw so ensure that we catch it and wrap it // so that the exception has the correct text throw new FormatException(SR.ContentDispositionInvalid, exception); } _parameters.IsChanged = false; }
// Helper methods. void ParseValue() { int offset = 0; Exception exception = null; parameters = new TrackingStringDictionary(); try{ mediaType = MailBnfHelper.ReadToken(type, ref offset, null); if (mediaType == null || mediaType.Length == 0 || offset >= type.Length || type[offset++] != '/') { exception = new FormatException(SR.GetString(SR.ContentTypeInvalid)); } if (exception == null) { subType = MailBnfHelper.ReadToken(type, ref offset, null); if (subType == null || subType.Length == 0) { exception = new FormatException(SR.GetString(SR.ContentTypeInvalid)); } } if (exception == null) { while (MailBnfHelper.SkipCFWS(type, ref offset)) { if (type[offset++] != ';') { exception = new FormatException(SR.GetString(SR.ContentTypeInvalid)); break; } if (!MailBnfHelper.SkipCFWS(type, ref offset)) { break; } string paramAttribute = MailBnfHelper.ReadParameterAttribute(type, ref offset, null); if (paramAttribute == null || paramAttribute.Length == 0) { exception = new FormatException(SR.GetString(SR.ContentTypeInvalid)); break; } string paramValue; if (offset >= type.Length || type[offset++] != '=') { exception = new FormatException(SR.GetString(SR.ContentTypeInvalid)); break; } if (!MailBnfHelper.SkipCFWS(type, ref offset)) { exception = new FormatException(SR.GetString(SR.ContentTypeInvalid)); break; } if (type[offset] == '"') { paramValue = MailBnfHelper.ReadQuotedString(type, ref offset, null); } else { paramValue = MailBnfHelper.ReadToken(type, ref offset, null); } if (paramValue == null) { exception = new FormatException(SR.GetString(SR.ContentTypeInvalid)); break; } parameters.Add(paramAttribute, paramValue); } } parameters.IsChanged = false; } catch (FormatException) { throw new FormatException(SR.GetString(SR.ContentTypeInvalid)); } if (exception != null) { throw new FormatException(SR.GetString(SR.ContentTypeInvalid)); } }
private void ParseValue() { int offset = 0; Exception exception = null; this.parameters = new TrackingStringDictionary(); try { this.mediaType = MailBnfHelper.ReadToken(this.type, ref offset, null); if (((this.mediaType == null) || (this.mediaType.Length == 0)) || ((offset >= this.type.Length) || (this.type[offset++] != '/'))) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); } if (exception == null) { this.subType = MailBnfHelper.ReadToken(this.type, ref offset, null); if ((this.subType == null) || (this.subType.Length == 0)) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); } } if (exception == null) { while (MailBnfHelper.SkipCFWS(this.type, ref offset)) { string str2; if (this.type[offset++] != ';') { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } if (!MailBnfHelper.SkipCFWS(this.type, ref offset)) { break; } string key = MailBnfHelper.ReadParameterAttribute(this.type, ref offset, null); if ((key == null) || (key.Length == 0)) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } if ((offset >= this.type.Length) || (this.type[offset++] != '=')) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } if (!MailBnfHelper.SkipCFWS(this.type, ref offset)) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } if (this.type[offset] == '"') { str2 = MailBnfHelper.ReadQuotedString(this.type, ref offset, null); } else { str2 = MailBnfHelper.ReadToken(this.type, ref offset, null); } if (str2 == null) { exception = new FormatException(SR.GetString("ContentTypeInvalid")); break; } this.parameters.Add(key, str2); } } this.parameters.IsChanged = false; } catch (FormatException) { throw new FormatException(SR.GetString("ContentTypeInvalid")); } if (exception != null) { throw new FormatException(SR.GetString("ContentTypeInvalid")); } }
private void ParseValue() { int num = 0; try { _dispositionType = MailBnfHelper.ReadToken(_disposition, ref num, null); if (string.IsNullOrEmpty(_dispositionType)) { throw new FormatException(); } if (_parameters == null) { _parameters = new Dictionary <string, string>(); } else { _parameters.Clear(); } while (MailBnfHelper.SkipCfWs(_disposition, ref num)) { if (_disposition[num++] != ';') { throw new FormatException(); } if (!MailBnfHelper.SkipCfWs(_disposition, ref num)) { break; } string text = MailBnfHelper.ReadParameterAttribute(_disposition, ref num, null); if (_disposition[num++] != '=') { throw new FormatException(); } if (!MailBnfHelper.SkipCfWs(_disposition, ref num)) { throw new FormatException(); } string value = _disposition[num] == '"' ? MailBnfHelper.ReadQuotedString(_disposition, ref num, null) : MailBnfHelper.ReadToken(_disposition, ref num, null); if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(value)) { throw new FormatException(); } Parameters.Add(text, value); } } catch (FormatException innerException) { throw new FormatException(); } }