Created
July 26, 2013 14:08
-
-
Save DinoChiesa/6089138 to your computer and use it in GitHub Desktop.
content negotiation in JS for ASP-classic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// contentNego.js | |
// | |
// Simple content negotiation logic for ASP-classic modules. | |
// | |
// This module exposes one public function - figureDesiredContentType(). | |
// It checks the HTTP_ACCEPT server variable (whose value is set to the | |
// value of the Accept request header), and sets a content-type | |
// mime-string accordingly. | |
// | |
// When there are multiple mime-types in the Accept header, or when the | |
// Accept header includes */*, this means that multiple response | |
// formats are acceptable. In that case, the order of preference used | |
// by this function priority is hard-coded to be roughly: html, XML, JSON. | |
// | |
// | |
// created: Fri, 16 Mar 2012 15:27 | |
// last saved: <2012-April-20 08:43:45> | |
// | |
// ------------------------------------------------------- | |
// | |
// Example usage: | |
// | |
// <script language="javascript" runat="server" src='contentNego.js'></script> | |
// ... | |
// function handleGet() { | |
// var cnego = CNego.figureDesiredContentType(), | |
// r = getResult(); | |
// | |
// header('Content-Type: ' + cnego[0]); | |
// | |
// switch (cnego[1]) { | |
// case CNego.contentFlavor.HTML: | |
// Response.Write(formatAsHtml(r)); | |
// break; | |
// case CNego.contentFlavor.XML: | |
// Response.Write(formatAsXml(r)); | |
// break; | |
// case CNego.contentFlavor.JSON: | |
// Response.Write(JSON.stringify(r)); | |
// break; | |
// case CNego.contentFlavor.Text: | |
// Response.Write(formatAsText(r)); | |
// break; | |
// } | |
// } | |
// | |
// | |
// Bugs: | |
// | |
(function() { | |
// When CNego is declared as a symbol in the containing scope, as is | |
// done here, it gets "exported" to the global naming scope when | |
// used in an ASP-classic environment. An .asp module that includes | |
// this script will be able to "see" and use this symbol. | |
var flavor = { | |
Unknown: 0, | |
Text: 1, | |
XML: 2, | |
HTML: 3, | |
JSON: 4 | |
}; | |
CNego = { | |
contentFlavor : flavor, | |
figureDesiredContentType : function() { | |
// Return a 2-element tuple; the first element is the mime-type | |
// string, the second is a number indicating the flavor. The | |
// mapping of mime-type to flavor is N:1. In other words, there | |
// are several distinct mime-type strings corresponding to the | |
// JSON "flavor", and several different mime-type strings that | |
// fall under the XML "flavor". | |
var accept = (Request.ServerVariables('HTTP_ACCEPT') + '').toLowerCase(); | |
if (accept == 'undefined') { | |
return ["text/plain", flavor.Text]; | |
} | |
if (accept.indexOf('*/*') != -1) { | |
return ["text/html", flavor.HTML]; | |
} | |
if (accept.indexOf('application/html') != -1) { | |
return ["application/html", flavor.HTML]; | |
} | |
if (accept.indexOf('text/html') != -1) { | |
return ["text/html", flavor.HTML]; | |
} | |
if (accept.indexOf("application/xml") != -1) { | |
return ["application/xml", flavor.XML]; | |
} | |
if (accept.indexOf("text/xml") != -1) { | |
return ["text/xml", flavor.XML]; | |
} | |
if (accept.indexOf("application/x-xml") != -1) { | |
return ["application/x-xml", flavor.XML]; | |
} | |
if (accept.indexOf("application/json") != -1) { | |
return ["application/json", flavor.JSON]; | |
} | |
if (accept.indexOf("text/x-json") != -1) { | |
return ["text/x-json", flavor.JSON]; | |
} | |
if (accept.indexOf("text/javascript") != -1) { | |
return ["text/javascript", flavor.JSON]; | |
} | |
if (accept.indexOf("application/javascript") != -1) { | |
return ["application/javascript", flavor.JSON]; | |
} | |
if (accept.indexOf("application/x-javascript") != -1) { | |
return ["application/x-javascript", flavor.JSON]; | |
} | |
return ["text/plain", flavor.Text]; | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment