add WebSocket to transports bouquet
[brisk.git] / web / xynt_test01.php
1 <?php
2
3 $desc = array( "Semplice: da 1 a 9 ogni secondo, poi ricomincia (status sempre verde).",
4                "Continuo: da 1 a N ogni secondo, ricomincia ogni 9 (status sempre verde).",
5                "Restart: da 1 a 8 ogni secondo, pausa 16 secondi (status passa ad arancione e poi a rosso), poi ricomincia (e status torna a verde).",
6                "Pausa: da 1 a 5 ogni secondo, pausa 3 secondi, e poi 8 e 9 ogni secondo, e poi ricomincia (status sempre verde).",
7                "Keyword: da 1 a 5 ogni secondo, @BEGIN@, @END@, @BEGIN@ xxx yyy @END@, 9, (status sempre verde).",
8                "Reload limiter: da 1 a 8 ogni secondo e chiude, 9 setta e chiude subito,<br>il client aspetta 3 secondi, e poi da 10 a N ogni secondo, (status sempre verde).");
9
10
11 // trim(mb_convert_case($split[0], MB_CASE_TITLE, 'UTF-8'))
12
13 function headers_render($header, $len)
14 {
15     $cookies = "";
16
17     if (isset($header['Cookies'])) {
18         $cookies = $header['Cookies']->render();
19         unset($header['Cookies']);
20     }
21     if (isset($header['Location'])) {
22         header(sprintf("HTTP/1.1 302 OK\r\n%sLocation: %s", $cookies, $header['Location']));
23     }
24     else if (isset($header['HTTP-Response'])) {
25         header(sprintf("HTTP/1.1 %s", $header['HTTP-Response']));
26         foreach($header as $key => $value) {
27             if (strtolower($key) == "http-response")
28                 continue;
29             header(sprintf("%s: %s", $key, $value));
30         }
31         if ($len >= 0) {
32             header(sprintf("Content-Length: %ld", $len));
33         }
34     }
35     else {
36         header("HTTP/1.1 200 OK\r\n");
37
38         if (!isset($header['Date']))
39             header(sprintf("Date: %s", date(DATE_RFC822)));
40         if (!isset($header['Connection']))
41             header("Connection: close");
42         if (!isset($header['Content-Type']))
43             header("Content-Type: text/html");
44         foreach($header as $key => $value) {
45             header(sprintf("%s: %s", $key, $value));
46         }
47         if ($len >= 0) {
48             header(sprintf("Content-Length: %d", $len));
49         }
50         else {
51             header("Cache-Control: no-cache, must-revalidate");
52             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
53             if (!isset($header['Content-Encoding'])) {
54                 header("Content-Encoding: chunked");
55             }
56             header("Transfer-Encoding: chunked");
57         }
58         header($cookies);
59     }
60
61     return (TRUE);
62 }
63
64 $transs = array( "iframe", "websocket", "xhr", "htmlfile" );
65 if (!isset($f_trans))
66     $f_trans = $transs[0];
67
68 if (!isset($f_test))
69     $f_test = 1;
70
71
72 function mop_flush()
73 {
74     for ($i = 0; $i < ob_get_level(); $i++)
75         ob_end_flush();
76     ob_implicit_flush(1);
77     flush();
78 }
79
80 $escpush_from = array("\\", "\"");
81 $escpush_to   = array("\\\\", "\\\"");
82 function escpush($s)
83 {
84     GLOBAL $escpush_from, $escpush_to;
85
86     return str_replace($escpush_from, $escpush_to, $s);
87 }
88
89 function xcape($s)
90 {
91   $from = array (   '\\',     '@',        '|' );
92   $to   = array ( '\\\\', '&#64;', '&brvbar;' );
93
94   return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
95 }
96
97 if (isset($isstream) && $isstream == "true") {
98
99     require_once("Obj/transports.phh");
100
101     if (isset($transp) && $transp == "websocket") {
102         $trobj = new Transport_websocket();
103     }
104     else if (isset($transp) && $transp == "xhr") {
105         $trobj = new Transport_xhr();
106     }
107     else if (isset($transp) && $transp == "htmlfile") {
108         $trobj = new Transport_htmlfile();
109     }
110     else {
111         $trobj = new Transport_iframe();
112     }
113     $headers_out = array();
114
115     $init_string = "";
116     for ($i = 0 ; $i < 4096 ; $i++) {
117         if (($i % 128) == 0)
118             $init_string .= " ";
119         else
120             $init_string .= chr(mt_rand(65, 90));
121     }
122     $headers_in = getallheaders();
123     $headers = array();
124     foreach ($headers_in as $header_in => $value) {
125         $headers[mb_convert_case($header_in, MB_CASE_TITLE, 'UTF-8')] = $value;
126     }
127     $fp = fopen("/tmp/xynt.log", "a+");
128     fprintf($fp, "here we are\n");
129     fclose($fp);
130
131     $body = $trobj->init("plain", $headers, $headers_out, $init_string, "", "0");
132
133     if ($body === FALSE) {
134         $fp = fopen("/tmp/xynt.log", "a+");
135         fprintf($fp, "init failed\n");
136         fclose($fp);
137     }
138     else {
139         $fp = fopen("/tmp/xynt.log", "a+");
140         fprintf($fp, "after_init [%s] [%s]\n", $transp, print_r($headers_out, TRUE));
141         fprintf($fp, "body [%s][%d]\n", $body, mb_strlen($body, "ASCII"));
142         fclose($fp);
143     }
144
145     if (isset($transp) && $transp == "websocket") {
146         header_remove('Connection');
147         header_remove('Content-Encoding');
148         header_remove('Content-Type');
149         header_remove('Date');
150         header_remove('Keep-Alive');
151         header_remove('Server');
152         header_remove('Transfer-Encoding');
153         header_remove('Vary');
154         header_remove('X-Powered-By');
155
156         headers_render($headers_out, 100);
157     }
158     $lnz = 0;
159
160     print($body);
161     $lnz += mb_strlen($body, "ASCII");
162     mop_flush();
163
164     switch ($f_test) {
165     case 1:
166         // from 1 to 9 into the innerHTML and than close
167         for ($i = 1 ; $i < 10 ; $i++) {
168             $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
169             print($chunk);
170             mop_flush();
171             sleep(1);
172         }
173
174         break;
175     case 2:
176         // from 1 to 9 into the innerHTML and than close
177         for ($i = 1 ; $i < 10 ; $i++) {
178             $chunk = $trobj->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
179             print($chunk);
180             $lnz += mb_strlen($chunk, "ASCII");
181             mop_flush();
182             sleep(1);
183         }
184         break;
185     case 3:
186         // from 1 to 9 with 60 secs after 8, the client js api must restart stream after 12 secs
187         for ($i = 1 ; $i < 10 ; $i++) {
188             $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
189             print($chunk);
190             mop_flush();
191             sleep(1);
192             if ($i == 8)
193                 sleep(60);
194         }
195         break;
196     case 4:
197         // from 1 to 9 into the innerHTML and than close
198         for ($i = 1 ; $i < 10 ; $i++) {
199             if ($i != 5) {
200                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
201             }
202             else {
203                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';|sleep(gst,3000);", $i));
204             }
205             print($chunk);
206             mop_flush();
207             sleep(1);
208         }
209         break;
210     case 5:
211         // from 1 to 9 into the innerHTML and than close
212         $cont = array('@BEGIN@', '@END@', '@BEGIN@ sleep(1); @END@');
213         for ($i = 1 ; $i < 10 ; $i++) {
214             switch($i) {
215             case 6:
216             case 7:
217             case 8:
218                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%s';", xcape($cont[$i - 6])));
219                 break;
220             default:
221                 $chunk = $trobj->chunk($i, sprintf("\$('container').innerHTML = '%d';", $i));
222                 break;
223             }
224             print($chunk);
225             mop_flush();
226             if ($i < 9)
227                 sleep(1);
228         }
229         break;
230     case 6:
231         // from 1 to 9 into the innerHTML and than close
232         if ($step == 8) {
233             $chunk = $trobj->chunk(1, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
234             print($chunk);
235             // without this usleep the delay is doubled in iframe stream because 
236             // no transp.xynt_streaming back-set is performed
237             usleep(250000);
238             mop_flush();
239         }
240         else {
241             for ($i = 1 ; $i < 10 ; $i++) {
242                 $chunk = $trobj->chunk($i, sprintf("gst.st++; \$('container').innerHTML = gst.st;"));
243                 print($chunk);
244                 mop_flush();
245                 if ($i < 9)
246                     sleep(1);
247             }
248         }
249         break;
250     }
251
252     print($trobj->close());
253     mop_flush();
254
255     exit;
256 }
257 ?>
258 <html>
259 <head>
260 <title>XYNT TEST01</title>
261 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
262 <script type="text/javascript" src="xynt-streaming.js"></script>
263 <script type="text/javascript" src="commons.js"></script>
264 <script type="text/javascript" src="heartbit.js"></script>
265
266 <!-- <script type="text/javascript" src="myconsole.js"></script> -->
267
268 <script type="text/javascript"><!--
269      var sess = "for_test";
270      var stat = "";
271      var subst = "";
272      var gst = new globst();
273      window.onload = function() {
274
275          xstm = new xynt_streaming(window, "<?php echo "$f_trans";?>", 80, 0, null /* console */, gst, 'xynt_test01_php', 'sess', sess, null, 'xynt_test01.php?isstream=true&f_test=<?php echo "$f_test";?>', function(com){eval(com);});
276      xstm.hbit_set(heartbit);
277      xstm.start();
278  }
279  //-->
280 </script>
281 </head>
282 <body>
283 <div>
284 <?php
285
286
287
288 printf("<table>");
289 for ($test = 1 ; $test <= count($desc) ; $test++) {
290     printf("<tr>");
291     foreach ($transs as $trans) {
292         printf("<td style=\"padding: 8px; border: 1px solid black;\"><a href=\"?f_trans=%s&f_test=%d\">Test %s %02d</a></td>", $trans, $test, $trans, $test);
293     }
294     printf("</tr>\n");
295 }
296 printf("</table>");
297 printf("<br>[%s]<br>Test: %d<br>", $f_trans, $f_test);
298 ?>
299 </div>
300 <div>
301 <b>Descrizione</b>: <?php echo $desc[$f_test - 1]; ?>
302 </div>
303
304 <div>
305 <b>Status</b>: <img id="stm_stat" class="nobo" style="vertical-align: bottom;" src="img/line-status_b.png"></div>
306
307 </div>
308 <div>
309 <b>Counter</b>: <span id="container">
310 BEGIN
311 </span>
312 </div>
313 </body>
314 </html>