Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dav): use of webdav library #821

Merged
merged 1 commit into from Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 29 additions & 15 deletions lib/dav/dav.ts
Expand Up @@ -20,7 +20,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import type { DAVResultResponseProps, FileStat, Response, ResponseDataDetailed, WebDAVClient } from 'webdav'
import type { DAVResultResponseProps, FileStat, ResponseDataDetailed, WebDAVClient } from 'webdav'
import type { Node } from '../files/node'

import { File } from '../files/file'
Expand All @@ -29,10 +29,9 @@
import { davParsePermissions } from './davPermissions'
import { davGetFavoritesReport } from './davProperties'

import { getCurrentUser, getRequestToken } from '@nextcloud/auth'
import { getCurrentUser, getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth'
import { generateRemoteUrl } from '@nextcloud/router'
import { createClient, getPatcher, RequestOptions } from 'webdav'
import { request } from 'webdav/dist/node/request.js'
import { createClient, getPatcher } from 'webdav'

/**
* Nextcloud DAV result response
Expand All @@ -59,28 +58,43 @@
* @param remoteURL The DAV server remote URL
*/
export const davGetClient = function(remoteURL = davRemoteURL) {
const client = createClient(remoteURL, {
headers: {
requesttoken: getRequestToken() || '',
},
})
const client = createClient(remoteURL)

Check warning on line 61 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L61

Added line #L61 was not covered by tests

/**
* Set headers for DAV requests
* @param token CSRF token
*/
function setHeaders(token: string | null) {
client.setHeaders({

Check warning on line 68 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L68

Added line #L68 was not covered by tests
// Add this so the server knows it is an request from the browser
'X-Requested-With': 'XMLHttpRequest',
// Inject user auth
requesttoken: token ?? '',
})
}

// refresh headers when request token changes
onRequestTokenUpdate(setHeaders)
setHeaders(getRequestToken())

Check warning on line 78 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L77-L78

Added lines #L77 - L78 were not covered by tests

/**
* Allow to override the METHOD to support dav REPORT
*
* @see https://github.com/perry-mitchell/webdav-client/blob/8d9694613c978ce7404e26a401c39a41f125f87f/source/request.ts
*/
const patcher = getPatcher()
// https://github.com/perry-mitchell/hot-patcher/issues/6
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
patcher.patch('request', (options: RequestOptions): Promise<Response> => {
if (options.headers?.method) {
options.method = options.headers.method
delete options.headers.method
// https://github.com/perry-mitchell/hot-patcher/issues/6
patcher.patch('fetch', (url: string, options: RequestInit): Promise<Response> => {
const headers = options.headers as Record<string, string>

Check warning on line 90 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L89-L90

Added lines #L89 - L90 were not covered by tests
if (headers?.method) {
options.method = headers.method
delete headers.method

Check warning on line 93 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L92-L93

Added lines #L92 - L93 were not covered by tests
}
return request(options)
return fetch(url, options)

Check warning on line 95 in lib/dav/dav.ts

View check run for this annotation

Codecov / codecov/patch

lib/dav/dav.ts#L95

Added line #L95 was not covered by tests
})

return client
}

Expand Down