-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
request.ts
175 lines (169 loc) · 5.73 KB
/
request.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { Agent as HTTPAgent } from "http";
import { Agent as HTTPSAgent } from "https";
import { fetch } from "@buttercup/fetch";
import type { RequestInit as RequestInitNF } from "node-fetch";
import { getPatcher } from "./compat/patcher.js";
import { isReactNative, isWeb } from "./compat/env.js";
import {
generateDigestAuthHeader,
parseDigestAuth,
responseIndicatesDigestAuth
} from "./auth/digest.js";
import { cloneShallow, merge } from "./tools/merge.js";
import { mergeHeaders } from "./tools/headers.js";
import { requestDataToFetchBody } from "./tools/body.js";
import {
Headers,
RequestOptionsCustom,
RequestOptionsWithState,
RequestOptions,
Response,
WebDAVClientContext,
WebDAVMethodOptions,
AuthType
} from "./types.js";
import { setupAuth } from "./auth/index.js";
function getFetchOptions(requestOptions: RequestOptions): RequestInit | RequestInitNF {
let headers: Headers = {};
// Handle standard options
const opts: RequestInit | RequestInitNF = {
method: requestOptions.method
};
if (requestOptions.headers) {
headers = mergeHeaders(headers, requestOptions.headers);
}
if (typeof requestOptions.data !== "undefined") {
const [body, newHeaders] = requestDataToFetchBody(requestOptions.data);
opts.body = body;
headers = mergeHeaders(headers, newHeaders);
}
if (requestOptions.signal) {
opts.signal = requestOptions.signal;
}
if (requestOptions.withCredentials) {
(opts as RequestInit).credentials = "include";
}
// Check for node-specific options
if (!isWeb() && !isReactNative()) {
if (requestOptions.httpAgent || requestOptions.httpsAgent) {
(opts as RequestInitNF).agent = (parsedURL: URL) => {
if (parsedURL.protocol === "http:") {
return requestOptions.httpAgent || new HTTPAgent();
}
return requestOptions.httpsAgent || new HTTPSAgent();
};
}
}
// Attach headers
opts.headers = headers;
return opts;
}
export function prepareRequestOptions(
requestOptions: RequestOptionsCustom | RequestOptionsWithState,
context: WebDAVClientContext,
userOptions: WebDAVMethodOptions
): RequestOptionsWithState {
const finalOptions = cloneShallow(requestOptions) as RequestOptionsWithState;
finalOptions.headers = mergeHeaders(
context.headers,
finalOptions.headers || {},
userOptions.headers || {}
);
if (typeof userOptions.data !== "undefined") {
finalOptions.data = userOptions.data;
}
if (userOptions.signal) {
finalOptions.signal = userOptions.signal;
}
if (context.httpAgent) {
finalOptions.httpAgent = context.httpAgent;
}
if (context.httpsAgent) {
finalOptions.httpsAgent = context.httpsAgent;
}
if (context.digest) {
finalOptions._digest = context.digest;
}
if (typeof context.withCredentials === "boolean") {
finalOptions.withCredentials = context.withCredentials;
}
return finalOptions;
}
export async function request(
requestOptions: RequestOptionsWithState,
context: WebDAVClientContext
): Promise<Response> {
if (context.authType === AuthType.Auto) {
return requestAuto(requestOptions, context);
}
if (requestOptions._digest) {
return requestDigest(requestOptions);
}
return requestStandard(requestOptions);
}
async function requestAuto(
requestOptions: RequestOptionsWithState,
context: WebDAVClientContext
): Promise<Response> {
const response = await requestStandard(requestOptions);
if (response.ok) {
context.authType = AuthType.Password;
return response;
}
if (response.status == 401 && responseIndicatesDigestAuth(response)) {
context.authType = AuthType.Digest;
setupAuth(context, context.username, context.password, undefined, undefined);
requestOptions._digest = context.digest;
return requestDigest(requestOptions);
}
return response;
}
async function requestDigest(requestOptions: RequestOptionsWithState): Promise<Response> {
// Remove client's digest authentication object from request options
const _digest = requestOptions._digest;
delete requestOptions._digest;
// If client is already using digest authentication, include the digest authorization header
if (_digest.hasDigestAuth) {
requestOptions = merge(requestOptions, {
headers: {
Authorization: generateDigestAuthHeader(requestOptions, _digest)
}
});
}
// Perform digest request + check
const response = await requestStandard(requestOptions);
if (response.status == 401) {
_digest.hasDigestAuth = parseDigestAuth(response, _digest);
if (_digest.hasDigestAuth) {
requestOptions = merge(requestOptions, {
headers: {
Authorization: generateDigestAuthHeader(requestOptions, _digest)
}
});
const response2 = await requestStandard(requestOptions);
if (response2.status == 401) {
_digest.hasDigestAuth = false;
} else {
_digest.nc++;
}
return response2;
}
} else {
_digest.nc++;
}
return response;
}
function requestStandard(requestOptions: RequestOptions): Promise<Response> {
const patcher = getPatcher();
return patcher.patchInline(
"request",
(options: RequestOptions) =>
patcher.patchInline(
"fetch",
fetch,
options.url,
getFetchOptions(options) as RequestInit
),
requestOptions
);
}