Passed
Push — master ( 598605...ab3682 )
by Pauli
02:53 queued 19s
created

HttpUtil::findHeader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 8
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2022
11
 */
12
13
namespace OCA\Music\Utility;
14
15
/**
16
 * Static utility functions to work with HTTP requests
17
 */
18
class HttpUtil {
19
20
	/**
21
	 * Use HTTP GET to load the requested URL
22
	 * @return array with three keys: ['content' => string|false, 'status_code' => int, 'message' => string]
23
	 */
24
	public static function loadFromUrl(string $url, ?int $maxLength=null) : array {
25
		$status_code = 0;
26
		$content_type = null;
27
28
		if (!Util::startsWith($url, 'http://', /*ignoreCase=*/true)
29
				&& !Util::startsWith($url, 'https://', /*ignoreCase=*/true)) {
30
			$content = false;
31
			$message = 'URL scheme must be HTTP or HTTPS';
32
		} else {
33
			$opts = [
34
				'http' => [
35
					'header' => self::userAgentHeader(),	// some servers don't allow requests without a user agent header
36
					'ignore_errors' => true 				// don't emit warnings for bad/unavailable URL, we handle errors manually
37
				]
38
			];
39
			$context = \stream_context_create($opts);
40
			// The length parameter of file_get_contents isn't nullable prior to PHP8.0
41
			if ($maxLength === null) {
42
				$content = \file_get_contents($url, false, $context);
43
			} else {
44
				$content = \file_get_contents($url, false, $context, 0, $maxLength);
45
			}
46
47
			// It's some PHP magic that calling file_get_contents creates and populates also a local
48
			// variable array $http_response_header, provided that the server could be reached.
49
			// PhpStan thinks that this varialbe would always exist, and doesn't like the isset.
50
			// @phpstan-ignore-next-line
51
			if (isset($http_response_header)) {
52
				list($version, $status_code, $message) = \explode(' ', $http_response_header[0], 3);
53
				$status_code = (int)$status_code;
54
				$content_type = self::findHeader($http_response_header, 'Content-Type');
55
			} else {
56
				$message = 'The requested URL did not respond';
57
			}
58
		}
59
60
		return \compact('content', 'status_code', 'message', 'content_type');
61
	}
62
63
	public static function userAgentHeader() : string {
64
		// Note: the following is deprecated since NC14 but the replacement
65
		// \OCP\App\IAppManager::getAppVersion is not available before NC14.
66
		$appVersion = \OCP\App::getAppVersion('music');
67
68
		return 'User-Agent: OCMusic/' . $appVersion;
69
	}
70
71
	private static function findHeader(array $headers, string $headerKey) : ?string {
72
		foreach ($headers as $header) {
73
			$find = \strstr($header, $headerKey . ':');
74
			if ($find !== false) {
75
				return \trim(\substr($find, \strlen($headerKey)+1));
76
			}
77
		}
78
		return null;
79
	}
80
}
81