pull of unix socket to interact with apache2
[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_PFX', "/tmp/brisk");
28 define('USOCK_POOL_N', 10);
29
30 define('PENDINGPAGE_CONTINUE', 0);
31 define('PENDINGPAGE_WAITDATA', 1);
32 define('PENDINGPAGE_FLUSH',    2);
33
34 class PendingPage {
35   var $socket; // socket handler of page stream
36   var $status; // status can be 0: waiting for data, 1: flush phase
37
38   var $kalive; // if no message are sent after RD_KEEPALIVE_TOUT secs we send a keepalive from server
39   var $msg;    // place where store failed fwrite data
40   var $msg_sz; // size of content
41
42   var $method; // method used to request the page
43   var $header; // array of header fields
44   var $get;    // array of get args
45   var $post;   // array of post args
46   var $cookie; // array of cookie args
47   var $path;   // requested path
48   var $addr;   // source address
49   var $contsz; // expected content size
50   var $rest;   // number of missing bytes
51   var $cont;   // content of unfinished POST
52
53   function PendingPage($socket, $curtime, $kalive)
54   {
55       $this->socket = $socket;
56       // fprintf(STDERR, "SOCKET ADD: %s\n", $this->socket);
57       $this->kalive = $curtime + $kalive;
58   }
59
60   static function pendingpage_continue($socket, $curtime, $kalive, $method,
61                                        $header,     $get,   $post, $cookie,
62                                          $path,    $addr,   $rest, $cont)
63   {
64       $thiz = static::pendingpage_staminal($socket, PENDINGPAGE_CONTINUE, $curtime, $kalive, $method,
65                                            $header, $get, $post, $cookie, $path, $addr, $rest, $cont);
66       $thiz->to_continuing();
67
68       return $thiz;
69   }
70
71   function context_get(&$header, &$socket, &$path, &$addr, &$get, &$post, &$cookie)
72   {
73       $header = $this->header;
74       $socket = $this->socket;
75       $path   = $this->path;
76       $addr   = $this->addr;
77       $get    = $this->get;
78       post_manage($post, $this->cont);
79       $cookie = $this->cookie;
80       fprintf(STDERR, "SOCKET GET: %s\n", $this->socket);
81   }
82
83   function to_continuing()
84   {
85       // printf("TRY FLUSH CREATE\n");
86       $header = array();
87       $header['HTTP-Response'] = "100 Continue";
88       $hea = headers_render($header, 0);
89       $hea_sz = mb_strlen($hea, "ASCII");
90
91       $this->status = PENDINGPAGE_CONTINUE;
92       $this->msg    = $hea;
93       $this->msg_sz = $hea_sz;
94   }
95
96   static function pendingpage_waiting($socket, $curtime, $kalive, $method, $header,
97                                       $get, $post, $cookie, $path, $addr, $rest, $cont)
98   {
99       return (static::pendingpage_staminal($socket, PENDINGPAGE_WAITDATA, $curtime, $kalive, $method,
100                                            $header, $get, $post, $cookie, $path, $addr, $rest, $cont));
101   }
102
103   static function pendingpage_staminal($socket, $status, $curtime, $kalive, $method, $header,
104                                        $get, $post, $cookie, $path, $addr, $rest, $cont)
105   {
106       $thiz = new PendingPage($socket, $curtime, $kalive);
107       $thiz->status = $status;
108
109       $thiz->method = $method;
110       $thiz->header = $header;
111       $thiz->get    = $get;
112       $thiz->post   = $post;
113       $thiz->cookie = $cookie;
114       $thiz->path   = $path;
115       $thiz->addr   = $addr;
116       $thiz->contsz = $header['Content-Length'];
117       $thiz->rest   = $rest;
118       $thiz->cont   = $cont;
119
120       return ($thiz);
121   }
122
123   function try_wait($curtime)
124   {
125       // if completed return TRUE to allow data to be processed, 
126       // if timeout or max content dimension is exceeded move to flushing
127   }
128
129   static function pendingpage_flushing($socket, $curtime, $kalive, $enc, $header_out, $body)
130   {
131       $thiz = new PendingPage($socket, $curtime, $kalive);
132
133       $thiz->to_flushing($enc, $header_out, $body);
134
135       return ($thiz);
136   }
137
138   function to_flushing($enc, &$header_out, $body)
139   {
140       // printf("TRY FLUSH CREATE: enc[%s]\n", $enc);
141       $body_out = ZLibStream::compress($enc, $body);
142       if ($enc != 'plain')
143           $header_out['Content-Encoding'] = $enc;
144       $body_out_sz = mb_strlen($body_out, "ASCII");
145       $hea = headers_render($header_out, $body_out_sz);
146       $hea_sz = mb_strlen($hea, "ASCII");
147
148       $this->status = PENDINGPAGE_FLUSH;
149       $this->msg    = $hea.$body_out;
150       $this->msg_sz = $hea_sz + $body_out_sz;
151       // printf("TRY FLUSH CREATE: enc[%s]\n", $enc);
152   }
153
154   /* return TRUE if is removable from it's list */
155   function try_flush($curtime)
156   {
157       // fprintf(STDERR, "IMPORTANT: TRY_FLUSH: start %d\n", $this->status);
158       if ($this->status != PENDINGPAGE_FLUSH &&
159           $this->status != PENDINGPAGE_CONTINUE)
160           return (FALSE);
161
162       if ($this->kalive < $curtime) {
163           // printf("TRY FLUSH CLOSE 1\n");
164           @fclose($this->socket);
165           return TRUE;
166       }   
167
168       $wret = @fwrite($this->socket, $this->msg, mb_strlen($this->msg, "ASCII"));
169       if ($wret == FALSE && $wret !== FALSE) {
170           // printf("TRY FLUSH PendingPage::try_flush: wret 0 but not FALSE [%d]\n", mb_strlen($this->msg, "ASCII"));
171       }
172       if ($wret == $this->msg_sz) {
173           if ($this->status == PENDINGPAGE_CONTINUE) {
174               $this->status = PENDINGPAGE_WAITDATA;
175               return FALSE;
176           }
177           else {
178               // printf("TRY FLUSH CLOSE 2\n");
179               fclose($this->socket);
180               return TRUE;
181           }
182       }
183       $this->msg_sz -= $wret;
184       $this->msg    = mb_substr($this->msg, $wret, $this->msg_sz, "ASCII");
185
186       // printf("TRY FLUSH RETURN FALSE\n");
187
188       return FALSE;
189   }
190
191   function socket_get()
192   {
193       return ($this->socket);
194   }
195
196 } // class PendingPage {
197
198 ?>