BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System / System.Net.Mime / ContentType.cs
1 //
2 // System.Net.Mime.ContentType.cs
3 //
4 // Authors:
5 //      Tim Coleman (tim@timcoleman.com)
6 //      John Luke (john.luke@gmail.com)
7 //
8 // Copyright (C) Tim Coleman, 2004
9 // Copyright (C) John Luke, 2005
10 //
11
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 #if NET_2_0
34
35 using System.Collections;
36 using System.Collections.Specialized;
37 using System.Text;
38
39 namespace System.Net.Mime {
40         public class ContentType
41         {
42                 #region Fields
43                 static Encoding utf8unmarked;
44
45                 string mediaType;
46                 StringDictionary parameters = new StringDictionary ();
47
48                 #endregion // Fields
49
50                 #region Constructors
51
52                 public ContentType ()
53                 {
54                         mediaType = "application/octet-stream";
55                 }
56         
57                 public ContentType (string contentType)
58                 {
59                         if (contentType == null)
60                                 throw new ArgumentNullException ("contentType");
61                         if (contentType.Length == 0)
62                                 throw new ArgumentException ("contentType");
63
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 ());
68                 }
69
70                 // parse key=value pairs like:
71                 // "charset=us-ascii"
72                 static char [] eq = new char [] { '=' };
73                 void Parse (string pair)
74                 {
75                         if (String.IsNullOrEmpty (pair))
76                                 return;
77
78                         string [] split = pair.Split (eq, 2);
79                         string key = split [0].Trim ();
80                         string val =  (split.Length > 1) ? split [1].Trim () : "";
81                         int l = val.Length;
82                         if (l >= 2 && val [0] == '"' && val [l - 1] == '"')
83                                 val = val.Substring (1, l - 2);
84                         parameters [key] = val;
85                 }
86
87                 #endregion // Constructors
88
89                 #region Properties
90
91                 static Encoding UTF8Unmarked {
92                         get {
93                                 if (utf8unmarked == null)
94                                         utf8unmarked = new UTF8Encoding (false);
95                                 return utf8unmarked;
96                         }
97                 }
98
99                 public string Boundary {
100                         get { return parameters["boundary"]; }
101                         set { parameters["boundary"] = value; }
102                 }
103
104                 public string CharSet {
105                         get { return parameters["charset"]; }
106                         set { parameters["charset"] = value; }
107                 }
108
109                 public string MediaType {
110                         get { return mediaType; }
111                         set {
112                                 if (value == null)
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 ();
120                                 mediaType = value;
121                         }
122                 }
123
124                 public string Name {
125                         get { return parameters["name"]; }
126                         set { parameters["name"] = value; }
127                 }
128
129                 public StringDictionary Parameters {
130                         get { return parameters; }
131                 }
132
133                 #endregion // Properties
134
135                 #region Methods
136
137                 public override bool Equals (object obj)
138                 {
139                         return Equals (obj as ContentType);
140                 }
141
142                 bool Equals (ContentType other)
143                 {
144                         return other != null && ToString () == other.ToString ();
145                 }
146                 
147                 public override int GetHashCode ()
148                 {
149                         return ToString ().GetHashCode ();
150                 }
151
152                 public override string ToString ()
153                 {
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)
159                                 {
160                                         if (pair.Value != null && pair.Value.ToString ().Length > 0) {  
161                                                 sb.Append ("; ");
162                                                 sb.Append (pair.Key);
163                                                 sb.Append ("=");
164                                                 sb.Append (WrapIfEspecialsExist (EncodeSubjectRFC2047 (pair.Value as string, enc)));
165                                         }
166                                 }
167                         }
168                         return sb.ToString ();
169                 }
170
171                 // see RFC 2047
172                 static readonly char [] especials = {'(', ')', '<', '>', '@', ',', ';', ':', '<', '>', '/', '[', ']', '?', '.', '='};
173
174                 static string WrapIfEspecialsExist (string s)
175                 {
176                         s = s.Replace ("\"", "\\\"");
177                         if (s.IndexOfAny (especials) >= 0)
178                                 return '"' + s + '"';
179                         else
180                                 return s;
181                 }
182
183                 internal static Encoding GuessEncoding (string s)
184                 {
185                         for (int i = 0; i < s.Length; i++)
186                                 if (s [i] >= '\u0080')
187                                         return UTF8Unmarked;
188                         return null;
189                 }
190
191                 internal static TransferEncoding GuessTransferEncoding (Encoding enc)
192                 {
193                         if (Encoding.ASCII.Equals (enc))
194                                 return TransferEncoding.SevenBit;
195                         else if (Encoding.UTF8.CodePage == enc.CodePage ||
196 #if !NET_2_1
197                             Encoding.Unicode.CodePage == enc.CodePage || Encoding.UTF32.CodePage == enc.CodePage
198 #else
199                             Encoding.Unicode.CodePage == enc.CodePage
200 #endif
201                                          )
202                                 return TransferEncoding.Base64;
203                         else
204                                 return TransferEncoding.QuotedPrintable;
205                 }
206
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)
209                 {
210                         StringBuilder sb = new StringBuilder ();
211                         foreach (byte i in bytes) {
212                                 if (i < 0x21 || i > 0x7E || i == '?' || i == '=' || i == '_') {
213                                         sb.Append ('=');
214                                         sb.Append (hex [(i >> 4) & 0x0f]);
215                                         sb.Append (hex [i & 0x0f]);
216                                 } else
217                                         sb.Append ((char) i);
218                         }
219                         return sb.ToString ();
220                 }
221
222                 internal static string EncodeSubjectRFC2047 (string s, Encoding enc)
223                 {
224                         if (s == null || Encoding.ASCII.Equals (enc))
225                                 return s;
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, "?=");
230                                 }
231                         return s;
232                 }
233
234                 #endregion // Methods
235         }
236 }
237
238 #endif // NET_2_0