GetAssetsHandler.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. };
  70. private IAssetService m_assetService;
  71. public GetAssetsHandler(IAssetService assService)
  72. {
  73. m_assetService = assService;
  74. }
  75. public Hashtable Handle(Hashtable request)
  76. {
  77. Hashtable responsedata = new Hashtable();
  78. responsedata["content_type"] = "text/plain";
  79. responsedata["int_bytes"] = 0;
  80. if (m_assetService == null)
  81. {
  82. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.ServiceUnavailable;
  83. responsedata["str_response_string"] = "The asset service is unavailable";
  84. responsedata["keepalive"] = false;
  85. return responsedata;
  86. }
  87. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.BadRequest;
  88. string[] queries = null;
  89. if(request.Contains("querystringkeys"))
  90. queries = (string[])request["querystringkeys"];
  91. if(queries == null || queries.Length == 0)
  92. return responsedata;
  93. string query = queries[0];
  94. if(!queryTypes.ContainsKey(query))
  95. {
  96. m_log.Warn("[GETASSET]: Unknown type: " + query);
  97. return responsedata;
  98. }
  99. AssetType type = queryTypes[query];
  100. string assetStr = string.Empty;
  101. if (request.ContainsKey(query))
  102. assetStr = request[query].ToString();
  103. if (String.IsNullOrEmpty(assetStr))
  104. return responsedata;
  105. UUID assetID = UUID.Zero;
  106. if(!UUID.TryParse(assetStr, out assetID))
  107. return responsedata;
  108. AssetBase asset = m_assetService.Get(assetID.ToString());
  109. if(asset == null)
  110. {
  111. // m_log.Warn("[GETASSET]: not found: " + query + " " + assetStr);
  112. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.NotFound;
  113. responsedata["str_response_string"] = "Asset not found.";
  114. return responsedata;
  115. }
  116. if (asset.Type != (sbyte)type)
  117. {
  118. responsedata["str_response_string"] = "Got wrong asset type";
  119. return responsedata;
  120. }
  121. if(type == AssetType.Mesh || type == AssetType.Texture)
  122. responsedata["throttle"] = true;
  123. // else
  124. // m_log.Warn("[GETASSETS]: type: " + query);
  125. responsedata["content_type"] = asset.Metadata.ContentType;
  126. responsedata["bin_response_data"] = asset.Data;
  127. responsedata["int_bytes"] = asset.Data.Length;
  128. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.OK;
  129. string range = String.Empty;
  130. if (((Hashtable)request["headers"])["range"] != null)
  131. range = (string)((Hashtable)request["headers"])["range"];
  132. else if (((Hashtable)request["headers"])["Range"] != null)
  133. range = (string)((Hashtable)request["headers"])["Range"];
  134. else
  135. return responsedata; // full asset
  136. if (String.IsNullOrEmpty(range))
  137. return responsedata; // full asset
  138. // range request
  139. int start, end;
  140. if (Util.TryParseHttpRange(range, out start, out end))
  141. {
  142. // Before clamping start make sure we can satisfy it in order to avoid
  143. // sending back the last byte instead of an error status
  144. if (start >= asset.Data.Length)
  145. {
  146. responsedata["str_response_string"] = "This range doesnt exist.";
  147. return responsedata;
  148. }
  149. if (end == -1)
  150. end = asset.Data.Length - 1;
  151. else
  152. end = Utils.Clamp(end, 0, asset.Data.Length - 1);
  153. start = Utils.Clamp(start, 0, end);
  154. int len = end - start + 1;
  155. //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
  156. Hashtable headers = new Hashtable();
  157. headers["Content-Range"] = String.Format("bytes {0}-{1}/{2}", start, end, asset.Data.Length);
  158. responsedata["headers"] = headers;
  159. responsedata["int_response_code"] = (int)System.Net.HttpStatusCode.PartialContent;
  160. responsedata["bin_start"] = start;
  161. responsedata["int_bytes"] = len;
  162. return responsedata;
  163. }
  164. m_log.Warn("[GETASSETS]: Failed to parse a range, sending full asset: " + assetStr);
  165. return responsedata;
  166. }
  167. }
  168. }