/
http_subdomain_changed.R
50 lines (41 loc) · 1.23 KB
/
http_subdomain_changed.R
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
45
46
47
48
49
50
#' http_subdomain_changed
#'
#' @param response an httr response object, e.g. from a call to httr::GET()
#'
#' @return logical of length 1 indicating whether or not any domain change
#' happened during the HTTP request
#'
#'
http_subdomain_changed <-
function(response){
# get domain of original HTTP request
orig_domain <- guess_domain(response$request$url)
orig_domain <- stringr::str_replace(orig_domain, "www\\.", "")
# extract location headers
location <-
unlist(
lapply(
X = response$all_headers,
FUN =
function(x){
x$headers$location
}
)
)
location <- utils::tail(location, 1)
location <- stringr::str_replace(location, "www\\.", "")
location_domain <- guess_domain(location)
# if there is no location header nothing has changed
if ( length(location) > 0 ) {
orig_domain_regex <-
stringr::regex(
pattern = paste0("^", stringr::str_replace_all(orig_domain, "\\.", "\\\\."), "$"),
ignore_case = TRUE
)
return(
!( stringr::str_detect(pattern = orig_domain_regex, string = location_domain) )
)
} else {
return(FALSE)
}
}