GetAssetsHandler.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Services.Interfaces;
  40. using Caps = OpenSim.Framework.Capabilities.Caps;
  41. namespace OpenSim.Capabilities.Handlers
  42. {
  43. public class GetAssetsHandler
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private static readonly Dictionary<string, AssetType> queryTypes = new Dictionary<string, AssetType>()
  48. {
  49. {"texture_id", AssetType.Texture},
  50. {"sound_id", AssetType.Sound},
  51. {"callcard_id", AssetType.CallingCard},
  52. {"landmark_id", AssetType.Landmark},
  53. {"script_id", AssetType.LSLText},
  54. {"clothing_id", AssetType.Clothing},
  55. {"object_id", AssetType.Object},
  56. {"notecard_id", AssetType.Notecard},
  57. {"lsltext_id", AssetType.LSLText},
  58. {"lslbyte_id", AssetType.LSLBytecode},
  59. {"txtr_tga_id", AssetType.TextureTGA},
  60. {"bodypart_id", AssetType.Bodypart},
  61. {"snd_wav_id", AssetType.SoundWAV},
  62. {"img_tga_id", AssetType.ImageTGA},
  63. {"jpeg_id", AssetType.ImageJPEG},
  64. {"animatn_id", AssetType.Animation},
  65. {"gesture_id", AssetType.Gesture},
  66. {"mesh_id", AssetType.Mesh},
  67. {"settings_id", AssetType.Settings}
  68. };
  69. private IAssetService m_assetService;
  70. public GetAssetsHandler(IAssetService assService)
  71. {
  72. m_assetService = assService;
  73. }
  74. public void Handle(OSHttpRequest req, OSHttpResponse response, string serviceURL = null)
  75. {
  76. response.ContentType = "text/plain";
  77. if (m_assetService == null)
  78. {
  79. response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
  80. response.KeepAlive = false;
  81. return;
  82. }
  83. response.StatusCode = (int)HttpStatusCode.BadRequest;
  84. var queries = req.QueryAsDictionary;
  85. if(queries.Count == 0)
  86. return;
  87. AssetType type = AssetType.Unknown;
  88. string assetStr = string.Empty;
  89. foreach (KeyValuePair<string,string> kvp in queries)
  90. {
  91. if (queryTypes.ContainsKey(kvp.Key))
  92. {
  93. type = queryTypes[kvp.Key];
  94. assetStr = kvp.Value;
  95. break;
  96. }
  97. }
  98. if(type == AssetType.Unknown)
  99. {
  100. //m_log.Warn("[GETASSET]: Unknown type: " + query);
  101. m_log.Warn("[GETASSET]: Unknown type");
  102. response.StatusCode = (int)HttpStatusCode.NotFound;
  103. return;
  104. }
  105. if (String.IsNullOrEmpty(assetStr))
  106. return;
  107. UUID assetID = UUID.Zero;
  108. if(!UUID.TryParse(assetStr, out assetID))
  109. return;
  110. AssetBase asset = m_assetService.Get(assetID.ToString());
  111. if (asset == null)
  112. {
  113. if (String.IsNullOrWhiteSpace(serviceURL))
  114. {
  115. // m_log.Warn("[GETASSET]: not found: " + query + " " + assetStr);
  116. response.StatusCode = (int)HttpStatusCode.NotFound;
  117. return;
  118. }
  119. string newid = serviceURL + "/" + assetID.ToString();
  120. asset = m_assetService.Get(newid);
  121. if (asset == null)
  122. {
  123. // m_log.Warn("[GETASSET]: not found: " + query + " " + assetStr);
  124. response.StatusCode = (int)HttpStatusCode.NotFound;
  125. return;
  126. }
  127. // m_log.Warn("[GETASSET]: not found: " + query + " " + assetStr);
  128. }
  129. if (asset.Type != (sbyte)type)
  130. return;
  131. int len = asset.Data.Length;
  132. string range = null;
  133. if (req.Headers["Range"] != null)
  134. range = req.Headers["Range"];
  135. else if (req.Headers["range"] != null)
  136. range = req.Headers["range"];
  137. // range request
  138. int start, end;
  139. if (Util.TryParseHttpRange(range, out start, out end))
  140. {
  141. // Before clamping start make sure we can satisfy it in order to avoid
  142. // sending back the last byte instead of an error status
  143. if (start >= asset.Data.Length)
  144. {
  145. response.StatusCode = (int)HttpStatusCode.RequestedRangeNotSatisfiable;
  146. return;
  147. }
  148. if (end == -1)
  149. end = asset.Data.Length - 1;
  150. else
  151. end = Utils.Clamp(end, 0, asset.Data.Length - 1);
  152. start = Utils.Clamp(start, 0, end);
  153. len = end - start + 1;
  154. //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
  155. response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", start, end, asset.Data.Length));
  156. response.StatusCode = (int)HttpStatusCode.PartialContent;
  157. response.RawBufferStart = start;
  158. }
  159. else
  160. response.StatusCode = (int)HttpStatusCode.OK;
  161. response.ContentType = asset.Metadata.ContentType;
  162. response.RawBuffer = asset.Data;
  163. response.RawBufferLen = len;
  164. if (type == AssetType.Mesh || type == AssetType.Texture)
  165. {
  166. if(len > 8196)
  167. {
  168. //if(type == AssetType.Texture && ((asset.Flags & AssetFlags.AvatarBake)!= 0))
  169. // responsedata["prio"] = 1;
  170. //else
  171. response.Priority = 2;
  172. }
  173. else
  174. response.Priority = 1;
  175. }
  176. else
  177. response.Priority = -1;
  178. }
  179. }
  180. }