Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Net / System / Net / Mail / ContentType.cs / 5 / ContentType.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Net.Mime
{
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Globalization;
using System.Net.Mail;
///
/// Typed Content-Type header
///
/// We parse the type during construction and set.
/// null and string.empty will throw for construction,set and mediatype/subtype
/// constructors set isPersisted to false. isPersisted needs to be tracked seperately
/// than isChanged because isChanged only determines if the cached value should be used.
/// isPersisted tracks if the object has been persisted. However, obviously if isChanged is true
/// the object isn't persisted.
/// If any subcomponents are changed, isChanged is set to true and isPersisted is false
/// ToString caches the value until a isChanged is true, then it recomputes the full value.
///
public class ContentType
{
string mediaType;
string subType;
bool isChanged;
string type;
bool isPersisted;
TrackingStringDictionary parameters;
///
/// Default content type - can be used if the Content-Type header
/// is not defined in the message headers.
///
internal readonly static string Default = "application/octet-stream";
public ContentType() : this(Default)
{
}
///
/// ctor.
///
/// Unparsed value of the Content-Type header.
public ContentType(string contentType)
{
if (contentType == null) {
throw new ArgumentNullException("contentType");
}
if (contentType == String.Empty) {
throw new ArgumentException(SR.GetString(SR.net_emptystringcall,"contentType"), "contentType");
}
isChanged = true;
type = contentType;
ParseValue();
}
public string Boundary
{
get
{
return Parameters["boundary"];
}
set
{
if (value == null || value == string.Empty) {
Parameters.Remove("boundary");
}
else{
Parameters["boundary"] = value;
}
}
}
public string CharSet
{
get
{
return Parameters["charset"];
}
set
{
if (value == null || value == string.Empty) {
Parameters.Remove("charset");
}
else{
Parameters["charset"] = value;
}
}
}
///
/// Gets the media type.
///
public string MediaType
{
get
{
return mediaType + "/" + subType;
}
set
{
if (value == null) {
throw new ArgumentNullException("value");
}
if (value == string.Empty) {
throw new ArgumentException(SR.GetString(SR.net_emptystringset), "value");
}
int offset = 0;
mediaType = MailBnfHelper.ReadToken(value, ref offset, null);
if (mediaType.Length == 0 || offset >= value.Length || value[offset++] != '/')
throw new FormatException(SR.GetString(SR.MediaTypeInvalid));
subType = MailBnfHelper.ReadToken(value, ref offset, null);
if(subType.Length == 0 || offset < value.Length){
throw new FormatException(SR.GetString(SR.MediaTypeInvalid));
}
isChanged = true;
isPersisted = false;
}
}
public string Name {
get {
string value = Parameters["name"];
Encoding nameEncoding = MimeBasePart.DecodeEncoding(value);
if(nameEncoding != null)
value = MimeBasePart.DecodeHeaderValue(value);
return value;
}
set {
if (value == null || value == string.Empty) {
Parameters.Remove("name");
}
else{
if (MimeBasePart.IsAscii(value, false)) {
Parameters["name"] = value;
} else {
Encoding encoding = Encoding.GetEncoding(MimeBasePart.defaultCharSet);
Parameters["name"] = MimeBasePart.EncodeHeaderValue(value, encoding, MimeBasePart.ShouldUseBase64Encoding(encoding));
}
}
}
}
public StringDictionary Parameters
{
get
{
if (parameters == null)
{
if (type == null) {
parameters = new TrackingStringDictionary();
}
}
return parameters;
}
}
internal void Set(string contentType, HeaderCollection headers) {
type = contentType;
ParseValue();
headers.InternalSet(MailHeaderInfo.GetString(MailHeaderID.ContentType), ToString());
isPersisted = true;
}
internal void PersistIfNeeded(HeaderCollection headers, bool forcePersist) {
if (IsChanged || !isPersisted || forcePersist) {
headers.InternalSet(MailHeaderInfo.GetString(MailHeaderID.ContentType), ToString());
isPersisted = true;
}
}
internal bool IsChanged {
get {
return (isChanged || parameters != null && parameters.IsChanged);
}
}
public override string ToString() {
if (type == null || IsChanged)
{
StringBuilder builder = new StringBuilder();
builder.Append(mediaType);
builder.Append('/');
builder.Append(subType);
foreach (string key in Parameters.Keys)
{
builder.Append("; ");
builder.Append(key);
builder.Append('=');
MailBnfHelper.GetTokenOrQuotedString(parameters[key], builder);
}
type = builder.ToString();
isChanged = false;
parameters.IsChanged = false;
isPersisted = false;
}
return type;
}
public override bool Equals(object rparam) {
if (rparam == null) {
return false;
}
return (String.Compare(ToString(), rparam.ToString(), StringComparison.OrdinalIgnoreCase ) == 0);
}
public override int GetHashCode(){
return ToString().GetHashCode();
}
// 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));
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Net.Mime
{
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Globalization;
using System.Net.Mail;
///
/// Typed Content-Type header
///
/// We parse the type during construction and set.
/// null and string.empty will throw for construction,set and mediatype/subtype
/// constructors set isPersisted to false. isPersisted needs to be tracked seperately
/// than isChanged because isChanged only determines if the cached value should be used.
/// isPersisted tracks if the object has been persisted. However, obviously if isChanged is true
/// the object isn't persisted.
/// If any subcomponents are changed, isChanged is set to true and isPersisted is false
/// ToString caches the value until a isChanged is true, then it recomputes the full value.
///
public class ContentType
{
string mediaType;
string subType;
bool isChanged;
string type;
bool isPersisted;
TrackingStringDictionary parameters;
///
/// Default content type - can be used if the Content-Type header
/// is not defined in the message headers.
///
internal readonly static string Default = "application/octet-stream";
public ContentType() : this(Default)
{
}
///
/// ctor.
///
/// Unparsed value of the Content-Type header.
public ContentType(string contentType)
{
if (contentType == null) {
throw new ArgumentNullException("contentType");
}
if (contentType == String.Empty) {
throw new ArgumentException(SR.GetString(SR.net_emptystringcall,"contentType"), "contentType");
}
isChanged = true;
type = contentType;
ParseValue();
}
public string Boundary
{
get
{
return Parameters["boundary"];
}
set
{
if (value == null || value == string.Empty) {
Parameters.Remove("boundary");
}
else{
Parameters["boundary"] = value;
}
}
}
public string CharSet
{
get
{
return Parameters["charset"];
}
set
{
if (value == null || value == string.Empty) {
Parameters.Remove("charset");
}
else{
Parameters["charset"] = value;
}
}
}
///
/// Gets the media type.
///
public string MediaType
{
get
{
return mediaType + "/" + subType;
}
set
{
if (value == null) {
throw new ArgumentNullException("value");
}
if (value == string.Empty) {
throw new ArgumentException(SR.GetString(SR.net_emptystringset), "value");
}
int offset = 0;
mediaType = MailBnfHelper.ReadToken(value, ref offset, null);
if (mediaType.Length == 0 || offset >= value.Length || value[offset++] != '/')
throw new FormatException(SR.GetString(SR.MediaTypeInvalid));
subType = MailBnfHelper.ReadToken(value, ref offset, null);
if(subType.Length == 0 || offset < value.Length){
throw new FormatException(SR.GetString(SR.MediaTypeInvalid));
}
isChanged = true;
isPersisted = false;
}
}
public string Name {
get {
string value = Parameters["name"];
Encoding nameEncoding = MimeBasePart.DecodeEncoding(value);
if(nameEncoding != null)
value = MimeBasePart.DecodeHeaderValue(value);
return value;
}
set {
if (value == null || value == string.Empty) {
Parameters.Remove("name");
}
else{
if (MimeBasePart.IsAscii(value, false)) {
Parameters["name"] = value;
} else {
Encoding encoding = Encoding.GetEncoding(MimeBasePart.defaultCharSet);
Parameters["name"] = MimeBasePart.EncodeHeaderValue(value, encoding, MimeBasePart.ShouldUseBase64Encoding(encoding));
}
}
}
}
public StringDictionary Parameters
{
get
{
if (parameters == null)
{
if (type == null) {
parameters = new TrackingStringDictionary();
}
}
return parameters;
}
}
internal void Set(string contentType, HeaderCollection headers) {
type = contentType;
ParseValue();
headers.InternalSet(MailHeaderInfo.GetString(MailHeaderID.ContentType), ToString());
isPersisted = true;
}
internal void PersistIfNeeded(HeaderCollection headers, bool forcePersist) {
if (IsChanged || !isPersisted || forcePersist) {
headers.InternalSet(MailHeaderInfo.GetString(MailHeaderID.ContentType), ToString());
isPersisted = true;
}
}
internal bool IsChanged {
get {
return (isChanged || parameters != null && parameters.IsChanged);
}
}
public override string ToString() {
if (type == null || IsChanged)
{
StringBuilder builder = new StringBuilder();
builder.Append(mediaType);
builder.Append('/');
builder.Append(subType);
foreach (string key in Parameters.Keys)
{
builder.Append("; ");
builder.Append(key);
builder.Append('=');
MailBnfHelper.GetTokenOrQuotedString(parameters[key], builder);
}
type = builder.ToString();
isChanged = false;
parameters.IsChanged = false;
isPersisted = false;
}
return type;
}
public override bool Equals(object rparam) {
if (rparam == null) {
return false;
}
return (String.Compare(ToString(), rparam.ToString(), StringComparison.OrdinalIgnoreCase ) == 0);
}
public override int GetHashCode(){
return ToString().GetHashCode();
}
// 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));
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- UIPropertyMetadata.cs
- ProtocolsSection.cs
- ModuleBuilderData.cs
- SqlTypeSystemProvider.cs
- HtmlToClrEventProxy.cs
- AppSettingsExpressionBuilder.cs
- _ReceiveMessageOverlappedAsyncResult.cs
- WebServiceErrorEvent.cs
- QualifiedCellIdBoolean.cs
- HScrollProperties.cs
- ClientFormsAuthenticationCredentials.cs
- Nullable.cs
- TextDecorationLocationValidation.cs
- ProfileWorkflowElement.cs
- SafeArrayTypeMismatchException.cs
- BypassElementCollection.cs
- XmlSchemaInfo.cs
- SubqueryRules.cs
- LateBoundBitmapDecoder.cs
- LinqDataSourceSelectEventArgs.cs
- SharedStatics.cs
- SchemaDeclBase.cs
- PackageFilter.cs
- TreeNode.cs
- RuntimeConfig.cs
- PropertyKey.cs
- IPCCacheManager.cs
- TimeSpanOrInfiniteValidator.cs
- ManagedIStream.cs
- ReferencedCollectionType.cs
- ResourceSet.cs
- XmlReaderSettings.cs
- FileChangesMonitor.cs
- IdentityHolder.cs
- ToolStripDropDownButton.cs
- XmlTextWriter.cs
- SqlUtils.cs
- HelpProvider.cs
- VerticalAlignConverter.cs
- SafeWaitHandle.cs
- EndpointFilterProvider.cs
- UnsafeNativeMethods.cs
- SQLBinary.cs
- StylusEventArgs.cs
- LinqDataSourceUpdateEventArgs.cs
- PathSegmentCollection.cs
- ViewStateModeByIdAttribute.cs
- SourceSwitch.cs
- DataViewSetting.cs
- TimeStampChecker.cs
- CodeAssignStatement.cs
- DrawingServices.cs
- PageBreakRecord.cs
- StringExpressionSet.cs
- FlowDecisionLabelFeature.cs
- ChtmlMobileTextWriter.cs
- CqlErrorHelper.cs
- Thumb.cs
- SchemeSettingElement.cs
- WebPartAddingEventArgs.cs
- HelpEvent.cs
- DocobjHost.cs
- SmtpReplyReader.cs
- ContextMarshalException.cs
- PolyLineSegmentFigureLogic.cs
- KeyInterop.cs
- CodeTypeParameter.cs
- RuntimeVariableList.cs
- ResourceProviderFactory.cs
- SmiEventSink.cs
- ToggleButton.cs
- RecognizerInfo.cs
- SessionPageStatePersister.cs
- ColumnCollection.cs
- InternalResources.cs
- EventSchemaTraceListener.cs
- SafeCryptHandles.cs
- BooleanStorage.cs
- UTF8Encoding.cs
- WebPartTransformer.cs
- MDIControlStrip.cs
- PageTheme.cs
- BooleanSwitch.cs
- XmlSerializerFactory.cs
- AccessedThroughPropertyAttribute.cs
- SQLBinaryStorage.cs
- ProxyAttribute.cs
- ContextMenuStripActionList.cs
- FeatureSupport.cs
- MultiDataTrigger.cs
- brushes.cs
- PeerNameRegistration.cs
- SecurityAttributeGenerationHelper.cs
- ElementsClipboardData.cs
- UntrustedRecipientException.cs
- KeyPullup.cs
- CaseExpr.cs
- FocusManager.cs
- GridViewCancelEditEventArgs.cs
- Int32CAMarshaler.cs