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
43 static Encoding utf8unmarked;
46 StringDictionary parameters = new StringDictionary ();
54 mediaType = "application/octet-stream";
57 public ContentType (string contentType)
59 if (contentType == null)
60 throw new ArgumentNullException ("contentType");
61 if (contentType.Length == 0)
62 throw new ArgumentException ("contentType");
64 string[] split = contentType.Split (';');
65 this.MediaType = split[0].Trim ();
66 for (int i = 1; i < split.Length; i++)
67 Parse (split[i].Trim ());
70 // parse key=value pairs like:
72 static char [] eq = new char [] { '=' };
73 void Parse (string pair)
75 if (String.IsNullOrEmpty (pair))
78 string [] split = pair.Split (eq, 2);
79 string key = split [0].Trim ();
80 string val = (split.Length > 1) ? split [1].Trim () : "";
82 if (l >= 2 && val [0] == '"' && val [l - 1] == '"')
83 val = val.Substring (1, l - 2);
84 parameters [key] = val;
87 #endregion // Constructors
91 static Encoding UTF8Unmarked {
93 if (utf8unmarked == null)
94 utf8unmarked = new UTF8Encoding (false);
99 public string Boundary {
100 get { return parameters["boundary"]; }
101 set { parameters["boundary"] = value; }
104 public string CharSet {
105 get { return parameters["charset"]; }
106 set { parameters["charset"] = value; }
109 public string MediaType {
110 get { return mediaType; }
113 throw new ArgumentNullException ();
114 if (value.Length < 1)
115 throw new ArgumentException ();
116 if (value.IndexOf ('/') < 1)
117 throw new FormatException ();
118 if (value.IndexOf (';') != -1)
119 throw new FormatException ();
125 get { return parameters["name"]; }
126 set { parameters["name"] = value; }
129 public StringDictionary Parameters {
130 get { return parameters; }
133 #endregion // Properties
137 public override bool Equals (object obj)
139 return Equals (obj as ContentType);
142 bool Equals (ContentType other)
144 return other != null && ToString () == other.ToString ();
147 public override int GetHashCode ()
149 return ToString ().GetHashCode ();
152 public override string ToString ()
154 StringBuilder sb = new StringBuilder ();
155 Encoding enc = CharSet != null ? Encoding.GetEncoding (CharSet) : Encoding.UTF8;
156 sb.Append (MediaType);
157 if (Parameters != null && Parameters.Count > 0) {
158 foreach (DictionaryEntry pair in parameters)
160 if (pair.Value != null && pair.Value.ToString ().Length > 0) {
162 sb.Append (pair.Key);
164 sb.Append (WrapIfEspecialsExist (EncodeSubjectRFC2047 (pair.Value as string, enc)));
168 return sb.ToString ();
172 static readonly char [] especials = {'(', ')', '<', '>', '@', ',', ';', ':', '<', '>', '/', '[', ']', '?', '.', '='};
174 static string WrapIfEspecialsExist (string s)
176 s = s.Replace ("\"", "\\\"");
177 if (s.IndexOfAny (especials) >= 0)
178 return '"' + s + '"';
183 internal static Encoding GuessEncoding (string s)
185 for (int i = 0; i < s.Length; i++)
186 if (s [i] >= '\u0080')
191 internal static TransferEncoding GuessTransferEncoding (Encoding enc)
193 if (Encoding.ASCII.Equals (enc))
194 return TransferEncoding.SevenBit;
195 else if (Encoding.UTF8.CodePage == enc.CodePage ||
197 Encoding.Unicode.CodePage == enc.CodePage || Encoding.UTF32.CodePage == enc.CodePage
199 Encoding.Unicode.CodePage == enc.CodePage
202 return TransferEncoding.Base64;
204 return TransferEncoding.QuotedPrintable;
207 static char [] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
208 internal static string To2047(byte [] bytes)
210 StringBuilder sb = new StringBuilder ();
211 foreach (byte i in bytes) {
212 if (i < 0x21 || i > 0x7E || i == '?' || i == '=' || i == '_') {
214 sb.Append (hex [(i >> 4) & 0x0f]);
215 sb.Append (hex [i & 0x0f]);
217 sb.Append ((char) i);
219 return sb.ToString ();
222 internal static string EncodeSubjectRFC2047 (string s, Encoding enc)
224 if (s == null || Encoding.ASCII.Equals (enc))
226 for (int i = 0; i < s.Length; i++)
227 if (s [i] >= '\u0080') {
228 string quoted = To2047(enc.GetBytes (s));
229 return String.Concat ("=?", enc.HeaderName, "?Q?", quoted, "?=");
234 #endregion // Methods