]> git.mxchange.org Git - friendica.git/blob - src/Util/HTTPHeaders.php
Merge pull request #11519 from MrPetovan/task/11511-console-domain-move
[friendica.git] / src / Util / HTTPHeaders.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 /**
25  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Web/HTTPHeaders.php
26  */
27 class HTTPHeaders
28 {
29         private $in_progress = [];
30         private $parsed = [];
31
32         function __construct($headers)
33         {
34                 $lines = explode("\n", str_replace("\r", '', $headers));
35
36                 if ($lines) {
37                         foreach ($lines as $line) {
38                                 if (preg_match('/^\s+/', $line, $matches) && trim($line)) {
39                                         if (!empty($this->in_progress['k'])) {
40                                                 $this->in_progress['v'] .= ' ' . ltrim($line);
41                                                 continue;
42                                         }
43                                 } else {
44                                         if (!empty($this->in_progress['k'])) {
45                                                 $this->parsed[] = [$this->in_progress['k'] => $this->in_progress['v']];
46                                                 $this->in_progress = [];
47                                         }
48
49                                         $this->in_progress['k'] = strtolower(substr($line, 0, strpos($line, ':')));
50                                         $this->in_progress['v'] = ltrim(substr($line, strpos($line, ':') + 1));
51                                 }
52                         }
53
54                         if (!empty($this->in_progress['k'])) {
55                                 $this->parsed[$this->in_progress['k']] = $this->in_progress['v'];
56                                 $this->in_progress = [];
57                         }
58                 }
59         }
60
61         function fetch()
62         {
63                 return $this->parsed;
64         }
65 }