HttpRemotingHandlerFactory.jvm.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory
  3. //
  4. // Authors:
  5. // Martin Willemoes Hansen ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // (C) 2003 Martin Willemoes Hansen
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Web;
  31. using System.IO;
  32. using System.Runtime.Remoting;
  33. using System.Runtime.Remoting.Channels;
  34. namespace System.Runtime.Remoting.Channels.Http
  35. {
  36. public class HttpRemotingHandlerFactory : IHttpHandlerFactory
  37. {
  38. public HttpRemotingHandlerFactory ()
  39. {
  40. }
  41. private static HttpRemotingHandlerFactoryData CurrentHttpRemotingHandlerFactoryData
  42. {
  43. get
  44. {
  45. HttpRemotingHandlerFactoryData res = (HttpRemotingHandlerFactoryData)AppDomain.CurrentDomain.GetData("HttpRemotingHandlerFactory");
  46. if (res == null)
  47. {
  48. res = new HttpRemotingHandlerFactoryData();
  49. AppDomain.CurrentDomain.SetData("HttpRemotingHandlerFactory", res);
  50. }
  51. return res;
  52. }
  53. }
  54. public IHttpHandler GetHandler (HttpContext context,
  55. string verb,
  56. string url,
  57. string filePath)
  58. {
  59. if (!CurrentHttpRemotingHandlerFactoryData.webConfigLoaded)
  60. ConfigureHttpChannel (context);
  61. return new HttpRemotingHandler (CurrentHttpRemotingHandlerFactoryData.transportSink);
  62. }
  63. void ConfigureHttpChannel (HttpContext context)
  64. {
  65. lock (GetType())
  66. {
  67. if (CurrentHttpRemotingHandlerFactoryData.webConfigLoaded) return;
  68. // Look for a channel that wants to receive http request
  69. IChannelReceiverHook chook = null;
  70. foreach (IChannel channel in ChannelServices.RegisteredChannels)
  71. {
  72. chook = channel as IChannelReceiverHook;
  73. if (chook == null) continue;
  74. if (chook.ChannelScheme != "http")
  75. throw new RemotingException ("Only http channels are allowed when hosting remoting objects in a web server");
  76. if (!chook.WantsToListen)
  77. {
  78. chook = null;
  79. continue;
  80. }
  81. //found chook
  82. break;
  83. }
  84. if (chook == null)
  85. {
  86. HttpChannel chan = new HttpChannel();
  87. ChannelServices.RegisterChannel(chan);
  88. chook = chan;
  89. }
  90. // Register the uri for the channel. The channel uri includes the scheme, the
  91. // host and the application path
  92. string channelUrl = context.Request.Url.GetLeftPart(UriPartial.Authority);
  93. channelUrl += context.Request.ApplicationPath;
  94. chook.AddHookChannelUri (channelUrl);
  95. CurrentHttpRemotingHandlerFactoryData.transportSink = new HttpServerTransportSink (chook.ChannelSinkChain, null);
  96. CurrentHttpRemotingHandlerFactoryData.webConfigLoaded = true;
  97. }
  98. }
  99. public void ReleaseHandler (IHttpHandler handler)
  100. {
  101. }
  102. }
  103. internal class HttpRemotingHandlerFactoryData
  104. {
  105. internal bool webConfigLoaded = false;
  106. internal HttpServerTransportSink transportSink = null;
  107. }
  108. }