Skip to content

Commit 54555f8

Browse files
Merge branch 'php/http_api' into phpdev-async-http
2 parents 10b4af8 + 319d026 commit 54555f8

File tree

6 files changed

+60
-46
lines changed

6 files changed

+60
-46
lines changed

php/examples/VaasExample/AuthenticationExamples.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
->build();
3232

3333
// Get verdict for an eicar hash
34-
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8"))->await();
34+
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8")->await())->await();
3535
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is $vaasVerdict->verdict->value \n");

php/examples/VaasExample/GetVerdictByHash.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121

2222

2323
// Malicious hash
24-
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8"))->await();
24+
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("000005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8")->await())->await();
2525
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");
2626

2727

2828
// Some file
29-
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("70caea443deb0d0a890468f9ac0a9b1187676ba3e66eb60a722b187107eb1ea8"))->await();
29+
$vaasVerdict = $vaas->forSha256Async(Sha256::TryFromString("70caea443deb0d0a890468f9ac0a9b1187676ba3e66eb60a722b187107eb1ea8")->await())->await();
3030
fwrite(STDOUT, "Verdict for $vaasVerdict->sha256 is " . $vaasVerdict->verdict->value . " \n");

php/src/vaas/Sha256.php

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace VaasSdk;
44

5+
use Amp\Future;
56
use VaasSdk\Exceptions\FileDoesNotExistException;
67
use VaasSdk\Exceptions\InvalidSha256Exception;
8+
use function Amp\async;
79

