83a37371cc3b1ce83a1401f72e4812fdec065c28
[plcapi.git] / src / Helper / Http.php
1 <?php
2
3 namespace PhpXmlRpc\Helper;
4
5 class Http
6 {
7     /**
8      * decode a string that is encoded w/ "chunked" transfer encoding
9      * as defined in rfc2068 par. 19.4.6
10      * code shamelessly stolen from nusoap library by Dietrich Ayala
11      *
12      * @param string $buffer the string to be decoded
13      * @return string
14      */
15     public static function decode_chunked($buffer)
16     {
17         // length := 0
18         $length = 0;
19         $new = '';
20
21         // read chunk-size, chunk-extension (if any) and crlf
22         // get the position of the linebreak
23         $chunkend = strpos($buffer,"\r\n") + 2;
24         $temp = substr($buffer,0,$chunkend);
25         $chunk_size = hexdec( trim($temp) );
26         $chunkstart = $chunkend;
27         while($chunk_size > 0)
28         {
29             $chunkend = strpos($buffer, "\r\n", $chunkstart + $chunk_size);
30
31             // just in case we got a broken connection
32             if($chunkend == false)
33             {
34                 $chunk = substr($buffer,$chunkstart);
35                 // append chunk-data to entity-body
36                 $new .= $chunk;
37                 $length += strlen($chunk);
38                 break;
39             }
40
41             // read chunk-data and crlf
42             $chunk = substr($buffer,$chunkstart,$chunkend-$chunkstart);
43             // append chunk-data to entity-body
44             $new .= $chunk;
45             // length := length + chunk-size
46             $length += strlen($chunk);
47             // read chunk-size and crlf
48             $chunkstart = $chunkend + 2;
49
50             $chunkend = strpos($buffer,"\r\n",$chunkstart)+2;
51             if($chunkend == false)
52             {
53                 break; //just in case we got a broken connection
54             }
55             $temp = substr($buffer,$chunkstart,$chunkend-$chunkstart);
56             $chunk_size = hexdec( trim($temp) );
57             $chunkstart = $chunkend;
58         }
59         return $new;
60     }
61
62 }