-
Notifications
You must be signed in to change notification settings - Fork 256
/
plumber-response.R
143 lines (126 loc) · 3.85 KB
/
plumber-response.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
PlumberResponse <- R6Class(
"PlumberResponse",
public = list(
initialize = function(serializer=serializer_json()){
self$serializer <- serializer
},
status = 200L,
body = NULL,
headers = stats::setNames(list(), character()),
serializer = NULL,
setHeader = function(name, value){
he <- list()
he[[name]] <- value
self$headers <- c(self$headers, he)
},
toResponse = function(){
h <- self$headers
body <- self$body
if (is.null(body)){
body <- ""
}
charset <- get_character_set(h$HTTP_CONTENT_TYPE)
if (is.character(body)) {
Encoding(body) <- charset
}
list(
status = self$status,
headers = h,
body = body
)
},
# TODO if name and value are a vector of same length, call set cookie many times
setCookie = function(name, value, path, expiration = FALSE, http = FALSE, secure = FALSE, same_site = FALSE) {
self$setHeader("Set-Cookie", cookieToStr(
name = name,
value = value,
path = path,
expiration = expiration,
http = http,
secure = secure,
same_site = same_site
))
},
removeCookie = function(name, path, http = FALSE, secure = FALSE, same_site = FALSE, ...) {
self$setHeader("Set-Cookie", removeCookieStr(
name = name,
path = path,
http = http,
secure = secure,
same_site = same_site
))
}
)
)
removeCookieStr <- function(name, path, http = FALSE, secure = FALSE, same_site = FALSE) {
str <- paste0(name, "=; ")
if (!missing(path)){
str <- paste0(str, "Path=", path, "; ")
}
if (!missing(http) && http){
str <- paste0(str, "HttpOnly; ")
}
if (!missing(secure) && secure){
str <- paste0(str, "Secure; ")
}
if (!missing(same_site) && is.character(same_site)) {
str <- paste0(str, "SameSite=", same_site, "; ")
}
str <- paste0(str, "Expires=Thu, 01 Jan 1970 00:00:00 GMT")
str
}
#' @noRd
cookieToStr <- function(
name,
value,
path,
expiration = FALSE,
http = FALSE,
secure = FALSE,
same_site = FALSE,
now = Sys.time() # used for testing. Should not be used in regular code.
){
val <- httpuv::encodeURIComponent(as.character(value))
str <- paste0(name, "=", val, "; ")
if (!missing(path)){
str <- paste0(str, "Path=", path, "; ")
}
if (!missing(http) && http){
str <- paste0(str, "HttpOnly; ")
}
if (!missing(secure) && secure){
str <- paste0(str, "Secure; ")
}
if (!missing(same_site) && is.character(same_site)) {
str <- paste0(str, "SameSite=", same_site, "; ")
}
if (!missing(expiration)){
if (is.numeric(expiration)){
# Number of seconds in the future
expy <- now + expiration
expyStr <- format(expy, format="%a, %e %b %Y %T", tz="GMT", usetz=TRUE)
str <- paste0(str, "Expires= ", expyStr, "; ")
str <- paste0(str, "Max-Age= ", expiration, "; ")
} else if (inherits(expiration, "POSIXt")){
seconds <- difftime(expiration, now, units="secs")
# TODO: DRY
expyStr <- format(expiration, format="%a, %e %b %Y %T", tz="GMT", usetz=TRUE)
str <- paste0(str, "Expires= ", expyStr, "; ")
str <- paste0(str, "Max-Age= ", as.integer(seconds), "; ")
} # interpret all other values as session cookies.
}
# Trim last '; '
ret <- substr(str, 0, nchar(str)-2)
# double check size limit isn't reached
cookieByteSize <- length(charToRaw(ret))
# http://browsercookielimits.squawky.net/#limits
# typical browsers support 4096. A couple safari based browsers max out at 4093.
if (cookieByteSize > 4093) {
warning(
"Cookie being saved is too large",
" (> 4093 bytes; found ", cookieByteSize, " bytes).",
" Browsers may not support such a large value.\n",
"Consider using a database and only storing minimal information.")
}
ret
}