@@ -37,6 +37,11 @@ request = request.defaults({
37
37
**/
38
38
39
39
function http ( url , opt ) {
40
+ if ( opt ?. headers ) {
41
+ opt . headers = Object . fromEntries (
42
+ Object . entries ( opt . headers ) . map ( ( [ k , v ] ) => [ k . toLowerCase ( ) , v ] )
43
+ )
44
+ }
40
45
if ( opt && ! opt . jar && Object . keys ( opt ) . indexOf ( 'jar' ) > - 1 ) {
41
46
opt . jar = options . jar
42
47
}
@@ -82,26 +87,24 @@ exports.func = function (args) {
82
87
return follow
83
88
}
84
89
return http ( args . url , opt ) . then ( function ( res ) {
85
- if ( opt && opt . headers && opt . headers [ 'X-CSRF-TOKEN' ] ) {
86
- if ( res . statusCode === 403 && res . headers [ 'X-CSRF-TOKEN' ] ) {
87
- if ( depth >= 2 ) {
88
- throw new Error ( 'Tried ' + ( depth + 1 ) + ' times and could not refresh XCSRF token successfully' )
89
- }
90
+ if ( res . statusCode === 403 && res . headers [ 'x-csrf-token' ] && Object . hasOwn ( opt . headers ?? { } , 'x-csrf-token' ) ) {
91
+ if ( depth >= 2 ) {
92
+ throw new Error ( 'Tried ' + ( depth + 1 ) + ' times and could not refresh XCSRF token successfully' )
93
+ }
90
94
91
- const token = res . headers [ 'x-csrf-token' ]
95
+ const token = res . headers [ 'x-csrf-token' ]
92
96
93
- if ( token ) {
94
- opt . headers [ 'X-CSRF-TOKEN' ] = token
95
- opt . jar = jar
96
- args . depth = depth + 1
97
- return exports . func ( args )
98
- } else {
99
- throw new Error ( 'Could not refresh X-CSRF-TOKEN' )
100
- }
97
+ if ( token ) {
98
+ opt . headers [ 'x-csrf-token' ] = token
99
+ opt . jar = jar
100
+ args . depth = depth + 1
101
+ return exports . func ( args )
101
102
} else {
102
- if ( depth > 0 ) {
103
- cache . add ( options . cache , 'XCSRF' , getHash ( { jar } ) , opt . headers [ 'X-CSRF-TOKEN' ] )
104
- }
103
+ throw new Error ( 'Could not refresh X-CSRF-TOKEN' )
104
+ }
105
+ } else {
106
+ if ( depth > 0 ) {
107
+ cache . add ( options . cache , 'XCSRF' , getHash ( { jar } ) , opt . headers [ 'x-csrf-token' ] )
105
108
}
106
109
}
107
110
if ( res . statusCode === 302 && ! args . ignoreLoginError && res . headers . location && ( res . headers . location . startsWith ( 'https://www.roblox.com/newlogin' ) || res . headers . location . startsWith ( '/Login/Default.aspx' ) ) ) {
0 commit comments