GetTextureRobustHandler.cs 19 KB

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