PollServiceHttpRequest.cs 9.6 KB

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