GetTextureHandler.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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.Specialized;
  30. using System.Drawing;
  31. using System.Drawing.Imaging;
  32. using System.Reflection;
  33. using System.IO;
  34. using System.Web;
  35. using log4net;
  36. using Nini.Config;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. using OpenMetaverse.Imaging;
  40. using OpenSim.Framework;
  41. using OpenSim.Framework.Servers;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenSim.Region.Framework.Interfaces;
  44. using OpenSim.Services.Interfaces;
  45. using Caps = OpenSim.Framework.Capabilities.Caps;
  46. namespace OpenSim.Capabilities.Handlers
  47. {
  48. public class GetTextureHandler : BaseStreamHandler
  49. {
  50. private static readonly ILog m_log =
  51. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private IAssetService m_assetService;
  53. public const string DefaultFormat = "x-j2c";
  54. // TODO: Change this to a config option
  55. const string REDIRECT_URL = null;
  56. public GetTextureHandler(string path, IAssetService assService, string name, string description)
  57. : base("GET", path, name, description)
  58. {
  59. m_assetService = assService;
  60. }
  61. protected override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  62. {
  63. // Try to parse the texture ID from the request URL
  64. NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
  65. string textureStr = query.GetOne("texture_id");
  66. string format = query.GetOne("format");
  67. //m_log.DebugFormat("[GETTEXTURE]: called {0}", textureStr);
  68. if (m_assetService == null)
  69. {
  70. m_log.Error("[GETTEXTURE]: Cannot fetch texture " + textureStr + " without an asset service");
  71. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
  72. }
  73. UUID textureID;
  74. if (!String.IsNullOrEmpty(textureStr) && UUID.TryParse(textureStr, out textureID))
  75. {
  76. // m_log.DebugFormat("[GETTEXTURE]: Received request for texture id {0}", textureID);
  77. string[] formats;
  78. if (!string.IsNullOrEmpty(format))
  79. {
  80. formats = new string[1] { format.ToLower() };
  81. }
  82. else
  83. {
  84. formats = WebUtil.GetPreferredImageTypes(httpRequest.Headers.Get("Accept"));
  85. if (formats.Length == 0)
  86. formats = new string[1] { DefaultFormat }; // default
  87. }
  88. // OK, we have an array with preferred formats, possibly with only one entry
  89. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
  90. foreach (string f in formats)
  91. {
  92. if (FetchTexture(httpRequest, httpResponse, textureID, f))
  93. break;
  94. }
  95. }
  96. else
  97. {
  98. m_log.Warn("[GETTEXTURE]: Failed to parse a texture_id from GetTexture request: " + httpRequest.Url);
  99. }
  100. // m_log.DebugFormat(
  101. // "[GETTEXTURE]: For texture {0} sending back response {1}, data length {2}",
  102. // textureID, httpResponse.StatusCode, httpResponse.ContentLength);
  103. return null;
  104. }
  105. /// <summary>
  106. ///
  107. /// </summary>
  108. /// <param name="httpRequest"></param>
  109. /// <param name="httpResponse"></param>
  110. /// <param name="textureID"></param>
  111. /// <param name="format"></param>
  112. /// <returns>False for "caller try another codec"; true otherwise</returns>
  113. private bool FetchTexture(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, UUID textureID, string format)
  114. {
  115. // m_log.DebugFormat("[GETTEXTURE]: {0} with requested format {1}", textureID, format);
  116. AssetBase texture;
  117. string fullID = textureID.ToString();
  118. if (format != DefaultFormat)
  119. fullID = fullID + "-" + format;
  120. if (!String.IsNullOrEmpty(REDIRECT_URL))
  121. {
  122. // Only try to fetch locally cached textures. Misses are redirected
  123. texture = m_assetService.GetCached(fullID);
  124. if (texture != null)
  125. {
  126. if (texture.Type != (sbyte)AssetType.Texture)
  127. {
  128. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
  129. return true;
  130. }
  131. WriteTextureData(httpRequest, httpResponse, texture, format);
  132. }
  133. else
  134. {
  135. string textureUrl = REDIRECT_URL + textureID.ToString();
  136. m_log.Debug("[GETTEXTURE]: Redirecting texture request to " + textureUrl);
  137. httpResponse.RedirectLocation = textureUrl;
  138. return true;
  139. }
  140. }
  141. else // no redirect
  142. {
  143. // try the cache
  144. texture = m_assetService.GetCached(fullID);
  145. if (texture == null)
  146. {
  147. // m_log.DebugFormat("[GETTEXTURE]: texture was not in the cache");
  148. // Fetch locally or remotely. Misses return a 404
  149. texture = m_assetService.Get(textureID.ToString());
  150. if (texture != null)
  151. {
  152. if (texture.Type != (sbyte)AssetType.Texture)
  153. {
  154. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
  155. return true;
  156. }
  157. if (format == DefaultFormat)
  158. {
  159. WriteTextureData(httpRequest, httpResponse, texture, format);
  160. return true;
  161. }
  162. else
  163. {
  164. AssetBase newTexture = new AssetBase(texture.ID + "-" + format, texture.Name, (sbyte)AssetType.Texture, texture.Metadata.CreatorID);
  165. newTexture.Data = ConvertTextureData(texture, format);
  166. if (newTexture.Data.Length == 0)
  167. return false; // !!! Caller try another codec, please!
  168. newTexture.Flags = AssetFlags.Collectable;
  169. newTexture.Temporary = true;
  170. newTexture.Local = true;
  171. m_assetService.Store(newTexture);
  172. WriteTextureData(httpRequest, httpResponse, newTexture, format);
  173. return true;
  174. }
  175. }
  176. }
  177. else // it was on the cache
  178. {
  179. // m_log.DebugFormat("[GETTEXTURE]: texture was in the cache");
  180. WriteTextureData(httpRequest, httpResponse, texture, format);
  181. return true;
  182. }
  183. }
  184. // not found
  185. // m_log.Warn("[GETTEXTURE]: Texture " + textureID + " not found");
  186. httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
  187. return true;
  188. }
  189. private void WriteTextureData(IOSHttpRequest request, IOSHttpResponse response, AssetBase texture, string format)
  190. {
  191. string range = request.Headers.GetOne("Range");
  192. if (!String.IsNullOrEmpty(range)) // JP2's only
  193. {
  194. // Range request
  195. int start, end;
  196. if (TryParseRange(range, out start, out end))
  197. {
  198. // Before clamping start make sure we can satisfy it in order to avoid
  199. // sending back the last byte instead of an error status
  200. if (start >= texture.Data.Length)
  201. {
  202. // m_log.DebugFormat(
  203. // "[GETTEXTURE]: Client requested range for texture {0} starting at {1} but texture has end of {2}",
  204. // texture.ID, start, texture.Data.Length);
  205. // Stricly speaking, as per http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html, we should be sending back
  206. // Requested Range Not Satisfiable (416) here. However, it appears that at least recent implementations
  207. // of the Linden Lab viewer (3.2.1 and 3.3.4 and probably earlier), a viewer that has previously
  208. // received a very small texture may attempt to fetch bytes from the server past the
  209. // range of data that it received originally. Whether this happens appears to depend on whether
  210. // the viewer's estimation of how large a request it needs to make for certain discard levels
  211. // (http://wiki.secondlife.com/wiki/Image_System#Discard_Level_and_Mip_Mapping), chiefly discard
  212. // level 2. If this estimate is greater than the total texture size, returning a RequestedRangeNotSatisfiable
  213. // here will cause the viewer to treat the texture as bad and never display the full resolution
  214. // However, if we return PartialContent (or OK) instead, the viewer will display that resolution.
  215. // response.StatusCode = (int)System.Net.HttpStatusCode.RequestedRangeNotSatisfiable;
  216. // response.AddHeader("Content-Range", String.Format("bytes */{0}", texture.Data.Length));
  217. // response.StatusCode = (int)System.Net.HttpStatusCode.OK;
  218. response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent;
  219. response.ContentType = texture.Metadata.ContentType;
  220. }
  221. else
  222. {
  223. // Handle the case where no second range value was given. This is equivalent to requesting
  224. // the rest of the entity.
  225. if (end == -1)
  226. end = int.MaxValue;
  227. end = Utils.Clamp(end, 0, texture.Data.Length - 1);
  228. start = Utils.Clamp(start, 0, end);
  229. int len = end - start + 1;
  230. // m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
  231. // Always return PartialContent, even if the range covered the entire data length
  232. // We were accidentally sending back 404 before in this situation
  233. // https://issues.apache.org/bugzilla/show_bug.cgi?id=51878 supports sending 206 even if the
  234. // entire range is requested, and viewer 3.2.2 (and very probably earlier) seems fine with this.
  235. //
  236. // We also do not want to send back OK even if the whole range was satisfiable since this causes
  237. // HTTP textures on at least Imprudence 1.4.0-beta2 to never display the final texture quality.
  238. // if (end > maxEnd)
  239. // response.StatusCode = (int)System.Net.HttpStatusCode.OK;
  240. // else
  241. response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent;
  242. response.ContentLength = len;
  243. response.ContentType = texture.Metadata.ContentType;
  244. response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", start, end, texture.Data.Length));
  245. response.Body.Write(texture.Data, start, len);
  246. }
  247. }
  248. else
  249. {
  250. m_log.Warn("[GETTEXTURE]: Malformed Range header: " + range);
  251. response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
  252. }
  253. }
  254. else // JP2's or other formats
  255. {
  256. // Full content request
  257. response.StatusCode = (int)System.Net.HttpStatusCode.OK;
  258. response.ContentLength = texture.Data.Length;
  259. if (format == DefaultFormat)
  260. response.ContentType = texture.Metadata.ContentType;
  261. else
  262. response.ContentType = "image/" + format;
  263. response.Body.Write(texture.Data, 0, texture.Data.Length);
  264. }
  265. // if (response.StatusCode < 200 || response.StatusCode > 299)
  266. // m_log.WarnFormat(
  267. // "[GETTEXTURE]: For texture {0} requested range {1} responded {2} with content length {3} (actual {4})",
  268. // texture.FullID, range, response.StatusCode, response.ContentLength, texture.Data.Length);
  269. // else
  270. // m_log.DebugFormat(
  271. // "[GETTEXTURE]: For texture {0} requested range {1} responded {2} with content length {3} (actual {4})",
  272. // texture.FullID, range, response.StatusCode, response.ContentLength, texture.Data.Length);
  273. }
  274. /// <summary>
  275. /// Parse a range header.
  276. /// </summary>
  277. /// <remarks>
  278. /// As per http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html,
  279. /// this obeys range headers with two values (e.g. 533-4165) and no second value (e.g. 533-).
  280. /// Where there is no value, -1 is returned.
  281. /// FIXME: Need to cover the case where only a second value is specified (e.g. -4165), probably by returning -1
  282. /// for start.</remarks>
  283. /// <returns></returns>
  284. /// <param name='header'></param>
  285. /// <param name='start'>Start of the range. Undefined if this was not a number.</param>
  286. /// <param name='end'>End of the range. Will be -1 if no end specified. Undefined if there was a raw string but this was not a number.</param>
  287. private bool TryParseRange(string header, out int start, out int end)
  288. {
  289. start = end = 0;
  290. if (header.StartsWith("bytes="))
  291. {
  292. string[] rangeValues = header.Substring(6).Split('-');
  293. if (rangeValues.Length == 2)
  294. {
  295. if (!Int32.TryParse(rangeValues[0], out start))
  296. return false;
  297. string rawEnd = rangeValues[1];
  298. if (rawEnd == "")
  299. {
  300. end = -1;
  301. return true;
  302. }
  303. else if (Int32.TryParse(rawEnd, out end))
  304. {
  305. return true;
  306. }
  307. }
  308. }
  309. start = end = 0;
  310. return false;
  311. }
  312. private byte[] ConvertTextureData(AssetBase texture, string format)
  313. {
  314. m_log.DebugFormat("[GETTEXTURE]: Converting texture {0} to {1}", texture.ID, format);
  315. byte[] data = new byte[0];
  316. MemoryStream imgstream = new MemoryStream();
  317. Bitmap mTexture = new Bitmap(1, 1);
  318. ManagedImage managedImage;
  319. Image image = (Image)mTexture;
  320. try
  321. {
  322. // Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular data
  323. imgstream = new MemoryStream();
  324. // Decode image to System.Drawing.Image
  325. if (OpenJPEG.DecodeToImage(texture.Data, out managedImage, out image))
  326. {
  327. // Save to bitmap
  328. mTexture = new Bitmap(image);
  329. EncoderParameters myEncoderParameters = new EncoderParameters();
  330. myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
  331. // Save bitmap to stream
  332. ImageCodecInfo codec = GetEncoderInfo("image/" + format);
  333. if (codec != null)
  334. {
  335. mTexture.Save(imgstream, codec, myEncoderParameters);
  336. // Write the stream to a byte array for output
  337. data = imgstream.ToArray();
  338. }
  339. else
  340. m_log.WarnFormat("[GETTEXTURE]: No such codec {0}", format);
  341. }
  342. }
  343. catch (Exception e)
  344. {
  345. m_log.WarnFormat("[GETTEXTURE]: Unable to convert texture {0} to {1}: {2}", texture.ID, format, e.Message);
  346. }
  347. finally
  348. {
  349. // Reclaim memory, these are unmanaged resources
  350. // If we encountered an exception, one or more of these will be null
  351. if (mTexture != null)
  352. mTexture.Dispose();
  353. if (image != null)
  354. image.Dispose();
  355. if (imgstream != null)
  356. {
  357. imgstream.Close();
  358. imgstream.Dispose();
  359. }
  360. }
  361. return data;
  362. }
  363. // From msdn
  364. private static ImageCodecInfo GetEncoderInfo(String mimeType)
  365. {
  366. ImageCodecInfo[] encoders;
  367. encoders = ImageCodecInfo.GetImageEncoders();
  368. for (int j = 0; j < encoders.Length; ++j)
  369. {
  370. if (encoders[j].MimeType == mimeType)
  371. return encoders[j];
  372. }
  373. return null;
  374. }
  375. }
  376. }