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