3 * brisk - spush/brisk-spush.phh
5 * Copyright (C) 2012 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details. You should have received a
19 * copy of the GNU General Public License along with this program; if
20 * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21 * Suite 330, Boston, MA 02111-1307, USA.
26 $HTTP_HOST="dodo.birds.lan";
27 define('USOCK_PATH', "/tmp/brisk.sock");
29 define('PENDINGPAGE_WAITDATA', 0);
30 define('PENDINGPAGE_FLUSH', 1);
34 var $socket; // socket handler of page stream
35 var $status; // status can be 0: waiting for data, 1: flush phase
37 var $kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
38 var $msg; // place where store failed fwrite data
39 var $msg_sz; // size of content
41 var $method; // method used to request the page
42 var $header; // array of header fields
43 var $get; // array of get args
44 var $post; // array of post args
45 var $cookie; // array of cookie args
46 var $path; // requested path
47 var $addr; // source address
48 var $contsz; // expected content size
49 var $rest; // number of missing bytest
51 function PendingPage($socket, $curtime, $kalive)
53 $this->socket = $socket;
54 $this->kalive = $curtime + $kalive;
57 static function pendingpage_waiting($socket, $curtime, $kalive, $method, $header,
58 $get, $post, $cookie, $path, $addr, $rest)
60 $thiz = new PendingPage($socket, $curtime, $kalive);
61 $thiz->status = PENDINGPAGE_WAITDATA;
63 $thiz->method = $method;
64 $thiz->header = $header;
67 $thiz->cookie = $cookie;
70 $thiz->contsz = $header['Content-Length'];
76 function try_wait($curtime)
78 // if completed return TRUE to allow data to be processed,
79 // if timeout or max content dimension is exceeded move to flushing
82 static function pendingpage_flushing($socket, $curtime, $kalive, $enc, $header_out, $body)
84 $thiz = new PendingPage($socket, $curtime, $kalive);
86 $thiz->to_flushing($enc, $header_out, $body);
91 function to_flushing($enc, &$header_out, $body)
93 printf("TRY FLUSH CREATE\n");
94 $body_out = ZLibStream::compress($enc, $body);
96 $header_out['Content-Encoding'] = $enc;
97 $body_out_sz = mb_strlen($body_out, "ASCII");
98 $hea = headers_render($header_out, $body_out_sz);
99 $hea_sz = mb_strlen($hea, "ASCII");
101 $this->status = PENDINGPAGE_FLUSH;
102 $this->msg = $hea.$body_out;
103 $this->msg_sz = $hea_sz + $body_out_sz;
106 /* return TRUE if is removable from it's list */
107 function try_flush($curtime)
109 if ($this->status != PENDINGPAGE_FLUSH)
112 printf("TRY FLUSH IN\n");
113 if ($this->kalive < $curtime) {
114 printf("TRY FLUSH CLOSE 1\n");
115 fclose($this->socket);
119 $wret = @fwrite($this->socket, $this->msg);
120 if ($wret == FALSE && $wret !== FALSE) {
121 printf("TRY FLUSH PendingPage::try_flush: wret 0 but not FALSE\n");
123 if ($wret == $this->msg_sz) {
124 printf("TRY FLUSH CLOSE 2\n");
125 fclose($this->socket);
128 $this->msg_sz -= $wret;
129 $this->msg = mb_substr($this->msg, $wret, $this->msg_sz, "ASCII");
131 printf("TRY FLUSH RETURN FALSE\n");