PollServiceHttpRequest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.Net;
  31. using System.Reflection;
  32. using System.Text;
  33. using OSHttpServer;
  34. using log4net;
  35. using OpenMetaverse;
  36. namespace OpenSim.Framework.Servers.HttpServer
  37. {
  38. public class PollServiceHttpRequest
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. public readonly PollServiceEventArgs PollServiceArgs;
  42. public readonly IHttpRequest Request;
  43. public readonly int RequestTime;
  44. public readonly UUID RequestID;
  45. public PollServiceHttpRequest(PollServiceEventArgs pPollServiceArgs, IHttpRequest pRequest)
  46. {
  47. PollServiceArgs = pPollServiceArgs;
  48. Request = pRequest;
  49. RequestTime = System.Environment.TickCount;
  50. RequestID = UUID.Random();
  51. }
  52. internal void DoHTTPGruntWork(Hashtable responsedata)
  53. {
  54. if (Request.Body.CanRead)
  55. Request.Body.Dispose();
  56. if(responsedata.Contains("h"))
  57. {
  58. OSHttpResponse r = (OSHttpResponse)responsedata["h"];
  59. try
  60. {
  61. r.Send();
  62. }
  63. catch { }
  64. PollServiceArgs.RequestsHandled++;
  65. return;
  66. }
  67. OSHttpResponse response = new OSHttpResponse(new HttpResponse(Request));
  68. if (responsedata is null)
  69. {
  70. SendNoContentError(response);
  71. return;
  72. }
  73. int responsecode = 200;
  74. string responseString = null;
  75. string contentType;
  76. byte[] buffer = null;
  77. int rangeStart = 0;
  78. int rangeLen = -1;
  79. try
  80. {
  81. //m_log.Info("[BASE HTTP SERVER]: Doing HTTP Grunt work with response");
  82. if(responsedata["int_response_code"] != null)
  83. responsecode = (int)responsedata["int_response_code"];
  84. if (responsedata["bin_response_data"] != null)
  85. {
  86. buffer = (byte[])responsedata["bin_response_data"];
  87. responsedata["bin_response_data"] = null;
  88. if (responsedata["bin_start"] != null)
  89. rangeStart = (int)responsedata["bin_start"];
  90. if (responsedata["int_bytes"] != null)
  91. rangeLen = (int)responsedata["int_bytes"];
  92. }
  93. else
  94. responseString = (string)responsedata["str_response_string"];
  95. contentType = (string)responsedata["content_type"];
  96. }
  97. catch
  98. {
  99. SendNoContentError(response);
  100. return;
  101. }
  102. if (responsecode == (int)HttpStatusCode.Moved)
  103. {
  104. response.Redirect((string)responsedata["str_redirect_location"], HttpStatusCode.Moved);
  105. response.KeepAlive = false;
  106. PollServiceArgs.RequestsHandled++;
  107. response.Send();
  108. return;
  109. }
  110. response.StatusCode = responsecode;
  111. if (responsedata.ContainsKey("http_protocol_version"))
  112. response.ProtocolVersion = (string)responsedata["http_protocol_version"];
  113. if (responsedata.ContainsKey("keepalive"))
  114. response.KeepAlive = (bool)responsedata["keepalive"];
  115. if (responsedata.ContainsKey("keepaliveTimeout"))
  116. response.KeepAliveTimeout = (int)responsedata["keepaliveTimeout"];
  117. if (responsedata.ContainsKey("error_status_text"))
  118. response.StatusDescription = (string)responsedata["error_status_text"];
  119. // Cross-Origin Resource Sharing with simple requests
  120. if (responsedata.ContainsKey("access_control_allow_origin"))
  121. response.AddHeader("Access-Control-Allow-Origin", (string)responsedata["access_control_allow_origin"]);
  122. if (string.IsNullOrEmpty(contentType))
  123. response.AddHeader("Content-Type", "text/html");
  124. else
  125. response.AddHeader("Content-Type", contentType);
  126. if (responsedata.ContainsKey("headers"))
  127. {
  128. Hashtable headerdata = (Hashtable)responsedata["headers"];
  129. foreach (string header in headerdata.Keys)
  130. response.AddHeader(header, headerdata[header].ToString());
  131. }
  132. if(buffer == null)
  133. {
  134. if(string.IsNullOrEmpty(responseString))
  135. buffer = Array.Empty<byte>();
  136. else if (contentType != null && (!(contentType.Contains("image")
  137. || contentType.Contains("x-shockwave-flash")
  138. || contentType.Contains("application/x-oar")
  139. || contentType.Contains("application/vnd.ll.mesh"))))
  140. {
  141. // Text
  142. buffer = Encoding.UTF8.GetBytes(responseString);
  143. }
  144. else
  145. {
  146. // Binary!
  147. buffer = Convert.FromBase64String(responseString);
  148. }
  149. response.ContentEncoding = Encoding.UTF8;
  150. }
  151. if (rangeStart < 0 || rangeStart > buffer.Length)
  152. rangeStart = 0;
  153. if (rangeLen < 0)
  154. rangeLen = buffer.Length;
  155. else if (rangeLen + rangeStart > buffer.Length)
  156. rangeLen = buffer.Length - rangeStart;
  157. response.ContentLength64 = rangeLen;
  158. try
  159. {
  160. if(rangeLen > 0)
  161. {
  162. response.RawBufferStart = rangeStart;
  163. response.RawBufferLen = rangeLen;
  164. response.RawBuffer = buffer;
  165. //response.OutputStream.Write(buffer, rangeStart, rangeLen);
  166. }
  167. buffer = null;
  168. response.Send();
  169. }
  170. catch (Exception ex)
  171. {
  172. if(ex is System.Net.Sockets.SocketException)
  173. {
  174. // only mute connection reset by peer so we are not totally blind for now
  175. if(((System.Net.Sockets.SocketException)ex).SocketErrorCode != System.Net.Sockets.SocketError.ConnectionReset)
  176. m_log.Warn("[POLL SERVICE WORKER THREAD]: Error ", ex);
  177. }
  178. else
  179. m_log.Warn("[POLL SERVICE WORKER THREAD]: Error ", ex);
  180. }
  181. PollServiceArgs.RequestsHandled++;
  182. }
  183. internal void SendNoContentError(OSHttpResponse response)
  184. {
  185. response.ContentLength64 = 0;
  186. response.ContentEncoding = Encoding.UTF8;
  187. response.StatusCode = 500;
  188. try
  189. {
  190. response.Send();
  191. }
  192. catch { }
  193. return;
  194. }
  195. internal void DoHTTPstop()
  196. {
  197. OSHttpResponse response = new OSHttpResponse(new HttpResponse(Request));
  198. if(Request.Body.CanRead)
  199. Request.Body.Dispose();
  200. response.ContentLength64 = 0;
  201. response.ContentEncoding = Encoding.UTF8;
  202. response.KeepAlive = false;
  203. response.StatusCode = 503;
  204. try
  205. {
  206. response.Send();
  207. }
  208. catch
  209. {
  210. }
  211. }
  212. }
  213. }