[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / WindowsBase / System.IO.Packaging / PackUriHelper.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Chris Toshok (toshok@ximian.com)
24 //
25
26 using System;
27
28 namespace System.IO.Packaging {
29
30         public static class PackUriHelper
31         {
32                 public static readonly string UriSchemePack = "pack";
33                 static readonly Uri PackSchemeUri = new Uri("pack://", UriKind.Absolute);
34                 static readonly char[] _escapedChars = new char[] { '%', ',', '?', '@' };
35
36                 
37                 static PackUriHelper ()
38                 {
39                         if (!UriParser.IsKnownScheme (UriSchemePack))
40                                 UriParser.Register (new PackUriParser (), UriSchemePack, -1);
41                 }
42                 
43                 public static int ComparePackUri (Uri firstPackUri, Uri secondPackUri)
44                 {
45                         // FIXME: Do i need to do validation that it is a pack:// uri?
46                         if (firstPackUri == null)
47                                 return secondPackUri == null ? 0 : -1;
48                         if (secondPackUri == null)
49                                 return 1;
50
51                         // FIXME: What exactly is compared. Lets assume originalstring
52                         return firstPackUri.OriginalString.CompareTo (secondPackUri.OriginalString);
53                 }
54
55                 public static int ComparePartUri (Uri firstPartUri, Uri secondPartUri)
56                 {
57                         // FIXME: Do i need to do validation that it is a part URI?
58                         if (firstPartUri == null)
59                                 return secondPartUri == null ? 0 : -1;
60                         if (secondPartUri == null)
61                                 return 1;
62
63                         return firstPartUri.OriginalString.CompareTo (secondPartUri.OriginalString);
64                 }
65
66                 public static Uri Create (Uri packageUri)
67                 {
68                         return Create (packageUri, null, null);
69                 }
70
71                 public static Uri Create (Uri packageUri, Uri partUri)
72                 {
73                         return Create (packageUri, partUri, null);
74                 }
75
76                 public static Uri Create (Uri packageUri, Uri partUri, string fragment)
77                 {
78                         Check.PackageUri (packageUri);
79                         Check.PackageUriIsValid (packageUri);
80                         
81                         if (partUri != null)
82                                 Check.PartUriIsValid (partUri);
83                         
84                         if (fragment != null && (fragment.Length == 0 || fragment[0] != '#'))
85                                 throw new ArgumentException ("Fragment", "Fragment must not be empty and must start with '#'");
86
87                         // FIXME: Validate that partUri is a valid one? Must be relative, must start with '/'
88
89                         // First replace the slashes, then escape the special characters
90                         //string orig = packageUri.GetComponents(UriComponents.AbsoluteUri, UriFormat.UriEscaped);
91                         string orig = packageUri.OriginalString;
92
93                         foreach (var ch in _escapedChars)
94                         {
95                                 orig = !orig.Contains(ch.ToString()) ? orig : orig.Replace(ch.ToString(), Uri.HexEscape(ch));
96                         }
97
98                         orig = orig.Replace('/', ',');
99
100                         if (partUri != null)
101                                 orig += partUri.OriginalString;
102
103                         if ((fragment == null && partUri == null)&& orig[orig.Length - 1] != '/')
104                                 orig += '/';
105
106                         if (fragment != null)
107                                 orig += fragment;
108                         
109                         return new Uri ("pack://" + orig);
110                 }
111
112                 public static Uri CreatePartUri (Uri partUri)
113                 {
114                         Check.PartUri (partUri);
115                         
116                         if (partUri.OriginalString[0] != '/')
117                                 partUri = new Uri("/" + partUri.ToString (), UriKind.Relative);
118                         return partUri;
119                 }
120
121                 public static Uri GetNormalizedPartUri (Uri partUri)
122                 {
123                         Check.PartUri (partUri);
124                         return new Uri (partUri.ToString ().ToUpperInvariant (), UriKind.Relative);
125                 }
126
127                 public static Uri GetPackageUri (Uri packUri)
128                 {
129                         Check.PackUri (packUri);
130                         Check.PackUriIsValid (packUri);
131
132                         string s = packUri.Host.Replace(',', '/');
133                         return new Uri (Uri.UnescapeDataString(s), UriKind.RelativeOrAbsolute);
134                 }
135
136                 public static Uri GetPartUri (Uri packUri)
137                 {
138                         Check.PackUri(packUri);
139                         Check.PackUriIsValid(packUri);
140
141                         if (string.IsNullOrEmpty(packUri.AbsolutePath) || packUri.AbsolutePath == "/")
142                                 return null;
143
144                         return new Uri(packUri.AbsolutePath, UriKind.Relative);
145                 }
146
147                 public static Uri GetRelationshipPartUri (Uri partUri)
148                 {
149                         Check.PartUri (partUri);
150                         Check.PartUriIsValid (partUri);
151                         
152                         int index = partUri.OriginalString.LastIndexOf ("/");
153                         string s = partUri.OriginalString.Substring (0, index);
154                         s += "/_rels" + partUri.OriginalString.Substring (index) + ".rels";
155                         return new Uri (s, UriKind.Relative);
156                 }
157
158                 public static Uri GetRelativeUri (Uri sourcePartUri, Uri targetPartUri)
159                 {
160                         Check.SourcePartUri (sourcePartUri);
161                         Check.TargetPartUri (targetPartUri);
162
163                         Uri uri = new Uri ("http://fake.com");
164                         Uri a = new Uri (uri, sourcePartUri.OriginalString);
165                         Uri b = new Uri (uri, targetPartUri.OriginalString);
166
167                         return a.MakeRelativeUri(b);
168                 }
169
170                 public static Uri GetSourcePartUriFromRelationshipPartUri (Uri relationshipPartUri)
171                 {
172                         //Check.RelationshipPartUri (relationshipPartUri);
173                         if (!IsRelationshipPartUri (relationshipPartUri))
174                                 throw new Exception  ("is not a relationship part!?");
175                         return null;
176                 }
177
178                 public static bool IsRelationshipPartUri (Uri partUri)
179                 {
180                         Check.PartUri (partUri);
181                         return partUri.OriginalString.StartsWith ("/_rels") && partUri.OriginalString.EndsWith (".rels");
182                 }
183
184                 public static Uri ResolvePartUri (Uri sourcePartUri, Uri targetUri)
185                 {
186                         Check.SourcePartUri (sourcePartUri);
187                         Check.TargetUri (targetUri);
188                         
189                         Check.PartUriIsValid (sourcePartUri);
190                         if (targetUri.IsAbsoluteUri)
191                                 throw new ArgumentException ("targetUri", "Absolute URIs are not supported");
192
193                         Uri uri = new Uri ("http://fake.com");
194                         uri = new Uri (uri, sourcePartUri);
195                         uri = new Uri (uri, targetUri);
196
197                         // Trim out 'http://fake.com'
198                         return new Uri (uri.OriginalString.Substring (15), UriKind.Relative); 
199                 }
200         }
201
202 }