OSHttpHttpHandler.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Net;
  32. using System.Reflection;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using log4net;
  37. using Nwc.XmlRpc;
  38. namespace OpenSim.Framework.Servers.HttpServer
  39. {
  40. public delegate XmlRpcResponse OSHttpHttpProcessor(XmlRpcRequest request);
  41. public class OSHttpHttpHandler: OSHttpHandler
  42. {
  43. private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. // contains handler for processing HTTP Request
  45. private GenericHTTPMethod _handler;
  46. /// <summary>
  47. /// Instantiate an HTTP handler.
  48. /// </summary>
  49. /// <param name="handler">a GenericHTTPMethod</param>
  50. /// <param name="method">null or HTTP method regex</param>
  51. /// <param name="path">null or path regex</param>
  52. /// <param name="query">null or dictionary with query regexs</param>
  53. /// <param name="headers">null or dictionary with header
  54. /// regexs</param>
  55. /// <param name="whitelist">null or IP address whitelist</param>
  56. public OSHttpHttpHandler(GenericHTTPMethod handler, Regex method, Regex path,
  57. Dictionary<string, Regex> query,
  58. Dictionary<string, Regex> headers, Regex whitelist)
  59. : base(method, path, query, headers, new Regex(@"^text/html", RegexOptions.IgnoreCase | RegexOptions.Compiled),
  60. whitelist)
  61. {
  62. _handler = handler;
  63. }
  64. /// <summary>
  65. /// Instantiate an HTTP handler.
  66. /// </summary>
  67. /// <param name="handler">a GenericHTTPMethod</param>
  68. public OSHttpHttpHandler(GenericHTTPMethod handler)
  69. : this(handler, new Regex(@"^GET$", RegexOptions.IgnoreCase | RegexOptions.Compiled), null, null, null, null)
  70. {
  71. }
  72. /// <summary>
  73. /// Invoked by OSHttpRequestPump.
  74. /// </summary>
  75. public override OSHttpHandlerResult Process(OSHttpRequest request)
  76. {
  77. // call handler method
  78. Hashtable responseData = _handler(request.Query);
  79. int responseCode = (int)responseData["int_response_code"];
  80. string responseString = (string)responseData["str_response_string"];
  81. string contentType = (string)responseData["content_type"];
  82. //Even though only one other part of the entire code uses HTTPHandlers, we shouldn't expect this
  83. //and should check for NullReferenceExceptions
  84. if (string.IsNullOrEmpty(contentType))
  85. {
  86. contentType = "text/html";
  87. }
  88. OSHttpResponse response = new OSHttpResponse(request);
  89. // We're forgoing the usual error status codes here because the client
  90. // ignores anything but 200 and 301
  91. response.StatusCode = (int)OSHttpStatusCode.SuccessOk;
  92. if (responseCode == (int)OSHttpStatusCode.RedirectMovedPermanently)
  93. {
  94. response.RedirectLocation = (string)responseData["str_redirect_location"];
  95. response.StatusCode = responseCode;
  96. }
  97. response.AddHeader("Content-type", contentType);
  98. byte[] buffer;
  99. if (!contentType.Contains("image"))
  100. {
  101. buffer = Encoding.UTF8.GetBytes(responseString);
  102. }
  103. else
  104. {
  105. buffer = Convert.FromBase64String(responseString);
  106. }
  107. response.SendChunked = false;
  108. response.ContentLength64 = buffer.Length;
  109. response.ContentEncoding = Encoding.UTF8;
  110. try
  111. {
  112. response.Body.Write(buffer, 0, buffer.Length);
  113. }
  114. catch (Exception ex)
  115. {
  116. _log.ErrorFormat("[OSHttpHttpHandler]: Error: {0}", ex.Message);
  117. }
  118. finally
  119. {
  120. response.Send();
  121. }
  122. return OSHttpHandlerResult.Done;
  123. }
  124. }
  125. }