summaryrefslogtreecommitdiff
path: root/web-ui/app/js/helpers/contenttype.js
blob: 81519452ad2a8a0a6fb0b90c27835699eaf13920 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
define([], function () {
  var exports = {};

  // Licence: PUBLIC DOMAIN <http://unlicense.org/>
  // Author: Austin Wright <http://github.com/Acubed>

  function MediaType(s, p){
	this.type = '';
	this.params = {};
	if(typeof s=='string'){
	  var c = splitQuotedString(s);
	  this.type = c.shift();
	  for(var i=0; i<c.length; i++){
		this.parseParameter(c[i]);
	  }
	}else if(s instanceof MediaType){
	  this.type = s.type;
	  this.q = s.q;
	  for(var n in s.params) this.params[n]=s.params[n];
	}
	if(typeof p=='string'){
	  var c = splitQuotedString(p);
	  for(var i=0; i<c.length; i++){
		this.parseParameter(c[i]);
	  }
	}else if(typeof p=='object'){
	  for(var n in p) this.params[n]=p[n];
	}
  }
  MediaType.prototype.parseParameter = function parseParameter(s){
	var param = s.split('=',1);
	var name = param[0].trim();
	var value = s.substr(param[0].length+1).trim();
	if(!value || !name) return;
	if(name=='q' && this.q===undefined){
	  this.q=parseFloat(value);
	}else{
	  if(value[0]=='"' && value[value.length-1]=='"'){
		value = value.substr(1, value.length-2);
		value = value.replace(/\\(.)/g, function(a,b){return b;});
	  }
	  this.params[name]=value;
	}
  }
  MediaType.prototype.toString = function toString(){
	var str = this.type + ';q='+this.q;
	for(var n in this.params){
	  str += ';'+n+'=';
	  if(this.params[n].match(/["=;<>\[\]\(\) ,\-]/)){
		str += '"' + this.params[n].replace(/["\\]/g, function(a){return '\\'+a;}) + '"';
	  }else{
		str += this.params[n];
	  }
	}
	return str;
  }
  exports.MediaType = MediaType;

  // Split a string by character, but ignore quoted parts and backslash-escaped characters
  function splitQuotedString(str, delim, quote){
	delim = delim || ';';
	quote = quote || '"';
	var res = [];
	var start = 0;
	var offset = 0;
	function findNextChar(v, c, i, a){
	  var p = str.indexOf(c, offset+1);
	  return (p<0)?v:Math.min(p,v);
	}
	while(offset>=0){
	  offset = [delim,quote].reduce(findNextChar, 1/0);
	  if(offset===1/0) break;
	  switch(str[offset]){
	  case quote:
		// Skip to end of quoted string
		while(1){
		  offset=str.indexOf(quote, offset+1);
		  if(offset<0) break;
		  if(str[offset-1]==='\\') continue;
		  break;
		}
		continue;
	  case delim:
		res.push(str.substr(start, offset-start).trim());
		start = ++offset;
		break;
	  }
	}
	res.push(str.substr(start).trim());
	return res;
  }
  exports.splitQuotedString = splitQuotedString;

  // Split a list of content types found in an Accept header
  // Maybe use it like: splitContentTypes(request.headers.accept).map(parseMedia)
  function splitContentTypes(str){
	return splitQuotedString(str, ',');
  }
  exports.splitContentTypes = splitContentTypes;

  function parseMedia(str){
	var o = new MediaType(str);
	if(o.q===undefined) o.q=1;
	return o;
  }
  exports.parseMedia = parseMedia;

  // Pick an ideal representation to send given a list of representations to choose from and the client-preferred list
  function select(reps, accept){
	var cr = {q:0};
	var ca = {q:0};
	var cq = 0;
	for(var i=0; i<reps.length; i++){
	  var r = reps[i];
	  var rq = r.q || 1;
	  for(var j=0; j<accept.length; j++){
		var a=accept[j];
		var aq = a.q || 1;
		var cmp = mediaCmp(a, r);
		if(cmp!==null && cmp>=0){
		  if(aq*rq>cq){
			ca = a;
			cr = r;
			cq = ca.q*cr.q;
			if(cq===1 && cr.type) return cr;
		  }
		}
	  }
	}
	return cr.type&&cr;
  }
  exports.select = select;

  // Determine if one media type is a subset of another
  // If a is a superset of b (b is smaller than a), return 1
  // If b is a superset of a, return -1
  // If they are the exact same, return 0
  // If they are disjoint, return null
  function mediaCmp(a, b){
	if(a.type==='*/*' && b.type!=='*/*') return 1;
	else if(a.type!=='*/*' && b.type==='*/*') return -1;
	var ac = (a.type||'').split('/');
	var bc = (b.type||'').split('/');
	if(ac[0]=='*' && bc[0]!='*') return 1;
	if(ac[0]!='*' && bc[0]=='*') return -1;
	if(a.type!==b.type) return null;
	var ap = a.params || {};
	var bp = b.params || {};
	var ak = Object.keys(ap);
	var bk = Object.keys(bp);
	if(ak.length < bk.length) return 1;
	if(ak.length > bk.length) return -1;
	var k = ak.concat(bk).sort();
	var dir = 0;
	for(var n in ap){
	  if(ap[n] && !bp[n]){ if(dir<0) return null; else dir=1; }
	  if(!ap[n] && bp[n]){ if(dir>0) return null; else dir=-1; }
	}
	return dir;
  }
  exports.mediaCmp = mediaCmp;

  return exports;
});