From 7da0e47a068dfe07550cdbeb7374d6f2a7921ca2 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Sat, 18 Mar 2023 20:15:41 +0100 Subject: [PATCH 1/5] refactor: use curl instead of file_get_contents --- src/Classes/Api/V1/HttpRequest.php | 144 ++++++++++++++++++++--------- 1 file changed, 99 insertions(+), 45 deletions(-) diff --git a/src/Classes/Api/V1/HttpRequest.php b/src/Classes/Api/V1/HttpRequest.php index 2d395041..22f6f95c 100644 --- a/src/Classes/Api/V1/HttpRequest.php +++ b/src/Classes/Api/V1/HttpRequest.php @@ -13,6 +13,7 @@ namespace RobinTheHood\ModifiedModuleLoaderClient\Api\V1; +use Exception; use RobinTheHood\ModifiedModuleLoaderClient\App; class HttpRequest @@ -28,60 +29,57 @@ public function isServerAvailable(string $url): bool return false; } - public function sendPostRequest(string $url, $data) + public function sendPostRequest(string $url, $data): string { - // http verwenden, auch wenn die Url mit https://... beginnt - $options = [ - 'http' => [ - 'user_agent' => 'Modified Module Loader Client', - 'method' => 'POST', - 'header' => implode("\r\n", [ - 'Content-type: application/x-www-form-urlencoded;' - ]), - 'content' => http_build_query($data) - ] - ]; - $context = stream_context_create($options); - $result = @file_get_contents($url, false, $context); - - // Logging - if ($this->logging) { - $logFilepath = App::getLogsRoot() . '/log.txt'; - $logDirectory = dirname($logFilepath); - - if (!file_exists($logDirectory)) { - mkdir($logDirectory); + try { + // HTTP POST-Request konfigurieren und senden + $result = $this->sendCurlPostRequest($url, $data); + + // Logging + if ($this->logging) { + $logFilepath = App::getLogsRoot() . '/log.txt'; + $logDirectory = dirname($logFilepath); + + if (!file_exists($logDirectory)) { + mkdir($logDirectory); + } + + file_put_contents($logFilepath, $result); } - file_put_contents($logFilepath, $result); + return $result; + } catch (Exception $e) { + // Fehler behandeln + //error_log($e->getMessage()); + return ''; } - - return $result; } - public function sendGetRequest($url) + public function sendGetRequest(string $url): string { - // http verwenden, auch wenn die Url mit https://... beginnt - $options = [ - 'http' => [ - 'user_agent' => 'Modified Module Loader Client', - 'method' => "GET", - 'header' => implode("\r\n", [ - 'Content-type: text/plain;' - ]) - ] - ]; - - $context = stream_context_create($options); - $result = @file_get_contents($url, false, $context); - - // Logging - if ($this->logging) { - file_put_contents(App::getLogsRoot() . '/log.txt', $result); - } + try { + // HTTP GET-Request konfigurieren und senden + $result = $this->sendCurlGetRequest($url); - return $result; + // Logging + if ($this->logging) { + $logFilepath = App::getLogsRoot() . '/log.txt'; + $logDirectory = dirname($logFilepath); + + if (!file_exists($logDirectory)) { + mkdir($logDirectory); + } + + file_put_contents($logFilepath, $result); + } + + return $result; + } catch (Exception $e) { + // Fehler behandeln + //error_log($e->getMessage()); + return ''; + } } public static function createQuery(array $queryValues): string @@ -92,4 +90,60 @@ public static function createQuery(array $queryValues): string } return $query; } + + private function sendCurlGetRequest(string $url) + { + // HTTP GET-Request konfigurieren + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_USERAGENT, 'Modified Module Loader Client'); + + // Request senden und Ergebnis erhalten + $result = curl_exec($curl); + + // HTTP-Statuscode und Fehler prüfen + $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + if ($result === false) { + $error = curl_error($curl); + curl_close($curl); + throw new Exception('Fehler beim Senden des GET-Requests: ' . $error); + } elseif ($httpCode < 200 || $httpCode >= 300) { + curl_close($curl); + throw new Exception('Fehler beim Senden des GET-Requests: HTTP-Statuscode ' . $httpCode); + } + + // Request beenden + curl_close($curl); + + return $result; + } + + private function sendCurlPostRequest(string $url, $data) + { + // HTTP POST-Request konfigurieren + $curl = curl_init($url); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_USERAGENT, 'Modified Module Loader Client'); + + // Request senden und Ergebnis erhalten + $result = curl_exec($curl); + + // HTTP-Statuscode und Fehler prüfen + $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + if ($result === false) { + $error = curl_error($curl); + curl_close($curl); + throw new Exception('Fehler beim Senden des POST-Requests: ' . $error); + } elseif ($httpCode < 200 || $httpCode >= 300) { + curl_close($curl); + throw new Exception('Fehler beim Senden des POST-Requests: HTTP-Statuscode ' . $httpCode); + } + + // Request beenden + curl_close($curl); + + return $result; + } } From 495ff92a0ec1ab1afd7ea972000c75c426a4fe5a Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Sat, 18 Mar 2023 20:16:27 +0100 Subject: [PATCH 2/5] test: add test for HttpRequest --- tests/unit/HttpRequestTest.php | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/unit/HttpRequestTest.php diff --git a/tests/unit/HttpRequestTest.php b/tests/unit/HttpRequestTest.php new file mode 100644 index 00000000..c84d3bfd --- /dev/null +++ b/tests/unit/HttpRequestTest.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit; + +use PHPUnit\Framework\TestCase; +use RobinTheHood\ModifiedModuleLoaderClient\Api\V1\HttpRequest; + +class HttpRequestTest extends TestCase +{ + public function testSendPostRequestSuccess() + { + $httpClient = new HttpRequest(); + $response = $httpClient->sendPostRequest('https://postman-echo.com/post', ['foo' => 'bar']); + + $this->assertNotNull($response); + $this->assertIsString($response); + $this->assertStringContainsString('application/x-www-form-urlencoded', $response); + $this->assertStringContainsString('"foo": "bar"', $response); + } + + public function testSendPostRequestFailure() + { + $httpClient = new HttpRequest(); + $response = $httpClient->sendPostRequest('https://this-url-does-not-exist.com', []); + $this->assertIsString($response); + } +} From c6d21b08ee68cb7e05364c6a4ec93339696148ad Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Thu, 29 Jun 2023 22:19:54 +0200 Subject: [PATCH 3/5] test: improve test for HttpRequest --- tests/unit/HttpRequestTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/unit/HttpRequestTest.php b/tests/unit/HttpRequestTest.php index c84d3bfd..e0c9bb5a 100644 --- a/tests/unit/HttpRequestTest.php +++ b/tests/unit/HttpRequestTest.php @@ -31,6 +31,11 @@ public function testSendPostRequestSuccess() public function testSendPostRequestFailure() { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage( + 'Fehler beim Senden des POST-Requests: Could not resolve host: this-url-does-not-exist.com' + ); + $httpClient = new HttpRequest(); $response = $httpClient->sendPostRequest('https://this-url-does-not-exist.com', []); $this->assertIsString($response); From a792455d12dc993ca4eba1280b5354d9889b2f5e Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Thu, 29 Jun 2023 22:20:45 +0200 Subject: [PATCH 4/5] fix: type errors --- src/Classes/Api/V1/HttpRequest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Classes/Api/V1/HttpRequest.php b/src/Classes/Api/V1/HttpRequest.php index adfdc236..846bb438 100644 --- a/src/Classes/Api/V1/HttpRequest.php +++ b/src/Classes/Api/V1/HttpRequest.php @@ -86,7 +86,7 @@ private function sendCurlGetRequest(string $url): string StaticLogger::log(LogLevel::DEBUG, "$httpCode Response from $url\n" . print_r($result, true)); - return $result; + return (string) $result; } /** @@ -125,6 +125,6 @@ private function sendCurlPostRequest(string $url, $data): string // Request beenden curl_close($curl); - return $result; + return (string) $result; } } From 1da44945edd0d299cd087bfc505a38e1887f6a90 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Thu, 29 Jun 2023 22:27:50 +0200 Subject: [PATCH 5/5] refactor: add old file_get_contents method for tests --- src/Classes/Api/V1/HttpRequest.php | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/Classes/Api/V1/HttpRequest.php b/src/Classes/Api/V1/HttpRequest.php index 846bb438..a525a144 100644 --- a/src/Classes/Api/V1/HttpRequest.php +++ b/src/Classes/Api/V1/HttpRequest.php @@ -127,4 +127,60 @@ private function sendCurlPostRequest(string $url, $data): string return (string) $result; } + + private function sendFileGetContentsPostRequest(string $url, $data): string + { + // http verwenden, auch wenn die Url mit https://... beginnt + $options = [ + 'http' => [ + 'user_agent' => 'Modified Module Loader Client', + 'method' => 'POST', + 'header' => implode("\r\n", [ + 'Content-type: application/x-www-form-urlencoded;' + ]), + 'content' => http_build_query($data) + ] + ]; + $context = stream_context_create($options); + + StaticLogger::log( + LogLevel::DEBUG, + "Send POST request to $url\n[OPTIONS]\n" . print_r($options, true) . "[DATA]\n" . print_r($data, true) + ); + + $timeBeforeRequest = microtime(true); + $result = @file_get_contents($url, false, $context); + $time = microtime(true) - $timeBeforeRequest; + + StaticLogger::log(LogLevel::DEBUG, "Response from $url ($time sec)\n" . print_r($result, true)); + + return $result; + } + + private function sendFileGetContentsGetRequest(string $url): string + { + // http verwenden, auch wenn die Url mit https://... beginnt + $options = [ + 'http' => [ + 'user_agent' => 'Modified Module Loader Client', + 'method' => "GET", + 'header' => implode("\r\n", [ + 'Content-type: text/plain;' + ]) + ] + ]; + + $context = stream_context_create($options); + + StaticLogger::log( + LogLevel::DEBUG, + "Send GET request to $url\n[OPTIONS]\n" . print_r($options, true) + ); + + $result = @file_get_contents($url, false, $context); + + StaticLogger::log(LogLevel::DEBUG, "Response from $url\n" . print_r($result, true)); + + return $result; + } }