4 * brisk - spush/brisk-spush.php
6 * Copyright (C) 2012 Matteo Nastasi
7 * mailto: nastasi@alternativeoutput.it
8 * matteo.nastasi@milug.org
9 * web: http://www.alternativeoutput.it
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details. You should have received a
20 * copy of the GNU General Public License along with this program; if
21 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
22 * Suite 330, Boston, MA 02111-1307, USA.
27 * - BUG: logout failed
28 * - BUG: fast loop on stream index_rd_ifra page
30 * - garbage management
31 * - log_legal address fix
32 * - from room to table
33 * - from table to room
34 * - index_wr other issues
35 * - manage and test cross forwarder between table and room
36 * - setcookie (for tables only)
37 * - keepalive management
39 * DONE/FROZEN - problema con getpeer (HOSTADDR)
42 * DONE - bug: read from a not resource handle (already closed because a new socket substitute it)
43 * DONE - partial write for normal page management
44 * DONE - index_rd_ifra: last_clean issue
45 * DONE - fwrite failed error management (select, buffer where store unsent data, and fwrite check and retry)
46 * ABRT - index_wr.php::reload - reload is js-only function
47 * DONE - bug: after restart index_rd.php receive from prev clients a lot of req
48 * DONE - index_wr.php::chat
49 * DONE - index_wr.php::exit
50 * DONE - index_rd.php porting
51 * DONE - generic var management from internet
52 * DONE - index.php auth part
57 require_once("./sac-a-push.phh");
58 require_once("./brisk-spush.phh");
59 require_once($G_base."Obj/user.phh");
60 require_once($G_base."Obj/brisk.phh");
61 require_once($G_base."Obj/auth.phh");
62 // require_once("../Obj/proxyscan.phh");
63 require_once($G_base."index.php");
64 require_once($G_base."index_wr.php");
65 require_once($G_base."index_rd_ifra.php");
66 require_once($G_base."briskin5/Obj/briskin5.phh");
68 define('SITE_PREFIX', '/brisk/');
70 function headers_render($header, $len)
74 $s .= "HTTP/1.1 200 OK\r\n";
75 if (!isset($header['Date']))
76 $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
77 if (!isset($header['Connection']))
78 $s .= "Connection: close\r\n";
79 if (!isset($header['Content-Type']))
80 $s .= "Content-Type: text/html\r\n";
81 foreach($header as $key => $value) {
82 $s .= sprintf("%s: %s\r\n", $key, $value);
85 $s .= "Cache-Control: no-cache, must-revalidate\r\n";
86 $s .= "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n";
87 $s .= "Content-Encoding: chunked\r\n";
88 $s .= "Transfer-Encoding: chunked\r\n";
91 $s .= sprintf("Content-Length: %d\r\n", $len);
99 * Caching system using ob php system to cache old style pages
100 * to a var and than send it with more calm
105 log_rd2("SHUTTA [".connection_status()."] !");
108 register_shutdown_function('shutta');
114 function chunked_content($content)
116 $content_l = mb_strlen($content, "ASCII");
118 return (sprintf("%X\r\n%s\r\n", $content_l, $content));
121 function chunked_fini()
123 return sprintf("0\r\n");
127 static $fixed_fd = 2;
149 function Sac_a_push()
153 // Sac_a_push::create("/tmp/brisk.sock", 0, 0
155 static function create(&$room, $sockname, $debug, $blocking_mode)
157 $thiz = new Sac_a_push();
160 $thiz->file_socket = $sockname;
161 $thiz->unix_socket = "unix://$sockname";
162 $thiz->debug = $debug;
163 $thiz->socks = array();
164 $thiz->s2u = array();
165 $thiz->pages_flush = array();
167 $thiz->blocking_mode = 0; // 0 for non-blocking
170 for ($i = 0 ; $i < 4096 ; $i++) {
171 $thiz->rndstr .= chr(mt_rand(65, 90));
174 if (file_exists($thiz->file_socket)) {
175 unlink($thiz->file_socket);
178 $old_umask = umask(0);
179 if (($thiz->list = stream_socket_server($thiz->unix_socket, $err, $errs)) === FALSE) {
183 stream_set_blocking($thiz->list, $thiz->blocking_mode); # Set the stream to non-blocking
185 if (($thiz->in = fopen("php://stdin", "r")) === FALSE) {
189 $thiz->main_loop = FALSE;
194 function socks_set($sock, $user)
198 $this->s2u[$id] = $user;
199 $this->socks[$id] = $sock;
202 function socks_unset($sock)
206 unset($this->s2u[$id]);
207 unset($this->socks[$id]);
210 function pgflush_try_add(&$new_socket, $tout, $header_out, $content)
212 $pgflush = new PageFlush($new_socket, $this->curtime, $tout, $header_out, $content);
214 if ($pgflush->try_flush($this->curtime) == FALSE) {
215 // Add $pgflush to the pgflush array
216 $this->pgflush_add($pgflush);
220 function pgflush_add($pgflush)
222 array_push($this->pages_flush, $pgflush);
227 if ($this->main_loop) {
231 $this->main_loop = TRUE;
233 while ($this->main_loop) {
234 $this->curtime = time();
235 printf("IN LOOP: Current opened: %d pages_flush: %d - ", count($this->socks), count($this->pages_flush));
237 /* Prepare the read array */
238 /* // when we manage it ... */
240 /* $read = array_merge(array("$in" => $in), $socks); */
242 $read = array_merge(array(intval($this->list) => $this->list, intval($this->in) => $this->in),
245 if ($this->debug > 1) {
246 printf("PRE_SELECT\n");
251 $num_changed_sockets = stream_select($read, $write, $except, 0, 250000);
253 if ($num_changed_sockets == 0) {
254 printf(" no data in 5 secs ");
256 else if ($num_changed_sockets > 0) {
257 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
258 if ($this->debug > 1) {
261 /* At least at one of the sockets something interesting happened */
262 foreach ($read as $i => $sock) {
263 /* is_resource check is required because there is the possibility that
264 during new request an old connection is closed */
265 if (!is_resource($sock)) {
268 if ($sock === $this->list) {
269 printf("NUOVA CONNEX\n");
270 $new_unix = stream_socket_accept($this->list);
276 if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
277 printf("NEW_SOCKET: %d\n", intval($new_socket));
278 stream_set_blocking($new_socket, $this->blocking_mode); // Set the stream to non-blocking
279 printf("RECEIVED HEADER:\n%s", $stream_info);
280 $path = spu_process_info($stream_info, $method, $header, $get, $post, $cookie);
281 printf("PATH: [%s]\n", $path);
282 printf("M: %s\nHEADER:\n", $method);
291 $addr = stream_socket_get_name($new_socket, TRUE);
292 $header_out = array();
294 $this->room->request_mgr($this, $header_out, $new_socket, $path, $addr, $get, $post, $cookie);
295 printf("number of sockets after %d\n", count($this->socks));
298 printf("WARNING: ancillary_getstream failed\n");
302 if (($buf = fread($sock, 512)) === FALSE) {
303 printf("error read\n");
306 else if (strlen($buf) === 0) {
307 if ($sock === $this->list) {
308 printf("Arrivati %d bytes da list\n", strlen($buf));
310 else if ($sock === $this->in) {
311 printf("Arrivati %d bytes da stdin\n", strlen($buf));
314 // $user_a[$s2u[intval($sock)]]->disable();
315 if ($this->s2u[intval($sock)]->rd_socket_get() != NULL) {
316 $this->s2u[intval($sock)]->rd_socket_set(NULL);
318 unset($this->socks[intval($sock)]);
319 unset($this->s2u[intval($sock)]);
321 printf("CLOSE ON READ\n");
323 if ($this->debug > 1) {
324 printf("post unset\n");
325 print_r($this->socks);
332 if ($sock === $this->list) {
333 printf("Arrivati %d bytes da list\n", strlen($buf));
335 else if ($sock === $this->in) {
336 printf("Arrivati %d bytes da stdin\n", strlen($buf));
339 $key = array_search("$sock", $this->socks);
340 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
348 /* manage unfinished pages */
349 foreach ($this->pages_flush as $k => $pgflush) {
350 if ($pgflush->try_flush($this->curtime) == TRUE) {
351 unset($this->pages_flush[$k]);
355 /* manage open streaming */
356 foreach ($this->socks as $k => $sock) {
357 if (isset($this->s2u[intval($sock)])) {
358 $user = $this->s2u[intval($sock)];
359 $response = $user->rd_cache_get();
360 if ($response == "") {
362 $user->stream_main($content, $get, $post, $cookie);
364 if ($content == "" && $user->rd_kalive_is_expired($this->curtime)) {
365 $content = $user->stream_keepalive();
367 if ($content != "") {
368 $response = chunked_content($content);
372 if ($response != "") {
373 echo "SPIA: [".substr($response, 0, 60)."...]\n";
374 $response_l = mb_strlen($response, "ASCII");
375 $wret = @fwrite($sock, $response);
376 if ($wret < $response_l) {
377 printf("TROUBLE WITH FWRITE: %d\n", $wret);
378 $user->rd_cache_set(mb_substr($response, $wret, $response_l - $wret, "ASCII"));
381 $user->rd_cache_set("");
384 $user->rd_kalive_reset($this->curtime);
387 // close socket after a while to prevent client memory consumption
388 if ($user->rd_endtime_is_expired($this->curtime)) {
389 if ($this->s2u[intval($sock)]->rd_socket_get() != NULL) {
390 $this->s2u[intval($sock)]->rd_socket_set(NULL);
392 unset($this->socks[intval($sock)]);
393 unset($this->s2u[intval($sock)]);
395 printf("CLOSE ON LOOP\n");
398 } // foreach ($this->socks...
401 } // function run(...
406 if (($room = Room::create()) == FALSE) {
407 log_crit("room::create failed");
411 if (($s_a_p = Sac_a_push::create($room, "/tmp/brisk.sock", 0, 0)) === FALSE) {