| Conditions | 44 |
| Paths | > 20000 |
| Total Lines | 174 |
| Code Lines | 98 |
| Lines | 23 |
| Ratio | 13.22 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 71 | public function parseResponseHeaders(&$data, $headersProcessed = false, $debug=0) |
||
| 72 | { |
||
| 73 | $httpResponse = array('raw_data' => $data, 'headers'=> array(), 'cookies' => array()); |
||
| 74 | |||
| 75 | // Support "web-proxy-tunelling" connections for https through proxies |
||
| 76 | if (preg_match('/^HTTP\/1\.[0-1] 200 Connection established/', $data)) { |
||
| 77 | // Look for CR/LF or simple LF as line separator, |
||
| 78 | // (even though it is not valid http) |
||
| 79 | $pos = strpos($data, "\r\n\r\n"); |
||
| 80 | View Code Duplication | if ($pos || is_int($pos)) { |
|
| 81 | $bd = $pos + 4; |
||
| 82 | } else { |
||
| 83 | $pos = strpos($data, "\n\n"); |
||
| 84 | if ($pos || is_int($pos)) { |
||
| 85 | $bd = $pos + 2; |
||
| 86 | } else { |
||
| 87 | // No separation between response headers and body: fault? |
||
| 88 | $bd = 0; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | if ($bd) { |
||
| 92 | // this filters out all http headers from proxy. |
||
| 93 | // maybe we could take them into account, too? |
||
| 94 | $data = substr($data, $bd); |
||
| 95 | } else { |
||
| 96 | error_log('XML-RPC: ' . __METHOD__ . ': HTTPS via proxy error, tunnel connection possibly failed'); |
||
| 97 | throw new \Exception(PhpXmlRpc::$xmlrpcstr['http_error'] . ' (HTTPS via proxy error, tunnel connection possibly failed)', PhpXmlRpc::$xmlrpcerr['http_error']); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | // Strip HTTP 1.1 100 Continue header if present |
||
| 102 | while (preg_match('/^HTTP\/1\.1 1[0-9]{2} /', $data)) { |
||
| 103 | $pos = strpos($data, 'HTTP', 12); |
||
| 104 | // server sent a Continue header without any (valid) content following... |
||
| 105 | // give the client a chance to know it |
||
| 106 | if (!$pos && !is_int($pos)) { |
||
| 107 | // works fine in php 3, 4 and 5 |
||
| 108 | |||
| 109 | break; |
||
| 110 | } |
||
| 111 | $data = substr($data, $pos); |
||
| 112 | } |
||
| 113 | if (!preg_match('/^HTTP\/[0-9.]+ 200 /', $data)) { |
||
| 114 | $errstr = substr($data, 0, strpos($data, "\n") - 1); |
||
| 115 | error_log('XML-RPC: ' . __METHOD__ . ': HTTP error, got response: ' . $errstr); |
||
| 116 | throw new \Exception(PhpXmlRpc::$xmlrpcstr['http_error'] . ' (' . $errstr . ')', PhpXmlRpc::$xmlrpcerr['http_error']); |
||
| 117 | } |
||
| 118 | |||
| 119 | // be tolerant to usage of \n instead of \r\n to separate headers and data |
||
| 120 | // (even though it is not valid http) |
||
| 121 | $pos = strpos($data, "\r\n\r\n"); |
||
| 122 | View Code Duplication | if ($pos || is_int($pos)) { |
|
| 123 | $bd = $pos + 4; |
||
| 124 | } else { |
||
| 125 | $pos = strpos($data, "\n\n"); |
||
| 126 | if ($pos || is_int($pos)) { |
||
| 127 | $bd = $pos + 2; |
||
| 128 | } else { |
||
| 129 | // No separation between response headers and body: fault? |
||
| 130 | // we could take some action here instead of going on... |
||
| 131 | $bd = 0; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | // be tolerant to line endings, and extra empty lines |
||
| 135 | $ar = preg_split("/\r?\n/", trim(substr($data, 0, $pos))); |
||
| 136 | while (list(, $line) = @each($ar)) { |
||
| 137 | // take care of multi-line headers and cookies |
||
| 138 | $arr = explode(':', $line, 2); |
||
| 139 | if (count($arr) > 1) { |
||
| 140 | $headerName = strtolower(trim($arr[0])); |
||
| 141 | /// @todo some other headers (the ones that allow a CSV list of values) |
||
| 142 | /// do allow many values to be passed using multiple header lines. |
||
| 143 | /// We should add content to $xmlrpc->_xh['headers'][$headerName] |
||
| 144 | /// instead of replacing it for those... |
||
| 145 | if ($headerName == 'set-cookie' || $headerName == 'set-cookie2') { |
||
| 146 | if ($headerName == 'set-cookie2') { |
||
| 147 | // version 2 cookies: |
||
| 148 | // there could be many cookies on one line, comma separated |
||
| 149 | $cookies = explode(',', $arr[1]); |
||
| 150 | } else { |
||
| 151 | $cookies = array($arr[1]); |
||
| 152 | } |
||
| 153 | foreach ($cookies as $cookie) { |
||
| 154 | // glue together all received cookies, using a comma to separate them |
||
| 155 | // (same as php does with getallheaders()) |
||
| 156 | if (isset($httpResponse['headers'][$headerName])) { |
||
| 157 | $httpResponse['headers'][$headerName] .= ', ' . trim($cookie); |
||
| 158 | } else { |
||
| 159 | $httpResponse['headers'][$headerName] = trim($cookie); |
||
| 160 | } |
||
| 161 | // parse cookie attributes, in case user wants to correctly honour them |
||
| 162 | // feature creep: only allow rfc-compliant cookie attributes? |
||
| 163 | // @todo support for server sending multiple time cookie with same name, but using different PATHs |
||
| 164 | $cookie = explode(';', $cookie); |
||
| 165 | foreach ($cookie as $pos => $val) { |
||
| 166 | $val = explode('=', $val, 2); |
||
| 167 | $tag = trim($val[0]); |
||
| 168 | $val = trim(@$val[1]); |
||
| 169 | /// @todo with version 1 cookies, we should strip leading and trailing " chars |
||
| 170 | if ($pos == 0) { |
||
| 171 | $cookiename = $tag; |
||
| 172 | $httpResponse['cookies'][$tag] = array(); |
||
| 173 | $httpResponse['cookies'][$cookiename]['value'] = urldecode($val); |
||
| 174 | } else { |
||
| 175 | if ($tag != 'value') { |
||
| 176 | $httpResponse['cookies'][$cookiename][$tag] = $val; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } else { |
||
| 182 | $httpResponse['headers'][$headerName] = trim($arr[1]); |
||
| 183 | } |
||
| 184 | } elseif (isset($headerName)) { |
||
| 185 | /// @todo version1 cookies might span multiple lines, thus breaking the parsing above |
||
| 186 | $httpResponse['headers'][$headerName] .= ' ' . trim($line); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $data = substr($data, $bd); |
||
| 191 | |||
| 192 | if ($debug && count($httpResponse['headers'])) { |
||
| 193 | $msg = ''; |
||
| 194 | foreach ($httpResponse['headers'] as $header => $value) { |
||
| 195 | $msg .= "HEADER: $header: $value\n"; |
||
| 196 | } |
||
| 197 | foreach ($httpResponse['cookies'] as $header => $value) { |
||
| 198 | $msg .= "COOKIE: $header={$value['value']}\n"; |
||
| 199 | } |
||
| 200 | Logger::instance()->debugMessage($msg); |
||
| 201 | } |
||
| 202 | |||
| 203 | // if CURL was used for the call, http headers have been processed, |
||
| 204 | // and dechunking + reinflating have been carried out |
||
| 205 | if (!$headersProcessed) { |
||
| 206 | // Decode chunked encoding sent by http 1.1 servers |
||
| 207 | if (isset($httpResponse['headers']['transfer-encoding']) && $httpResponse['headers']['transfer-encoding'] == 'chunked') { |
||
| 208 | if (!$data = Http::decodeChunked($data)) { |
||
| 209 | error_log('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to rebuild the chunked data received from server'); |
||
| 210 | throw new \Exception(PhpXmlRpc::$xmlrpcstr['dechunk_fail'], PhpXmlRpc::$xmlrpcerr['dechunk_fail']); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | // Decode gzip-compressed stuff |
||
| 215 | // code shamelessly inspired from nusoap library by Dietrich Ayala |
||
| 216 | if (isset($httpResponse['headers']['content-encoding'])) { |
||
| 217 | $httpResponse['headers']['content-encoding'] = str_replace('x-', '', $httpResponse['headers']['content-encoding']); |
||
| 218 | if ($httpResponse['headers']['content-encoding'] == 'deflate' || $httpResponse['headers']['content-encoding'] == 'gzip') { |
||
| 219 | // if decoding works, use it. else assume data wasn't gzencoded |
||
| 220 | if (function_exists('gzinflate')) { |
||
| 221 | if ($httpResponse['headers']['content-encoding'] == 'deflate' && $degzdata = @gzuncompress($data)) { |
||
| 222 | $data = $degzdata; |
||
| 223 | if ($debug) { |
||
| 224 | Logger::instance()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); |
||
| 225 | } |
||
| 226 | } elseif ($httpResponse['headers']['content-encoding'] == 'gzip' && $degzdata = @gzinflate(substr($data, 10))) { |
||
| 227 | $data = $degzdata; |
||
| 228 | if ($debug) { |
||
| 229 | Logger::instance()->debugMessage("---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n$data\n---END---"); |
||
| 230 | } |
||
| 231 | } else { |
||
| 232 | error_log('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to decode the deflated data received from server'); |
||
| 233 | throw new \Exception(PhpXmlRpc::$xmlrpcstr['decompress_fail'], PhpXmlRpc::$xmlrpcerr['decompress_fail']); |
||
| 234 | } |
||
| 235 | } else { |
||
| 236 | error_log('XML-RPC: ' . __METHOD__ . ': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); |
||
| 237 | throw new \Exception(PhpXmlRpc::$xmlrpcstr['cannot_decompress'], PhpXmlRpc::$xmlrpcerr['cannot_decompress']); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } // end of 'if needed, de-chunk, re-inflate response' |
||
| 242 | |||
| 243 | return $httpResponse; |
||
| 244 | } |
||
| 245 | } |
||
| 246 |