3333use Sabre \Xml \LibXMLException ;
3434use Sabre \Xml \Reader ;
3535use GuzzleHttp \Pool ;
36+ use Symfony \Component \HttpFoundation \Response ;
3637
3738/**
3839 * Helper for HTTP requests
@@ -74,7 +75,6 @@ public static function numRetriesOnHttpTooEarly(): int {
7475 * than download it all up-front.
7576 * @param int|null $timeout
7677 * @param Client|null $client
77- * @param string|null $bearerToken
7878 *
7979 * @return ResponseInterface
8080 * @throws GuzzleException
@@ -92,8 +92,42 @@ public static function sendRequestOnce(
9292 bool $ stream = false ,
9393 ?int $ timeout = 0 ,
9494 ?Client $ client = null ,
95- ?string $ bearerToken = null
9695 ): ResponseInterface {
96+ $ bearerToken = null ;
97+ if (TokenHelper::useBearerToken () && $ user && $ user !== 'public ' ) {
98+ $ bearerToken = TokenHelper::getTokens ($ user , $ password , $ url )['access_token ' ];
99+ // check token is still valid
100+ $ parsedUrl = parse_url ($ url );
101+ $ baseUrl = $ parsedUrl ['scheme ' ] . ':// ' . $ parsedUrl ['host ' ];
102+ $ baseUrl .= isset ($ parsedUrl ['port ' ]) ? ': ' . $ parsedUrl ['port ' ] : '' ;
103+ $ testUrl = $ baseUrl . "/graph/v1.0/use/ $ user " ;
104+ if (OcHelper::isTestingOnReva ()) {
105+ $ url = $ baseUrl . "/ocs/v2.php/cloud/users/ $ user " ;
106+ }
107+ // check token validity with a GET request
108+ $ c = self ::createClient (
109+ $ user ,
110+ $ password ,
111+ $ config ,
112+ $ cookies ,
113+ $ stream ,
114+ $ timeout ,
115+ $ bearerToken
116+ );
117+ $ testReq = self ::createRequest ($ testUrl , $ xRequestId , 'GET ' );
118+ try {
119+ $ testRes = $ c ->send ($ testReq );
120+ } catch (RequestException $ ex ) {
121+ $ testRes = $ ex ->getResponse ();
122+ if ($ testRes && $ testRes ->getStatusCode () === Response::HTTP_UNAUTHORIZED ) {
123+ // token is invalid or expired, get a new one
124+ echo "[INFO] Bearer token expired or invalid, getting a new one... \n" ;
125+ TokenHelper::clearAllTokens ();
126+ $ bearerToken = TokenHelper::getTokens ($ user , $ password , $ url )['access_token ' ];
127+ }
128+ }
129+ }
130+
97131 if ($ client === null ) {
98132 $ client = self ::createClient (
99133 $ user ,
@@ -160,6 +194,24 @@ public static function sendRequestOnce(
160194 }
161195
162196 HttpLogger::logResponse ($ response );
197+
198+ // wait for post-processing to finish if applicable
199+ if (WebdavHelper::isDAVRequest ($ url )
200+ && \str_starts_with ($ url , OcHelper::getServerUrl ())
201+ && \in_array ($ method , ["PUT " , "MOVE " , "COPY " ])
202+ && \in_array ($ response ->getStatusCode (), [Response::HTTP_CREATED , Response::HTTP_NO_CONTENT ])
203+ && OcConfigHelper::getPostProcessingDelay () === 0
204+ ) {
205+ if (\in_array ($ method , ["MOVE " , "COPY " ])) {
206+ $ url = $ headers ['Destination ' ];
207+ }
208+ WebDavHelper::waitForPostProcessingToFinish (
209+ $ url ,
210+ $ user ,
211+ $ password ,
212+ $ headers ,
213+ );
214+ }
163215 return $ response ;
164216 }
165217
@@ -203,13 +255,6 @@ public static function sendRequest(
203255 } else {
204256 $ debugResponses = false ;
205257 }
206- // use basic auth for 'public' user or no user
207- if ($ user === 'public ' || $ user === null || $ user === '' ) {
208- $ bearerToken = null ;
209- } else {
210- $ useBearerToken = TokenHelper::useBearerToken ();
211- $ bearerToken = $ useBearerToken ? TokenHelper::getTokens ($ user , $ password , $ url )['access_token ' ] : null ;
212- }
213258
214259 $ sendRetryLimit = self ::numRetriesOnHttpTooEarly ();
215260 $ sendCount = 0 ;
@@ -228,7 +273,6 @@ public static function sendRequest(
228273 $ stream ,
229274 $ timeout ,
230275 $ client ,
231- $ bearerToken ,
232276 );
233277
234278 if ($ response ->getStatusCode () >= 400
@@ -256,7 +300,8 @@ public static function sendRequest(
256300 // we need to repeat the send request, because we got HTTP_TOO_EARLY or HTTP_CONFLICT
257301 // wait 1 second before sending again, to give the server some time
258302 // to finish whatever post-processing it might be doing.
259- self ::debugResponse ($ response );
303+ echo "[INFO] Received ' " . $ response ->getStatusCode () .
304+ "' status code, retrying request ( $ sendCount)... \n" ;
260305 \sleep (1 );
261306 }
262307 } while ($ loopAgain );
0 commit comments