1
- as_headers <- function (x , error_call = caller_env()) {
1
+ as_headers <- function (x , redact = character (), error_call = caller_env()) {
2
2
if (is.character(x ) || is.raw(x )) {
3
3
parsed <- curl :: parse_headers(x )
4
4
valid <- parsed [grepl(" :" , parsed , fixed = TRUE )]
5
5
halves <- parse_in_half(valid , " :" )
6
6
7
7
headers <- set_names(trimws(halves $ right ), halves $ left )
8
- new_headers(as.list(headers ), error_call = error_call )
8
+ new_headers(as.list(headers ), redact = redact , error_call = error_call )
9
9
} else if (is.list(x )) {
10
- new_headers(x , error_call = error_call )
10
+ new_headers(x , redact = redact , error_call = error_call )
11
11
} else {
12
12
cli :: cli_abort(
13
13
" {.arg headers} must be a list, character vector, or raw." ,
@@ -16,39 +16,54 @@ as_headers <- function(x, error_call = caller_env()) {
16
16
}
17
17
}
18
18
19
- new_headers <- function (x , error_call = caller_env()) {
19
+ new_headers <- function (x , redact = character (), error_call = caller_env()) {
20
20
if (! is_list(x )) {
21
21
cli :: cli_abort(" {.arg x} must be a list." , call = error_call )
22
22
}
23
23
if (length(x ) > 0 && ! is_named(x )) {
24
24
cli :: cli_abort(" All elements of {.arg x} must be named." , call = error_call )
25
25
}
26
26
27
- structure(x , class = " httr2_headers" )
27
+ structure(x , redact = redact , class = " httr2_headers" )
28
28
}
29
29
30
30
# ' @export
31
31
print.httr2_headers <- function (x , ... , redact = TRUE ) {
32
- cli :: cli_text(" {.cls {class(x)}}" )
32
+ cli :: cat_line(cli :: format_inline(" {.cls {class(x)}}" ))
33
+ show_headers(x , redact = redact )
34
+ invisible (x )
35
+ }
36
+
37
+ show_headers <- function (x , redact = TRUE ) {
33
38
if (length(x ) > 0 ) {
34
- cli :: cat_line(cli :: style_bold(names(x )), " : " , headers_redact(x , redact ))
39
+ vals <- lapply(headers_redact(x , redact ), format )
40
+ cli :: cat_line(cli :: style_bold(names(x )), " : " , vals )
35
41
}
36
- invisible (x )
37
42
}
38
43
39
- headers_redact <- function (x , redact = TRUE , to_redact = NULL ) {
44
+ # ' @export
45
+ str.httr2_headers <- function (object , ... , no.list = FALSE ) {
46
+ object <- unclass(headers_redact(object ))
47
+ cat(" <httr2_headers>\n " )
48
+ utils :: str(object , ... , no.list = TRUE )
49
+ }
50
+
51
+ headers_redact <- function (x , redact = TRUE ) {
40
52
if (! redact ) {
41
53
x
42
54
} else {
43
- to_redact <- union( attr(x , " redact" ), to_redact )
55
+ to_redact <- attr(x , " redact" )
44
56
attr(x , " redact" ) <- NULL
45
57
46
58
list_redact(x , to_redact , case_sensitive = FALSE )
47
59
}
48
60
}
49
61
62
+ # https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.2
50
63
headers_flatten <- function (x ) {
51
- set_names(as.character(unlist(x , use.names = FALSE )), rep(names(x ), lengths(x )))
64
+ n <- lengths(x )
65
+ x [n > 1 ] <- lapply(x [n > 1 ], paste , collapse = " ," )
66
+ x
52
67
}
53
68
54
69
list_redact <- function (x , names , case_sensitive = TRUE ) {
@@ -57,15 +72,25 @@ list_redact <- function(x, names, case_sensitive = TRUE) {
57
72
} else {
58
73
i <- match(tolower(names ), tolower(names(x )))
59
74
}
60
- x [i ] <- redacted()
75
+ x [i ] <- list ( redacted() )
61
76
x
62
77
}
63
78
64
79
redacted <- function () {
80
+ structure(list (NULL ), class = " httr2_redacted" )
81
+ }
82
+
83
+ # ' @export
84
+ format.httr2_redacted <- function (x , ... ) {
65
85
cli :: col_grey(" <REDACTED>" )
66
86
}
87
+ # ' @export
88
+ str.httr2_redacted <- function (object , ... ) {
89
+ cat(" " , cli :: col_grey(" <REDACTED>" ), " \n " , sep = " " )
90
+ }
91
+
67
92
is_redacted <- function (x ) {
68
- is.character( x ) && length( x ) == 1 && x == redacted( )
93
+ inherits( x , " httr2_redacted " )
69
94
}
70
95
71
96
0 commit comments