rstudio / plumber
1
cookieFilter <- function(req){
2 1
  cookie <- req$HTTP_COOKIE
3 1
  req$cookies <- parseCookies(cookie)
4 1
  forward()
5
}
6

7
#' @noRd
8
parseCookies <- function(cookie) {
9 1
  if (is.null(cookie) || nchar(cookie) == 0) {
10 1
    return(list())
11
  }
12 1
  cookie <- strsplit(cookie, ";", fixed=TRUE)[[1]]
13 1
  cookie <- sub("\\s*([\\S*])\\s*", "\\1", cookie, perl=TRUE)
14

15 1
  cookieList <- stri_split_fixed(str = cookie, pattern = "=", n = 2)
16

17
  # Handle any non-existent cookie values.
18 1
  for (i in seq_along(cookieList)) {
19 1
    if(length(cookieList[[i]]) == 1) {
20 1
      cookieList[[i]][[2]] <- ""
21
    }
22
  }
23

24 1
  cookies <- vapply(cookieList, "[[", character(1), 2)
25 1
  decodedCookies <- as.list(httpuv::decodeURIComponent(cookies))
26 1
  cookieNames <- vapply(cookieList, "[[", character(1), 1)
27 1
  names(decodedCookies) <- cookieNames
28 1
  decodedCookies
29
}

Read our documentation on viewing source code .

Loading