GetMeshHandler.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.Specialized;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Web;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenMetaverse.StructuredData;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. using OpenSim.Services.Interfaces;
  41. using Caps = OpenSim.Framework.Capabilities.Caps;
  42. namespace OpenSim.Capabilities.Handlers
  43. {
  44. public class GetMeshHandler
  45. {
  46. private static readonly ILog m_log =
  47. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private IAssetService m_assetService;
  49. public const string DefaultFormat = "vnd.ll.mesh";
  50. public GetMeshHandler(IAssetService assService)
  51. {
  52. m_assetService = assService;
  53. }
  54. public Hashtable Handle(Hashtable request)
  55. {
  56. return ProcessGetMesh(request, UUID.Zero, null); ;
  57. }
  58. public Hashtable ProcessGetMesh(Hashtable request, UUID AgentId, Caps cap)
  59. {
  60. Hashtable responsedata = new Hashtable();
  61. if (m_assetService == null)
  62. {
  63. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.ServiceUnavailable;
  64. responsedata["str_response_string"] = "The asset service is unavailable";
  65. responsedata["keepalive"] = false;
  66. return responsedata;
  67. }
  68. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.BadRequest;
  69. responsedata["content_type"] = "text/plain";
  70. responsedata["int_bytes"] = 0;
  71. string meshStr = string.Empty;
  72. if (request.ContainsKey("mesh_id"))
  73. meshStr = request["mesh_id"].ToString();
  74. if (String.IsNullOrEmpty(meshStr))
  75. return responsedata;
  76. UUID meshID = UUID.Zero;
  77. if(!UUID.TryParse(meshStr, out meshID))
  78. return responsedata;
  79. AssetBase mesh = m_assetService.Get(meshID.ToString());
  80. if(mesh == null)
  81. {
  82. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.NotFound;
  83. responsedata["str_response_string"] = "Mesh not found.";
  84. return responsedata;
  85. }
  86. if (mesh.Type != (SByte)AssetType.Mesh)
  87. {
  88. responsedata["str_response_string"] = "Asset isn't a mesh.";
  89. return responsedata;
  90. }
  91. string range = String.Empty;
  92. if (((Hashtable)request["headers"])["range"] != null)
  93. range = (string)((Hashtable)request["headers"])["range"];
  94. else if (((Hashtable)request["headers"])["Range"] != null)
  95. range = (string)((Hashtable)request["headers"])["Range"];
  96. responsedata["content_type"] = "application/vnd.ll.mesh";
  97. if (String.IsNullOrEmpty(range))
  98. {
  99. // full mesh
  100. responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data);
  101. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.OK;
  102. return responsedata;
  103. }
  104. // range request
  105. int start, end;
  106. if (Util.TryParseHttpRange(range, out start, out end))
  107. {
  108. // Before clamping start make sure we can satisfy it in order to avoid
  109. // sending back the last byte instead of an error status
  110. if (start >= mesh.Data.Length)
  111. {
  112. responsedata["str_response_string"] = "This range doesnt exist.";
  113. return responsedata;
  114. }
  115. end = Utils.Clamp(end, 0, mesh.Data.Length - 1);
  116. start = Utils.Clamp(start, 0, end);
  117. int len = end - start + 1;
  118. //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
  119. Hashtable headers = new Hashtable();
  120. headers["Content-Range"] = String.Format("bytes {0}-{1}/{2}", start, end, mesh.Data.Length);
  121. responsedata["headers"] = headers;
  122. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.PartialContent;
  123. byte[] d = new byte[len];
  124. Array.Copy(mesh.Data, start, d, 0, len);
  125. responsedata["bin_response_data"] = d;
  126. responsedata["int_bytes"] = len;
  127. return responsedata;
  128. }
  129. m_log.Warn("[GETMESH]: Failed to parse a range from GetMesh request, sending full asset: " + (string)request["uri"]);
  130. responsedata["str_response_string"] = Convert.ToBase64String(mesh.Data);
  131. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.OK;
  132. return responsedata;
  133. }
  134. }
  135. }