]> git.mxchange.org Git - friendica.git/blob - src/Util/HTTPHeaders.php
Merge pull request #6127 from annando/notice
[friendica.git] / src / Util / HTTPHeaders.php
1 <?php
2 /**
3  * @file src/Util/HTTPHeaders.php
4  */
5 namespace Friendica\Util;
6
7 /**
8  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Web/HTTPHeaders.php
9  */
10 class HTTPHeaders
11 {
12         private $in_progress = [];
13         private $parsed = [];
14
15         function __construct($headers)
16         {
17                 $lines = explode("\n", str_replace("\r", '', $headers));
18
19                 if ($lines) {
20                         foreach ($lines as $line) {
21                                 if (preg_match('/^\s+/', $line, $matches) && trim($line)) {
22                                         if (!empty($this->in_progress['k'])) {
23                                                 $this->in_progress['v'] .= ' ' . ltrim($line);
24                                                 continue;
25                                         }
26                                 } else {
27                                         if (!empty($this->in_progress['k'])) {
28                                                 $this->parsed[] = [$this->in_progress['k'] => $this->in_progress['v']];
29                                                 $this->in_progress = [];
30                                         }
31
32                                         $this->in_progress['k'] = strtolower(substr($line, 0, strpos($line, ':')));
33                                         $this->in_progress['v'] = ltrim(substr($line, strpos($line, ':') + 1));
34                                 }
35                         }
36
37                         if (!empty($this->in_progress['k'])) {
38                                 $this->parsed[$this->in_progress['k']] = $this->in_progress['v'];
39                                 $this->in_progress = [];
40                         }
41                 }
42         }
43
44         function fetch()
45         {
46                 return $this->parsed;
47         }
48 }