aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/WebUtil.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/WebUtil.cs81
1 files changed, 81 insertions, 0 deletions
diff --git a/OpenSim/Framework/WebUtil.cs b/OpenSim/Framework/WebUtil.cs
index d16f9bf..1c856af 100644
--- a/OpenSim/Framework/WebUtil.cs
+++ b/OpenSim/Framework/WebUtil.cs
@@ -26,6 +26,7 @@
26 */ 26 */
27 27
28using System; 28using System;
29using System.Collections;
29using System.Collections.Generic; 30using System.Collections.Generic;
30using System.Collections.Specialized; 31using System.Collections.Specialized;
31using System.IO; 32using System.IO;
@@ -363,5 +364,85 @@ namespace OpenSim.Framework
363 } 364 }
364 365
365 #endregion Stream 366 #endregion Stream
367
368 public class QBasedComparer : IComparer
369 {
370 public int Compare(Object x, Object y)
371 {
372 float qx = GetQ(x);
373 float qy = GetQ(y);
374 if (qx < qy)
375 return -1;
376 if (qx == qy)
377 return 0;
378 return 1;
379 }
380
381 private float GetQ(Object o)
382 {
383 // Example: image/png;q=0.9
384
385 if (o is String)
386 {
387 string mime = (string)o;
388 string[] parts = mime.Split(new char[] { ';' });
389 if (parts.Length > 1)
390 {
391 string[] kvp = parts[1].Split(new char[] { '=' });
392 if (kvp.Length == 2 && kvp[0] == "q")
393 {
394 float qvalue = 1F;
395 float.TryParse(kvp[1], out qvalue);
396 return qvalue;
397 }
398 }
399 }
400
401 return 1F;
402 }
403 }
404
405 /// <summary>
406 /// Takes the value of an Accept header and returns the preferred types
407 /// ordered by q value (if it exists).
408 /// Example input: image/jpg;q=0.7, image/png;q=0.8, image/jp2
409 /// Exmaple output: ["jp2", "png", "jpg"]
410 /// NOTE: This doesn't handle the semantics of *'s...
411 /// </summary>
412 /// <param name="accept"></param>
413 /// <returns></returns>
414 public static string[] GetPreferredImageTypes(string accept)
415 {
416
417 if (accept == null || accept == string.Empty)
418 return new string[0];
419
420 string[] types = accept.Split(new char[] { ',' });
421 if (types.Length > 0)
422 {
423 List<string> list = new List<string>(types);
424 list.RemoveAll(delegate(string s) { return !s.ToLower().StartsWith("image"); });
425 ArrayList tlist = new ArrayList(list);
426 tlist.Sort(new QBasedComparer());
427
428 string[] result = new string[tlist.Count];
429 for (int i = 0; i < tlist.Count; i++)
430 {
431 string mime = (string)tlist[i];
432 string[] parts = mime.Split(new char[] { ';' });
433 string[] pair = parts[0].Split(new char[] { '/' });
434 if (pair.Length == 2)
435 result[i] = pair[1].ToLower();
436 else // oops, we don't know what this is...
437 result[i] = pair[0];
438 }
439
440 return result;
441 }
442
443 return new string[0];
444 }
445
446
366 } 447 }
367} 448}