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/brisk.phh");
60 require_once($G_base."Obj/auth.phh");
61 // require_once("../Obj/proxyscan.phh");
62 require_once($G_base."index.php");
63 require_once($G_base."index_wr.php");
64 require_once($G_base."index_rd_ifra.php");
65 require_once($G_base."briskin5/Obj/briskin5.phh");
67 define('SITE_PREFIX', '/brisk/');
69 function headers_render($header, $len)
73 $s .= "HTTP/1.1 200 OK\r\n";
74 if (!isset($header['Date']))
75 $s .= sprintf("Date: %s\r\n", date(DATE_RFC822));
76 if (!isset($header['Connection']))
77 $s .= "Connection: close\r\n";
78 if (!isset($header['Content-Type']))
79 $s .= "Content-Type: text/html\r\n";
80 foreach($header as $key => $value) {
81 $s .= sprintf("%s: %s\r\n", $key, $value);
84 $s .= "Cache-Control: no-cache, must-revalidate\r\n";
85 $s .= "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n";
86 $s .= "Content-Encoding: chunked\r\n";
87 $s .= "Transfer-Encoding: chunked\r\n";
90 $s .= sprintf("Content-Length: %d\r\n", $len);
98 * Caching system using ob php system to cache old style pages
99 * to a var and than send it with more calm
104 log_rd2("SHUTTA [".connection_status()."] !");
107 register_shutdown_function('shutta');
113 function chunked_content($content)
115 $content_l = mb_strlen($content, "ASCII");
117 return (sprintf("%X\r\n%s\r\n", $content_l, $content));
120 function chunked_fini()
122 return sprintf("0\r\n");
126 static $fixed_fd = 2;
146 function Sac_a_push()
150 // Sac_a_push::create("/tmp/brisk.sock", 0, 0
152 static function create($sockname, $debug, $blocking_mode)
154 $thiz = new Sac_a_push();
156 $thiz->file_socket = $sockname;
157 $thiz->unix_socket = "unix://$sockname";
158 $thiz->debug = $debug;
159 $thiz->socks = array();
160 $thiz->s2u = array();
161 $thiz->pages_flush = array();
163 $thiz->blocking_mode = 0; // 0 for non-blocking
165 if (($thiz->room = Room::create()) == FALSE) {
166 log_crit("room::create failed");
172 for ($i = 0 ; $i < 4096 ; $i++) {
173 $thiz->rndstr .= chr(mt_rand(65, 90));
176 if (file_exists($thiz->file_socket)) {
177 unlink($thiz->file_socket);
180 $old_umask = umask(0);
181 if (($thiz->list = stream_socket_server($thiz->unix_socket, $err, $errs)) === FALSE) {
185 stream_set_blocking($thiz->list, $thiz->blocking_mode); # Set the stream to non-blocking
187 if (($thiz->in = fopen("php://stdin", "r")) === FALSE) {
191 $thiz->main_loop = FALSE;
198 if ($this->main_loop) {
202 $this->main_loop = TRUE;
204 while ($this->main_loop) {
206 printf("IN LOOP: Current opened: %d pages_flush: %d\n", count($this->socks), count($this->pages_flush));
208 /* Prepare the read array */
209 /* // when we manage it ... */
211 /* $read = array_merge(array("$in" => $in), $socks); */
213 $read = array_merge(array(intval($this->list) => $this->list, intval($this->in) => $this->in),
216 if ($this->debug > 1) {
217 printf("PRE_SELECT\n");
222 $num_changed_sockets = stream_select($read, $write, $except, 0, 250000);
224 if ($num_changed_sockets == 0) {
225 printf("No data in 5 secs\n");
227 else if ($num_changed_sockets > 0) {
228 printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read));
229 if ($this->debug > 1) {
232 /* At least at one of the sockets something interesting happened */
233 foreach ($read as $i => $sock) {
234 /* is_resource check is required because there is the possibility that
235 during new request an old connection is closed */
236 if (!is_resource($sock)) {
239 if ($sock === $this->list) {
240 printf("NUOVA CONNEX\n");
241 $new_unix = stream_socket_accept($this->list);
247 if (($new_socket = ancillary_getstream($new_unix, $stream_info)) !== FALSE) {
248 stream_set_blocking($new_socket, $this->blocking_mode); // Set the stream to non-blocking
249 printf("RECEIVED HEADER:\n%s", $stream_info);
250 $path = spu_process_info($stream_info, $method, $header, $get, $post, $cookie);
251 printf("PATH: [%s]\n", $path);
252 printf("M: %s\nHEADER:\n", $method);
261 $addr = stream_socket_get_name($new_socket, TRUE);
262 $header_out = array();
266 case SITE_PREFIX."index.php":
268 index_main($this->room, $header_out, $addr, $get, $post, $cookie);
269 $content = ob_get_contents();
272 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
274 if ($pgflush->try_flush($curtime) == FALSE) {
275 // Add $pgflush to the pgflush array
276 array_push($this->pages_flush, $pgflush);
280 case SITE_PREFIX."index_wr.php":
282 index_wr_main($this->room, $addr, $get, $post, $cookie);
283 $content = ob_get_contents();
286 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
288 if ($pgflush->try_flush($curtime) == FALSE) {
289 // Add $pgflush to the pgflush array
290 array_push($this->pages_flush, $pgflush);
293 case SITE_PREFIX."index_rd_ifra.php":
295 if (!isset($cookie['sess'])
296 || (($user = $this->room->get_user($cookie['sess'], $idx)) == FALSE)) {
297 $content = index_rd_ifra_fini(TRUE);
299 $pgflush = new PageFlush($new_socket, $curtime, 20, $header_out, $content);
301 if ($pgflush->try_flush($curtime) == FALSE) {
302 // Add $pgflush to the pgflush array
303 array_push($this->pages_flush, $pgflush);
307 // close a previous opened index_read_ifra socket, if exists
308 if (($prev = $user->rd_socket_get()) != NULL) {
309 unset($this->s2u[intval($user->rd_socket_get())]);
310 unset($this->socks[intval($user->rd_socket_get())]);
311 fclose($user->rd_socket_get());
312 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
313 $user->rd_socket_set(NULL);
317 index_rd_ifra_init($this->room, $user, $header_out, $content, $get, $post, $cookie);
319 $response = headers_render($header_out, -1).chunked_content($content);
320 $response_l = mb_strlen($response, "ASCII");
322 $wret = @fwrite($new_socket, $response, $response_l);
323 if ($wret < $response_l) {
324 printf("TROUBLES WITH FWRITE: %d\n", $wret);
325 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
328 $user->rd_cache_set("");
332 $this->s2u[intval($new_socket)] = $idx;
333 $this->socks[intval($new_socket)] = $new_socket;
334 $user->rd_socket_set($new_socket);
340 /* $cl = strlen(SITE_PREFIX."briskin5/"); */
341 /* if (!strncmp($this->path, SITE_PREFIX."briskin5/", $cl)) { */
342 /* Bin5::page_manager($room, $header_out, substr($path,$cl), $method, $addr, $get, $post, $cookie); */
346 printf("WARNING: ancillary_getstream failed\n");
350 if (($buf = fread($sock, 512)) === FALSE) {
351 printf("error read\n");
354 else if (strlen($buf) === 0) {
355 if ($sock === $this->list) {
356 printf("Arrivati %d bytes da list\n", strlen($buf));
358 else if ($sock === $this->in) {
359 printf("Arrivati %d bytes da stdin\n", strlen($buf));
362 // $user_a[$s2u[intval($sock)]]->disable();
363 if ($this->room->user[$this->s2u[intval($sock)]]->rd_socket_get() != NULL) {
364 $this->room->user[$this->s2u[intval($sock)]]->rd_socket_set(NULL);
366 unset($this->socks[intval($sock)]);
367 unset($this->s2u[intval($sock)]);
369 printf("CLOSE ON READ\n");
371 if ($this->debug > 1) {
372 printf("post unset\n");
373 print_r($this->socks);
380 if ($sock === $this->list) {
381 printf("Arrivati %d bytes da list\n", strlen($buf));
383 else if ($sock === $this->in) {
384 printf("Arrivati %d bytes da stdin\n", strlen($buf));
387 $key = array_search("$sock", $this->socks);
388 printf("Arrivati %d bytes dalla socket n. %d\n", strlen($buf), $key);
396 foreach ($this->pages_flush as $k => $pgflush) {
397 if ($pgflush->try_flush($curtime) == TRUE) {
398 unset($this->pages_flush[$k]);
402 foreach ($this->socks as $k => $sock) {
403 if (isset($this->s2u[intval($sock)])) {
404 $user = $this->room->user[$this->s2u[intval($sock)]];
405 $response = $user->rd_cache_get();
406 if ($response == "") {
408 index_rd_ifra_main($this->room, $user, $content, $get, $post, $cookie);
410 if ($content == "" && $user->rd_kalive_is_expired($curtime)) {
411 $content = index_rd_ifra_keepalive($user);
413 if ($content != "") {
414 $response = chunked_content($content);
418 if ($response != "") {
419 echo "SPIA: [".substr($response, 0, 60)."...]\n";
420 $response_l = mb_strlen($response, "ASCII");
421 $wret = @fwrite($sock, $response);
422 if ($wret < $response_l) {
423 printf("TROUBLE WITH FWRITE: %d\n", $wret);
424 $user->rd_cache_set(mb_substr($response, $wret, $response_l - $wret, "ASCII"));
427 $user->rd_cache_set("");
430 $user->rd_kalive_reset($curtime);
433 // close socket after a while to prevent client memory consumption
434 if ($user->rd_endtime_is_expired($curtime)) {
435 // $user_a[$s2u[intval($sock)]]->disable();
436 if ($this->room->user[$this->s2u[intval($sock)]]->rd_socket_get() != NULL) {
437 $this->room->user[$this->s2u[intval($sock)]]->rd_socket_set(NULL);
439 unset($this->socks[intval($sock)]);
440 unset($this->s2u[intval($sock)]);
442 printf("CLOSE ON LOOP\n");
452 if (($sap = Sac_a_push::create("/tmp/brisk.sock", 0, 0)) === FALSE) {