AspNetReplyChannel.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // AspNetReplyChannel.cs
  3. //
  4. // Author:
  5. // Ankit Jain <[email protected]>
  6. //
  7. // Copyright (C) 2006 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;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Net;
  32. using System.ServiceModel;
  33. using System.Text;
  34. using System.Threading;
  35. using System.Web;
  36. namespace System.ServiceModel.Channels
  37. {
  38. internal class AspNetReplyChannel : HttpReplyChannel
  39. {
  40. HttpContext http_context;
  41. Uri uri;
  42. public AspNetReplyChannel (HttpChannelListener<IReplyChannel> listener,
  43. TimeSpan timeout)
  44. : base (listener, timeout)
  45. {
  46. uri = listener.Uri;
  47. }
  48. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  49. {
  50. #if false
  51. context = null;
  52. if (waiting.Count == 0 && !WaitForRequest (timeout))
  53. return false;
  54. HttpListenerContext ctx = null;
  55. lock (waiting) {
  56. if (waiting.Count > 0) {
  57. ctx = waiting [0];
  58. waiting.RemoveAt (0);
  59. }
  60. }
  61. if (ctx == null)
  62. // Though as long as this instance is used
  63. // synchronously, it should not happen.
  64. return false;
  65. #endif
  66. Message msg;
  67. if (http_context.Request.HttpMethod == "GET") {
  68. if (http_context.Request.QueryString ["wsdl"] != null) {
  69. msg = Message.CreateMessage (Encoder.MessageVersion,
  70. "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
  71. msg.Headers.To = http_context.Request.Url;
  72. } else {
  73. msg = Message.CreateMessage (Encoder.MessageVersion, null);
  74. msg.Headers.To = http_context.Request.Url;
  75. }
  76. } else {
  77. //FIXME: Do above stuff for HttpContext ?
  78. int maxSizeOfHeaders = 0x10000;
  79. msg = Encoder.ReadMessage (
  80. http_context.Request.InputStream, maxSizeOfHeaders);
  81. if (Encoder.MessageVersion.Envelope == EnvelopeVersion.Soap11) {
  82. string action = GetHeaderItem (http_context.Request.Headers ["SOAPAction"]);
  83. if (action != null)
  84. msg.Headers.Action = action;
  85. }
  86. }
  87. context = new AspNetRequestContext (this, msg, http_context);
  88. return true;
  89. }
  90. public HttpContext Context {
  91. get { return http_context; }
  92. set { http_context = value; }
  93. }
  94. public override bool WaitForRequest (TimeSpan timeout)
  95. {
  96. // FIXME: we might want to take other approaches.
  97. if (timeout.Ticks > int.MaxValue)
  98. timeout = TimeSpan.FromDays (20);
  99. SvcHttpHandler h = SvcHttpHandlerFactory.GetHandler (uri.OriginalString.Replace ("file://", ""));
  100. return h.WaitForRequest (this, timeout);
  101. #if false
  102. AutoResetEvent wait = new AutoResetEvent (false);
  103. source.Http.BeginGetContext (HttpContextReceived, wait);
  104. // FIXME: we might want to take other approaches.
  105. if (timeout.Ticks > int.MaxValue)
  106. timeout = TimeSpan.FromDays (20);
  107. return wait.WaitOne (timeout, false);
  108. #endif
  109. }
  110. void HttpContextReceived (IAsyncResult result)
  111. {
  112. throw new NotImplementedException ();
  113. #if false
  114. waiting.Add (source.Http.EndGetContext (result));
  115. AutoResetEvent wait = (AutoResetEvent) result.AsyncState;
  116. wait.Set ();
  117. #endif
  118. }
  119. }
  120. }