Skip to content

Commit

Permalink
fix(dav): use of webdav library
Browse files Browse the repository at this point in the history
nextcloud/server#41202

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
  • Loading branch information
pulsejet committed Oct 31, 2023
1 parent 5a568e2 commit 573f338
Showing 1 changed file with 29 additions and 15 deletions.
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 { NodeData } from '../files/nodeData'
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 @@ export const davRemoteURL = generateRemoteUrl('dav')
* @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

0 comments on commit 573f338

Please sign in to comment.