GetAssetsHandler.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.Collections.Specialized;
  31. using System.Reflection;
  32. using System.IO;
  33. using System.Web;
  34. using log4net;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Services.Interfaces;
  42. using Caps = OpenSim.Framework.Capabilities.Caps;
  43. namespace OpenSim.Capabilities.Handlers
  44. {
  45. public class GetAssetsHandler
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private static readonly Dictionary<string, AssetType> queryTypes = new Dictionary<string, AssetType>()
  50. {
  51. {"texture_id", AssetType.Texture},
  52. {"sound_id", AssetType.Sound},
  53. {"callcard_id", AssetType.CallingCard},
  54. {"landmark_id", AssetType.Landmark},
  55. {"script_id", AssetType.LSLText},
  56. {"clothing_id", AssetType.Clothing},
  57. {"object_id", AssetType.Object},
  58. {"notecard_id", AssetType.Notecard},
  59. {"lsltext_id", AssetType.LSLText},
  60. {"lslbyte_id", AssetType.LSLBytecode},
  61. {"txtr_tga_id", AssetType.TextureTGA},
  62. {"bodypart_id", AssetType.Bodypart},
  63. {"snd_wav_id", AssetType.SoundWAV},
  64. {"img_tga_id", AssetType.ImageTGA},
  65. {"jpeg_id", AssetType.ImageJPEG},
  66. {"animatn_id", AssetType.Animation},
  67. {"gesture_id", AssetType.Gesture},
  68. {"mesh_id", AssetType.Mesh},
  69. {"settings_id", AssetType.Settings}
  70. };
  71. private IAssetService m_assetService;
  72. public GetAssetsHandler(IAssetService assService)
  73. {
  74. m_assetService = assService;
  75. }
  76. public Hashtable Handle(Hashtable request)
  77. {
  78. Hashtable responsedata = new Hashtable();
  79. responsedata["content_type"] = "text/plain";
  80. responsedata["int_bytes"] = 0;
  81. if (m_assetService == null)
  82. {
  83. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.ServiceUnavailable;
  84. responsedata["str_response_string"] = "The asset service is unavailable";
  85. responsedata["keepalive"] = false;
  86. return responsedata;
  87. }
  88. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.BadRequest;
  89. string[] queries = null;
  90. if(request.Contains("querystringkeys"))
  91. queries = (string[])request["querystringkeys"];
  92. if(queries == null || queries.Length == 0)
  93. return responsedata;
  94. string query = queries[0];
  95. if(!queryTypes.ContainsKey(query))
  96. {
  97. m_log.Warn("[GETASSET]: Unknown type: " + query);
  98. return responsedata;
  99. }
  100. AssetType type = queryTypes[query];
  101. string assetStr = string.Empty;
  102. if (request.ContainsKey(query))
  103. assetStr = request[query].ToString();
  104. if (String.IsNullOrEmpty(assetStr))
  105. return responsedata;
  106. UUID assetID = UUID.Zero;
  107. if(!UUID.TryParse(assetStr, out assetID))
  108. return responsedata;
  109. AssetBase asset = m_assetService.Get(assetID.ToString());
  110. if(asset == null)
  111. {
  112. // m_log.Warn("[GETASSET]: not found: " + query + " " + assetStr);
  113. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.NotFound;
  114. responsedata["str_response_string"] = "Asset not found.";
  115. return responsedata;
  116. }
  117. if (asset.Type != (sbyte)type)
  118. {
  119. responsedata["str_response_string"] = "Got wrong asset type";
  120. return responsedata;
  121. }
  122. int len = asset.Data.Length;
  123. string range = String.Empty;
  124. if (((Hashtable)request["headers"])["range"] != null)
  125. range = (string)((Hashtable)request["headers"])["range"];
  126. else if (((Hashtable)request["headers"])["Range"] != null)
  127. range = (string)((Hashtable)request["headers"])["Range"];
  128. // range request
  129. int start, end;
  130. if (Util.TryParseHttpRange(range, out start, out end))
  131. {
  132. // Before clamping start make sure we can satisfy it in order to avoid
  133. // sending back the last byte instead of an error status
  134. if (start >= asset.Data.Length)
  135. {
  136. responsedata["str_response_string"] = "This range doesnt exist.";
  137. return responsedata;
  138. }
  139. if (end == -1)
  140. end = asset.Data.Length - 1;
  141. else
  142. end = Utils.Clamp(end, 0, asset.Data.Length - 1);
  143. start = Utils.Clamp(start, 0, end);
  144. len = end - start + 1;
  145. //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
  146. Hashtable headers = new Hashtable();
  147. headers["Content-Range"] = String.Format("bytes {0}-{1}/{2}", start, end, asset.Data.Length);
  148. responsedata["headers"] = headers;
  149. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.PartialContent;
  150. responsedata["bin_start"] = start;
  151. }
  152. else
  153. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.OK;
  154. responsedata["content_type"] = asset.Metadata.ContentType;
  155. responsedata["bin_response_data"] = asset.Data;
  156. responsedata["int_bytes"] = len;
  157. if (type == AssetType.Mesh || type == AssetType.Texture)
  158. {
  159. responsedata["throttle"] = true;
  160. responsedata["prio"] = len < 8196 ? 1 : 2;
  161. }
  162. return responsedata; // full asset
  163. }
  164. }
  165. }