810
class Sha256
911
{
@@ -12,56 +14,63 @@ class Sha256
1214
/**
1315
* Gets Sha256 from file
1416
* @param string $path the path of the file to hash
15-
* @return Sha256 the sha256 hash
17+
* @return Future that resolves as the sha256 object
1618
* @throws FileDoesNotExistException if the file does not exist
1719
* @throws InvalidSha256Exception if the hash is invalid
1820
*/
19-
public static function TryFromFile(string $path): Sha256
21+
public static function TryFromFile(string $path): Future
2022
{
21-
if (!file_exists($path)) {
22-
throw new FileDoesNotExistException();
23-
}
23+
return async(function () use ($path) {
24+
if (!file_exists($path)) {
25+
throw new FileDoesNotExistException();
26+
}
2427

25-
$hashString = hash_file("sha256", $path);
28+
$hashString = hash_file("sha256", $path);
2629

27-
if (Sha256::IsValid($hashString)) {
28-
$sha256 = new Sha256();
29-
$sha256->_hash = $hashString;
30-
return $sha256;
31-
}
32-
33-
throw new InvalidSha256Exception();
30+
if (Sha256::IsValid($hashString)->await()) {
31+
$sha256 = new Sha256();
32+
$sha256->_hash = $hashString;
33+
return $sha256;
34+
}
35+
36+
throw new InvalidSha256Exception();
37+
});
3438
}
3539

3640
/**
3741
* Gets Sha256 from string
3842
* @param string $hashString the string to create the hash from
39-
* @return Sha256 the sha256 hash
43+
* @return Future that resolves to the sha256 object
4044
* @throws InvalidSha256Exception if the hash is invalid
4145
*/
42-
public static function TryFromString(string $hashString): Sha256
46+
public static function TryFromString(string $hashString): Future
4347
{
44-
if (Sha256::IsValid($hashString)) {
45-
$sha256 = new Sha256();
46-
$sha256->_hash = $hashString;
47-
return $sha256;
48-
}
49-
throw new InvalidSha256Exception();
48+
return async(function () use($hashString) {
49+
if (Sha256::IsValid($hashString)->await()) {
50+
$sha256 = new Sha256();
51+
$sha256->_hash = $hashString;
52+
return $sha256;
53+
}
54+
throw new InvalidSha256Exception();
55+
});
5056
}
5157

5258
/**
5359
* Validates a hash to be a valid sha256
5460
* @param string $hash the string to validate
55-
* @return bool returns true if sha256 is valid
61+
* @return Future that resolves as true if sha256 is valid
5662
*/
57-
public static function IsValid(string $hash): bool
63+
public static function IsValid(string $hash): Future
5864
{
59-
if (preg_match("/^([a-f0-9]{64})$/", strtolower($hash)) == 1) {
60-
return true;
61-
} else {
62-
return false;
63-
}
65+
return async(function () use ($hash) {
66+
if (preg_match("/^([a-f0-9]{64})$/", strtolower($hash)) == 1) {
67+
return true;
68+
} else {
69+
return false;
70+
}
71+
});
6472
}
73+
6574

6675
public function __toString(): string
6776
{

php/src/vaas/Vaas.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Amp\Http\Client\Request;
1212
use Amp\Http\Client\Response;
1313
use Amp\Http\Client\StreamedContent;
14+
use Amp\Http\Client\TimeoutException;
1415
use Exception;
1516
use Psr\Log\LoggerInterface;
1617
use VaasSdk\Authentication\AuthenticatorInterface;
@@ -76,6 +77,7 @@ public function withLogger(LoggerInterface $logger): self
7677
* @param Cancellation|null $cancellation Cancellation token
7778
* @return Future A future that resolves to a VaasVerdict
7879
* @throws HttpException If the request fails
80+
* @throws TimeoutException If the request exceeds the timeout specified in the VaasOptions. Per default 300 seconds.
7981
* @throws VaasClientException The request is malformed or cannot be completed. Recommended actions: Don't repeat the request. Log. Analyze the error.
8082
* @throws VaasAuthenticationException The Vaas authentication failed. Recommended actions: Double-check your credentials in the authenticator object. Check if your authenticator connects to the correct token endpoint. Check if the token endpoint is reachable. If your problem persists contact G DATA.
8183
* @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.
@@ -132,6 +134,7 @@ public function forSha256Async(Sha256 $sha256, ?ForSha256Options $options = null
132134
* @param ForFileOptions|null $options Options for the request
133135
* @param Cancellation|null $cancellation Cancellation token
134136
* @return Future A future that resolves to a VaasVerdict
137+
* @throws TimeoutException If the request exceeds the timeout specified in the VaasOptions. Per default 300 seconds.
135138
* @throws VaasClientException The request is malformed or cannot be completed. Recommended actions: Don't repeat the request. Log. Analyze the error.
136139
* @throws VaasAuthenticationException The Vaas authentication failed. Recommended actions: Double-check your credentials in the authenticator object. Check if your authenticator connects to the correct token endpoint. Check if the token endpoint is reachable. If your problem persists contact G DATA.
137140
* @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.
@@ -151,7 +154,7 @@ public function forFileAsync(string $path, ?ForFileOptions $options = null, ?Can
151154
if ($options->useCache || $options->useHashLookup) {
152155
$forSha256Options = new ForSha256Options(
153156
$options->useCache, $options->useHashLookup, $options->vaasRequestId);
154-
$sha256 = Sha256::TryFromFile($path);
157+
$sha256 = Sha256::TryFromFile($path)->await();
155158
$this->logger->debug("Check if file $path is already known by its SHA256: $sha256");
156159
try {
157160
$response = $this->forSha256Async($sha256, $forSha256Options, $cancellation)->await();
@@ -194,6 +197,7 @@ public function forFileAsync(string $path, ?ForFileOptions $options = null, ?Can
194197
* @param Cancellation|null $cancellation Cancellation token
195198
* @return Future A future that resolves to a VaasVerdict
196199
* @throws HttpException If the request fails
200+
* @throws TimeoutException If the request exceeds the timeout specified in the VaasOptions. Per default 300 seconds.
197201
* @throws VaasClientException The request is malformed or cannot be completed. Recommended actions: Don't repeat the request. Log. Analyze the error.
198202
* @throws VaasAuthenticationException The Vaas authentication failed. Recommended actions: Double-check your credentials in the authenticator object. Check if your authenticator connects to the correct token endpoint. Check if the token endpoint is reachable. If your problem persists contact G DATA.
199203
* @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.
@@ -252,7 +256,7 @@ public function forStreamAsync(ReadableStream $stream, int $fileSize, ?ForStream
252256
$this->logger->error("Unexpected response from the server for stream");
253257
throw new VaasServerException('Unexpected response from the server');
254258
}
255-
$sha256 = Sha256::TryFromString($fileAnalysisStarted['sha256']);
259+
$sha256 = Sha256::TryFromString($fileAnalysisStarted['sha256'])->await();
256260

257261
$this->logger->debug("Requesting verdict for uploaded file with SHA256: $sha256");
258262
return $this->forSha256Async($sha256, $forSha256Options)->await();
@@ -266,6 +270,7 @@ public function forStreamAsync(ReadableStream $stream, int $fileSize, ?ForStream
266270
* @param Cancellation|null $cancellation Cancellation token
267271
* @return Future A future that resolves to a VaasVerdict
268272
* @throws HttpException If the request fails
273+
* @throws TimeoutException If the request exceeds the timeout specified in the VaasOptions. Per default 300 seconds.
269274
* @throws VaasClientException The request is malformed or cannot be completed. Recommended actions: Don't repeat the request. Log. Analyze the error.
270275
* @throws VaasAuthenticationException The Vaas authentication failed. Recommended actions: Double-check your credentials in the authenticator object. Check if your authenticator connects to the correct token endpoint. Check if the token endpoint is reachable. If your problem persists contact G DATA.
271276
* @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.
@@ -422,7 +427,7 @@ private static function logUri(Request $request): string
422427
* @return string The validated URI
423428
* @throws VaasClientException If the URI is invalid
424429
*/
425-
private static function validUri(string $uri): string
430+
public static function validUri(string $uri): string
426431
{
427432
if (!filter_var($uri, FILTER_VALIDATE_URL)) {
428433
throw new VaasClientException('Invalid URI');

php/tests/VaasTesting/Sha256Test.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ final class Sha256Test extends TestCase
1414

1515
public function testIsValidGetsValidSha256ReturnsTrue(): void
1616
{
17-
$this->assertTrue(Sha256::IsValid(Sha256Test::VALID_SHA256));
17+
$this->assertTrue(Sha256::IsValid(Sha256Test::VALID_SHA256)->await());
1818
}
1919

2020
public function testIsValidGetsInvalidSha256ReturnsFalse(): void
2121
{
22-
$this->assertFalse(Sha256::IsValid("00005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8"));
22+
$this->assertFalse(Sha256::IsValid("00005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8")->await());
2323
}
2424

2525
/**
2626
* @throws InvalidSha256Exception
2727
*/
2828
public function testTryFromStringGetsValidSha256ReturnsCorrectSha256(): void
2929
{
30-
$calculatedSha256 = Sha256::TryFromString(Sha256Test::VALID_SHA256);
30+
$calculatedSha256 = Sha256::TryFromString(Sha256Test::VALID_SHA256)->await();
3131
$this->assertEquals(Sha256Test::VALID_SHA256, $calculatedSha256);
3232
}
3333

3434
public function testTryFromStringGetsInvalidSha256ThrowsInvalidSha256Exception(): void
3535
{
3636
$this->expectException(InvalidSha256Exception::class);
37-
Sha256::TryFromString("00005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8");
37+
Sha256::TryFromString("00005c43196142f01d615a67b7da8a53cb0172f8e9317a2ec9a0a39a1da6fe8")->await();
3838
}
3939

4040
/**
@@ -43,7 +43,7 @@ public function testTryFromStringGetsInvalidSha256ThrowsInvalidSha256Exception()
4343
*/
4444
public function testTryFromFileGetsValidSha256ReturnsTrue(): void
4545
{
46-
$calculatedSha256 = Sha256::TryFromFile(Sha256Test::VALID_FILE);
46+
$calculatedSha256 = Sha256::TryFromFile(Sha256Test::VALID_FILE)->await();
4747
$this->assertEquals(Sha256Test::VALID_SHA256, $calculatedSha256);
4848
}
4949

@@ -53,6 +53,6 @@ public function testTryFromFileGetsValidSha256ReturnsTrue(): void
5353
public function testTryFromFileGetsInvalidPathThrowsFileDoesNotExistException(): void
5454
{
5555
$this->expectException(FileDoesNotExistException::class);
56-
Sha256::TryFromFile("./filedoesnotexist");
56+
Sha256::TryFromFile("./filedoesnotexist")->await();
5757
}
5858
}

php/tests/VaasTesting/VaasTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function setUp(): void
6363
$this->vaas = $this->getVaas();
6464
}
6565

66-
private function getVaas(bool $useCache = false, bool $useHashLookup = true): Vaas
66+
private function getVaas(bool $useCache = true, bool $useHashLookup = true): Vaas
6767
{
6868
$options = new VaasOptions(
6969
useHashLookup: $useHashLookup,
@@ -86,7 +86,7 @@ private function getVaas(bool $useCache = false, bool $useHashLookup = true): Va
8686

8787
public function testForSha256_WithMaliciousSha256_GetsMaliciousResponse(): void
8888
{
89-
$verdict = $this->vaas->forSha256Async(Sha256::TryFromString(self::MALICIOUS_HASH))->await();
89+
$verdict = $this->vaas->forSha256Async(Sha256::TryFromString(self::MALICIOUS_HASH)->await())->await();
9090

9191
$this->logger->info('Test for malicious SHA256', [
9292
'expected' => Verdict::MALICIOUS,
@@ -100,7 +100,7 @@ public function testForSha256_WithMaliciousSha256_GetsMaliciousResponse(): void
100100

101101
public function testForSha256_WithPupSha256_GetsPupResponse(): void
102102
{
103-
$verdict = $this->vaas->forSha256Async(Sha256::TryFromString(self::PUP_HASH))->await();
103+
$verdict = $this->vaas->forSha256Async(Sha256::TryFromString(self::PUP_HASH)->await())->await();
104104

105105
$this->logger->info('Test for PUP SHA256', [
106106
'expected' => Verdict::PUP,
@@ -114,7 +114,7 @@ public function testForSha256_WithPupSha256_GetsPupResponse(): void
114114

115115
public function testForSha256_WithCleanSha256_GetsCleanResponse(): void
116116
{
117-
$verdict = $this->vaas->forSha256Async(Sha256::TryFromString(self::CLEAN_HASH))->await();
117+
$verdict = $this->vaas->forSha256Async(Sha256::TryFromString(self::CLEAN_HASH)->await())->await();
118118

119119
$this->logger->info('Test for clean SHA256', [
120120
'expected' => Verdict::CLEAN,
@@ -130,7 +130,7 @@ public function testForSha256_WithUnknownSha256_GetsUnknownResponse(): void
130130
{
131131
$vaas = $this->getVaas(false, false);
132132

133-
$verdict = $vaas->forSha256Async(Sha256::TryFromString(self::UNKNOWN_HASH))->await();
133+
$verdict = $vaas->forSha256Async(Sha256::TryFromString(self::UNKNOWN_HASH)->await())->await();
134134

135135
$this->logger->info('Test for unknown SHA256', [
136136
'expected' => Verdict::UNKNOWN,
@@ -148,7 +148,7 @@ public function testForSha256_WithInvalidSha256_ThrowsInvalidSha256Exception():
148148
$this->expectExceptionMessage("Invalid SHA256 hash");
149149

150150
try {
151-
$this->vaas->forSha256Async(Sha256::TryFromString("invalid"))->await();
151+
$this->vaas->forSha256Async(Sha256::TryFromString("invalid")->await())->await();
152152
} catch (InvalidSha256Exception $e) {
153153
$this->logger->error('Test for invalid SHA256', [
154154
'exception' => $e->getMessage()

0 commit comments

Comments
 (0)