fix a mysterious bug for webdav
This commit is contained in:
parent
fd095a9093
commit
257c995090
@ -1,5 +1,5 @@
|
|||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import { Vault, requestUrl } from "obsidian";
|
import { Platform, Vault, requestUrl } from "obsidian";
|
||||||
|
|
||||||
import { Queue } from "@fyears/tsqueue";
|
import { Queue } from "@fyears/tsqueue";
|
||||||
import chunk from "lodash/chunk";
|
import chunk from "lodash/chunk";
|
||||||
@ -47,34 +47,47 @@ if (VALID_REQURL) {
|
|||||||
delete transformedHeaders["host"];
|
delete transformedHeaders["host"];
|
||||||
delete transformedHeaders["content-length"];
|
delete transformedHeaders["content-length"];
|
||||||
|
|
||||||
|
const reqContentType =
|
||||||
|
transformedHeaders["accept"] ?? transformedHeaders["content-type"];
|
||||||
|
|
||||||
console.debug(`before request:`);
|
console.debug(`before request:`);
|
||||||
console.debug(`url: ${options.url}`);
|
console.debug(`url: ${options.url}`);
|
||||||
console.debug(`method: ${options.method}`);
|
console.debug(`method: ${options.method}`);
|
||||||
console.debug(`headers: ${JSON.stringify(transformedHeaders, null, 2)}`);
|
console.debug(`headers: ${JSON.stringify(transformedHeaders, null, 2)}`);
|
||||||
|
console.debug(`reqContentType: ${reqContentType}`);
|
||||||
|
|
||||||
const r = await requestUrl({
|
let r = await requestUrl({
|
||||||
url: options.url,
|
url: options.url,
|
||||||
method: options.method,
|
method: options.method,
|
||||||
body: options.data as string | ArrayBuffer,
|
body: options.data as string | ArrayBuffer,
|
||||||
headers: transformedHeaders,
|
headers: transformedHeaders,
|
||||||
|
contentType: reqContentType,
|
||||||
throw: false,
|
throw: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
let contentType: string | undefined =
|
if (
|
||||||
r.headers["Content-Type"] || r.headers["content-type"];
|
r.status === 401 &&
|
||||||
if (options.headers !== undefined) {
|
Platform.isIosApp &&
|
||||||
contentType =
|
!options.url.endsWith("/") &&
|
||||||
contentType ||
|
!options.url.endsWith(".md") &&
|
||||||
transformedHeaders["content-type"] ||
|
options.method.toUpperCase() === "PROPFIND"
|
||||||
transformedHeaders["accept"];
|
) {
|
||||||
}
|
// don't ask me why,
|
||||||
if (contentType !== undefined) {
|
// some webdav servers have some mysterious behaviours,
|
||||||
contentType = contentType.toLowerCase();
|
// if a folder doesn't exist without slash, the servers return 401 instead of 404
|
||||||
|
// here is a dirty hack that works
|
||||||
|
console.debug(`so we have 401, try appending request url with slash`);
|
||||||
|
r = await requestUrl({
|
||||||
|
url: `${options.url}/`,
|
||||||
|
method: options.method,
|
||||||
|
body: options.data as string | ArrayBuffer,
|
||||||
|
headers: transformedHeaders,
|
||||||
|
contentType: reqContentType,
|
||||||
|
throw: false,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
console.debug(`after request:`);
|
console.debug(`after request:`);
|
||||||
console.debug(`contentType: ${contentType}`);
|
|
||||||
|
|
||||||
const rspHeaders = objKeyToLower({ ...r.headers });
|
const rspHeaders = objKeyToLower({ ...r.headers });
|
||||||
console.debug(`rspHeaders: ${JSON.stringify(rspHeaders, null, 2)}`);
|
console.debug(`rspHeaders: ${JSON.stringify(rspHeaders, null, 2)}`);
|
||||||
for (let key in rspHeaders) {
|
for (let key in rspHeaders) {
|
||||||
@ -98,55 +111,6 @@ if (VALID_REQURL) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// console.info(`requesting url=${options.url}`);
|
|
||||||
// console.info(`contentType=${contentType}`);
|
|
||||||
// console.info(`rspHeaders=${JSON.stringify(rspHeaders)}`)
|
|
||||||
|
|
||||||
// let r2: Response = undefined;
|
|
||||||
// if (contentType.includes("xml")) {
|
|
||||||
// r2 = new Response(r.text, {
|
|
||||||
// status: r.status,
|
|
||||||
// statusText: getReasonPhrase(r.status),
|
|
||||||
// headers: rspHeaders,
|
|
||||||
// });
|
|
||||||
// } else if (
|
|
||||||
// contentType.includes("json") ||
|
|
||||||
// contentType.includes("javascript")
|
|
||||||
// ) {
|
|
||||||
// console.info('inside json branch');
|
|
||||||
// // const j = r.json;
|
|
||||||
// // console.info(j);
|
|
||||||
// r2 = new Response(
|
|
||||||
// r.text, // yea, here is the text because Response constructor expects a text
|
|
||||||
// {
|
|
||||||
// status: r.status,
|
|
||||||
// statusText: getReasonPhrase(r.status),
|
|
||||||
// headers: rspHeaders,
|
|
||||||
// });
|
|
||||||
// } else if (contentType.includes("text")) {
|
|
||||||
// // avoid text/json,
|
|
||||||
// // so we split this out from the above xml or json branch
|
|
||||||
// r2 = new Response(r.text, {
|
|
||||||
// status: r.status,
|
|
||||||
// statusText: getReasonPhrase(r.status),
|
|
||||||
// headers: rspHeaders,
|
|
||||||
// });
|
|
||||||
// } else if (
|
|
||||||
// contentType.includes("octet-stream") ||
|
|
||||||
// contentType.includes("binary") ||
|
|
||||||
// contentType.includes("buffer")
|
|
||||||
// ) {
|
|
||||||
// // application/octet-stream
|
|
||||||
// r2 = new Response(r.arrayBuffer, {
|
|
||||||
// status: r.status,
|
|
||||||
// statusText: getReasonPhrase(r.status),
|
|
||||||
// headers: rspHeaders,
|
|
||||||
// });
|
|
||||||
// } else {
|
|
||||||
// throw Error(
|
|
||||||
// `do not know how to deal with requested content type = ${contentType}`
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
let r2: Response | undefined = undefined;
|
let r2: Response | undefined = undefined;
|
||||||
const statusText = getReasonPhrase(r.status);
|
const statusText = getReasonPhrase(r.status);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user