-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHeader.php
More file actions
119 lines (111 loc) · 3.12 KB
/
HttpHeader.php
File metadata and controls
119 lines (111 loc) · 3.12 KB
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
<?php
/**
* This file is licensed under MIT License.
*
* Copyright (c) 2019 WebFiori Framework
*
* For more information on the license, please visit:
* https://github.com/WebFiori/http/blob/master/LICENSE
*/
namespace WebFiori\Http;
/**
* A class that represents HTTP request or response header.
*
* For more information on HTTP headers, read
* https://datatracker.ietf.org/doc/html/rfc2616#section-4.2
*
* @author Ibrahim
*/
class HttpHeader {
/**
* The name of the HTTP header.
*
* @var string
*/
private $headerName;
private $headerValue;
/**
* The value of the HTTP header.
*
* @var string
*/
/**
* Creates new instance of the class.
*
* @param string $name The name of the header such as 'User-Agent'
*
* @param string $value
*/
public function __construct(string $name = '', string $value = '') {
$this->headerName = 'http-header';
$this->headerValue = 'http-val';
$this->setName($name);
$this->setValue($value);
}
/**
* Returns string representation of the header.
*
* @return string The returned string will consist of a name followed
* by a colon (":") and the header value.
*/
public function __toString() {
return $this->getName().': '.$this->getValue();
}
/**
* Returns a string that represents the name of the header.
*
* @return string A string that represents the name of the header. Default
* return value is 'http-header'
*/
public function getName() : string {
return $this->headerName;
}
/**
* Returns the value of the header.
*
* @return string A string that represents the value of the header. Default
* return value is 'http-val'
*/
public function getValue() : string {
return $this->headerValue;
}
/**
* Sets the name of the header.
*
* @param string $name The name of the header. A valid header name must
* follow following rules: only consist of the letters A-Z, a-z, underscore
* and hyphen.
*
* @return bool If the name is successfully set, true is returned. If
* not set, the method will return false.
*/
public function setName(string $name) : bool {
$trimmed = strtolower(trim($name));
if ($this->validateHeaderNameHelper($trimmed)) {
$this->headerName = $trimmed;
return true;
}
return false;
}
/**
* Sets the value of the header.
*
* @param string $val A string that represents the value of the header.
*/
public function setValue(string $val) {
$this->headerValue = $val;
}
private static function validateHeaderNameHelper($name) : bool {
$len = strlen($name);
if ($len == 0) {
return false;
}
for ($x = 0 ; $x < $len ; $x++) {
$char = $name[$x];
if (!(($char >= 'a' && $char <= 'z') || ($char >= 'A' && $char <= 'Z') || $char == '_' || $char == '-')) {
return false;
}
}
return true;
}
}