Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
URLEncode cookie values as otherwise the syntax makes the cookie unre…
Browse files Browse the repository at this point in the history
…adable, preventing enabling multiple features
  • Loading branch information
theefer committed Dec 18, 2013
1 parent c109a9a commit 02c0cb7
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gu.featureswitching

import org.scalatra._
import java.net.{URLDecoder, URLEncoder}

trait CookieFeatureSwitchingOverrideStrategy extends FeatureSwitchingOverrideStrategy
with ScalatraKernel with CookieSupport {
Expand All @@ -9,14 +10,18 @@ trait CookieFeatureSwitchingOverrideStrategy extends FeatureSwitchingOverrideStr

// TODO: clear obsolete keys for non-existing features, by reading the list of existing switches
private def rawValue = cookies.get(featureSwitchOverrideKey).getOrElse("")
private def rawCookie = URLDecoder.decode(rawValue, "utf-8")

private def cookieMap = rawValue.split(",").map(_.split("=").toList).flatMap {
private def cookieMap = rawCookie.split(",").map(_.split("=").toList).flatMap {
case List(key, "true") => Some((key -> true))
case List(key, "false") => Some((key -> false))
case _ => None // invalid token, ignore
}.toMap

private def renderCookie(newCookieMap: Map[String, Boolean]) = newCookieMap.map {
private def renderCookie(newCookieMap: Map[String, Boolean]) =
URLEncoder.encode(renderCookieString(newCookieMap), "utf-8")

private def renderCookieString(newCookieMap: Map[String, Boolean]) = newCookieMap.map {
case (key, value) => "%s=%s".format(key, value)
}.toList.mkString(",")

Expand Down

0 comments on commit 02c0cb7

Please sign in to comment.