X-Git-Url: https://mop.ddnsfree.com/gitweb/?a=blobdiff_plain;f=web%2FObj%2Fsac-a-push.phh;h=527d9b78d18d109f7671e38edb4ad195029699ab;hb=d951bcaafbc7f0f091879d6f47043492534d2d08;hp=3c66c126f52a29e0c7f27bbb087dbf4bcbb4df21;hpb=65c6a676cf9a655d33fa48441b1c7390492163fa;p=brisk.git diff --git a/web/Obj/sac-a-push.phh b/web/Obj/sac-a-push.phh index 3c66c12..527d9b7 100644 --- a/web/Obj/sac-a-push.phh +++ b/web/Obj/sac-a-push.phh @@ -24,6 +24,7 @@ */ define('SITE_PREFIX', '/brisk/'); +define('SITE_PREFIX_LEN', 7); function spu_process_info($stream_info, $method, &$header, &$get, &$post, &$cookie) { @@ -104,9 +105,13 @@ function gpcs_var($name, $get, $post, $cookie) function headers_render($header, $len) { - $s = ""; - $s .= "HTTP/1.1 200 OK\r\n"; + if (isset($header['Location'])) { + return sprintf("HTTP/1.1 302 OK\r\nLocation: %s\r\n\r\n", $header['Location']); + } + else { + $s .= "HTTP/1.1 200 OK\r\n"; + } if (!isset($header['Date'])) $s .= sprintf("Date: %s\r\n", date(DATE_RFC822)); if (!isset($header['Connection'])) @@ -116,15 +121,17 @@ function headers_render($header, $len) foreach($header as $key => $value) { $s .= sprintf("%s: %s\r\n", $key, $value); } - if ($len == -1) { + if ($len >= 0) { + $s .= sprintf("Content-Length: %d\r\n", $len); + } + else { $s .= "Cache-Control: no-cache, must-revalidate\r\n"; $s .= "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"; - $s .= "Content-Encoding: chunked\r\n"; + if (!isset($header['Content-Encoding'])) { + $s .= "Content-Encoding: chunked\r\n"; + } $s .= "Transfer-Encoding: chunked\r\n"; } - else if ($len > 0) { - $s .= sprintf("Content-Length: %d\r\n", $len); - } $s .= "\r\n"; return ($s); @@ -146,11 +153,18 @@ register_shutdown_function('shutta'); * MAIN */ -function chunked_content($content) +function chunked_content($zls, $content) { - $content_l = mb_strlen($content, "ASCII"); + if ($zls) { + $cont_comp = $zls->compress_chunk($content); + } + else { + $cont_comp = $content; + } + $cont_comp_l = mb_strlen($cont_comp, "ASCII"); + printf("CHUNK: [%s]\n", $content); - return (sprintf("%X\r\n%s\r\n", $content_l, $content)); + return (sprintf("%X\r\n", $cont_comp_l).$cont_comp."\r\n"); } function chunked_fini() @@ -158,6 +172,24 @@ function chunked_fini() return sprintf("0\r\n"); } +function get_encoding($header) +{ + $enc = "plain"; + if (isset($header['Accept-Encoding'])) { + $acc = explode(',', $header['Accept-Encoding']); + + if (array_search('gzip', $acc) !== FALSE) { + $enc = 'gzip'; + } + else if (array_search('deflate', $acc) !== FALSE) { + $enc = 'deflate'; + } + } + + return ($enc); +} + + class Sac_a_push { static $fixed_fd = 2; @@ -173,7 +205,7 @@ class Sac_a_push { var $debug; var $blocking_mode; - var $room; + var $app; var $bin5; var $curtime; @@ -187,11 +219,11 @@ class Sac_a_push { // Sac_a_push::create("/tmp/brisk.sock", 0, 0 - static function create(&$room, $sockname, $debug, $blocking_mode) + static function create(&$app, $sockname, $debug, $blocking_mode) { $thiz = new Sac_a_push(); - $thiz->room = $room; + $thiz->app = $app; $thiz->file_socket = $sockname; $thiz->unix_socket = "unix://$sockname"; $thiz->debug = $debug; @@ -203,7 +235,10 @@ class Sac_a_push { $thiz->rndstr = ""; for ($i = 0 ; $i < 4096 ; $i++) { - $thiz->rndstr .= chr(mt_rand(65, 90)); + if (($i % 128) == 0) + $thiz->rndstr .= "\n"; + else + $thiz->rndstr .= chr(mt_rand(65, 90)); } if (file_exists($thiz->file_socket)) { @@ -242,9 +277,9 @@ class Sac_a_push { unset($this->socks[$id]); } - function pgflush_try_add(&$new_socket, $tout, $header_out, $content) + function pgflush_try_add($enc, &$new_socket, $tout, $header_out, $content) { - $pgflush = new PageFlush($new_socket, $this->curtime, $tout, $header_out, $content); + $pgflush = new PageFlush($new_socket, $enc, $this->curtime, $tout, $header_out, $content); if ($pgflush->try_flush($this->curtime) == FALSE) { // Add $pgflush to the pgflush array @@ -257,8 +292,27 @@ class Sac_a_push { array_push($this->pages_flush, $pgflush); } + function garbage_manager($force) + { + $this->app->garbage_manager($force); + + foreach ($this->socks as $k => $sock) { + if ($this->s2u[intval($sock)]->sess == '') { + if ($this->s2u[intval($sock)]->rd_socket_get() != NULL) { + $this->s2u[intval($sock)]->rd_socket_set(NULL); + } + unset($this->socks[intval($sock)]); + unset($this->s2u[intval($sock)]); + fclose($sock); + printf("CLOSE ON GARBAGE MANAGER\n"); + } + } + } + function run() { + GLOBAL $DOCUMENT_ROOT, $HTTP_HOST, $G_with_splash; + if ($this->main_loop) { return (FALSE); } @@ -283,10 +337,10 @@ class Sac_a_push { } $write = NULL; $except = NULL; - $num_changed_sockets = stream_select($read, $write, $except, 0, 250000); + $num_changed_sockets = stream_select($read, $write, $except, 0, 500000); if ($num_changed_sockets == 0) { - printf(" no data in 5 secs "); + printf(" no data in 5 secs, splash [%d]\n", $G_with_splash); } else if ($num_changed_sockets > 0) { printf("num sock %d num_of_socket: %d\n", $num_changed_sockets, count($read)); @@ -302,7 +356,10 @@ class Sac_a_push { } if ($sock === $this->list) { printf("NUOVA CONNEX\n"); - $new_unix = stream_socket_accept($this->list); + if (($new_unix = stream_socket_accept($this->list)) == FALSE) { + printf("SOCKET_ACCEPT FAILED\n"); + continue; + } $stream_info = ""; $method = ""; $get = array(); @@ -322,11 +379,21 @@ class Sac_a_push { print_r($post); printf("COOKIE:\n"); print_r($cookie); - $addr = stream_socket_get_name($new_socket, TRUE); $header_out = array(); - $this->room->request_mgr($this, $header_out, $new_socket, $path, $addr, $get, $post, $cookie); + $enc = get_encoding($header); + + $subs = SITE_PREFIX."briskin5/"; + $subs_l = strlen($subs); + $rret = FALSE; + if (!strncmp($path, SITE_PREFIX, SITE_PREFIX_LEN)) { + $rret = $this->app->request_mgr($this, $enc, $header_out, $new_socket, substr($path, SITE_PREFIX_LEN), $addr, $get, $post, $cookie); + } + if ($rret == FALSE) { + // FIXME: manage 404 !!! + printf("TODO: fix unknown page\n"); + } printf("number of sockets after %d\n", count($this->socks)); } else { @@ -334,16 +401,19 @@ class Sac_a_push { } } else { - if (($buf = fread($sock, 512)) === FALSE) { - printf("error read\n"); - exit(123); - } - else if (strlen($buf) === 0) { + $buf = fread($sock, 512); + // if socket is closed + if ($buf == FALSE || strlen($buf) == 0) { + if ($buf == FALSE) { + printf("ERROR READING\n"); + } if ($sock === $this->list) { printf("Arrivati %d bytes da list\n", strlen($buf)); + exit(21); } else if ($sock === $this->in) { printf("Arrivati %d bytes da stdin\n", strlen($buf)); + exit(22); } else { // $user_a[$s2u[intval($sock)]]->disable(); @@ -352,16 +422,17 @@ class Sac_a_push { } unset($this->socks[intval($sock)]); unset($this->s2u[intval($sock)]); - fclose($sock); - printf("CLOSE ON READ\n"); } + fclose($sock); + printf("CLOSE ON READ\n"); + if ($this->debug > 1) { printf("post unset\n"); print_r($this->socks); } } else { - if ($debug > 1) { + if ($this->debug > 1) { print_r($read); } if ($sock === $this->list) { @@ -369,6 +440,10 @@ class Sac_a_push { } else if ($sock === $this->in) { printf("Arrivati %d bytes da stdin\n", strlen($buf)); + $line = trim($buf); + if ($line == "reload") { + require("$DOCUMENT_ROOT/Etc/".BRISK_CONF); + } } else { $key = array_search("$sock", $this->socks); @@ -378,7 +453,8 @@ class Sac_a_push { } } } - + + $this->garbage_manager(FALSE); /* manage unfinished pages */ foreach ($this->pages_flush as $k => $pgflush) { @@ -387,25 +463,44 @@ class Sac_a_push { } } + /* + $response: raw stream data not sent + $content: html consistent data (