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.
33 using System.Collections;
34 using System.Collections.Specialized;
37 namespace System.Net.Mime {
38 public class ContentType
41 static Encoding utf8unmarked;
44 StringDictionary parameters = new StringDictionary ();
52 mediaType = "application/octet-stream";
55 public ContentType (string contentType)
57 if (contentType == null)
58 throw new ArgumentNullException ("contentType");
59 if (contentType.Length == 0)
60 throw new ArgumentException ("contentType");
62 string[] split = contentType.Split (';');
63 this.MediaType = split[0].Trim ();
64 for (int i = 1; i < split.Length; i++)
65 Parse (split[i].Trim ());
68 // parse key=value pairs like:
70 static char [] eq = new char [] { '=' };
71 void Parse (string pair)
73 if (String.IsNullOrEmpty (pair))
76 string [] split = pair.Split (eq, 2);
77 string key = split [0].Trim ();
78 string val = (split.Length > 1) ? split [1].Trim () : "";
80 if (l >= 2 && val [0] == '"' && val [l - 1] == '"')
81 val = val.Substring (1, l - 2);
82 parameters [key] = val;
85 #endregion // Constructors
89 static Encoding UTF8Unmarked {
91 if (utf8unmarked == null)
92 utf8unmarked = new UTF8Encoding (false);
97 public string Boundary {
98 get { return parameters["boundary"]; }
99 set { parameters["boundary"] = value; }
102 public string CharSet {
103 get { return parameters["charset"]; }
104 set { parameters["charset"] = value; }
107 public string MediaType {
108 get { return mediaType; }
111 throw new ArgumentNullException ();
112 if (value.Length < 1)
113 throw new ArgumentException ();
114 if (value.IndexOf ('/') < 1)
115 throw new FormatException ();
116 if (value.IndexOf (';') != -1)
117 throw new FormatException ();
123 get { return parameters["name"]; }
124 set { parameters["name"] = value; }
127 public StringDictionary Parameters {
128 get { return parameters; }
131 #endregion // Properties
135 public override bool Equals (object obj)
137 return Equals (obj as ContentType);
140 bool Equals (ContentType other)
142 return other != null && ToString () == other.ToString ();
145 public override int GetHashCode ()
147 return ToString ().GetHashCode ();
150 public override string ToString ()
152 StringBuilder sb = new StringBuilder ();
153 Encoding enc = CharSet != null ? Encoding.GetEncoding (CharSet) : Encoding.UTF8;
154 sb.Append (MediaType);
155 if (Parameters != null && Parameters.Count > 0) {
156 foreach (DictionaryEntry pair in parameters)
158 if (pair.Value != null && pair.Value.ToString ().Length > 0) {
160 sb.Append (pair.Key);
162 sb.Append (WrapIfEspecialsExist (EncodeSubjectRFC2047 (pair.Value as string, enc)));
166 return sb.ToString ();
170 static readonly char [] especials = {'(', ')', '<', '>', '@', ',', ';', ':', '<', '>', '/', '[', ']', '?', '.', '='};
172 static string WrapIfEspecialsExist (string s)
174 s = s.Replace ("\"", "\\\"");
175 if (s.IndexOfAny (especials) >= 0)
176 return '"' + s + '"';
181 internal static Encoding GuessEncoding (string s)
183 for (int i = 0; i < s.Length; i++)
184 if (s [i] >= '\u0080')
189 internal static TransferEncoding GuessTransferEncoding (Encoding enc)
191 if (Encoding.ASCII.Equals (enc))
192 return TransferEncoding.SevenBit;
193 else if (Encoding.UTF8.CodePage == enc.CodePage ||
195 Encoding.Unicode.CodePage == enc.CodePage || Encoding.UTF32.CodePage == enc.CodePage
197 Encoding.Unicode.CodePage == enc.CodePage
200 return TransferEncoding.Base64;
202 return TransferEncoding.QuotedPrintable;
205 static char [] hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
206 internal static string To2047(byte [] bytes)
208 StringBuilder sb = new StringBuilder ();
209 foreach (byte i in bytes) {
210 if (i < 0x21 || i > 0x7E || i == '?' || i == '=' || i == '_') {
212 sb.Append (hex [(i >> 4) & 0x0f]);
213 sb.Append (hex [i & 0x0f]);
215 sb.Append ((char) i);
217 return sb.ToString ();
220 internal static string EncodeSubjectRFC2047 (string s, Encoding enc)
222 if (s == null || Encoding.ASCII.Equals (enc))
224 for (int i = 0; i < s.Length; i++)
225 if (s [i] >= '\u0080') {
226 string quoted = To2047(enc.GetBytes (s));
227 return String.Concat ("=?", enc.HeaderName, "?Q?", quoted, "?=");
232 #endregion // Methods