2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Http / HttpChannel.cs
blobfacd522f99ad55cd855f78a55ef6ef6454a82588
1 //
2 // HttpChannel.cs
3 //
4 // Author:
5 // Michael Hutchinson <mhutchinson@novell.com>
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.Runtime.Remoting.Messaging;
32 namespace System.Runtime.Remoting.Channels.Http
35 public class HttpChannel : BaseChannelWithProperties,
36 IChannel, IChannelReceiver, IChannelReceiverHook, IChannelSender
37 #if NET_2_0
38 , ISecurableChannel
39 #endif
41 HttpClientChannel client;
42 HttpServerChannel server;
43 string name = "http";
45 #region Constructors
47 public HttpChannel ()
49 client = new HttpClientChannel ();
50 server = new HttpServerChannel ();
53 public HttpChannel (int port)
55 client = new HttpClientChannel ();
56 server = new HttpServerChannel (port);
59 public HttpChannel (IDictionary properties,
60 IClientChannelSinkProvider clientSinkProvider,
61 IServerChannelSinkProvider serverSinkProvider)
63 if (properties != null && properties.Contains ("name")) {
64 this.name = (string)properties["name"];
67 client = new HttpClientChannel (properties, clientSinkProvider);
68 server = new HttpServerChannel (properties, serverSinkProvider);
71 #endregion
73 #region BaseChannelWithProperties overrides
75 public override object this[object key]
77 get { return Properties[key]; }
78 set { Properties[key] = value; }
81 public override ICollection Keys
83 get { return Properties.Keys; }
86 public override IDictionary Properties
88 get
90 return new AggregateDictionary (new IDictionary[] {
91 client.Properties,
92 server.Properties
93 });
97 #endregion
99 #region IChannel
101 public string ChannelName
103 get { return name; }
106 public int ChannelPriority
108 get { return server.ChannelPriority; }
111 public string Parse (string url, out string objectURI)
113 return ParseInternal (url, out objectURI);
116 internal static string ParseInternal (string url, out string objectURI)
118 if (url == null)
119 throw new ArgumentNullException ("url");
121 // format: "http://host:port/path/to/object"
122 objectURI = null;
124 // url needs to be at least "http:" or "https:"
125 if (url.Length < 5 ||
126 (url[0] != 'H' && url[0] != 'h') ||
127 (url[1] != 'T' && url[1] != 't') ||
128 (url[2] != 'T' && url[2] != 't') ||
129 (url[3] != 'P' && url[3] != 'p'))
130 return null;
132 int protolen;
133 if (url[4] == 'S' || url[4] == 's') {
134 if (url.Length < 6)
135 return null;
137 protolen = 5;
138 } else {
139 protolen = 4;
142 if (url[protolen] != ':')
143 return null;
145 // "http:" and "https:" are acceptable inputs
146 if (url.Length == protolen + 1)
147 return url;
149 // protocol must be followed by "//"
150 if (url.Length < protolen + 3 || url[protolen + 1] != '/' || url[protolen + 2] != '/')
151 return null;
153 // "http://" and "https://" are acceptable inputs
154 if (url.Length == protolen + 3)
155 return url;
157 int slash = url.IndexOf ('/', protolen + 3);
158 if (slash == -1)
159 return url;
161 objectURI = url.Substring (slash);
163 return url.Substring (0, slash);
166 #endregion
168 #region IChannelReceiver (: IChannel)
170 public object ChannelData
172 get { return server.ChannelData; }
175 public string[] GetUrlsForUri (string objectURI)
177 return server.GetUrlsForUri (objectURI);
180 public void StartListening (object data)
182 server.StartListening (data);
185 public void StopListening (object data)
187 server.StopListening (data);
190 #endregion
192 #region IChannelReceiverHook
194 public void AddHookChannelUri (string channelUri)
196 server.AddHookChannelUri (channelUri);
199 public string ChannelScheme
201 get { return server.ChannelScheme; }
204 public IServerChannelSink ChannelSinkChain
206 get { return server.ChannelSinkChain; }
209 public bool WantsToListen
211 get { return server.WantsToListen; }
212 set { server.WantsToListen = value; }
215 #endregion
217 #region IChannelSender (: IChannel)
219 public IMessageSink CreateMessageSink (string url, object remoteChannelData, out string objectURI)
221 return client.CreateMessageSink (url, remoteChannelData, out objectURI);
224 #endregion
226 #if NET_2_0
227 #region ISecurableChannel
229 public bool IsSecured
231 get { return client.IsSecured; }
232 set { client.IsSecured = value; }
235 #endregion
236 #endif