diff options
Diffstat (limited to 'OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs')
-rw-r--r-- | OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs | 357 |
1 files changed, 357 insertions, 0 deletions
diff --git a/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs new file mode 100644 index 0000000..00ff3d0 --- /dev/null +++ b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs | |||
@@ -0,0 +1,357 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections; | ||
30 | using System.Collections.Specialized; | ||
31 | using System.Drawing; | ||
32 | using System.Drawing.Imaging; | ||
33 | using System.Reflection; | ||
34 | using System.IO; | ||
35 | using System.Web; | ||
36 | using log4net; | ||
37 | using Nini.Config; | ||
38 | using OpenMetaverse; | ||
39 | using OpenMetaverse.StructuredData; | ||
40 | using OpenMetaverse.Imaging; | ||
41 | using OpenSim.Framework; | ||
42 | using OpenSim.Framework.Servers; | ||
43 | using OpenSim.Framework.Servers.HttpServer; | ||
44 | using OpenSim.Region.Framework.Interfaces; | ||
45 | using OpenSim.Services.Interfaces; | ||
46 | using Caps = OpenSim.Framework.Capabilities.Caps; | ||
47 | |||
48 | namespace OpenSim.Capabilities.Handlers | ||
49 | { | ||
50 | |||
51 | public class GetTextureHandler : BaseStreamHandler | ||
52 | { | ||
53 | private static readonly ILog m_log = | ||
54 | LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
55 | private IAssetService m_assetService; | ||
56 | |||
57 | public const string DefaultFormat = "x-j2c"; | ||
58 | |||
59 | // TODO: Change this to a config option | ||
60 | const string REDIRECT_URL = null; | ||
61 | |||
62 | public GetTextureHandler(string path, IAssetService assService) : | ||
63 | base("GET", path) | ||
64 | { | ||
65 | m_assetService = assService; | ||
66 | } | ||
67 | |||
68 | public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
69 | { | ||
70 | |||
71 | // Try to parse the texture ID from the request URL | ||
72 | NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query); | ||
73 | string textureStr = query.GetOne("texture_id"); | ||
74 | string format = query.GetOne("format"); | ||
75 | |||
76 | m_log.DebugFormat("[GETTEXTURE]: called {0}", textureStr); | ||
77 | |||
78 | if (m_assetService == null) | ||
79 | { | ||
80 | m_log.Error("[GETTEXTURE]: Cannot fetch texture " + textureStr + " without an asset service"); | ||
81 | httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound; | ||
82 | return null; | ||
83 | } | ||
84 | |||
85 | UUID textureID; | ||
86 | if (!String.IsNullOrEmpty(textureStr) && UUID.TryParse(textureStr, out textureID)) | ||
87 | { | ||
88 | string[] formats; | ||
89 | if (format != null && format != string.Empty) | ||
90 | { | ||
91 | formats = new string[1] { format.ToLower() }; | ||
92 | } | ||
93 | else | ||
94 | { | ||
95 | formats = WebUtil.GetPreferredImageTypes(httpRequest.Headers.Get("Accept")); | ||
96 | if (formats.Length == 0) | ||
97 | formats = new string[1] { DefaultFormat }; // default | ||
98 | |||
99 | } | ||
100 | // OK, we have an array with preferred formats, possibly with only one entry | ||
101 | |||
102 | httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound; | ||
103 | foreach (string f in formats) | ||
104 | { | ||
105 | if (FetchTexture(httpRequest, httpResponse, textureID, f)) | ||
106 | break; | ||
107 | } | ||
108 | |||
109 | } | ||
110 | else | ||
111 | { | ||
112 | m_log.Warn("[GETTEXTURE]: Failed to parse a texture_id from GetTexture request: " + httpRequest.Url); | ||
113 | } | ||
114 | |||
115 | httpResponse.Send(); | ||
116 | return null; | ||
117 | } | ||
118 | |||
119 | /// <summary> | ||
120 | /// | ||
121 | /// </summary> | ||
122 | /// <param name="httpRequest"></param> | ||
123 | /// <param name="httpResponse"></param> | ||
124 | /// <param name="textureID"></param> | ||
125 | /// <param name="format"></param> | ||
126 | /// <returns>False for "caller try another codec"; true otherwise</returns> | ||
127 | private bool FetchTexture(OSHttpRequest httpRequest, OSHttpResponse httpResponse, UUID textureID, string format) | ||
128 | { | ||
129 | // m_log.DebugFormat("[GETTEXTURE]: {0} with requested format {1}", textureID, format); | ||
130 | AssetBase texture; | ||
131 | |||
132 | string fullID = textureID.ToString(); | ||
133 | if (format != DefaultFormat) | ||
134 | fullID = fullID + "-" + format; | ||
135 | |||
136 | if (!String.IsNullOrEmpty(REDIRECT_URL)) | ||
137 | { | ||
138 | // Only try to fetch locally cached textures. Misses are redirected | ||
139 | texture = m_assetService.GetCached(fullID); | ||
140 | |||
141 | if (texture != null) | ||
142 | { | ||
143 | if (texture.Type != (sbyte)AssetType.Texture) | ||
144 | { | ||
145 | httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound; | ||
146 | return true; | ||
147 | } | ||
148 | WriteTextureData(httpRequest, httpResponse, texture, format); | ||
149 | } | ||
150 | else | ||
151 | { | ||
152 | string textureUrl = REDIRECT_URL + textureID.ToString(); | ||
153 | m_log.Debug("[GETTEXTURE]: Redirecting texture request to " + textureUrl); | ||
154 | httpResponse.RedirectLocation = textureUrl; | ||
155 | return true; | ||
156 | } | ||
157 | } | ||
158 | else // no redirect | ||
159 | { | ||
160 | // try the cache | ||
161 | texture = m_assetService.GetCached(fullID); | ||
162 | |||
163 | if (texture == null) | ||
164 | { | ||
165 | //m_log.DebugFormat("[GETTEXTURE]: texture was not in the cache"); | ||
166 | |||
167 | // Fetch locally or remotely. Misses return a 404 | ||
168 | texture = m_assetService.Get(textureID.ToString()); | ||
169 | |||
170 | if (texture != null) | ||
171 | { | ||
172 | if (texture.Type != (sbyte)AssetType.Texture) | ||
173 | { | ||
174 | httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound; | ||
175 | return true; | ||
176 | } | ||
177 | if (format == DefaultFormat) | ||
178 | { | ||
179 | WriteTextureData(httpRequest, httpResponse, texture, format); | ||
180 | return true; | ||
181 | } | ||
182 | else | ||
183 | { | ||
184 | AssetBase newTexture = new AssetBase(texture.ID + "-" + format, texture.Name, (sbyte)AssetType.Texture, texture.Metadata.CreatorID); | ||
185 | newTexture.Data = ConvertTextureData(texture, format); | ||
186 | if (newTexture.Data.Length == 0) | ||
187 | return false; // !!! Caller try another codec, please! | ||
188 | |||
189 | newTexture.Flags = AssetFlags.Collectable; | ||
190 | newTexture.Temporary = true; | ||
191 | m_assetService.Store(newTexture); | ||
192 | WriteTextureData(httpRequest, httpResponse, newTexture, format); | ||
193 | return true; | ||
194 | } | ||
195 | } | ||
196 | } | ||
197 | else // it was on the cache | ||
198 | { | ||
199 | //m_log.DebugFormat("[GETTEXTURE]: texture was in the cache"); | ||
200 | WriteTextureData(httpRequest, httpResponse, texture, format); | ||
201 | return true; | ||
202 | } | ||
203 | } | ||
204 | |||
205 | // not found | ||
206 | // m_log.Warn("[GETTEXTURE]: Texture " + textureID + " not found"); | ||
207 | httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound; | ||
208 | return true; | ||
209 | } | ||
210 | |||
211 | private void WriteTextureData(OSHttpRequest request, OSHttpResponse response, AssetBase texture, string format) | ||
212 | { | ||
213 | string range = request.Headers.GetOne("Range"); | ||
214 | //m_log.DebugFormat("[GETTEXTURE]: Range {0}", range); | ||
215 | if (!String.IsNullOrEmpty(range)) // JP2's only | ||
216 | { | ||
217 | // Range request | ||
218 | int start, end; | ||
219 | if (TryParseRange(range, out start, out end)) | ||
220 | { | ||
221 | // Before clamping start make sure we can satisfy it in order to avoid | ||
222 | // sending back the last byte instead of an error status | ||
223 | if (start >= texture.Data.Length) | ||
224 | { | ||
225 | response.StatusCode = (int)System.Net.HttpStatusCode.RequestedRangeNotSatisfiable; | ||
226 | return; | ||
227 | } | ||
228 | |||
229 | end = Utils.Clamp(end, 0, texture.Data.Length - 1); | ||
230 | start = Utils.Clamp(start, 0, end); | ||
231 | int len = end - start + 1; | ||
232 | |||
233 | //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID); | ||
234 | |||
235 | if (len < texture.Data.Length) | ||
236 | response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent; | ||
237 | |||
238 | response.ContentLength = len; | ||
239 | response.ContentType = texture.Metadata.ContentType; | ||
240 | response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", start, end, texture.Data.Length)); | ||
241 | |||
242 | response.Body.Write(texture.Data, start, len); | ||
243 | } | ||
244 | else | ||
245 | { | ||
246 | m_log.Warn("[GETTEXTURE]: Malformed Range header: " + range); | ||
247 | response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; | ||
248 | } | ||
249 | } | ||
250 | else // JP2's or other formats | ||
251 | { | ||
252 | // Full content request | ||
253 | response.StatusCode = (int)System.Net.HttpStatusCode.OK; | ||
254 | response.ContentLength = texture.Data.Length; | ||
255 | if (format == DefaultFormat) | ||
256 | response.ContentType = texture.Metadata.ContentType; | ||
257 | else | ||
258 | response.ContentType = "image/" + format; | ||
259 | response.Body.Write(texture.Data, 0, texture.Data.Length); | ||
260 | } | ||
261 | } | ||
262 | |||
263 | private bool TryParseRange(string header, out int start, out int end) | ||
264 | { | ||
265 | if (header.StartsWith("bytes=")) | ||
266 | { | ||
267 | string[] rangeValues = header.Substring(6).Split('-'); | ||
268 | if (rangeValues.Length == 2) | ||
269 | { | ||
270 | if (Int32.TryParse(rangeValues[0], out start) && Int32.TryParse(rangeValues[1], out end)) | ||
271 | return true; | ||
272 | } | ||
273 | } | ||
274 | |||
275 | start = end = 0; | ||
276 | return false; | ||
277 | } | ||
278 | |||
279 | |||
280 | private byte[] ConvertTextureData(AssetBase texture, string format) | ||
281 | { | ||
282 | m_log.DebugFormat("[GETTEXTURE]: Converting texture {0} to {1}", texture.ID, format); | ||
283 | byte[] data = new byte[0]; | ||
284 | |||
285 | MemoryStream imgstream = new MemoryStream(); | ||
286 | Bitmap mTexture = new Bitmap(1, 1); | ||
287 | ManagedImage managedImage; | ||
288 | Image image = (Image)mTexture; | ||
289 | |||
290 | try | ||
291 | { | ||
292 | // Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular data | ||
293 | |||
294 | imgstream = new MemoryStream(); | ||
295 | |||
296 | // Decode image to System.Drawing.Image | ||
297 | if (OpenJPEG.DecodeToImage(texture.Data, out managedImage, out image)) | ||
298 | { | ||
299 | // Save to bitmap | ||
300 | mTexture = new Bitmap(image); | ||
301 | |||
302 | EncoderParameters myEncoderParameters = new EncoderParameters(); | ||
303 | myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 95L); | ||
304 | |||
305 | // Save bitmap to stream | ||
306 | ImageCodecInfo codec = GetEncoderInfo("image/" + format); | ||
307 | if (codec != null) | ||
308 | { | ||
309 | mTexture.Save(imgstream, codec, myEncoderParameters); | ||
310 | // Write the stream to a byte array for output | ||
311 | data = imgstream.ToArray(); | ||
312 | } | ||
313 | else | ||
314 | m_log.WarnFormat("[GETTEXTURE]: No such codec {0}", format); | ||
315 | |||
316 | } | ||
317 | } | ||
318 | catch (Exception e) | ||
319 | { | ||
320 | m_log.WarnFormat("[GETTEXTURE]: Unable to convert texture {0} to {1}: {2}", texture.ID, format, e.Message); | ||
321 | } | ||
322 | finally | ||
323 | { | ||
324 | // Reclaim memory, these are unmanaged resources | ||
325 | // If we encountered an exception, one or more of these will be null | ||
326 | if (mTexture != null) | ||
327 | mTexture.Dispose(); | ||
328 | |||
329 | if (image != null) | ||
330 | image.Dispose(); | ||
331 | |||
332 | if (imgstream != null) | ||
333 | { | ||
334 | imgstream.Close(); | ||
335 | imgstream.Dispose(); | ||
336 | } | ||
337 | } | ||
338 | |||
339 | return data; | ||
340 | } | ||
341 | |||
342 | // From msdn | ||
343 | private static ImageCodecInfo GetEncoderInfo(String mimeType) | ||
344 | { | ||
345 | ImageCodecInfo[] encoders; | ||
346 | encoders = ImageCodecInfo.GetImageEncoders(); | ||
347 | for (int j = 0; j < encoders.Length; ++j) | ||
348 | { | ||
349 | if (encoders[j].MimeType == mimeType) | ||
350 | return encoders[j]; | ||
351 | } | ||
352 | return null; | ||
353 | } | ||
354 | |||
355 | |||
356 | } | ||
357 | } | ||