gzip compression management added
[brisk.git] / web / Obj / zlibstream.phh
1 <?php
2 class ZLibStream {
3     var $s;
4     var $head;
5     var $type;
6     var $filter;
7
8     function ZLibStream($type)
9     {
10         $this->type = $type;
11         $this->s = array( FALSE, FALSE );
12         $this->filter = FALSE;
13     }
14
15     static function create($type)
16     {
17         if ($type == 'plain')
18             return (FALSE);
19
20         if (($thiz = new ZLibStream($type)) == FALSE)
21             return (FALSE);
22
23         for ($i = 0 ; $i < 2 ; $i++)
24             $thiz->s[$i] = FALSE;
25         if (($thiz->s = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP)) == FALSE)
26             return (FALSE);
27
28         for ($i = 0 ; $i < 2 ; $i++)
29             stream_set_blocking  ( $thiz->s[$i], 0); // 0 -> not blocking
30         
31         if ($type == 'gzip') {
32             $params = array('level' => 6, 'window' => -15, 'memory' => 9);
33             
34             if (($thiz->filter = stream_filter_append($thiz->s[1], "zlib.deflate", STREAM_FILTER_READ, $params)) == FALSE) {
35                 return (FALSE);
36             }
37             $thiz->head = "\037\213\010\000\000\000\000\000\000\003";
38         }
39         else if ($type  == 'deflate') {
40             if (($thiz->filter = stream_filter_append($thiz->s[1], "zlib.deflate", STREAM_FILTER_READ)) == FALSE) {
41                 return (FALSE);
42             }
43         }
44         return ($thiz);
45     }
46
47     function destroy()
48     {
49         if ($this->filter != FALSE) {
50             stream_filter_remove($this->filter);
51         }
52
53         for ($i = 0 ; $i < 2 ; $i++) {
54             if ($this->s[$i] != FALSE)
55                 fclose($this->s[$i]);
56         }
57     }
58
59         /*
60           too many actors, an explanation is needed to clarify:
61
62           - fwrite: all data MUST be passed to write with success
63           - fflush: probably reduntant
64           - fread: all reads after successfull writes must go well, 
65           
66          */
67     function compress_chunk($s_in)
68     {
69         $s_in_l = mb_strlen($s_in, 'ASCII');
70
71         if ($this->head != FALSE) {
72             $s_out = $this->head;
73             $this->head = FALSE;
74         }
75         else {
76             $s_out = "";
77         }
78
79         for ($to_be_proc = $s_in_l, $max_fail = 0 ; $to_be_proc > 0 && $max_fail < 2 ; $max_fail++) {
80             if ($to_be_proc > 0) {
81                 $max_fail = 0;
82                 if (($ct = fwrite($this->s[0], $s_in)) == FALSE) 
83                     return FALSE;
84                 
85                 $to_be_proc -= $ct;
86             }
87             fflush($this->s[0]); // maybe reduntant but light so ...
88         
89             while (($ret = fread($this->s[1],  8192)) != FALSE) {
90                 $s_out .= $ret;
91             }
92         }
93
94         if ($max_fail < 2)
95             return ($s_out);
96         else
97             return (FALSE);
98     }
99
100     static function compress($enc, $s)
101     {
102         // fprintf(STDERR, "compress: [%s][%s]\n", $enc, $s);
103
104         if ($enc == 'gzip') {
105             return (gzencode($s, -1, FORCE_GZIP));
106         }
107         else if ($enc == 'deflate') {
108             return (gzencode($s, -1, FORCE_DEFLATE));
109         }
110         else
111             return $s;
112     }
113 } // class ZLibStream 
114
115
116
117 function zlibstream_test()
118 {
119     $cont = array( "pippo", "pluto", "paperino");
120
121     for ($f = 0 ; $f < 2 ; $f++) {
122         if (($zls = ZLibStream::create('gzip')) == FALSE) {
123             printf("ZLibStream Creation failed\n");
124             exit(1);
125         }
126         
127         if (($fp = fopen("../../test/zlibstream".$f.".gz", "w")) == FALSE) {
128             printf("ZLibStream test output file failed\n");
129             exit(2);
130         }   
131         
132         for ($i = 0 ; $i < 9 ; $i++) {
133             $idx = $i % 3;
134             
135             $comp = $zls->compress_chunk($cont[$idx]);
136             
137             fwrite($fp, $comp);
138             fflush($fp);
139             sleep(3);
140         }
141         fclose($fp);
142         $zls->destroy();
143     }
144 }
145
146 // zlibstream_test();
147
148 ?>