PollServiceHttpRequest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 HttpServer;
  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. private void GenContextHash()
  47. {
  48. Random rnd = new Random();
  49. contextHash = 0;
  50. if (Request.Headers["remote_addr"] != null)
  51. contextHash = (Request.Headers["remote_addr"]).GetHashCode() << 16;
  52. else
  53. contextHash = rnd.Next() << 16;
  54. if (Request.Headers["remote_port"] != null)
  55. {
  56. string[] strPorts = Request.Headers["remote_port"].Split(new char[] { ',' });
  57. contextHash += Int32.Parse(strPorts[0]);
  58. }
  59. else
  60. contextHash += rnd.Next() & 0xffff;
  61. }
  62. public PollServiceHttpRequest(
  63. PollServiceEventArgs pPollServiceArgs, IHttpClientContext pHttpContext, IHttpRequest pRequest)
  64. {
  65. PollServiceArgs = pPollServiceArgs;
  66. HttpContext = pHttpContext;
  67. Request = pRequest;
  68. RequestTime = System.Environment.TickCount;
  69. RequestID = UUID.Random();
  70. GenContextHash();
  71. }
  72. internal void DoHTTPGruntWork(BaseHttpServer server, Hashtable responsedata)
  73. {
  74. OSHttpResponse response
  75. = new OSHttpResponse(new HttpResponse(HttpContext, Request), HttpContext);
  76. byte[] buffer = server.DoHTTPGruntWork(responsedata, response);
  77. response.SendChunked = false;
  78. response.ContentLength64 = buffer.Length;
  79. response.ContentEncoding = Encoding.UTF8;
  80. response.ReuseContext = false;
  81. try
  82. {
  83. response.OutputStream.Write(buffer, 0, buffer.Length);
  84. }
  85. catch (Exception ex)
  86. {
  87. m_log.Warn("[POLL SERVICE WORKER THREAD]: Error ", ex);
  88. }
  89. finally
  90. {
  91. //response.OutputStream.Close();
  92. try
  93. {
  94. response.OutputStream.Flush();
  95. response.Send();
  96. //if (!response.KeepAlive && response.ReuseContext)
  97. // response.FreeContext();
  98. }
  99. catch (Exception e)
  100. {
  101. m_log.Warn("[POLL SERVICE WORKER THREAD]: Error ", e);
  102. }
  103. PollServiceArgs.RequestsHandled++;
  104. }
  105. }
  106. internal void DoHTTPstop(BaseHttpServer server)
  107. {
  108. OSHttpResponse response
  109. = new OSHttpResponse(new HttpResponse(HttpContext, Request), HttpContext);
  110. response.SendChunked = false;
  111. response.ContentLength64 = 0;
  112. response.ContentEncoding = Encoding.UTF8;
  113. response.ReuseContext = false;
  114. response.KeepAlive = false;
  115. response.SendChunked = false;
  116. response.StatusCode = 503;
  117. try
  118. {
  119. response.OutputStream.Flush();
  120. response.Send();
  121. }
  122. catch (Exception e)
  123. {
  124. }
  125. }
  126. }
  127. class PollServiceHttpRequestComparer : IEqualityComparer<PollServiceHttpRequest>
  128. {
  129. public bool Equals(PollServiceHttpRequest b1, PollServiceHttpRequest b2)
  130. {
  131. if (b1.contextHash != b2.contextHash)
  132. return false;
  133. bool b = Object.ReferenceEquals(b1.HttpContext, b2.HttpContext);
  134. return b;
  135. }
  136. public int GetHashCode(PollServiceHttpRequest b2)
  137. {
  138. return (int)b2.contextHash;
  139. }
  140. }
  141. }