2 // System.Net.Mime.ContentType.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // John Luke (john.luke@gmail.com)
7 // Atsushi Eno (atsushieno@veritas-vos-liberabit.com)
9 // Copyright (C) Tim Coleman, 2004
10 // Copyright (C) John Luke, 2005
11 // Copyright (C) Atsushi Eno, 2011
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections;
36 using System.Collections.Generic;
39 namespace System.ServiceModel.Channels
41 internal class ContentType
44 Dictionary<string,string> parameters = new Dictionary<string,string> ();
46 public ContentType (string contentType)
48 if (contentType == null)
49 throw new ArgumentNullException ("contentType");
50 if (contentType.Length == 0)
51 throw new ArgumentException ("contentType");
53 string[] split = contentType.Split (';');
54 this.MediaType = split[0].Trim ();
55 for (int i = 1; i < split.Length; i++)
56 Parse (split[i].Trim ());
59 static char [] eq = new char [] { '=' };
60 void Parse (string pair)
62 if (String.IsNullOrEmpty (pair))
65 string [] split = pair.Split (eq, 2);
66 string key = split [0].Trim ();
67 string val = (split.Length > 1) ? split [1].Trim () : "";
69 if (l >= 2 && val [0] == '"' && val [l - 1] == '"')
70 val = val.Substring (1, l - 2);
71 parameters.Add (key, val);
74 public string MediaType {
75 get { return mediaType; }
78 throw new ArgumentNullException ();
80 throw new ArgumentException ();
81 if (value.IndexOf ('/') < 1)
82 throw new FormatException ();
83 if (value.IndexOf (';') != -1)
84 throw new FormatException ();
89 public Dictionary<string,string> Parameters {
90 get { return parameters; }