- $this->httpResponse['headers'] = array();
- $this->httpResponse['cookies'] = array();
-
- // Support "web-proxy-tunelling" connections for https through proxies
- if(preg_match('/^HTTP\/1\.[0-1] 200 Connection established/', $data))
- {
- // Look for CR/LF or simple LF as line separator,
- // (even though it is not valid http)
- $pos = strpos($data,"\r\n\r\n");
- if($pos || is_int($pos))
- {
- $bd = $pos+4;
- }
- else
- {
- $pos = strpos($data,"\n\n");
- if($pos || is_int($pos))
- {
- $bd = $pos+2;
- }
- else
- {
- // No separation between response headers and body: fault?
- $bd = 0;
- }
- }
- if ($bd)
- {
- // this filters out all http headers from proxy.
- // maybe we could take them into account, too?
- $data = substr($data, $bd);
- }
- else
- {
- error_log('XML-RPC: '.__METHOD__.': HTTPS via proxy error, tunnel connection possibly failed');
- $r=new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], PhpXmlRpc::$xmlrpcstr['http_error']. ' (HTTPS via proxy error, tunnel connection possibly failed)');
- return $r;
- }
- }
-
- // Strip HTTP 1.1 100 Continue header if present
- while(preg_match('/^HTTP\/1\.1 1[0-9]{2} /', $data))
- {
- $pos = strpos($data, 'HTTP', 12);
- // server sent a Continue header without any (valid) content following...
- // give the client a chance to know it
- if(!$pos && !is_int($pos)) // works fine in php 3, 4 and 5
- {
- break;
- }
- $data = substr($data, $pos);
- }
- if(!preg_match('/^HTTP\/[0-9.]+ 200 /', $data))
- {
- $errstr= substr($data, 0, strpos($data, "\n")-1);
- error_log('XML-RPC: '.__METHOD__.': HTTP error, got response: ' .$errstr);
- $r=new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], PhpXmlRpc::$xmlrpcstr['http_error']. ' (' . $errstr . ')');
- return $r;
- }
-
- // be tolerant to usage of \n instead of \r\n to separate headers and data
- // (even though it is not valid http)
- $pos = strpos($data,"\r\n\r\n");
- if($pos || is_int($pos))
- {
- $bd = $pos+4;
- }
- else
- {
- $pos = strpos($data,"\n\n");
- if($pos || is_int($pos))
- {
- $bd = $pos+2;
- }
- else
- {
- // No separation between response headers and body: fault?
- // we could take some action here instead of going on...
- $bd = 0;
- }
- }
- // be tolerant to line endings, and extra empty lines
- $ar = preg_split("/\r?\n/", trim(substr($data, 0, $pos)));
- while(list(,$line) = @each($ar))
- {
- // take care of multi-line headers and cookies
- $arr = explode(':',$line,2);
- if(count($arr) > 1)
- {
- $header_name = strtolower(trim($arr[0]));
- /// @todo some other headers (the ones that allow a CSV list of values)
- /// do allow many values to be passed using multiple header lines.
- /// We should add content to $xmlrpc->_xh['headers'][$header_name]
- /// instead of replacing it for those...
- if ($header_name == 'set-cookie' || $header_name == 'set-cookie2')
- {
- if ($header_name == 'set-cookie2')
- {
- // version 2 cookies:
- // there could be many cookies on one line, comma separated
- $cookies = explode(',', $arr[1]);
- }
- else
- {
- $cookies = array($arr[1]);
- }
- foreach ($cookies as $cookie)
- {
- // glue together all received cookies, using a comma to separate them
- // (same as php does with getallheaders())
- if (isset($this->httpResponse['headers'][$header_name]))
- $this->httpResponse['headers'][$header_name] .= ', ' . trim($cookie);
- else
- $this->httpResponse['headers'][$header_name] = trim($cookie);
- // parse cookie attributes, in case user wants to correctly honour them
- // feature creep: only allow rfc-compliant cookie attributes?
- // @todo support for server sending multiple time cookie with same name, but using different PATHs
- $cookie = explode(';', $cookie);
- foreach ($cookie as $pos => $val)
- {
- $val = explode('=', $val, 2);
- $tag = trim($val[0]);
- $val = trim(@$val[1]);
- /// @todo with version 1 cookies, we should strip leading and trailing " chars
- if ($pos == 0)
- {
- $cookiename = $tag;
- $this->httpResponse['cookies'][$tag] = array();
- $this->httpResponse['cookies'][$cookiename]['value'] = urldecode($val);
- }
- else
- {
- if ($tag != 'value')
- {
- $this->httpResponse['cookies'][$cookiename][$tag] = $val;
- }
- }
- }
- }
- }
- else
- {
- $this->httpResponse['headers'][$header_name] = trim($arr[1]);
- }
- }
- elseif(isset($header_name))
- {
- /// @todo version1 cookies might span multiple lines, thus breaking the parsing above
- $this->httpResponse['headers'][$header_name] .= ' ' . trim($line);
- }