PollServiceHttpRequest.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 == null)
  69. {
  70. SendNoContentError(response);
  71. return;
  72. }
  73. int responsecode = 200;
  74. string responseString = String.Empty;
  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. if (responseString == null)
  97. responseString = String.Empty;
  98. }
  99. catch
  100. {
  101. SendNoContentError(response);
  102. return;
  103. }
  104. response.StatusCode = responsecode;
  105. if (responsecode == (int)HttpStatusCode.Moved)
  106. {
  107. response.AddHeader("Location", (string)responsedata["str_redirect_location"]);
  108. response.KeepAlive = false;
  109. PollServiceArgs.RequestsHandled++;
  110. response.Send();
  111. return;
  112. }
  113. if (responsedata.ContainsKey("http_protocol_version"))
  114. response.ProtocolVersion = (string)responsedata["http_protocol_version"];
  115. if (responsedata.ContainsKey("keepalive"))
  116. response.KeepAlive = (bool)responsedata["keepalive"];
  117. if (responsedata.ContainsKey("keepaliveTimeout"))
  118. response.KeepAliveTimeout = (int)responsedata["keepaliveTimeout"];
  119. if (responsedata.ContainsKey("prio"))
  120. response.Priority = (int)responsedata["prio"];
  121. if (responsedata.ContainsKey("error_status_text"))
  122. response.StatusDescription = (string)responsedata["error_status_text"];
  123. // Cross-Origin Resource Sharing with simple requests
  124. if (responsedata.ContainsKey("access_control_allow_origin"))
  125. response.AddHeader("Access-Control-Allow-Origin", (string)responsedata["access_control_allow_origin"]);
  126. if (string.IsNullOrEmpty(contentType))
  127. response.AddHeader("Content-Type", "text/html");
  128. else
  129. response.AddHeader("Content-Type", contentType);
  130. if (responsedata.ContainsKey("headers"))
  131. {
  132. Hashtable headerdata = (Hashtable)responsedata["headers"];
  133. foreach (string header in headerdata.Keys)
  134. response.AddHeader(header, headerdata[header].ToString());
  135. }
  136. if(buffer == null)
  137. {
  138. if (contentType != null && (!(contentType.Contains("image")
  139. || contentType.Contains("x-shockwave-flash")
  140. || contentType.Contains("application/x-oar")
  141. || contentType.Contains("application/vnd.ll.mesh"))))
  142. {
  143. // Text
  144. buffer = Encoding.UTF8.GetBytes(responseString);
  145. }
  146. else
  147. {
  148. // Binary!
  149. buffer = Convert.FromBase64String(responseString);
  150. }
  151. response.ContentEncoding = Encoding.UTF8;
  152. }
  153. if (rangeStart < 0 || rangeStart > buffer.Length)
  154. rangeStart = 0;
  155. if (rangeLen < 0)
  156. rangeLen = buffer.Length;
  157. else if (rangeLen + rangeStart > buffer.Length)
  158. rangeLen = buffer.Length - rangeStart;
  159. response.ContentLength64 = rangeLen;
  160. try
  161. {
  162. if(rangeLen > 0)
  163. {
  164. response.RawBufferStart = rangeStart;
  165. response.RawBufferLen = rangeLen;
  166. response.RawBuffer = buffer;
  167. //response.OutputStream.Write(buffer, rangeStart, rangeLen);
  168. }
  169. buffer = null;
  170. response.Send();
  171. }
  172. catch (Exception ex)
  173. {
  174. if(ex is System.Net.Sockets.SocketException)
  175. {
  176. // only mute connection reset by peer so we are not totally blind for now
  177. if(((System.Net.Sockets.SocketException)ex).SocketErrorCode != System.Net.Sockets.SocketError.ConnectionReset)
  178. m_log.Warn("[POLL SERVICE WORKER THREAD]: Error ", ex);
  179. }
  180. else
  181. m_log.Warn("[POLL SERVICE WORKER THREAD]: Error ", ex);
  182. }
  183. PollServiceArgs.RequestsHandled++;
  184. }
  185. internal void SendNoContentError(OSHttpResponse response)
  186. {
  187. response.ContentLength64 = 0;
  188. response.ContentEncoding = Encoding.UTF8;
  189. response.StatusCode = 500;
  190. try
  191. {
  192. response.Send();
  193. }
  194. catch { }
  195. return;
  196. }
  197. internal void DoHTTPstop()
  198. {
  199. OSHttpResponse response = new OSHttpResponse(new HttpResponse(Request));
  200. if(Request.Body.CanRead)
  201. Request.Body.Dispose();
  202. response.ContentLength64 = 0;
  203. response.ContentEncoding = Encoding.UTF8;
  204. response.KeepAlive = false;
  205. response.StatusCode = 503;
  206. try
  207. {
  208. response.Send();
  209. }
  210. catch
  211. {
  212. }
  213. }
  214. }
  215. }