summaryrefslogtreecommitdiff
path: root/share/server/render.js
diff options
context:
space:
mode:
authorJohn Christopher Anderson <jchris@apache.org>2009-08-04 23:29:25 +0000
committerJohn Christopher Anderson <jchris@apache.org>2009-08-04 23:29:25 +0000
commit9cddd68f4648620be9d81aedc125704e1824cf2d (patch)
treed28c5f45d65cd2ff3aa32d7c73163773feabf27e /share/server/render.js
parentd38226420bceee70cc07de4ae64b08c7262aff7c (diff)
Move mimeparse.js to it's own file, add to NOTICE as an external library.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@801018 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'share/server/render.js')
-rw-r--r--share/server/render.js102
1 files changed, 0 insertions, 102 deletions
diff --git a/share/server/render.js b/share/server/render.js
index 6efc9687..9537cd54 100644
--- a/share/server/render.js
+++ b/share/server/render.js
@@ -10,108 +10,6 @@
// License for the specific language governing permissions and limitations under
// the License.
-// mimeparse.js
-// http://code.google.com/p/mimeparse/
-// MIT Licensed http://www.opensource.org/licenses/mit-license.php
-// Code with comments: http://mimeparse.googlecode.com/svn/trunk/mimeparse.js
-// Tests: http://mimeparse.googlecode.com/svn/trunk/mimeparse-js-test.html
-// Ported by Chris Anderson from version 0.1.2
-
-var Mimeparse = (function() {
- function strip(string) {
- return string.replace(/^\s+/, '').replace(/\s+$/, '');
- };
- function parseRanges(ranges) {
- var parsedRanges = [], rangeParts = ranges.split(",");
- for (var i=0; i < rangeParts.length; i++) {
- parsedRanges.push(publicMethods.parseMediaRange(rangeParts[i]));
- };
- return parsedRanges;
- };
- var publicMethods = {
- parseMimeType : function(mimeType) {
- var fullType, typeParts, params = {}, parts = mimeType.split(';');
- for (var i=0; i < parts.length; i++) {
- var p = parts[i].split('=');
- if (p.length == 2) {
- params[strip(p[0])] = strip(p[1]);
- }
- };
- fullType = parts[0].replace(/^\s+/, '').replace(/\s+$/, '');
- if (fullType == '*') fullType = '*/*';
- typeParts = fullType.split('/');
- return [typeParts[0], typeParts[1], params];
- },
- parseMediaRange : function(range) {
- var q, parsedType = this.parseMimeType(range);
- if (!parsedType[2]['q']) {
- parsedType[2]['q'] = '1';
- } else {
- q = parseFloat(parsedType[2]['q']);
- if (isNaN(q)) {
- parsedType[2]['q'] = '1';
- } else if (q > 1 || q < 0) {
- parsedType[2]['q'] = '1';
- }
- }
- return parsedType;
- },
- fitnessAndQualityParsed : function(mimeType, parsedRanges) {
- var bestFitness = -1, bestFitQ = 0, target = this.parseMediaRange(mimeType);
- var targetType = target[0], targetSubtype = target[1], targetParams = target[2];
-
- for (var i=0; i < parsedRanges.length; i++) {
- var parsed = parsedRanges[i];
- var type = parsed[0], subtype = parsed[1], params = parsed[2];
- if ((type == targetType || type == "*" || targetType == "*") &&
- (subtype == targetSubtype || subtype == "*" || targetSubtype == "*")) {
- var matchCount = 0;
- for (param in targetParams) {
- if (param != 'q' && params[param] && params[param] == targetParams[param]) {
- matchCount += 1;
- }
- }
-
- var fitness = (type == targetType) ? 100 : 0;
- fitness += (subtype == targetSubtype) ? 10 : 0;
- fitness += matchCount;
-
- if (fitness > bestFitness) {
- bestFitness = fitness;
- bestFitQ = params["q"];
- }
- }
- };
- return [bestFitness, parseFloat(bestFitQ)];
- },
- qualityParsed : function(mimeType, parsedRanges) {
- return this.fitnessAndQualityParsed(mimeType, parsedRanges)[1];
- },
- quality : function(mimeType, ranges) {
- return this.qualityParsed(mimeType, parseRanges(ranges));
- },
-
- // Takes a list of supported mime-types and finds the best
- // match for all the media-ranges listed in header. The value of
- // header must be a string that conforms to the format of the
- // HTTP Accept: header. The value of 'supported' is a list of
- // mime-types.
- //
- // >>> bestMatch(['application/xbel+xml', 'text/xml'], 'text/*;q=0.5,*/*; q=0.1')
- // 'text/xml'
- bestMatch : function(supported, header) {
- var parsedHeader = parseRanges(header);
- var weighted = [];
- for (var i=0; i < supported.length; i++) {
- weighted.push([publicMethods.fitnessAndQualityParsed(supported[i], parsedHeader), supported[i]]);
- };
- weighted.sort();
- return weighted[weighted.length-1][0][1] ? weighted[weighted.length-1][1] : '';
- }
- };
- return publicMethods;
-})();
-
var respCT;
var respTail;
// this function provides a shortcut for managing responses by Accept header