From 7ea118c64eaa455c2e6141caca1b21386dce48d6 Mon Sep 17 00:00:00 2001 From: Clarth Date: Tue, 1 Dec 2026 15:22:09 -0500 Subject: [PATCH] Use stream HTTP instead of curl; avoid blank line when adding keys --- src/lib.php | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/lib.php b/src/lib.php index f4a0796..f431f51 100644 --- a/src/lib.php +++ b/src/lib.php @@ -103,28 +103,31 @@ function is_authorized(array $groups): bool function http_request(string $method, string $url, array $fields = [], array $headers = []): array { - $ch = curl_init($url); - curl_setopt_array($ch, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_TIMEOUT => 15, - CURLOPT_HTTPHEADER => $headers, - ]); if ($method === 'POST') { - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); + $headers[] = 'Content-Type: application/x-www-form-urlencoded'; + } + $options = [ + 'http' => [ + 'method' => $method, + 'header' => implode("\r\n", $headers), + 'timeout' => 15, + 'ignore_errors' => true, + ], + ]; + if ($method === 'POST') { + $options['http']['content'] = http_build_query($fields); } - $body = curl_exec($ch); - $code = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); - $error = curl_error($ch); - curl_close($ch); + $body = @file_get_contents($url, false, stream_context_create($options)); if ($body === false) { - throw new RuntimeException("Request to $url failed: $error"); + throw new RuntimeException("Request to $url failed."); } - if ($code >= 400) { + preg_match('#^HTTP/\S+\s+(\d{3})#', $http_response_header[0] ?? '', $m); + $code = (int) ($m[1] ?? 0); + if ($code >= 400 || $code === 0) { throw new RuntimeException("Request to $url returned HTTP $code"); } - $json = json_decode((string) $body, true); + $json = json_decode($body, true); return is_array($json) ? $json : []; } @@ -194,6 +197,9 @@ function env_apply(string $content, array $changes, array $deletes): string } $out[] = $line; } + while ($out !== [] && end($out) === '') { + array_pop($out); + } foreach ($changes as $key => $value) { if (!isset($written[$key])) { $out[] = $key . '=' . $value;