forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Header.php
executable file
·177 lines (155 loc) · 4.43 KB
/
Header.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Author: Tuğrul Topuz <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Http\Client;
use Phalcon\Http\Response\StatusCode;
class Header implements \Countable
{
private $fields = [];
public $version = '1.0';
public $statusCode = 0;
public $statusMessage = '';
public $status = '';
const BUILD_STATUS = 1;
const BUILD_FIELDS = 2;
/**
* @param string $name
* @param string $value
* @return $this
*/
public function set($name, $value)
{
$this->fields[$name] = $value;
return $this;
}
/**
* @param array $fields
* @return $this
*/
public function setMultiple(array $fields)
{
$this->fields = $fields;
return $this;
}
/**
* @param array $fields
* @return $this
*/
public function addMultiple(array $fields)
{
$this->fields = array_combine($this->fields, $fields);
return $this;
}
/**
* @param string $name
* @return mixed
*/
public function get($name)
{
return $this->fields[$name];
}
/**
* @return array
*/
public function getAll()
{
return $this->fields;
}
/**
* Determine if a header exists with a specific key.
*
* @param string $name Key to lookup.
*
* @return boolean
*/
public function has($name)
{
foreach ($this->getAll() as $key => $value) {
if (0 === strcmp(strtolower($key), strtolower($name))) {
return true;
}
}
return false;
}
/**
* @param string $name
* @return $this
*/
public function remove($name)
{
unset($this->fields[$name]);
return $this;
}
/**
* @param $content
* @return bool
*/
public function parse($content)
{
if (empty($content)) {
return false;
}
if (is_string($content)) {
$content = array_filter(explode("\r\n", $content));
} elseif (!is_array($content)) {
return false;
}
$status = [];
if (preg_match('%^HTTP/(\d(?:\.\d)?)\s+(\d{3})\s?+(.+)?$%i', $content[0], $status)) {
$this->status = array_shift($content);
$this->version = $status[1];
$this->statusCode = intval($status[2]);
$this->statusMessage = isset($status[3]) ? $status[3] : '';
}
foreach ($content as $field) {
if (!is_array($field)) {
$field = array_map('trim', explode(':', $field, 2));
}
if (count($field) == 2) {
$this->set($field[0], $field[1]);
}
}
return true;
}
/**
* @param int $flags
* @return array|string
*/
public function build($flags = 0)
{
$lines = [];
if (($flags & self::BUILD_STATUS) && StatusCode::message($this->statusCode)) {
$lines[] = 'HTTP/' . $this->version . ' ' .
$this->statusCode . ' ' .
StatusCode::message($this->statusCode);
}
foreach ($this->fields as $field => $value) {
$lines[] = $field . ': ' . $value;
}
if ($flags & self::BUILD_FIELDS) {
return implode("\r\n", $lines);
}
return $lines;
}
/**
* @return int
*/
public function count()
{
return count($this->fields);
}
}