MCPcopy Create free account
hub / github.com/GDATASoftwareAG/vaas / forUrlAsync

Method forUrlAsync

php/src/vaas/Vaas.php:275–354  ·  view source on GitHub ↗

* Get the verdict for a URL * @param string $uri The URL to check * @param ForUrlOptions|null $options Options for the request * @param Cancellation|null $cancellation Cancellation token * @return Future A future that resolves to a VaasVerdict * @throws HttpException If the request fails * @throws VaasClientException The request is malformed or cannot be completed. Re

(string $uri, ?ForUrlOptions $options = null, ?Cancellation $cancellation = null)

Source from the content-addressed store, hash-verified

273 * @throws VaasServerException The server encountered an internal error. Recommended actions: You may retry the request after a certain delay. If the problem persists contact G DATA.
274 */
275 public function forUrlAsync(string $uri, ?ForUrlOptions $options = null, ?Cancellation $cancellation = null): Future
276 {
277 return async(function () use ($uri, $options, $cancellation) {
278 $this->logger->debug("Requesting verdict for URL: $uri");
279 $uri = Vaas::validUri($uri);
280
281 $options = $options ?? ForUrlOptions::fromVaasOptions($this->options);
282
283 $urlAnalysisUri = sprintf('%s/urls', $this->options->vaasUrl);
284
285 $urlAnalysisRequest = new Request($urlAnalysisUri, 'POST');
286 $urlAnalysisRequest->setBody(json_encode([
287 'url' => $uri,
288 'useHashLookup' => $options->useHashLookup,
289 ]));
290
291 $this->enrichRequestAsync($urlAnalysisRequest, $options->timeout, $options->vaasRequestId)->await($cancellation);
292 $urlAnalysisRequest->setHeader('Content-Type', 'application/json');
293 $this->logger->debug("Send request for url analysis: " . self::logUri($urlAnalysisRequest));
294 $urlAnalysisResponse = $this->httpClient->request($urlAnalysisRequest, $cancellation);
295
296 switch ($urlAnalysisResponse->getStatus()) {
297 case 201:
298 try {
299 $urlAnalysisStarted = json_decode($urlAnalysisResponse->getBody()->buffer($cancellation), true);
300 $id = $urlAnalysisStarted['id'] ?? null;
301 } catch (Exception $ex) {
302 $this->logger->error("Error parsing response for URL: $uri");
303 throw new VaasServerException('Error parsing response from VaaS backend for URL scan', $ex->getCode(), $ex);
304 }
305 $this->logger->debug("URL analysis for " . $uri . " started with id: " . $id);
306 break;
307 case 400:
308 $this->logger->error("Bad request for URL: $uri");
309 throw new VaasClientException('Bad request.');
310 case 401:
311 $this->logger->error("Unauthorized request for URL: $uri");
312 throw new VaasAuthenticationException('Unauthorized. Check your credentials.');
313 case 403:
314 $this->logger->error("Forbidden request for URL: $uri");
315 throw new VaasClientException('Forbidden. You are not allowed to use this endpoint.');
316 default:
317 $this->logger->error("Error requesting verdict for URL: $uri");
318 throw $this->parseVaasError($urlAnalysisResponse);
319 }
320
321 if ($id === null) {
322 $this->logger->error("Unexpected response from the server for URL: $uri");
323 throw new VaasServerException('Unexpected response from the server');
324 }
325
326 while (true) {
327 $reportUri = sprintf('%s/urls/%s/report', $this->options->vaasUrl, $id);
328 $reportRequest = new Request($reportUri, 'GET');
329
330 $this->enrichRequestAsync($reportRequest, $options->timeout, $options->vaasRequestId)->await($cancellation);
331 $reportResponse = $this->httpClient->request($reportRequest, $cancellation);
332

Callers

nothing calls this directly

Calls 7

enrichRequestAsyncMethod · 0.95
logUriMethod · 0.95
parseVaasErrorMethod · 0.95
validUriMethod · 0.80
getStatusMethod · 0.80
fromVaasOptionsMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected