HttpRemotingHandlerFactory.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. static bool webConfigLoaded = false;
  39. static HttpServerTransportSink transportSink = null;
  40. public HttpRemotingHandlerFactory ()
  41. {
  42. }
  43. public IHttpHandler GetHandler (HttpContext context,
  44. string verb,
  45. string url,
  46. string filePath)
  47. {
  48. if (!webConfigLoaded)
  49. ConfigureHttpChannel (context);
  50. return new HttpRemotingHandler (transportSink);
  51. }
  52. void ConfigureHttpChannel (HttpContext context)
  53. {
  54. lock (GetType())
  55. {
  56. if (webConfigLoaded) return;
  57. // Look for a channel that wants to receive http request
  58. IChannelReceiverHook chook = null;
  59. foreach (IChannel channel in ChannelServices.RegisteredChannels)
  60. {
  61. chook = channel as IChannelReceiverHook;
  62. if (chook == null) continue;
  63. if (chook.ChannelScheme != "http")
  64. throw new RemotingException ("Only http channels are allowed when hosting remoting objects in a web server");
  65. if (!chook.WantsToListen)
  66. {
  67. chook = null;
  68. continue;
  69. }
  70. //found chook
  71. break;
  72. }
  73. if (chook == null)
  74. {
  75. HttpChannel chan = new HttpChannel();
  76. ChannelServices.RegisterChannel(chan);
  77. chook = chan;
  78. }
  79. // Register the uri for the channel. The channel uri includes the scheme, the
  80. // host and the application path
  81. string channelUrl = context.Request.Url.GetLeftPart(UriPartial.Authority);
  82. channelUrl += context.Request.ApplicationPath;
  83. chook.AddHookChannelUri (channelUrl);
  84. transportSink = new HttpServerTransportSink (chook.ChannelSinkChain, null);
  85. webConfigLoaded = true;
  86. }
  87. }
  88. public void ReleaseHandler (IHttpHandler handler)
  89. {
  90. }
  91. }
  92. }