reduced log
[brisk.git] / web / spush / brisk-spush.phh
1 <?php
2 /*
3  *  brisk - spush/brisk-spush.phh
4  *
5  *  Copyright (C) 2012 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
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.
14  *
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.
22  *
23  */
24
25 $DOCUMENT_ROOT="";
26 $HTTP_HOST="dodo.birds.lan";
27 define('USOCK_PATH', "/tmp/brisk.sock");
28
29 define('PENDINGPAGE_CONTINUE', 0);
30 define('PENDINGPAGE_WAITDATA', 1);
31 define('PENDINGPAGE_FLUSH',    2);
32
33 class PendingPage {
34   var $socket; // socket handler of page stream
35   var $status; // status can be 0: waiting for data, 1: flush phase
36
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
40
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 bytes
50   var $cont;   // content of unfinished POST
51
52   function PendingPage($socket, $curtime, $kalive)
53   {
54       $this->socket = $socket;
55       // fprintf(STDERR, "SOCKET ADD: %s\n", $this->socket);
56       $this->kalive = $curtime + $kalive;
57   }
58
59   static function pendingpage_continue($socket, $curtime, $kalive, $method,
60                                        $header,     $get,   $post, $cookie,
61                                          $path,    $addr,   $rest, $cont)
62   {
63       $thiz = static::pendingpage_staminal($socket, PENDINGPAGE_CONTINUE, $curtime, $kalive, $method,
64                                            $header, $get, $post, $cookie, $path, $addr, $rest, $cont);
65       $thiz->to_continuing();
66
67       return $thiz;
68   }
69
70   function context_get(&$header, &$socket, &$path, &$addr, &$get, &$post, &$cookie)
71   {
72       $header = $this->header;
73       $socket = $this->socket;
74       $path   = $this->path;
75       $addr   = $this->addr;
76       $get    = $this->get;
77       post_manage($post, $this->cont);
78       $cookie = $this->cookie;
79       fprintf(STDERR, "SOCKET GET: %s\n", $this->socket);
80   }
81
82   function to_continuing()
83   {
84       // printf("TRY FLUSH CREATE\n");
85       $header = array();
86       $header['HTTP-Response'] = "100 Continue";
87       $hea = headers_render($header, 0);
88       $hea_sz = mb_strlen($hea, "ASCII");
89
90       $this->status = PENDINGPAGE_CONTINUE;
91       $this->msg    = $hea;
92       $this->msg_sz = $hea_sz;
93   }
94
95   static function pendingpage_waiting($socket, $curtime, $kalive, $method, $header,
96                                       $get, $post, $cookie, $path, $addr, $rest, $cont)
97   {
98       return (static::pendingpage_staminal($socket, PENDINGPAGE_WAITDATA, $curtime, $kalive, $method,
99                                            $header, $get, $post, $cookie, $path, $addr, $rest, $cont));
100   }
101
102   static function pendingpage_staminal($socket, $status, $curtime, $kalive, $method, $header,
103                                        $get, $post, $cookie, $path, $addr, $rest, $cont)
104   {
105       $thiz = new PendingPage($socket, $curtime, $kalive);
106       $thiz->status = $status;
107
108       $thiz->method = $method;
109       $thiz->header = $header;
110       $thiz->get    = $get;
111       $thiz->post   = $post;
112       $thiz->cookie = $cookie;
113       $thiz->path   = $path;
114       $thiz->addr   = $addr;
115       $thiz->contsz = $header['Content-Length'];
116       $thiz->rest   = $rest;
117       $thiz->cont   = $cont;
118
119       return ($thiz);
120   }
121
122   function try_wait($curtime)
123   {
124       // if completed return TRUE to allow data to be processed, 
125       // if timeout or max content dimension is exceeded move to flushing
126   }
127
128   static function pendingpage_flushing($socket, $curtime, $kalive, $enc, $header_out, $body)
129   {
130       $thiz = new PendingPage($socket, $curtime, $kalive);
131
132       $thiz->to_flushing($enc, $header_out, $body);
133
134       return ($thiz);
135   }
136
137   function to_flushing($enc, &$header_out, $body)
138   {
139       // printf("TRY FLUSH CREATE: enc[%s]\n", $enc);
140       $body_out = ZLibStream::compress($enc, $body);
141       if ($enc != 'plain')
142           $header_out['Content-Encoding'] = $enc;
143       $body_out_sz = mb_strlen($body_out, "ASCII");
144       $hea = headers_render($header_out, $body_out_sz);
145       $hea_sz = mb_strlen($hea, "ASCII");
146
147       $this->status = PENDINGPAGE_FLUSH;
148       $this->msg    = $hea.$body_out;
149       $this->msg_sz = $hea_sz + $body_out_sz;
150       // printf("TRY FLUSH CREATE: enc[%s]\n", $enc);
151   }
152
153   /* return TRUE if is removable from it's list */
154   function try_flush($curtime)
155   {
156       // fprintf(STDERR, "IMPORTANT: TRY_FLUSH: start %d\n", $this->status);
157       if ($this->status != PENDINGPAGE_FLUSH &&
158           $this->status != PENDINGPAGE_CONTINUE)
159           return (FALSE);
160
161       if ($this->kalive < $curtime) {
162           // printf("TRY FLUSH CLOSE 1\n");
163           @fclose($this->socket);
164           return TRUE;
165       }   
166
167       $wret = @fwrite($this->socket, $this->msg, mb_strlen($this->msg, "ASCII"));
168       if ($wret == FALSE && $wret !== FALSE) {
169           // printf("TRY FLUSH PendingPage::try_flush: wret 0 but not FALSE [%d]\n", mb_strlen($this->msg, "ASCII"));
170       }
171       if ($wret == $this->msg_sz) {
172           if ($this->status == PENDINGPAGE_CONTINUE) {
173               $this->status = PENDINGPAGE_WAITDATA;
174               return FALSE;
175           }
176           else {
177               // printf("TRY FLUSH CLOSE 2\n");
178               fclose($this->socket);
179               return TRUE;
180           }
181       }
182       $this->msg_sz -= $wret;
183       $this->msg    = mb_substr($this->msg, $wret, $this->msg_sz, "ASCII");
184
185       // printf("TRY FLUSH RETURN FALSE\n");
186
187       return FALSE;
188   }
189
190   function socket_get()
191   {
192       return ($this->socket);
193   }
194
195 } // class PendingPage {
196
197 ?>