Maintaining Sessions Across Multiple ColdFusion CFHttp Requests

<cffunction
	name="GetResponseCookies"
	access="public"
	returntype="struct"
	output="false"
	hint="This parses the response of a CFHttp call and puts the cookies into a struct.">
 
	<!--- Define arguments. --->
	<cfargument
		name="Response"
		type="struct"
		required="true"
		hint="The response of a CFHttp call."
		/>
 
 
	<!--- Define the local scope. --->
	<cfset var LOCAL = StructNew() />
 
	<!---
		Create the default struct in which we will hold
		the response cookies. This struct will contain structs
		and will be keyed on the name of the cookie to be set.
	--->
	<cfset LOCAL.Cookies = StructNew() />
 
	<!---
		Get a reference to the cookies that werew returned
		from the page request. This will give us an numericly
		indexed struct of cookie strings (which we will have
		to parse out for values). BUT, check to make sure
		that cookies were even sent in the response. If they
		were not, then there is not work to be done.
	--->
	<cfif NOT StructKeyExists(
		ARGUMENTS.Response.ResponseHeader,
		"Set-Cookie"
		)>
 
		<!---
			No cookies were send back in the response. Just
			return the empty cookies structure.
		--->
		<cfreturn LOCAL.Cookies />
 
	</cfif>
 
 
	<!---
		ASSERT: We know that cookie were returned in the page
		response and that they are available at the key,
		"Set-Cookie" of the reponse header.
	--->
 
 
	<!---
		Now that we know that the cookies were returned, get
		a reference to the struct as described above.
	--->
	<cfset LOCAL.ReturnedCookies = ARGUMENTS.Response.ResponseHeader[ "Set-Cookie" ] />
 
 
	<!--- Loop over the returned cookies struct. --->
	<cfloop
		item="LOCAL.CookieIndex"
		collection="#LOCAL.ReturnedCookies#">
 
 
		<!---
			As we loop through the cookie struct, get
			the cookie string we want to parse.
		--->
		<cfset LOCAL.CookieString = LOCAL.ReturnedCookies[ LOCAL.CookieIndex ] />
 
 
		<!---
			For each of these cookie strings, we are going to
			need to parse out the values. We can treate the
			cookie string as a semi-colon delimited list.
		--->
		<cfloop
			index="LOCAL.Index"
			from="1"
			to="#ListLen( LOCAL.CookieString, ';' )#"
			step="1">
 
			<!--- Get the name-value pair. --->
			<cfset LOCAL.Pair = ListGetAt(
				LOCAL.CookieString,
				LOCAL.Index,
				";"
				) />
 
 
			<!---
				Get the name as the first part of the pair
				sepparated by the equals sign.
			--->
			<cfset LOCAL.Name = ListFirst( LOCAL.Pair, "=" ) />
 
			<!---
				Check to see if we have a value part. Not all
				cookies are going to send values of length,
				which can throw off ColdFusion.
			--->
			<cfif (ListLen( LOCAL.Pair, "=" ) GT 1)>
 
				<!--- Grab the rest of the list. --->
				<cfset LOCAL.Value = ListRest( LOCAL.Pair, "=" ) />
 
			<cfelse>
 
				<!---
					Since ColdFusion did not find more than one
					value in the list, just get the empty string
					as the value.
				--->
				<cfset LOCAL.Value = "" />
 
			</cfif>
 
 
			<!---
				Now that we have the name-value data values,
				we have to store them in the struct. If we are
				looking at the first part of the cookie string,
				this is going to be the name of the cookie and
				it's struct index.
			--->
			<cfif (LOCAL.Index EQ 1)>
 
				<!---
					Create a new struct with this cookie's name
					as the key in the return cookie struct.
				--->
				<cfset LOCAL.Cookies[ LOCAL.Name ] = StructNew() />
 
				<!---
					Now that we have the struct in place, lets
					get a reference to it so that we can refer
					to it in subseqent loops.
				--->
				<cfset LOCAL.Cookie = LOCAL.Cookies[ LOCAL.Name ] />
 
 
				<!--- Store the value of this cookie. --->
				<cfset LOCAL.Cookie.Value = LOCAL.Value />
 
 
				<!---
					Now, this cookie might have more than just
					the first name-value pair. Let's create an
					additional attributes struct to hold those
					values.
				--->
				<cfset LOCAL.Cookie.Attributes = StructNew() />
 
			<cfelse>
 
				<!---
					For all subseqent calls, just store the
					name-value pair into the established
					cookie's attributes strcut.
				--->
				<cfset LOCAL.Cookie.Attributes[ LOCAL.Name ] = LOCAL.Value />
 
			</cfif>
 
		</cfloop>
 
 
	</cfloop>
 
 
	<!--- Return the cookies. --->
	<cfreturn LOCAL.Cookies />
</cffunction>

For Cut-and-Paste