Last active
August 26, 2015 13:12
-
-
Save betandr/1a453e30198a20ac5e20 to your computer and use it in GitHub Desktop.
Recursive find of a value in a Cache-Control HTTP header value string
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
def find(key: String, in: String): scala.util.Try[Int] = { | |
scala.util.Try { | |
def findValue(items: Array[String]): String = { | |
val item = items.head | |
if (item.contains(key)) { item.split("=").last } | |
else { findValue(items.drop(1)) } | |
} | |
findValue(in.split(", ")).toInt | |
} | |
} | |
// NB: This returns 300 for "max-a" and the first value of 300 for "max" | |
val max = find("max-age", "max-age=300, s-maxage=300, stale-while-revalidate=30") match { | |
case scala.util.Success(value) => value | |
case scala.util.Failure(ex) => 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment