HttpRemotingHandler.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // System.Runtime.Remoting.Channels.Http.HttpRemotingHandler
  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;
  31. using System.IO;
  32. using System.Web;
  33. using System.Collections;
  34. namespace System.Runtime.Remoting.Channels.Http
  35. {
  36. public class HttpRemotingHandler : IHttpHandler
  37. {
  38. HttpServerTransportSink transportSink;
  39. public HttpRemotingHandler ()
  40. {
  41. }
  42. [MonoTODO]
  43. public HttpRemotingHandler (Type type, object srvID)
  44. {
  45. throw new NotImplementedException ();
  46. }
  47. internal HttpRemotingHandler (HttpServerTransportSink sink)
  48. {
  49. transportSink = sink;
  50. }
  51. public bool IsReusable {
  52. get { return true; }
  53. }
  54. public void ProcessRequest (HttpContext context)
  55. {
  56. HttpRequest request = context.Request;
  57. HttpResponse response = context.Response;
  58. // Create transport headers for the request
  59. TransportHeaders theaders = new TransportHeaders();
  60. string objectUri = request.RawUrl;
  61. objectUri = objectUri.Substring (request.ApplicationPath.Length); // application path is not part of the uri
  62. if (request.ApplicationPath.Length > 0 &&
  63. (objectUri.StartsWith("/") || objectUri.StartsWith(@"\")) )
  64. {
  65. objectUri = objectUri.Substring(1);
  66. }
  67. theaders ["__RequestUri"] = objectUri;
  68. theaders ["Content-Type"] = request.ContentType;
  69. theaders ["__RequestVerb"]= request.HttpMethod;
  70. theaders ["__HttpVersion"] = request.Headers ["http-version"];
  71. theaders ["User-Agent"] = request.UserAgent;
  72. theaders ["Host"] = request.Headers ["host"];
  73. ITransportHeaders responseHeaders;
  74. Stream responseStream;
  75. // Dispatch the request
  76. transportSink.DispatchRequest (request.InputStream, theaders, out responseStream, out responseHeaders);
  77. // Write the response
  78. if (responseHeaders != null && responseHeaders["__HttpStatusCode"] != null)
  79. {
  80. // The formatter can set the status code
  81. response.StatusCode = int.Parse ((string) responseHeaders["__HttpStatusCode"]);
  82. response.StatusDescription = (string) responseHeaders["__HttpReasonPhrase"];
  83. }
  84. if (responseHeaders != null)
  85. {
  86. foreach (DictionaryEntry entry in responseHeaders)
  87. {
  88. string key = entry.Key.ToString();
  89. if (key != CommonTransportKeys.HttpStatusCode && key != CommonTransportKeys.HttpReasonPhrase)
  90. response.AppendHeader(key, entry.Value.ToString());
  91. }
  92. }
  93. byte[] bodyBuffer = bodyBuffer = new byte [responseStream.Length];
  94. responseStream.Seek (0, SeekOrigin.Begin);
  95. int nr = 0;
  96. while (nr < responseStream.Length)
  97. nr += responseStream.Read (bodyBuffer, nr, bodyBuffer.Length - nr);
  98. response.OutputStream.Write (bodyBuffer, 0, bodyBuffer.Length);
  99. }
  100. }
  101. }