SimpleBinaryHandler.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Net;
  28. using System.IO;
  29. using OpenSim.Framework.ServiceAuth;
  30. using OpenMetaverse.StructuredData;
  31. namespace OpenSim.Framework.Servers.HttpServer
  32. {
  33. /// <summary>
  34. /// simple OSD streamed request handler.
  35. /// for well defined simple uri paths, single http method and a OSDMap encoded body
  36. /// </summary>
  37. /// <remarks>
  38. /// Inheriting classes should override ProcessRequest() rather than Handle()
  39. /// </remarks>
  40. public class SimpleBinaryHandler : SimpleBaseRequestHandler, ISimpleStreamHandler
  41. {
  42. protected string m_httMethod;
  43. protected IServiceAuth m_Auth;
  44. protected SimpleBinaryMethod m_processRequest;
  45. protected int m_maxDatasize = -1;
  46. public SimpleBinaryHandler(string httpmethod, string path) : base(path)
  47. {
  48. m_httMethod = httpmethod.ToUpper();
  49. }
  50. public SimpleBinaryHandler(string httpmethod, string path, string name) : base(path, name)
  51. {
  52. m_httMethod = httpmethod.ToUpper();
  53. }
  54. public SimpleBinaryHandler(string httpmethod, string path, SimpleBinaryMethod processRequest) : base(path)
  55. {
  56. m_httMethod = httpmethod.ToUpper();
  57. m_processRequest = processRequest;
  58. }
  59. public SimpleBinaryHandler(string httpmethod, string path, SimpleBinaryMethod processRequest, string name) : base(path, name)
  60. {
  61. m_httMethod = httpmethod.ToUpper();
  62. m_processRequest = processRequest;
  63. }
  64. public SimpleBinaryHandler(string httpmethod, string path, IServiceAuth auth) : base(path)
  65. {
  66. m_httMethod = httpmethod.ToUpper();
  67. m_Auth = auth;
  68. }
  69. public SimpleBinaryHandler(string httpmethod, string path, IServiceAuth auth, SimpleBinaryMethod processRequest)
  70. : base(path)
  71. {
  72. m_httMethod = httpmethod.ToUpper();
  73. m_Auth = auth;
  74. m_processRequest = processRequest;
  75. }
  76. public SimpleBinaryHandler(string httpmethod, string path, IServiceAuth auth, SimpleBinaryMethod processRequest, string name)
  77. : base(path, name)
  78. {
  79. m_httMethod = httpmethod.ToUpper();
  80. m_Auth = auth;
  81. m_processRequest = processRequest;
  82. }
  83. public virtual void Handle(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  84. {
  85. RequestsReceived++;
  86. if(httpRequest.HttpMethod != m_httMethod)
  87. {
  88. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  89. return;
  90. }
  91. if (httpRequest.InputStream == null || httpRequest.InputStream.Length == 0)
  92. {
  93. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  94. return;
  95. }
  96. if (m_maxDatasize > 0 && httpRequest.InputStream.Length > m_maxDatasize)
  97. {
  98. httpResponse.StatusCode = (int)HttpStatusCode.RequestEntityTooLarge;
  99. return;
  100. }
  101. byte[] data;
  102. try
  103. {
  104. Stream request = httpRequest.InputStream;
  105. if (request is MemoryStream)
  106. data = ((MemoryStream)request).ToArray();
  107. else
  108. {
  109. request.Seek(0, SeekOrigin.Begin);
  110. using (MemoryStream ms = new MemoryStream((int)request.Length))
  111. {
  112. request.CopyTo(ms);
  113. data = ms.ToArray();
  114. }
  115. }
  116. request.Dispose();
  117. }
  118. catch
  119. {
  120. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  121. return;
  122. }
  123. if (m_Auth != null)
  124. {
  125. if (!m_Auth.Authenticate(httpRequest.Headers, httpResponse.AddHeader, out HttpStatusCode statusCode))
  126. {
  127. httpResponse.StatusCode = (int)statusCode;
  128. return;
  129. }
  130. }
  131. try
  132. {
  133. if(m_processRequest != null)
  134. m_processRequest(httpRequest, httpResponse, data);
  135. else
  136. ProcessRequest(httpRequest, httpResponse, data);
  137. }
  138. catch
  139. {
  140. httpResponse.StatusCode = (int)HttpStatusCode.InternalServerError;
  141. }
  142. RequestsHandled++;
  143. }
  144. public int MaxDataSize { get { return m_maxDatasize; } set { m_maxDatasize = value; }}
  145. protected virtual void ProcessRequest(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, byte[] data)
  146. {
  147. }
  148. }
  149. }