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. //m_log.Warn("[GETASSET]: no service");
  80. response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
  81. response.KeepAlive = false;
  82. return;
  83. }
  84. response.StatusCode = (int)HttpStatusCode.BadRequest;
  85. var queries = req.QueryAsDictionary;
  86. if(queries.Count == 0)
  87. return;
  88. AssetType type = AssetType.Unknown;
  89. string assetStr = string.Empty;
  90. foreach (KeyValuePair<string,string> kvp in queries)
  91. {
  92. if (queryTypes.ContainsKey(kvp.Key))
  93. {
  94. type = queryTypes[kvp.Key];
  95. assetStr = kvp.Value;
  96. break;
  97. }
  98. }
  99. if(type == AssetType.Unknown)
  100. {
  101. //m_log.Warn("[GETASSET]: Unknown type: " + query);
  102. m_log.Warn("[GETASSET]: Unknown type");
  103. response.StatusCode = (int)HttpStatusCode.NotFound;
  104. return;
  105. }
  106. if (string.IsNullOrEmpty(assetStr))
  107. return;
  108. UUID assetID = UUID.Zero;
  109. if(!UUID.TryParse(assetStr, out assetID))
  110. return;
  111. AssetBase asset = m_assetService.Get(assetID.ToString(), serviceURL, false);
  112. if (asset == null)
  113. {
  114. // m_log.Warn("[GETASSET]: not found: " + query + " " + assetStr);
  115. response.StatusCode = (int)HttpStatusCode.NotFound;
  116. return;
  117. }
  118. if (asset.Type != (sbyte)type)
  119. {
  120. m_log.Warn("[GETASSET]: asset with wrong type: " + assetStr + " " + asset.Type.ToString() + " != " + ((sbyte)type).ToString());
  121. response.StatusCode = (int)HttpStatusCode.NotFound;
  122. return;
  123. }
  124. if (asset.Data.Length == 0)
  125. {
  126. m_log.Warn("[GETASSET]: asset with empty data: " + assetStr +" type " + asset.Type.ToString());
  127. response.StatusCode = (int)HttpStatusCode.NotFound;
  128. return;
  129. }
  130. int len = asset.Data.Length;
  131. string range = null;
  132. if (req.Headers["Range"] != null)
  133. range = req.Headers["Range"];
  134. else if (req.Headers["range"] != null)
  135. range = req.Headers["range"];
  136. // range request
  137. int start, end;
  138. if (Util.TryParseHttpRange(range, out start, out end))
  139. {
  140. // viewers do send broken start, then flag good assets as bad
  141. if (start >= asset.Data.Length)
  142. {
  143. //m_log.Warn("[GETASSET]: bad start: " + range);
  144. response.StatusCode = (int)HttpStatusCode.OK;
  145. }
  146. else
  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. }
  160. else
  161. response.StatusCode = (int)HttpStatusCode.OK;
  162. response.ContentType = asset.Metadata.ContentType;
  163. response.RawBuffer = asset.Data;
  164. response.RawBufferLen = len;
  165. if (type == AssetType.Mesh || type == AssetType.Texture)
  166. {
  167. if(len > 8196)
  168. {
  169. //if(type == AssetType.Texture && ((asset.Flags & AssetFlags.AvatarBake)!= 0))
  170. // responsedata["prio"] = 1;
  171. //else
  172. response.Priority = 2;
  173. }
  174. else
  175. response.Priority = 1;
  176. }
  177. else
  178. response.Priority = -1;
  179. }
  180. }
  181. }