-
-
Save elpete/95282a913762a956e132609736c60660 to your computer and use it in GitHub Desktop.
SES Interceptor with Accept Header parsing for Format
This file contains 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
<!--- detectExtension ---> | |
<cffunction name="detectExtension" output="false" access="private" returntype="any" hint="Detect extensions from the incoming request"> | |
<cfargument name="requestString" required="true" hint="The requested URL string"> | |
<cfargument name="event" required="true" hint="The event object."> | |
<cfscript> | |
var extension = listLast(arguments.requestString,"."); | |
var extensionLen = len(extension); | |
// cleanup of extension, just in case rewrites add garbage. | |
extension = reReplace(extension, "/$","","all" ); | |
// check if extension found | |
if( listLen(arguments.requestString,".") GT 1 AND len(extension) AND NOT find("/",extension)){ | |
// Check if extension is valid? | |
if( listFindNoCase(instance.validExtensions, extension) ){ | |
// set the format request collection variable | |
event.setValue("format", lcase(extension)); | |
// debug logging | |
if( log.canDebug() ){ | |
log.debug("Extension: #lcase(extension)# detected and set in rc.format"); | |
} | |
// remove it from the string and return string for continued parsing. | |
return left(requestString, len(arguments.requestString) - extensionLen - 1 ); | |
} | |
else{ | |
// log invalid extension | |
if( log.canWarn() ){ | |
log.warn("Invalid Extension Detected: #lcase(extension)# detected but it is not in the valid extension list: #instance.validExtensions#"); | |
} | |
// throw exception if enabled, else just continue | |
if( instance.throwOnInvalidExtension ){ | |
getUtil().throwInvalidHTTP(className="SES", | |
detail="Invalid Request Format Extension Detected: #lcase(extension)#. Valid extensions are: #instance.validExtensions#", | |
statusText="Invalid Requested Format Extension: #lcase(extension)#", | |
statusCode="406"); | |
} | |
} | |
} | |
// check accepts headers for the best match | |
else{ | |
var match = ""; | |
for( var accept in listToArray( event.getHTTPHeader( "Accept" ), "," ) ){ | |
for( var extension in instance.validExtensions ){ | |
if( findNoCase( extension, accept ) > 0 ){ | |
match = extension; | |
break; | |
} | |
} | |
if( len( match ) ){ | |
break; | |
} | |
} | |
if( len( match ) ){ | |
event.setValue( "format", lcase( match ) ); | |
} | |
} | |
// return the same request string, extension not found | |
return requestString; | |
</cfscript> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment