GetAssetsHandler.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.Threading;
  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 GetAssetsHandler
  45. {
  46. private static readonly ILog m_log =
  47. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private static readonly Dictionary<string, AssetType> queryTypes = new()
  49. {
  50. {"texture_id", AssetType.Texture},
  51. {"sound_id", AssetType.Sound},
  52. {"callcard_id", AssetType.CallingCard},
  53. {"landmark_id", AssetType.Landmark},
  54. {"script_id", AssetType.LSLText},
  55. {"clothing_id", AssetType.Clothing},
  56. {"object_id", AssetType.Object},
  57. {"notecard_id", AssetType.Notecard},
  58. {"lsltext_id", AssetType.LSLText},
  59. {"lslbyte_id", AssetType.LSLBytecode},
  60. {"txtr_tga_id", AssetType.TextureTGA},
  61. {"bodypart_id", AssetType.Bodypart},
  62. {"snd_wav_id", AssetType.SoundWAV},
  63. {"img_tga_id", AssetType.ImageTGA},
  64. {"jpeg_id", AssetType.ImageJPEG},
  65. {"animatn_id", AssetType.Animation},
  66. {"gesture_id", AssetType.Gesture},
  67. {"mesh_id", AssetType.Mesh},
  68. {"settings_id", AssetType.Settings},
  69. {"material_id", AssetType.Material}
  70. };
  71. private IAssetService m_assetService;
  72. public GetAssetsHandler(IAssetService assService)
  73. {
  74. m_assetService = assService;
  75. }
  76. public void Handle(OSHttpRequest req, OSHttpResponse response, string serviceURL = null)
  77. {
  78. response.ContentType = "text/plain";
  79. if (m_assetService == null)
  80. {
  81. //m_log.Warn("[GETASSET]: no service");
  82. response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
  83. response.KeepAlive = false;
  84. return;
  85. }
  86. response.StatusCode = (int)HttpStatusCode.BadRequest;
  87. var queries = req.QueryAsDictionary;
  88. if(queries.Count == 0)
  89. return;
  90. AssetType type = AssetType.Unknown;
  91. string assetStr = string.Empty;
  92. foreach (KeyValuePair<string,string> kvp in queries)
  93. {
  94. if (queryTypes.TryGetValue(kvp.Key, out type))
  95. {
  96. assetStr = kvp.Value;
  97. break;
  98. }
  99. }
  100. if(type == AssetType.Unknown)
  101. {
  102. //m_log.Warn("[GETASSET]: Unknown type: " + query);
  103. m_log.Warn("[GETASSET]: Unknown type");
  104. response.StatusCode = (int)HttpStatusCode.NotFound;
  105. return;
  106. }
  107. if (string.IsNullOrEmpty(assetStr))
  108. return;
  109. if(!UUID.TryParse(assetStr, out UUID assetID))
  110. return;
  111. ManualResetEventSlim done = new ManualResetEventSlim(false);
  112. AssetBase asset = null;
  113. m_assetService.Get(assetID.ToString(), serviceURL, false, (AssetBase a) =>
  114. {
  115. asset = a;
  116. done.Set();
  117. });
  118. done.Wait();
  119. done.Dispose();
  120. done = null;
  121. if (asset == null)
  122. {
  123. // m_log.Warn("[GETASSET]: not found: " + query + " " + assetStr);
  124. response.StatusCode = (int)HttpStatusCode.NotFound;
  125. return;
  126. }
  127. int len = asset.Data.Length;
  128. if (len == 0)
  129. {
  130. m_log.Warn("[GETASSET]: asset with empty data: " + assetStr + " type " + asset.Type.ToString());
  131. response.StatusCode = (int)HttpStatusCode.NotFound;
  132. return;
  133. }
  134. if (asset.Type != (sbyte)type)
  135. {
  136. m_log.Warn("[GETASSET]: asset with wrong type: " + assetStr + " " + asset.Type.ToString() + " != " + ((sbyte)type).ToString());
  137. //response.StatusCode = (int)HttpStatusCode.NotFound;
  138. //return;
  139. }
  140. // range request
  141. if (Util.TryParseHttpRange(req.Headers["range"], out int start, out int end))
  142. {
  143. // viewers do send broken start, then flag good assets as bad
  144. if (start >= len)
  145. {
  146. //m_log.Warn("[GETASSET]: bad start: " + range);
  147. response.StatusCode = (int)HttpStatusCode.OK;
  148. }
  149. else
  150. {
  151. if (end == -1)
  152. end = len - 1;
  153. else
  154. end = Utils.Clamp(end, 0, len - 1);
  155. start = Utils.Clamp(start, 0, end);
  156. len = end - start + 1;
  157. //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
  158. response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", start, end, asset.Data.Length));
  159. response.StatusCode = (int)HttpStatusCode.PartialContent;
  160. response.RawBufferStart = start;
  161. }
  162. }
  163. else
  164. response.StatusCode = (int)HttpStatusCode.OK;
  165. response.ContentType = asset.Metadata.ContentType;
  166. response.RawBuffer = asset.Data;
  167. response.RawBufferLen = len;
  168. if (type == AssetType.Mesh || type == AssetType.Texture)
  169. response.Priority = 2;
  170. else
  171. response.Priority = 1;
  172. }
  173. }
  174. }