/mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Http/HttpChannel.cs

https://github.com/iainlane/mono · C# · 238 lines · 157 code · 49 blank · 32 comment · 37 complexity · e64120aa1b3052f34cba1df2e92d5947 MD5 · raw file

  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.
  27. //
  28. using System.Collections;
  29. using System.Runtime.Remoting.Messaging;
  30. namespace System.Runtime.Remoting.Channels.Http
  31. {
  32. public class HttpChannel : BaseChannelWithProperties,
  33. IChannel, IChannelReceiver, IChannelReceiverHook, IChannelSender
  34. #if NET_2_0
  35. , ISecurableChannel
  36. #endif
  37. {
  38. HttpClientChannel client;
  39. HttpServerChannel server;
  40. string name = "http";
  41. #region Constructors
  42. public HttpChannel ()
  43. {
  44. client = new HttpClientChannel ();
  45. server = new HttpServerChannel ();
  46. }
  47. public HttpChannel (int port)
  48. {
  49. client = new HttpClientChannel ();
  50. server = new HttpServerChannel (port);
  51. }
  52. public HttpChannel (IDictionary properties,
  53. IClientChannelSinkProvider clientSinkProvider,
  54. IServerChannelSinkProvider serverSinkProvider)
  55. {
  56. if (properties != null && properties.Contains ("name")) {
  57. this.name = (string)properties["name"];
  58. }
  59. client = new HttpClientChannel (properties, clientSinkProvider);
  60. server = new HttpServerChannel (properties, serverSinkProvider);
  61. }
  62. #endregion
  63. #region BaseChannelWithProperties overrides
  64. public override object this[object key]
  65. {
  66. get { return Properties[key]; }
  67. set { Properties[key] = value; }
  68. }
  69. public override ICollection Keys
  70. {
  71. get { return Properties.Keys; }
  72. }
  73. public override IDictionary Properties
  74. {
  75. get
  76. {
  77. return new AggregateDictionary (new IDictionary[] {
  78. client.Properties,
  79. server.Properties
  80. });
  81. }
  82. }
  83. #endregion
  84. #region IChannel
  85. public string ChannelName
  86. {
  87. get { return name; }
  88. }
  89. public int ChannelPriority
  90. {
  91. get { return server.ChannelPriority; }
  92. }
  93. public string Parse (string url, out string objectURI)
  94. {
  95. return ParseInternal (url, out objectURI);
  96. }
  97. internal static string ParseInternal (string url, out string objectURI)
  98. {
  99. if (url == null)
  100. throw new ArgumentNullException ("url");
  101. // format: "http://host:port/path/to/object"
  102. objectURI = null;
  103. // url needs to be at least "http:" or "https:"
  104. if (url.Length < 5 ||
  105. (url[0] != 'H' && url[0] != 'h') ||
  106. (url[1] != 'T' && url[1] != 't') ||
  107. (url[2] != 'T' && url[2] != 't') ||
  108. (url[3] != 'P' && url[3] != 'p'))
  109. return null;
  110. int protolen;
  111. if (url[4] == 'S' || url[4] == 's') {
  112. if (url.Length < 6)
  113. return null;
  114. protolen = 5;
  115. } else {
  116. protolen = 4;
  117. }
  118. if (url[protolen] != ':')
  119. return null;
  120. // "http:" and "https:" are acceptable inputs
  121. if (url.Length == protolen + 1)
  122. return url;
  123. // protocol must be followed by "//"
  124. if (url.Length < protolen + 3 || url[protolen + 1] != '/' || url[protolen + 2] != '/')
  125. return null;
  126. // "http://" and "https://" are acceptable inputs
  127. if (url.Length == protolen + 3)
  128. return url;
  129. int slash = url.IndexOf ('/', protolen + 3);
  130. if (slash == -1)
  131. return url;
  132. objectURI = url.Substring (slash);
  133. return url.Substring (0, slash);
  134. }
  135. #endregion
  136. #region IChannelReceiver (: IChannel)
  137. public object ChannelData
  138. {
  139. get { return server.ChannelData; }
  140. }
  141. public string[] GetUrlsForUri (string objectURI)
  142. {
  143. return server.GetUrlsForUri (objectURI);
  144. }
  145. public void StartListening (object data)
  146. {
  147. server.StartListening (data);
  148. }
  149. public void StopListening (object data)
  150. {
  151. server.StopListening (data);
  152. }
  153. #endregion
  154. #region IChannelReceiverHook
  155. public void AddHookChannelUri (string channelUri)
  156. {
  157. server.AddHookChannelUri (channelUri);
  158. }
  159. public string ChannelScheme
  160. {
  161. get { return server.ChannelScheme; }
  162. }
  163. public IServerChannelSink ChannelSinkChain
  164. {
  165. get { return server.ChannelSinkChain; }
  166. }
  167. public bool WantsToListen
  168. {
  169. get { return server.WantsToListen; }
  170. set { server.WantsToListen = value; }
  171. }
  172. #endregion
  173. #region IChannelSender (: IChannel)
  174. public IMessageSink CreateMessageSink (string url, object remoteChannelData, out string objectURI)
  175. {
  176. return client.CreateMessageSink (url, remoteChannelData, out objectURI);
  177. }
  178. #endregion
  179. #if NET_2_0
  180. #region ISecurableChannel
  181. public bool IsSecured
  182. {
  183. get { return client.IsSecured; }
  184. set { client.IsSecured = value; }
  185. }
  186. #endregion
  187. #endif
  188. }
  189. }