-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
165 lines (140 loc) · 4.11 KB
/
Request.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
<?php
/**
* A class that defines an HTTP request that follows RFC 2616
* See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
*
* @package Request
*/
class Request
{
private $method = '';
private $url = '';
private $http_version = '';
private $headers = [];
private $message_body = '';
/**
* Constructor
*/
function __construct($request_method, $request_url, $request_http_version,
$request_headers, $request_message_body) {
$this->method = $request_method;
$this->url = $request_url;
$this->http_version = $request_http_version;
$this->headers = $request_headers;
$this->message_body = $request_message_body;
}
/**
* Gets request method
*
* @return string
*/
public function getMethod() {
return $this->method;
}
/**
* Gets request URL
*
* @return string
*/
public function getUrl() {
return $this->url;
}
/**
* Gets HTTP version of request
*
* @return string
*/
public function getHttpVersion() {
return $this->http_version;
}
/**
* Gets the full array of headers
*
* @return array
*/
public function getHeaders() {
return $this->headers;
}
/**
* Gets a single header
*
* @param string $header Name of the header
* @return string Value of the header
*/
public function getHeader($header) {
if (array_key_exists($header, $this->headers)) {
return $this->headers[$header];
} else {
return null;
}
}
/**
* Gets message body of the request
*
* @return string
*/
public function getMessageBody() {
return $this->message_body;
}
}
/**
* A custom RequestException class for the Request class
*
* @package Request
*/
class RequestException extends Exception {
}
/**
* A class that contains one (for now) utility method for handling HTTP requests
*
* @package Request
*/
class HttpRequestHandler {
/**
* Creates a Request object from an HTTP request string
* Does some simple validation and throws an exception if finds invalid text
*
* note: the first line should actually be
* $request_headers = preg_split("/\r\n/", $request_string);
* according to the RFC but it doesn't work because our
* sample strings have unix newlines (\n)
*
* @param string $request_string The raw HTTP request string
* @return Request A new Request object
*/
public static function handleRequest($request_string) {
$request_headers = explode(PHP_EOL, $request_string);
$request_line = array_shift($request_headers);
$request_line_array = preg_split("/\s+/", $request_line);
if (count($request_line_array) != 3) {
throw new RequestException("Invalid request line: $request_line \n");
}
$request_method = $request_line_array[0];
$request_url = $request_line_array[1];
$request_http_version = $request_line_array[2];
$headers = [];
$request_message_body = '';
while (count($request_headers) > 0) {
$header = array_shift($request_headers);
// in RFC, blank line precedes body, and newline has been stripped
// so we should be left with an empty string before the body
if ($header === "") {
// check if body after
if (count($request_headers) > 0) {
$request_message_body = implode("\n", $request_headers);
}
break;
}
$header_array = explode(":", $header, 2);
if (count($header_array) != 2) {
throw new RequestException("Invalid header: $header \n");
}
$header_name = trim($header_array[0]);
$header_value = trim($header_array[1]);
$headers[$header_name] = $header_value;
}
return new Request($request_method, $request_url, $request_http_version,
$headers, $request_message_body);
}
}
?>