aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs227
1 files changed, 113 insertions, 114 deletions
diff --git a/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs
index 828e943..59d8b9a 100644
--- a/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs
+++ b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs
@@ -47,10 +47,11 @@ using Caps = OpenSim.Framework.Capabilities.Caps;
47 47
48namespace OpenSim.Capabilities.Handlers 48namespace OpenSim.Capabilities.Handlers
49{ 49{
50 public class GetTextureHandler : BaseStreamHandler 50 public class GetTextureHandler
51 { 51 {
52 private static readonly ILog m_log = 52 private static readonly ILog m_log =
53 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 53 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
54
54 private IAssetService m_assetService; 55 private IAssetService m_assetService;
55 56
56 public const string DefaultFormat = "x-j2c"; 57 public const string DefaultFormat = "x-j2c";
@@ -58,28 +59,28 @@ namespace OpenSim.Capabilities.Handlers
58 // TODO: Change this to a config option 59 // TODO: Change this to a config option
59 private string m_RedirectURL = null; 60 private string m_RedirectURL = null;
60 61
61 public GetTextureHandler(string path, IAssetService assService, string name, string description, string redirectURL) 62
62 : base("GET", path, name, description) 63 public GetTextureHandler(IAssetService assService)
63 { 64 {
64 m_assetService = assService; 65 m_assetService = assService;
65 m_RedirectURL = redirectURL;
66 if (m_RedirectURL != null && !m_RedirectURL.EndsWith("/"))
67 m_RedirectURL += "/";
68 } 66 }
69 67
70 protected override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) 68 public Hashtable Handle(Hashtable request)
71 { 69 {
72 // Try to parse the texture ID from the request URL 70 Hashtable ret = new Hashtable();
73 NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query); 71 ret["int_response_code"] = (int)System.Net.HttpStatusCode.NotFound;
74 string textureStr = query.GetOne("texture_id"); 72 ret["content_type"] = "text/plain";
75 string format = query.GetOne("format"); 73 ret["keepalive"] = false;
74 ret["reusecontext"] = false;
75 ret["int_bytes"] = 0;
76 string textureStr = (string)request["texture_id"];
77 string format = (string)request["format"];
76 78
77 //m_log.DebugFormat("[GETTEXTURE]: called {0}", textureStr); 79 //m_log.DebugFormat("[GETTEXTURE]: called {0}", textureStr);
78 80
79 if (m_assetService == null) 81 if (m_assetService == null)
80 { 82 {
81 m_log.Error("[GETTEXTURE]: Cannot fetch texture " + textureStr + " without an asset service"); 83 m_log.Error("[GETTEXTURE]: Cannot fetch texture " + textureStr + " without an asset service");
82 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
83 } 84 }
84 85
85 UUID textureID; 86 UUID textureID;
@@ -94,30 +95,41 @@ namespace OpenSim.Capabilities.Handlers
94 } 95 }
95 else 96 else
96 { 97 {
97 formats = WebUtil.GetPreferredImageTypes(httpRequest.Headers.Get("Accept")); 98 formats = new string[1] { DefaultFormat }; // default
99 if (((Hashtable)request["headers"])["Accept"] != null)
100 formats = WebUtil.GetPreferredImageTypes((string)((Hashtable)request["headers"])["Accept"]);
98 if (formats.Length == 0) 101 if (formats.Length == 0)
99 formats = new string[1] { DefaultFormat }; // default 102 formats = new string[1] { DefaultFormat }; // default
100 103
101 } 104 }
102 // OK, we have an array with preferred formats, possibly with only one entry 105 // OK, we have an array with preferred formats, possibly with only one entry
103 106 bool foundtexture = false;
104 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
105 foreach (string f in formats) 107 foreach (string f in formats)
106 { 108 {
107 if (FetchTexture(httpRequest, httpResponse, textureID, f)) 109 foundtexture = FetchTexture(request, ret, textureID, f);
110 if (foundtexture)
108 break; 111 break;
109 } 112 }
113 if (!foundtexture)
114 {
115 ret["int_response_code"] = 404;
116 ret["error_status_text"] = "not found";
117 ret["str_response_string"] = "not found";
118 ret["content_type"] = "text/plain";
119 ret["keepalive"] = false;
120 ret["reusecontext"] = false;
121 ret["int_bytes"] = 0;
122 }
110 } 123 }
111 else 124 else
112 { 125 {
113 m_log.Warn("[GETTEXTURE]: Failed to parse a texture_id from GetTexture request: " + httpRequest.Url); 126 m_log.Warn("[GETTEXTURE]: Failed to parse a texture_id from GetTexture request: " + (string)request["uri"]);
114 } 127 }
115 128
116// m_log.DebugFormat( 129// m_log.DebugFormat(
117// "[GETTEXTURE]: For texture {0} sending back response {1}, data length {2}", 130// "[GETTEXTURE]: For texture {0} sending back response {1}, data length {2}",
118// textureID, httpResponse.StatusCode, httpResponse.ContentLength); 131// textureID, httpResponse.StatusCode, httpResponse.ContentLength);
119 132 return ret;
120 return null;
121 } 133 }
122 134
123 /// <summary> 135 /// <summary>
@@ -128,7 +140,7 @@ namespace OpenSim.Capabilities.Handlers
128 /// <param name="textureID"></param> 140 /// <param name="textureID"></param>
129 /// <param name="format"></param> 141 /// <param name="format"></param>
130 /// <returns>False for "caller try another codec"; true otherwise</returns> 142 /// <returns>False for "caller try another codec"; true otherwise</returns>
131 private bool FetchTexture(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse, UUID textureID, string format) 143 private bool FetchTexture(Hashtable request, Hashtable response, UUID textureID, string format)
132 { 144 {
133// m_log.DebugFormat("[GETTEXTURE]: {0} with requested format {1}", textureID, format); 145// m_log.DebugFormat("[GETTEXTURE]: {0} with requested format {1}", textureID, format);
134 AssetBase texture; 146 AssetBase texture;
@@ -137,86 +149,70 @@ namespace OpenSim.Capabilities.Handlers
137 if (format != DefaultFormat) 149 if (format != DefaultFormat)
138 fullID = fullID + "-" + format; 150 fullID = fullID + "-" + format;
139 151
140 if (!String.IsNullOrEmpty(m_RedirectURL)) 152 // try the cache
153 texture = m_assetService.GetCached(fullID);
154
155 if (texture == null)
141 { 156 {
142 // Only try to fetch locally cached textures. Misses are redirected 157 //m_log.DebugFormat("[GETTEXTURE]: texture was not in the cache");
143 texture = m_assetService.GetCached(fullID); 158
159 // Fetch locally or remotely. Misses return a 404
160 texture = m_assetService.Get(textureID.ToString());
144 161
145 if (texture != null) 162 if (texture != null)
146 { 163 {
147 if (texture.Type != (sbyte)AssetType.Texture) 164 if (texture.Type != (sbyte)AssetType.Texture)
165 return true;
166
167 if (format == DefaultFormat)
148 { 168 {
149 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound; 169 WriteTextureData(request, response, texture, format);
150 return true; 170 return true;
151 } 171 }
152 WriteTextureData(httpRequest, httpResponse, texture, format); 172 else
153 }
154 else
155 {
156 string textureUrl = m_RedirectURL + "?texture_id="+ textureID.ToString();
157 m_log.Debug("[GETTEXTURE]: Redirecting texture request to " + textureUrl);
158 httpResponse.StatusCode = (int)OSHttpStatusCode.RedirectMovedPermanently;
159 httpResponse.RedirectLocation = textureUrl;
160 return true;
161 }
162 }
163 else // no redirect
164 {
165 // try the cache
166 texture = m_assetService.GetCached(fullID);
167
168 if (texture == null)
169 {
170// m_log.DebugFormat("[GETTEXTURE]: texture was not in the cache");
171
172 // Fetch locally or remotely. Misses return a 404
173 texture = m_assetService.Get(textureID.ToString());
174
175 if (texture != null)
176 { 173 {
177 if (texture.Type != (sbyte)AssetType.Texture) 174 AssetBase newTexture = new AssetBase(texture.ID + "-" + format, texture.Name, (sbyte)AssetType.Texture, texture.Metadata.CreatorID);
178 { 175 newTexture.Data = ConvertTextureData(texture, format);
179 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound; 176 if (newTexture.Data.Length == 0)
180 return true; 177 return false; // !!! Caller try another codec, please!
181 } 178
182 if (format == DefaultFormat) 179 newTexture.Flags = AssetFlags.Collectable;
183 { 180 newTexture.Temporary = true;
184 WriteTextureData(httpRequest, httpResponse, texture, format); 181 newTexture.Local = true;
185 return true; 182 m_assetService.Store(newTexture);
186 } 183 WriteTextureData(request, response, newTexture, format);
187 else 184 return true;
188 {
189 AssetBase newTexture = new AssetBase(texture.ID + "-" + format, texture.Name, (sbyte)AssetType.Texture, texture.Metadata.CreatorID);
190 newTexture.Data = ConvertTextureData(texture, format);
191 if (newTexture.Data.Length == 0)
192 return false; // !!! Caller try another codec, please!
193
194 newTexture.Flags = AssetFlags.Collectable;
195 newTexture.Temporary = true;
196 newTexture.Local = true;
197 m_assetService.Store(newTexture);
198 WriteTextureData(httpRequest, httpResponse, newTexture, format);
199 return true;
200 }
201 } 185 }
202 } 186 }
203 else // it was on the cache 187 }
204 { 188 else // it was on the cache
205// m_log.DebugFormat("[GETTEXTURE]: texture was in the cache"); 189 {
206 WriteTextureData(httpRequest, httpResponse, texture, format); 190 //m_log.DebugFormat("[GETTEXTURE]: texture was in the cache");
207 return true; 191 WriteTextureData(request, response, texture, format);
208 } 192 return true;
209 } 193 }
210 194
195 //response = new Hashtable();
196
197
198 //WriteTextureData(request,response,null,format);
211 // not found 199 // not found
212// m_log.Warn("[GETTEXTURE]: Texture " + textureID + " not found"); 200 //m_log.Warn("[GETTEXTURE]: Texture " + textureID + " not found");
213 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound; 201 return false;
214 return true;
215 } 202 }
216 203
217 private void WriteTextureData(IOSHttpRequest request, IOSHttpResponse response, AssetBase texture, string format) 204 private void WriteTextureData(Hashtable request, Hashtable response, AssetBase texture, string format)
218 { 205 {
219 string range = request.Headers.GetOne("Range"); 206 Hashtable headers = new Hashtable();
207 response["headers"] = headers;
208
209 string range = String.Empty;
210
211 if (((Hashtable)request["headers"])["range"] != null)
212 range = (string)((Hashtable)request["headers"])["range"];
213
214 else if (((Hashtable)request["headers"])["Range"] != null)
215 range = (string)((Hashtable)request["headers"])["Range"];
220 216
221 if (!String.IsNullOrEmpty(range)) // JP2's only 217 if (!String.IsNullOrEmpty(range)) // JP2's only
222 { 218 {
@@ -244,10 +240,8 @@ namespace OpenSim.Capabilities.Handlers
244 // However, if we return PartialContent (or OK) instead, the viewer will display that resolution. 240 // However, if we return PartialContent (or OK) instead, the viewer will display that resolution.
245 241
246// response.StatusCode = (int)System.Net.HttpStatusCode.RequestedRangeNotSatisfiable; 242// response.StatusCode = (int)System.Net.HttpStatusCode.RequestedRangeNotSatisfiable;
247// response.AddHeader("Content-Range", String.Format("bytes */{0}", texture.Data.Length)); 243 // viewers don't seem to handle RequestedRangeNotSatisfiable and keep retrying with same parameters
248// response.StatusCode = (int)System.Net.HttpStatusCode.OK; 244 response["int_response_code"] = (int)System.Net.HttpStatusCode.NotFound;
249 response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent;
250 response.ContentType = texture.Metadata.ContentType;
251 } 245 }
252 else 246 else
253 { 247 {
@@ -262,41 +256,46 @@ namespace OpenSim.Capabilities.Handlers
262 256
263// m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID); 257// m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
264 258
265 // Always return PartialContent, even if the range covered the entire data length 259 response["content-type"] = texture.Metadata.ContentType;
266 // We were accidentally sending back 404 before in this situation 260
267 // https://issues.apache.org/bugzilla/show_bug.cgi?id=51878 supports sending 206 even if the 261 if (start == 0 && len == texture.Data.Length) // well redudante maybe
268 // entire range is requested, and viewer 3.2.2 (and very probably earlier) seems fine with this. 262 {
269 // 263 response["int_response_code"] = (int)System.Net.HttpStatusCode.OK;
270 // We also do not want to send back OK even if the whole range was satisfiable since this causes 264 response["bin_response_data"] = texture.Data;
271 // HTTP textures on at least Imprudence 1.4.0-beta2 to never display the final texture quality. 265 response["int_bytes"] = texture.Data.Length;
272// if (end > maxEnd) 266 }
273// response.StatusCode = (int)System.Net.HttpStatusCode.OK; 267 else
274// else 268 {
275 response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent; 269 response["int_response_code"] = (int)System.Net.HttpStatusCode.PartialContent;
276 270 headers["Content-Range"] = String.Format("bytes {0}-{1}/{2}", start, end, texture.Data.Length);
277 response.ContentLength = len; 271
278 response.ContentType = texture.Metadata.ContentType; 272 byte[] d = new byte[len];
279 response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", start, end, texture.Data.Length)); 273 Array.Copy(texture.Data, start, d, 0, len);
280 274 response["bin_response_data"] = d;
281 response.Body.Write(texture.Data, start, len); 275 response["int_bytes"] = len;
276 }
277// response.Body.Write(texture.Data, start, len);
282 } 278 }
283 } 279 }
284 else 280 else
285 { 281 {
286 m_log.Warn("[GETTEXTURE]: Malformed Range header: " + range); 282 m_log.Warn("[GETTEXTURE]: Malformed Range header: " + range);
287 response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; 283 response["int_response_code"] = (int)System.Net.HttpStatusCode.BadRequest;
288 } 284 }
289 } 285 }
290 else // JP2's or other formats 286 else // JP2's or other formats
291 { 287 {
292 // Full content request 288 // Full content request
293 response.StatusCode = (int)System.Net.HttpStatusCode.OK; 289 response["int_response_code"] = (int)System.Net.HttpStatusCode.OK;
294 response.ContentLength = texture.Data.Length;
295 if (format == DefaultFormat) 290 if (format == DefaultFormat)
296 response.ContentType = texture.Metadata.ContentType; 291 response["content_type"] = texture.Metadata.ContentType;
297 else 292 else
298 response.ContentType = "image/" + format; 293 response["content_type"] = "image/" + format;
299 response.Body.Write(texture.Data, 0, texture.Data.Length); 294
295 response["bin_response_data"] = texture.Data;
296 response["int_bytes"] = texture.Data.Length;
297
298// response.Body.Write(texture.Data, 0, texture.Data.Length);
300 } 299 }
301 300
302// if (response.StatusCode < 200 || response.StatusCode > 299) 301// if (response.StatusCode < 200 || response.StatusCode > 299)
@@ -428,4 +427,4 @@ namespace OpenSim.Capabilities.Handlers
428 return null; 427 return null;
429 } 428 }
430 } 429 }
431} \ No newline at end of file 430}