You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.9 KiB

7 years ago
  1. module.exports = preferredCharsets;
  2. preferredCharsets.preferredCharsets = preferredCharsets;
  3. function parseAcceptCharset(accept) {
  4. return accept.split(',').map(function(e, i) {
  5. return parseCharset(e.trim(), i);
  6. }).filter(function(e) {
  7. return e;
  8. });
  9. }
  10. function parseCharset(s, i) {
  11. var match = s.match(/^\s*(\S+?)\s*(?:;(.*))?$/);
  12. if (!match) return null;
  13. var charset = match[1];
  14. var q = 1;
  15. if (match[2]) {
  16. var params = match[2].split(';')
  17. for (var i = 0; i < params.length; i ++) {
  18. var p = params[i].trim().split('=');
  19. if (p[0] === 'q') {
  20. q = parseFloat(p[1]);
  21. break;
  22. }
  23. }
  24. }
  25. return {
  26. charset: charset,
  27. q: q,
  28. i: i
  29. };
  30. }
  31. function getCharsetPriority(charset, accepted) {
  32. return (accepted.map(function(a) {
  33. return specify(charset, a);
  34. }).filter(Boolean).sort(function (a, b) {
  35. if(a.s == b.s) {
  36. return a.q > b.q ? -1 : 1;
  37. } else {
  38. return a.s > b.s ? -1 : 1;
  39. }
  40. })[0] || {s: 0, q:0});
  41. }
  42. function specify(charset, spec) {
  43. var s = 0;
  44. if(spec.charset.toLowerCase() === charset.toLowerCase()){
  45. s |= 1;
  46. } else if (spec.charset !== '*' ) {
  47. return null
  48. }
  49. return {
  50. s: s,
  51. q: spec.q,
  52. }
  53. }
  54. function preferredCharsets(accept, provided) {
  55. // RFC 2616 sec 14.2: no header = *
  56. accept = parseAcceptCharset(accept === undefined ? '*' : accept || '');
  57. if (provided) {
  58. return provided.map(function(type) {
  59. return [type, getCharsetPriority(type, accept)];
  60. }).filter(function(pair) {
  61. return pair[1].q > 0;
  62. }).sort(function(a, b) {
  63. var pa = a[1];
  64. var pb = b[1];
  65. return (pb.q - pa.q) || (pb.s - pa.s) || (pa.i - pb.i);
  66. }).map(function(pair) {
  67. return pair[0];
  68. });
  69. } else {
  70. return accept.sort(function (a, b) {
  71. // revsort
  72. return (b.q - a.q) || (a.i - b.i);
  73. }).filter(function(type) {
  74. return type.q > 0;
  75. }).map(function(type) {
  76. return type.charset;
  77. });
  78. }
  79. }