02bfd578bad8205634c852aa7a92f1e501149ed3
[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      *
14      * @return string
15      */
16     public static function decode_chunked($buffer)
17     {
18         // length := 0
19         $length = 0;
20         $new = '';
21
22         // read chunk-size, chunk-extension (if any) and crlf
23         // get the position of the linebreak
24         $chunkend = strpos($buffer, "\r\n") + 2;
25         $temp = substr($buffer, 0, $chunkend);
26         $chunk_size = hexdec(trim($temp));
27         $chunkstart = $chunkend;
28         while ($chunk_size > 0) {
29             $chunkend = strpos($buffer, "\r\n", $chunkstart + $chunk_size);
30
31             // just in case we got a broken connection
32             if ($chunkend == false) {
33                 $chunk = substr($buffer, $chunkstart);
34                 // append chunk-data to entity-body
35                 $new .= $chunk;
36                 $length += strlen($chunk);
37                 break;
38             }
39
40             // read chunk-data and crlf
41             $chunk = substr($buffer, $chunkstart, $chunkend - $chunkstart);
42             // append chunk-data to entity-body
43             $new .= $chunk;
44             // length := length + chunk-size
45             $length += strlen($chunk);
46             // read chunk-size and crlf
47             $chunkstart = $chunkend + 2;
48
49             $chunkend = strpos($buffer, "\r\n", $chunkstart) + 2;
50             if ($chunkend == false) {
51                 break; //just in case we got a broken connection
52             }
53             $temp = substr($buffer, $chunkstart, $chunkend - $chunkstart);
54             $chunk_size = hexdec(trim($temp));
55             $chunkstart = $chunkend;
56         }
57
58         return $new;
59     }
60 }