forked from youngj/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
httprequest.php
174 lines (150 loc) · 5.91 KB
/
httprequest.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
<?php
/*
* Copyright (c) 2011, Trust for Conservation Innovation
* Released under MIT license; see LICENSE.txt
* http://github.com/youngj/httpserver
*/
class HTTPRequest
{
public $method; // HTTP method, e.g. "GET" or "POST"
public $request_uri; // original requested URI, with query string
public $uri; // path component of URI, without query string, after decoding %xx entities
public $http_version; // version from the request line, e.g. "HTTP/1.1"
public $query_string; // query string, like "a=b&c=d"
public $headers; // associative array of HTTP headers
public $lc_headers; // associative array of HTTP headers, with header names in lowercase
public $content_stream; // stream containing content of HTTP request (e.g. POST data)
public $remote_addr; // IP address of client, as string
public $request_line; // The HTTP request line exactly as it came from the client
public $start_time; // unix timestamp of initial request data, as float with microseconds
// internal fields to track the state of reading the HTTP request
private $cur_state = 0;
private $header_buf = '';
private $content_len = 0;
private $content_len_read = 0;
const READ_HEADERS = 0;
const READ_CONTENT = 1;
const READ_COMPLETE = 2;
// fields used by HTTPServer to track other data along with the request
public $socket;
public $response;
function __construct($socket)
{
$this->socket = $socket;
$this->content_stream = fopen("data://text/plain,", 'r+b');
$remote_name = stream_socket_get_name($socket, true);
if ($remote_name)
{
$port_pos = strrpos($remote_name, ":");
if ($port_pos)
{
$this->remote_addr = substr($remote_name, 0, $port_pos);
}
else
{
$this->remote_addr = $remote_name;
}
}
}
function cleanup()
{
fclose($this->content_stream);
$this->content_stream = null;
}
/*
* Reads a chunk of a HTTP request from a client socket.
*/
function add_data($data)
{
switch ($this->cur_state)
{
case static::READ_HEADERS:
if (!$this->start_time)
{
$this->start_time = microtime(true);
}
$header_buf =& $this->header_buf;
$header_buf .= $data;
$end_headers = strpos($header_buf, "\r\n\r\n", 4);
if ($end_headers === false)
{
break;
}
// parse HTTP request line
$end_req = strpos($header_buf, "\r\n");
$this->request_line = substr($header_buf, 0, $end_req);
$req_arr = explode(' ', $this->request_line, 3);
$this->method = $req_arr[0];
$this->request_uri = $req_arr[1];
$this->http_version = $req_arr[2];
$parsed_uri = parse_url($this->request_uri);
$this->uri = urldecode($parsed_uri['path']);
$this->query_string = @$parsed_uri['query'];
// parse HTTP headers
$start_headers = $end_req + 2;
$headers_str = substr($header_buf, $start_headers, $end_headers - $start_headers);
$this->headers = HTTPServer::parse_headers($headers_str);
$this->lc_headers = array();
foreach ($this->headers as $k => $v)
{
$this->lc_headers[strtolower($k)] = $v;
}
$this->content_len = (int)@$this->lc_headers['content-length'];
$start_content = $end_headers + 4; // $end_headers is before last \r\n\r\n
// add leftover to content
$content = substr($header_buf, $start_content);
fwrite($this->content_stream, $content);
$this->content_len_read = strlen($content);
$header_buf = '';
break;
case static::READ_CONTENT:
fwrite($this->content_stream, $data);
$this->content_len_read += strlen($data);
break;
case static::READ_COMPLETE:
break;
}
if (!$this->headers)
{
$this->cur_state = static::READ_HEADERS;
}
else if ($this->needs_content())
{
$this->cur_state = static::READ_CONTENT;
}
else
{
fseek($this->content_stream, 0);
$this->cur_state = static::READ_COMPLETE;
}
}
/*
* Returns the value of a HTTP header from this request (case-insensitive)
*/
function get_header($name)
{
return @$this->lc_headers[strtolower($name)];
}
/*
* Returns true if a full HTTP request has been read by add_data().
*/
function is_read_complete()
{
return $this->cur_state == static::READ_COMPLETE;
}
/*
* Sets a HTTPResponse object associated with this request
*/
function set_response($response)
{
$this->response = $response;
}
/*
* Returns true if more content still needs to be read from the client socket.
* Only valid after the headers have been read.
*/
protected function needs_content()
{
return $this->content_len - $this->content_len_read > 0;
}
}