Merge pull request #2310 from lambdageek/dev/bug-36305
[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 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Text;
36
37 namespace System.Net.Mime {
38         public class ContentType
39         {
40                 #region Fields
41                 static Encoding utf8unmarked;
42
43                 string mediaType;
44                 StringDictionary parameters = new StringDictionary ();
45
46                 #endregion // Fields
47
48                 #region Constructors
49
50                 public ContentType ()
51                 {
52                         mediaType = "application/octet-stream";
53                 }
54         
55                 public ContentType (string contentType)
56                 {
57                         if (contentType == null)
58                                 throw new ArgumentNullException ("contentType");
59                         if (contentType.Length == 0)
60                                 throw new ArgumentException ("contentType");
61
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 ());
66                 }
67
68                 // parse key=value pairs like:
69                 // "charset=us-ascii"
70                 static char [] eq = new char [] { '=' };
71                 void Parse (string pair)
72                 {
73                         if (String.IsNullOrEmpty (pair))
74                                 return;
75
76                         string [] split = pair.Split (eq, 2);
77                         string key = split [0].Trim ();
78                         string val =  (split.Length > 1) ? split [1].Trim () : "";
79                         int l = val.Length;
80                         if (l >= 2 && val [0] == '"' && val [l - 1] == '"')
81                                 val = val.Substring (1, l - 2);
82                         parameters [key] = val;
83                 }
84
85                 #endregion // Constructors
86
87                 #region Properties
88
89                 static Encoding UTF8Unmarked {
90                         get {
91                                 if (utf8unmarked == null)
92                                         utf8unmarked = new UTF8Encoding (false);
93                                 return utf8unmarked;
94                         }
95                 }
96
97                 public string Boundary {
98                         get { return parameters["boundary"]; }
99                         set { parameters["boundary"] = value; }
100                 }
101
102                 public string CharSet {
103                         get { return parameters["charset"]; }
104                         set { parameters["charset"] = value; }
105                 }
106
107                 public string MediaType {
108                         get { return mediaType; }
109                         set {
110                                 if (value == null)
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 ();
118                                 mediaType = value;
119                         }
120                 }
121
122                 public string Name {
123                         get { return parameters["name"]; }
124                         set { parameters["name"] = value; }
125                 }
126
127                 public StringDictionary Parameters {
128                         get { return parameters; }
129                 }
130
131                 #endregion // Properties
132
133                 #region Methods
134
135                 public override bool Equals (object obj)
136                 {
137                         return Equals (obj as ContentType);
138                 }
139
140                 bool Equals (ContentType other)
141                 {
142                         return other != null && ToString () == other.ToString ();
143                 }
144                 
145                 public override int GetHashCode ()
146                 {
147                         return ToString ().GetHashCode ();
148                 }
149
150                 public override string ToString ()
151                 {
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)
157                                 {
158                                         if (pair.Value != null && pair.Value.ToString ().Length > 0) {  
159                                                 sb.Append ("; ");
160                                                 sb.Append (pair.Key);
161                                                 sb.Append ("=");
162                                                 sb.Append (WrapIfEspecialsExist (EncodeSubjectRFC2047 (pair.Value as string, enc)));
163                                         }
164                                 }
165                         }
166                         return sb.ToString ();
167                 }
168
169                 // see RFC 2047
170                 static readonly char [] especials = {'(', ')', '<', '>', '@', ',', ';', ':', '<', '>', '/', '[', ']', '?', '.', '='};
171
172                 static string WrapIfEspecialsExist (string s)
173                 {
174                         s = s.Replace ("\"", "\\\"");
175                         if (s.IndexOfAny (especials) >= 0)
176                                 return '"' + s + '"';
177                         else
178                                 return s;
179                 }
180
181                 internal static Encoding GuessEncoding (string s)
182                 {
183                         for (int i = 0; i < s.Length; i++)
184                                 if (s [i] >= '\u0080')
185                                         return UTF8Unmarked;
186                         return null;
187                 }
188
189                 internal static TransferEncoding GuessTransferEncoding (Encoding enc)
190                 {
191                         if (Encoding.ASCII.Equals (enc))
192                                 return TransferEncoding.SevenBit;
193                         else if (Encoding.UTF8.CodePage == enc.CodePage ||
194 #if !NET_2_1
195                             Encoding.Unicode.CodePage == enc.CodePage || Encoding.UTF32.CodePage == enc.CodePage
196 #else
197                             Encoding.Unicode.CodePage == enc.CodePage
198 #endif
199                                          )
200                                 return TransferEncoding.Base64;
201                         else
202                                 return TransferEncoding.QuotedPrintable;
203                 }
204
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)
207                 {
208                         StringBuilder sb = new StringBuilder ();
209                         foreach (byte i in bytes) {
210                                 if (i < 0x21 || i > 0x7E || i == '?' || i == '=' || i == '_') {
211                                         sb.Append ('=');
212                                         sb.Append (hex [(i >> 4) & 0x0f]);
213                                         sb.Append (hex [i & 0x0f]);
214                                 } else
215                                         sb.Append ((char) i);
216                         }
217                         return sb.ToString ();
218                 }
219
220                 internal static string EncodeSubjectRFC2047 (string s, Encoding enc)
221                 {
222                         if (s == null || Encoding.ASCII.Equals (enc))
223                                 return s;
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, "?=");
228                                 }
229                         return s;
230                 }
231
232                 #endregion // Methods
233         }
234 }
235