2 // System.Net.Mime.ContentType.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // John Luke (john.luke@gmail.com)
8 // Copyright (C) Tim Coleman, 2004
9 // Copyright (C) John Luke, 2005
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Collections.Specialized;
39 namespace System.Net.Mime {
40 public class ContentType
45 StringDictionary parameters = new StringDictionary ();
53 mediaType = "application/octet-stream";
56 public ContentType (string contentType)
58 if (contentType == null)
59 throw new ArgumentNullException ("contentType");
60 if (contentType.Length < 1)
61 throw new ArgumentException ("contentType");
64 int index = contentType.IndexOf (";");
66 string[] split = contentType.Split (';');
67 this.mediaType = split[0].Trim ();
68 for (int i = 1; i < split.Length; i++)
74 this.mediaType = contentType.Trim ();
77 throw new FormatException ();
81 // parse key=value pairs like:
83 void Parse (string pair)
85 if (pair == null || pair.Length < 1)
88 string[] split = pair.Split ('=');
89 if (split.Length == 2) {
90 switch (split[0].Trim ()) {
94 parameters.Add (split[0].Trim (), split[1].Trim ());
97 // apparently parameters must go through Parameters.Add
98 throw new FormatException ("invalid content-type format");
103 #endregion // Constructors
107 public string Boundary {
108 get { return parameters["boundary"]; }
109 set { parameters["boundary"] = value; }
112 public string CharSet {
113 get { return parameters["charset"]; }
114 set { parameters["charset"] = value; }
117 public string MediaType {
118 get { return mediaType; }
121 throw new ArgumentNullException ();
122 if (value.Length < 1)
123 throw new ArgumentException ();
124 if (value.IndexOf (';') != -1)
125 throw new FormatException ();
131 get { return parameters["name"]; }
132 set { parameters["name"] = value; }
135 public StringDictionary Parameters {
136 get { return parameters; }
139 #endregion // Properties
143 public override bool Equals (object obj)
145 return Equals (obj as ContentType);
148 bool Equals (ContentType other)
150 return other != null && ToString () == other.ToString ();
153 public override int GetHashCode ()
155 return ToString ().GetHashCode ();
158 public override string ToString ()
160 StringBuilder sb = new StringBuilder ();
161 sb.Append (MediaType);
162 if (Parameters != null && Parameters.Count > 0) {
163 foreach (DictionaryEntry pair in parameters)
165 if (pair.Value != null && pair.Value.ToString ().Length > 0) {
167 sb.Append (pair.Key);
169 sb.Append (pair.Value);
173 return sb.ToString ();
176 #endregion // Methods