forked from krakjoe/pthreads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeepAliveSession.php
194 lines (161 loc) · 6.59 KB
/
KeepAliveSession.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* This is small example that shows the implementation of a thread based HTTP server
* supporting Connection: keep-alive and how session handling can be implemented.
*
* To start the server, open the command line and enter:
*
* $ php -f keep-alive-session.php 8822
*
* where 8822 is an example port to let the server listen to. To run this example you
* need a thread-safe compiled PHP > 5.3 with pthreads enabled.
*
* @author Tim Wagner <tw@appserver.io>
* @version 0.1.0
* @link https://github.com/krakjoe/pthreads
*/
class Test extends Thread
{
/**
* Socket resource to read/write from/to.
*
* @var resource
*/
protected $socket;
/**
* Initializes the thread with the socket resource
* necessary for reading/writing.
*
* @param resource $socket The socket resource
* @return void
*/
public function __construct($socket)
{
$this->socket = $socket;
}
/**
* Found on php.net {@link http://pa1.php.net/function.http-parse-headers#111226}, thanks
* to anonymous!
*
* @param string $header The header to parse
* @return array The headers parsed from the passed string
* @see http://pa1.php.net/function.http-parse-headers#111226
*/
public function http_parse_headers($header)
{
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach( $fields as $field ) {
if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
if (!is_array($retVal[$match[1]])) {
$retVal[$match[1]] = array($retVal[$match[1]]);
}
$retVal[$match[1]][] = $match[2];
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
return $retVal;
}
/**
* The thread's run() method that runs in parallel.
*
* @link http://www.php.net/manual/en/thread.run.php
*/
public function run()
{
// initialize the local variables and the socket
$threadId = $this->getThreadId();
$counter = 1;
$connectionOpen = true;
$startTime = time();
$timeout = 5;
$maxRequests = 5;
$client = socket_accept($this->socket);
socket_set_option($client, SOL_SOCKET, SO_RCVTIMEO, array("sec" => $timeout, "usec" => 0));
do {
// we only read headers here, because it's an example
$buffer = '';
while ($buffer .= socket_read($client, 1024)) {
if (false !== strpos($buffer, "\r\n\r\n")) {
break;
}
}
// check if the clients stopped sending data
if ($buffer === '') {
socket_close($client);
$connectionOpen = false;
} else {
// parse the request headers
$requestHeaders = $this->http_parse_headers($buffer);
// simulate $_COOKIE array
$_COOKIE = array();
if (array_key_exists('Cookie', $requestHeaders)) {
$cookies = explode('; ', $requestHeaders['Cookie']);
foreach ($cookies as $cookie) {
list ($key, $value) = explode('=', $cookie);
$_COOKIE[$key] = $value;
}
}
// calculate the number of available requests (after this one)
$availableRequests = $maxRequests - $counter++;
// prepare response headers
$headers = array();
$headers[] = "HTTP/1.1 200 OK";
$headers[] = "Content-Type: text/html";
// start the session if not already done
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
// write data to a REAL PHP session, started with session_start()!
$_SESSION["thread_$threadId"]['availableRequest'] = $availableRequests;
// add a header to create session cookie
$headers[] = "Set-Cookie: " . session_name() . "=" . session_id() . "; Path=/";
// prepare HTML body
$body = '<html><head><title>A Title</title></head><body><p>Generated by thread: ' . $threadId . '</p><p>' . var_export($_SESSION, true) . '</p></body></html>';
// prepare header with content-length
$contentLength = strlen($body);
$headers[] = "Content-Length: $contentLength";
// check if this will be the last requests handled by this thread
if ($availableRequests > 0) {
$headers[] = "Connection: keep-alive";
$headers[] = "Keep-Alive: max=$availableRequests, timeout=$timeout, thread={$this->getThreadId()}";
} else {
$headers[] = "Connection: close";
}
// prepare the response head/body
$response = array(
"head" => implode("\r\n", $headers) . "\r\n",
"body" => $body
);
// write the result back to the socket
socket_write($client, implode("\r\n", $response));
// check if this is the last request
if ($availableRequests <= 0) {
// if yes, close the socket and end the do/while
socket_close($client);
$connectionOpen = false;
}
}
} while ($connectionOpen);
}
}
// intialize the threads and the socket
$workers = array();
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, '0.0.0.0', $argv[1]);
socket_listen($socket);
// we start 5 worker threads here
if ($socket) {
$worker = 0;
while (++ $worker < 5) {
$workers[$worker] = new Test($socket);
$workers[$worker]->start(PTHREADS_INHERIT_ALL|PTHREADS_ALLOW_HEADERS);
}
foreach ($workers as $worker) {
$worker->join();
}
}