File: filter_headers.R

package info (click to toggle)
r-cran-vcr 0.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,360 kB
  • sloc: cpp: 15; sh: 13; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#' Remove headers or replace header values
#' @noRd
#' @details
#' Applies to request and response headers.
#' @examples
#' # remove one header
#' filter_request_headers <- "User-Agent"
#' # remove multiple headers
#' filter_request_headers <- c("User-Agent", "Authorization")
#' # replace one header's value
#' filter_request_headers <- list(Authorization = "foo-bar")
#' # replace many header's values
#' filter_request_headers <- list(Authorization = "foo-bar", Accept = "everything!")
#' # mix: remove one header, replace another header's value
#' filter_request_headers <- list("Accept", Authorization = "foo-bar")
headers_remove <- function(x) {
  filter_req_or_res <- function(int, h, which) {
    if (!is.null(h)) {
      if (is.null(names(h))) toremove <- unlist(h)
      if (!is.null(names(h))) toremove <- unname(unlist(h[!nzchar(names(h))]))
      # remove zero length strings
      toremove <- Filter(nzchar, toremove)
      for (i in seq_along(toremove)) {
        int <- lapply(int, function(b) {
          b[[which]]$headers[[toremove[i]]] <- NULL
          return(b)
        })
      }

      toreplace <- h[nzchar(names(h))]
      if (length(toreplace)) {
        for (i in seq_along(toreplace)) {
          int <- lapply(int, function(b) {
            b[[which]]$headers[[names(toreplace)[i]]] <- toreplace[[i]]
            return(b)
          })
        }
      }
    }
    return(int)
  }
  x <- filter_req_or_res(x, vcr_c$filter_request_headers, "request")
  filter_req_or_res(x, vcr_c$filter_response_headers, "response")
}