* RemotingServices.cs: Added missing Conditional attribute and
[mono.git] / mcs / class / corlib / System.Runtime.Remoting / RemotingServices.cs
1 //
2 // System.Runtime.Remoting.RemotingServices.cs
3 //
4 // Authors:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //   Lluis Sanchez Gual (lluis@ideary.com)
7 //   Patrik Torstensson
8 //
9 // (C) 2001 Ximian, Inc.  http://www.ximian.com
10 //
11
12 using System;
13 using System.Diagnostics;
14 using System.Text;
15 using System.Reflection;
16 using System.Threading;
17 using System.Collections;
18 using System.Runtime.Remoting.Messaging;
19 using System.Runtime.Remoting.Proxies;
20 using System.Runtime.Remoting.Channels;
21 using System.Runtime.Remoting.Contexts;
22 using System.Runtime.Remoting.Activation;
23 using System.Runtime.Remoting.Lifetime;
24 using System.Runtime.CompilerServices;
25 using System.Runtime.Serialization;
26 using System.IO;
27
28 namespace System.Runtime.Remoting
29 {
30         public sealed class RemotingServices 
31         {
32                 // Holds the identities of the objects, using uri as index
33                 static Hashtable uri_hash = new Hashtable ();           
34
35                 internal static string app_id;
36                 static int next_id = 1;
37                 
38                 static RemotingServices ()
39                 {
40                         RegisterInternalChannels ();
41                         app_id = Guid.NewGuid().ToString().Replace('-', '_') + "/";
42                         CreateWellKnownServerIdentity (typeof(RemoteActivator), "RemoteActivationService.rem", WellKnownObjectMode.Singleton);
43                 }
44         
45                 private RemotingServices () {}
46
47                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48                 internal extern static object InternalExecute (MonoMethod method, Object obj,
49                                                                Object[] parameters, out object [] out_args);
50
51                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52                 public extern static bool IsTransparentProxy (object proxy);
53                 
54                 internal static IMethodReturnMessage InternalExecuteMessage (
55                         MarshalByRefObject target, IMethodCallMessage reqMsg)
56                 {
57                         ReturnMessage result;
58                         
59                         MonoMethod method = (MonoMethod) target.GetType().GetMethod(reqMsg.MethodName, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance, null, (Type[]) reqMsg.MethodSignature, null);
60                         object oldContext = CallContext.SetCurrentCallContext (reqMsg.LogicalCallContext);
61                         
62                         try 
63                         {
64                                 object [] out_args;
65                                 object rval = InternalExecute (method, target, reqMsg.Args, out out_args);
66                         
67                                 // Collect parameters with Out flag from the request message
68                                 // FIXME: This can be done in the unmanaged side and will be
69                                 // more efficient
70                                 
71                                 ParameterInfo[] parameters = method.GetParameters();
72                                 object[] returnArgs = new object [parameters.Length];
73                                 
74                                 int n = 0;
75                                 int noa = 0;
76                                 foreach (ParameterInfo par in parameters)
77                                 {
78                                         if (par.IsOut && !par.ParameterType.IsByRef) 
79                                                 returnArgs [n++] = reqMsg.GetArg (par.Position);
80                                         else if (par.ParameterType.IsByRef)
81                                                 returnArgs [n++] = out_args [noa++]; 
82                                         else
83                                                 returnArgs [n++] = null; 
84                                 }
85                                 
86                                 result = new ReturnMessage (rval, returnArgs, n, CallContext.CreateLogicalCallContext(), reqMsg);
87                         } 
88                         catch (Exception e) 
89                         {
90                                 result = new ReturnMessage (e, reqMsg);
91                         }
92                         
93                         CallContext.RestoreCallContext (oldContext);
94                         return result;
95                 }
96
97                 public static IMethodReturnMessage ExecuteMessage (
98                         MarshalByRefObject target, IMethodCallMessage reqMsg)
99                 {
100                         if (IsTransparentProxy(target))
101                         {
102                                 // Message must go through all chain of sinks
103                                 RealProxy rp = GetRealProxy (target);
104                                 return (IMethodReturnMessage) rp.Invoke (reqMsg);
105                         }
106                         else    // Direct call
107                                 return InternalExecuteMessage (target, reqMsg);
108                 }
109
110                 public static object Connect (Type classToProxy, string url)
111                 {
112                         ObjRef objRef = new ObjRef (classToProxy, url, null);
113                         return GetRemoteObject (objRef, classToProxy);
114                 }
115
116                 public static object Connect (Type classToProxy, string url, object data)
117                 {
118                         ObjRef objRef = new ObjRef (classToProxy, url, data);
119                         return GetRemoteObject (objRef, classToProxy);
120                 }
121
122                 public static bool Disconnect (MarshalByRefObject obj)
123                 {
124                         if (obj == null) throw new ArgumentNullException ("obj");
125
126                         ServerIdentity identity;
127
128                         if (IsTransparentProxy (obj))
129                         {
130                                 // CBOs are always accessed through a proxy, even in the server, so
131                                 // for server CBOs it is ok to disconnect a proxy
132
133                                 RealProxy proxy = GetRealProxy(obj);
134                                 if (proxy.GetProxiedType().IsContextful && (proxy.ObjectIdentity is ServerIdentity))
135                                         identity = proxy.ObjectIdentity as ServerIdentity;
136                                 else
137                                         throw new ArgumentException ("The obj parameter is a proxy.");
138                         }
139                         else
140                                 identity = obj.ObjectIdentity;
141
142                         if (identity == null || !identity.IsConnected)
143                                 return false;
144                         else
145                         {
146                                 LifetimeServices.StopTrackingLifetime (identity);
147                                 DisposeIdentity (identity);
148                                 return true;
149                         }
150                 }
151
152                 public static Type GetServerTypeForUri (string uri)
153                 {
154                         ServerIdentity ident = GetIdentityForUri (uri) as ServerIdentity;
155                         if (ident == null) return null;
156                         return ident.ObjectType;
157                 }
158
159                 public static string GetObjectUri (MarshalByRefObject obj)
160                 {
161                         Identity ident = GetObjectIdentity(obj);
162                         if (ident is ClientIdentity) return ((ClientIdentity)ident).TargetUri;
163                         else if (ident != null) return ident.ObjectUri;
164                         else return null;
165                 }
166
167                 public static object Unmarshal (ObjRef objref)
168                 {
169                         return Unmarshal(objref, true);
170                 }
171
172                 public static object Unmarshal (ObjRef objref, bool fRefine)
173                 {
174                         Type classToProxy = fRefine ? objref.ServerType : typeof (MarshalByRefObject);
175                         if (classToProxy == null) classToProxy = typeof (MarshalByRefObject);
176
177                         if (objref.IsReferenceToWellKnow)
178                                 return GetRemoteObject(objref, classToProxy);
179                         else
180                         {
181                                 if (classToProxy.IsContextful)
182                                 {
183                                         // Look for a ProxyAttribute
184                                         ProxyAttribute att = (ProxyAttribute) Attribute.GetCustomAttribute (classToProxy, typeof(ProxyAttribute),true);
185                                         if (att != null)
186                                                 return att.CreateProxy (objref, classToProxy, null, null).GetTransparentProxy();
187                                 }
188                                 return GetProxyForRemoteObject (objref, classToProxy);
189                         }
190                 }
191
192                 public static ObjRef Marshal (MarshalByRefObject obj)
193                 {
194                         return Marshal (obj, null, null);
195                 }
196                 
197                 public static ObjRef Marshal (MarshalByRefObject obj, string uri)
198                 {
199                         return Marshal (obj, uri, null);
200                 }
201                 
202                 public static ObjRef Marshal (MarshalByRefObject obj, string uri, Type requested_type)
203                 {
204                         if (IsTransparentProxy (obj))
205                         {
206                                 RealProxy proxy = RemotingServices.GetRealProxy(obj);
207                                 Identity identity = proxy.ObjectIdentity;
208
209                                 if (identity != null)
210                                 {
211                                         if (proxy.GetProxiedType().IsContextful && !identity.IsConnected)
212                                         {
213                                                 // Unregistered local contextbound object. Register now.
214                                                 ClientActivatedIdentity cboundIdentity = (ClientActivatedIdentity)identity;
215                                                 if (uri == null) uri = NewUri();
216                                                 cboundIdentity.ObjectUri = uri;
217                                                 RegisterServerIdentity (cboundIdentity);
218                                                 cboundIdentity.StartTrackingLifetime ((ILease)obj.InitializeLifetimeService());
219                                                 return cboundIdentity.CreateObjRef(requested_type);
220                                         }
221                                         else if (uri != null)
222                                                 throw new RemotingException ("It is not possible marshal a proxy of a remote object.");
223
224                                         return proxy.ObjectIdentity.CreateObjRef(requested_type);
225                                 }
226                         }
227
228                         if (requested_type == null) requested_type = obj.GetType();
229
230                         if (uri == null) 
231                         {
232                                 if (obj.ObjectIdentity == null)
233                                 {
234                                         uri = NewUri();
235                                         CreateClientActivatedServerIdentity (obj, requested_type, uri);
236                                 }
237                         }
238                         else
239                         {
240                                 ClientActivatedIdentity identity = GetIdentityForUri ("/" + uri) as ClientActivatedIdentity;
241                                 if (identity == null || obj != identity.GetServerObject()) 
242                                         CreateClientActivatedServerIdentity (obj, requested_type, uri);
243                         }
244
245                         if (IsTransparentProxy (obj))
246                                 return RemotingServices.GetRealProxy(obj).ObjectIdentity.CreateObjRef (requested_type);
247                         else
248                                 return obj.CreateObjRef(requested_type);
249                 }
250
251                 static string NewUri ()
252                 {
253                         int n = Interlocked.Increment (ref next_id);
254                         return app_id + Environment.TickCount + "_" + n + ".rem";
255                 }
256
257                 public static RealProxy GetRealProxy (object proxy)
258                 {
259                         if (!IsTransparentProxy(proxy)) throw new RemotingException("Cannot get the real proxy from an object that is not a transparent proxy.");
260                         return (RealProxy)((TransparentProxy)proxy)._rp;
261                 }
262
263                 public static MethodBase GetMethodBaseFromMethodMessage(IMethodMessage msg)
264                 {
265                         Type type = Type.GetType (msg.TypeName);
266                         if (type == null)
267                                 throw new RemotingException ("Type '" + msg.TypeName + "' not found.");
268
269                         BindingFlags bflags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
270                         
271                         MethodBase method;
272                         if (msg.MethodSignature == null)
273                                 method = type.GetMethod (msg.MethodName, bflags);
274                         else
275                                 method = type.GetMethod (msg.MethodName, bflags, null, (Type[]) msg.MethodSignature, null);
276                         
277                         if (method != null) 
278                                 return method;
279                         
280                         if (msg.MethodSignature == null)
281                                 return type.GetConstructor (bflags, null, Type.EmptyTypes, null);
282                         else
283                                 return type.GetConstructor (bflags, null, (Type[]) msg.MethodSignature, null);
284                 }
285
286                 public static void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
287                 {
288                         if (obj == null) throw new ArgumentNullException ("obj");
289
290                         ObjRef oref = Marshal ((MarshalByRefObject)obj);
291                         oref.GetObjectData (info, context);
292                 }
293
294                 public static ObjRef GetObjRefForProxy(MarshalByRefObject obj)
295                 {
296                         Identity ident = GetObjectIdentity(obj);
297                         if (ident == null) return null;
298                         else return ident.CreateObjRef(null);
299                 }
300
301                 public static object GetLifetimeService (MarshalByRefObject obj)
302                 {
303                         if (obj == null) return null;
304                         return obj.GetLifetimeService ();
305                 }
306
307                 public static IMessageSink GetEnvoyChainForProxy (MarshalByRefObject obj)
308                 {
309                         if (IsTransparentProxy(obj))
310                                 return ((ClientIdentity)GetRealProxy (obj).ObjectIdentity).EnvoySink;
311                         else
312                                 throw new ArgumentException ("obj must be a proxy.","obj");                     
313                 }
314
315                 [MonoTODO]
316                 [Conditional ("REMOTING_PERF")]
317                 public static void LogRemotingStage (int stage)
318                 {
319                         throw new NotImplementedException ();
320                 }
321
322                 public static string GetSessionIdForMethodMessage(IMethodMessage msg)
323                 {
324                         // It seems that this it what MS returns.
325                         return msg.Uri;
326                 }
327
328                 public static bool IsMethodOverloaded(IMethodMessage msg)
329                 {
330                         // TODO: use internal call for better performance
331                         Type type = msg.MethodBase.DeclaringType;
332                         MemberInfo[] members = type.GetMember (msg.MethodName, MemberTypes.Method, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
333                         return members.Length > 1;
334                 }
335
336                 public static bool IsObjectOutOfAppDomain(object tp)
337                 {
338                         // TODO: use internal call for better performance
339                         Identity ident = GetObjectIdentity((MarshalByRefObject)tp);
340                         if (ident != null) return !ident.IsFromThisAppDomain;
341                         else return false;
342                 }
343
344                 public static bool IsObjectOutOfContext(object tp)
345                 {
346                         // TODO: use internal call for better performance
347                         ServerIdentity ident = GetObjectIdentity((MarshalByRefObject)tp) as ServerIdentity;
348                         if (ident != null) return ident.Context != System.Threading.Thread.CurrentContext;
349                         else return false;
350                 }
351
352                 public static bool IsOneWay(MethodBase method)
353                 {
354                         // TODO: use internal call for better performance
355                         object[] atts = method.GetCustomAttributes (typeof (OneWayAttribute), false);
356                         return atts.Length > 0;
357                 }
358
359                 internal static bool IsAsyncMessage(IMessage msg)
360                 {
361                         if (! (msg is MonoMethodMessage)) return false;
362                         else if (((MonoMethodMessage)msg).IsAsync) return true;
363                         else if (IsOneWay (((MonoMethodMessage)msg).MethodBase)) return true;
364                         else return false;
365                 }
366
367                 public static void SetObjectUriForMarshal(MarshalByRefObject obj, string uri)
368                 {
369                         if (IsTransparentProxy (obj)) throw new RemotingException ("SetObjectUriForMarshal method should only be called for MarshalByRefObjects that exist in the current AppDomain.");
370                         Marshal (obj, uri);
371                 }
372
373                 #region Internal Methods
374                 
375                 internal static object CreateClientProxy (ActivatedClientTypeEntry entry, object[] activationAttributes)
376                 {
377                         if (entry.ContextAttributes != null || activationAttributes != null)
378                         {
379                                 ArrayList props = new ArrayList ();
380                                 if (entry.ContextAttributes != null) props.AddRange (entry.ContextAttributes);
381                                 if (activationAttributes != null) props.AddRange (activationAttributes);
382                                 return CreateClientProxy (entry.ObjectType, entry.ApplicationUrl, props.ToArray ());
383                         }
384                         else
385                                 return CreateClientProxy (entry.ObjectType, entry.ApplicationUrl, null);
386                 }
387         
388                 internal static object CreateClientProxy (Type objectType, string url, object[] activationAttributes)
389                 {
390                         string activationUrl = url + "/RemoteActivationService.rem";
391
392                         string objectUri;
393                         IMessageSink sink = GetClientChannelSinkChain (activationUrl, null, out objectUri);
394
395                         RemotingProxy proxy = new RemotingProxy (objectType, activationUrl, activationAttributes);
396                         return proxy.GetTransparentProxy();
397                 }
398         
399                 internal static object CreateClientProxy (WellKnownClientTypeEntry entry)
400                 {
401                         return Connect (entry.ObjectType, entry.ObjectUrl, null);
402                 }
403         
404                 internal static object CreateClientProxyForContextBound (Type type, object[] activationAttributes)
405                 {
406                         if (type.IsContextful)
407                         {
408                                 // Look for a ProxyAttribute
409                                 ProxyAttribute att = (ProxyAttribute) Attribute.GetCustomAttribute (type, typeof(ProxyAttribute), true);
410                                 if (att != null)
411                                         return att.CreateInstance (type);
412                         }
413                         RemotingProxy proxy = new RemotingProxy (type, ChannelServices.CrossContextUrl, activationAttributes);
414                         return proxy.GetTransparentProxy();
415                 }
416         
417                 internal static Identity GetIdentityForUri (string uri)
418                 {
419                         lock (uri_hash)
420                         {
421                                 return (Identity)uri_hash [GetNormalizedUri(uri)];
422                         }
423                 }
424
425                 internal static Identity GetObjectIdentity (MarshalByRefObject obj)
426                 {
427                         if (IsTransparentProxy(obj))
428                                 return GetRealProxy (obj).ObjectIdentity;
429                         else
430                                 return obj.ObjectIdentity;
431                 }
432
433                 internal static ClientIdentity GetOrCreateClientIdentity(ObjRef objRef, Type proxyType, out object clientProxy)
434                 {
435                         // This method looks for an identity for the given url. 
436                         // If an identity is not found, it creates the identity and 
437                         // assigns it a proxy to the remote object.
438
439                         // Creates the client sink chain for the given url or channelData.
440                         // It will also get the object uri from the url.
441
442                         object channelData = objRef.ChannelInfo != null ? objRef.ChannelInfo.ChannelData : null;
443                         string url = (channelData == null) ? objRef.URI : null;
444
445                         string objectUri;
446                         IMessageSink sink = GetClientChannelSinkChain (url, channelData, out objectUri);
447
448                         if (objectUri == null) objectUri = objRef.URI;
449
450                         lock (uri_hash)
451                         {
452                                 clientProxy = null;
453                                 string uri = GetNormalizedUri (objRef.URI);
454                                 
455                                 ClientIdentity identity = uri_hash [uri] as ClientIdentity;
456                                 if (identity != null)
457                                 {
458                                         // Object already registered
459                                         clientProxy = identity.ClientProxy;
460                                         if (clientProxy != null) return identity;
461                                         
462                                         // The proxy has just been GCed, so its identity cannot
463                                         // be reused. Just dispose it.
464                                         DisposeIdentity (identity);
465                                 }
466
467                                 // Creates an identity and a proxy for the remote object
468
469                                 identity = new ClientIdentity (objectUri, objRef);
470                                 identity.ChannelSink = sink;
471
472                                 // Registers the identity
473                                 uri_hash [uri] = identity;
474                                 
475                                 if (proxyType != null)
476                                 {
477                                         RemotingProxy proxy = new RemotingProxy (proxyType, identity);
478                                         clientProxy = proxy.GetTransparentProxy();
479                                         identity.ClientProxy = (MarshalByRefObject) clientProxy;
480                                 }
481
482                                 return identity;
483                         }
484                 }
485
486                 static IMessageSink GetClientChannelSinkChain(string url, object channelData, out string objectUri)
487                 {
488                         IMessageSink sink = ChannelServices.CreateClientChannelSinkChain (url, channelData, out objectUri);
489                         if (sink == null) 
490                         {
491                                 if (url != null) 
492                                 {
493                                         string msg = String.Format ("Cannot create channel sink to connect to URL {0}. An appropriate channel has probably not been registered.", url); 
494                                         throw new RemotingException (msg);
495                                 }
496                                 else 
497                                 {
498                                         string msg = String.Format ("Cannot create channel sink to connect to the remote object. An appropriate channel has probably not been registered.", url); 
499                                         throw new RemotingException (msg);
500                                 }
501                         }
502                         return sink;
503                 }
504
505                 internal static ClientActivatedIdentity CreateContextBoundObjectIdentity(Type objectType)
506                 {
507                         ClientActivatedIdentity identity = new ClientActivatedIdentity (null, objectType);
508                         identity.ChannelSink = ChannelServices.CrossContextChannel;
509                         return identity;
510                 }
511
512                 internal static ClientActivatedIdentity CreateClientActivatedServerIdentity(MarshalByRefObject realObject, Type objectType, string objectUri)
513                 {
514                         ClientActivatedIdentity identity = new ClientActivatedIdentity (objectUri, objectType);
515                         identity.AttachServerObject (realObject, Context.DefaultContext);
516                         RegisterServerIdentity (identity);
517                         identity.StartTrackingLifetime ((ILease)realObject.InitializeLifetimeService ());
518                         return identity;
519                 }
520
521                 internal static ServerIdentity CreateWellKnownServerIdentity(Type objectType, string objectUri, WellKnownObjectMode mode)
522                 {
523                         ServerIdentity identity;
524
525                         if (mode == WellKnownObjectMode.SingleCall)
526                                 identity = new  SingleCallIdentity(objectUri, Context.DefaultContext, objectType);
527                         else
528                                 identity = new  SingletonIdentity(objectUri, Context.DefaultContext, objectType);
529
530                         RegisterServerIdentity (identity);
531                         return identity;
532                 }
533
534                 private static void RegisterServerIdentity(ServerIdentity identity)
535                 {
536                         lock (uri_hash)
537                         {
538                                 if (uri_hash.ContainsKey (identity.ObjectUri)) 
539                                         throw new RemotingException ("Uri already in use: " + identity.ObjectUri + ".");
540
541                                 uri_hash[identity.ObjectUri] = identity;
542                         }
543                 }
544
545                 internal static object GetProxyForRemoteObject (ObjRef objref, Type classToProxy)
546                 {
547                         ClientActivatedIdentity identity = GetIdentityForUri (objref.URI) as ClientActivatedIdentity;
548                         if (identity != null) return identity.GetServerObject ();
549                         else return GetRemoteObject (objref, classToProxy);
550                 }
551
552                 internal static object GetRemoteObject(ObjRef objRef, Type proxyType)
553                 {
554                         object proxy;
555                         GetOrCreateClientIdentity (objRef, proxyType, out proxy);
556                         return proxy;
557                 }
558
559                 internal static object GetDomainProxy(AppDomain domain) 
560                 {
561                         byte[] data = null;
562
563                         Context currentContext = Thread.CurrentContext;
564
565                         try
566                         {
567                                 data = (byte[])AppDomain.InvokeInDomain (domain, typeof (AppDomain).GetMethod ("GetMarshalledDomainObjRef", BindingFlags.Instance|BindingFlags.NonPublic), domain, null);
568                         }
569                         finally
570                         {
571                                 AppDomain.InternalSetContext (currentContext);
572                         }                               
573
574                         MemoryStream stream = new MemoryStream (data);
575                         ObjRef appref = (ObjRef) CADSerializer.DeserializeObject (stream);
576                         return (AppDomain) RemotingServices.Unmarshal(appref);
577                 }
578
579                 private static void RegisterInternalChannels() 
580                 {
581                         CrossAppDomainChannel.RegisterCrossAppDomainChannel();
582                 }
583                 
584                 internal static void DisposeIdentity (Identity ident)
585                 {
586                         lock (uri_hash)
587                         {
588                                 if (!ident.Disposed) {
589                                         ClientIdentity clientId = ident as ClientIdentity;
590                                         if (clientId != null)
591                                                 uri_hash.Remove (GetNormalizedUri (clientId.TargetUri));
592                                         else
593                                                 uri_hash.Remove (ident.ObjectUri);
594                                                 
595                                         ident.Disposed = true;
596                                 }
597                         }
598                 }
599
600                 internal static Identity GetMessageTargetIdentity (IMessage msg)
601                 {
602                         // Returns the identity where the message is sent
603
604                         if (msg is IInternalMessage) 
605                                 return ((IInternalMessage)msg).TargetIdentity;
606
607                         lock (uri_hash)
608                         {
609                                 string uri = GetNormalizedUri (((IMethodMessage)msg).Uri);
610                                 return uri_hash [uri] as ServerIdentity;
611                         }
612                 }
613
614                 internal static void SetMessageTargetIdentity (IMessage msg, Identity ident)
615                 {
616                         if (msg is IInternalMessage) 
617                                 ((IInternalMessage)msg).TargetIdentity = ident;
618                 }
619                 
620                 internal static bool UpdateOutArgObject (ParameterInfo pi, object local, object remote)
621                 {
622                         if (local is StringBuilder) 
623                         {
624                                 StringBuilder sb = local as StringBuilder;
625                                 sb.Remove (0, sb.Length);
626                                 sb.Append (remote.ToString());
627                                 return true;
628                         }
629                         else if (pi.ParameterType.IsArray && ((Array)local).Rank == 1)
630                         {
631                                 Array alocal = (Array) local;
632                                 if (alocal.Rank == 1)
633                                 {
634                                         Array.Copy ((Array) remote, alocal, alocal.Length);
635                                         return true;
636                                 }
637                                 else
638                                 {
639                                         // TODO
640                                 }
641                         }
642                         return false;
643                 }
644                 
645                 static string GetNormalizedUri (string uri)
646                 {
647                         if (uri.StartsWith ("/")) return uri.Substring (1);
648                         else return uri;
649                 }
650
651                 #endregion
652         }
653 }