Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save spillsthrills/764d4a818183fc9496da4833eef535c5 to your computer and use it in GitHub Desktop.
Save spillsthrills/764d4a818183fc9496da4833eef535c5 to your computer and use it in GitHub Desktop.
url util functions
<cfscript>
/*
//*********************** Tests ******************************
orig Struct = {itemA_1:1,itemA_2:2,itemA_3:"Question Mark=?",itemA_4:""};
//newStruct = {itemB_1:1,itemB_2:2,itemA_2:5};
newStruct = {
'utm_source' : 'Craigslist',
'utm_campaign' : 'cpc',
'clcp' : 'test'
};
outString = queryStringFromStructs(origStruct,newStruct,false,true);
writeOutput(outString);
writedump(queryStringToStruct('http://agspanos.touraptnow.com/emailus.cfm?clcp=t&id=21870&code=2&floorplanID=99034&adid=38220559&aptid=0'));
writedump(queryStringToStruct(""));
writedump(queryStringToStruct("?qsParam1=tjjjj&qsParam2=lllll&qsParamempty=",true));
writedump(queryStringToStruct("?qsParam1=tjjjj&qsParamempty=&qsParam2=lllll",true));
writedump(queryStringToStruct("?qsParam1=tjjjj&qsParam2=lllll&qsParamempty=",false));
writedump(queryStringToStruct("?qsParam1=tjjjj&qsParam2=lllll&qsParamempty=&qsParamEncoced=%28my%20val%20in%20parens%29",false));
writedump(queryStringToStruct("?qsParam1=tjjjj&qsParam2=lllll&qsParamempty=&qsParamEncoced=%28my%20val%20in%20parens%29",false,true));
origStruct = queryStringToStruct('http://agspanos.touraptnow.com/emailus.cfm?clcp=t&id=21870&code=2&floorplanID=99034&adid=38220559&aptid=0&emptyOrig=&encodedKeyOrig=%28my%20val%20in%20parens%29',true,true);
writedump(origStruct);
origURLStr = "http://agspanos.touraptnow.com/emailus.cfm?clcp=t&id=21870&code=2&floorplanID=99034&adid=38220559&aptid=0&emptyOrig=&encodedKeyOrig=%28my%20val%20in%20parens%29";
//no query crete fresh new query string
//origURLStr = "http://agspanos.touraptnow.com/emailus.cfm";
origStruct = queryStringToStruct(origURLStr,true,true);
newStruct = {
'utm_source' : 'Craigslist',
'utm_campaign' : 'cpc',
'clcp' : 'test',
'id' : 'overWriteID',
'emptyKeyNew': '',
'encocedKeyNew': '%28my%20val%20in%20parens%29',
'unEncodedKeyNew' : '( my val in parens )'
};
writeOutput("<h3>Struct to Update From</h3>");
writedump(var=newStruct,label="Struct to Update From");
writeOutput("<h3>Original URL String</h3>");
writedump(var=origUrlStr, label="Original URL String");
//default settings
outURL = newURLStringfromStructs(
urlString=origURLStr,
updateStruct = newStruct,
preserveEmptyKeys = true,
decodeValues = true,
overWrite = false,
includeEmptyKeys = true,
encodeVals = true
);
writeOutput("<h3>Default Settings</h3>");
writedump(var=outURL,label="Default Settings");
//overwrite settings
outURL = newURLStringfromStructs(
urlString=origURLStr,
updateStruct = newStruct,
preserveEmptyKeys = true,
decodeValues = true,
overWrite = true,
includeEmptyKeys = true,
encodeVals = true
);
writeOutput("<h3>Overwrite to True</h3>");
writedump(var=outURL, label="Overwrite to True");
//overwrite settings and no empty keys
outURL = newURLStringfromStructs(
urlString=origURLStr,
updateStruct = newStruct,
preserveEmptyKeys = true,
decodeValues = true,
overWrite = true,
includeEmptyKeys = false,
encodeVals = true
);
writeOutput("<h3>Overwrite and No Empty Keys</h3>");
writedump(var=outURL,label="Overwrite and No Empty Keys");
*/
public struct function queryStringToStruct(
required string urlString,
boolean preserveEmptyKeys = true,
boolean decodeValues = true
){
var emptyKeyValBool = arguments.preserveEmptyKeys;
var decodeURLValues = arguments.decodeValues;
var queryStr = reReplace(arguments.urlString,"^.*\?","");
var queryParamStruct = {};
var queryStrArr = listToArray(queryStr,"&",true);
if ( arrayisEmpty(queryStrArr) ) {
throw("Empty or invalid query string.");
}
try{
queryStrArr.each(function(qParam, qIdx) {
tempStruct = {};
tempArr = listToArray(qParam,"=", true);
if ( len(tempArr[2]) ) {
tempStruct[tempArr[1]] = ((decodeURLValues)? decodeFromURL(tempArr[2]) : tempArr[2]);
} else if( emptyKeyValBool ) {
tempStruct[tempArr[1]] = "";
}
queryParamStruct.append(tempStruct, true);
});
}catch(any excpt) {
//log etc.....
}
finally{
return queryParamStruct;
}
}
public string function queryStringFromStructs(
required struct origValStruct,
required struct newValStruct,
boolean overWrite = false,
boolean includeEmptyKeys = true,
boolean encodeVals = true
){
var response = "";
var overWriteBool = arguments.overWrite;
var encodeValBool = arguments.encodeVals;
if ( structIsEmpty(arguments.origValStruct) &&
structIsEmpty(arguments.newValStruct)) {
throw("Empty Structs");
}
var outArray = [];
try{
structAppend(arguments.origValStruct, arguments.newValStruct, overWriteBool);
structEach(arguments.origValStruct,function(key, val){
if( len(val) ) {
outArray.append(key&"="&((encodeValBool)? encodeForURL(val) : val));
} else if ( includeEmptyKeys ) {
outArray.append(key&"=");
}
response = outArray.toList("&");
});
} catch( any excpt ) {
//log etc...
writedump(excpt);
} finally {
return response;
}
}
public string function newURLStringfromStructs(
required string urlString = "",
required struct updateStruct,
boolean preserveEmptyKeys = true,
boolean decodeValues = false,
boolean overWrite = false,
boolean includeEmptyKeys = false,
boolean encodeVals = true
){
var response = "";
var newStruct = arguments.updateStruct;
var baseURL = reReplace(arguments.urlString, "\?.*$", "");
var newQueryString = "";
try{
var origStruct = queryStringToStruct(
urlString = arguments.urlSTring,
preserveEmptyKeys = arguments.preserveEmptyKeys,
decodeValues = arguments.decodeValues
);
newQueryString = queryStringFromStructs(
origValStruct = origStruct,
newValStruct = arguments.updateStruct,
overWrite = arguments.overWrite,
includeEmptyKeys = arguments.includeEmptyKeys,
encodeVals = arguments.encodeVals
);
response = baseURL & "?" & newQueryString;
} catch( any excpt ) {
//log etc....
}
finally {
return response;
}
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment