// Toggle gzip decompression when appropriate. if ($headerLine == "Content-Encoding: gzip") { global $is_gzip; $is_gzip = true; // Toggle chunk merging when appropriate } elseif ($headerLine == "Transfer-Encoding: chunked") { global $is_chunked; $is_chunked = true; } else { // todo: Find out why this doesn't work (removes all contents!) // header($headerLine, FALSE); } } $contents = $response['content']; if ($is_chunked) { $contents = decode_chunked($contents); } if ($is_gzip) { $contents = gzdecode($contents); } echo $contents; } else { echo $domainName . " is not an authorized domain."; } function proxy_request($url, $data, $method) { // Based on post_request from http://www.jonasjohn.de/snippets/php/post-request.htm $req_dump = print_r($data, TRUE); global $ip; // Convert the data array into URL Parameters like a=b&foo=bar etc. if ($method == "GET") {
/** * Parses HTTP headers and separates them from data. * @access private */ function &parseResponseHeaders(&$data, $headers_processed = false) { // 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 xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $GLOBALS['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)) { 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 xmlrpcresp(0, $GLOBALS['xmlrpcerr']['http_error'], $GLOBALS['xmlrpcstr']['http_error'] . ' (' . $errstr . ')'); return $r; } $GLOBALS['_xh']['headers'] = array(); $GLOBALS['_xh']['cookies'] = array(); // 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 $GLOBALS['_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($GLOBALS['_xh']['headers'][$header_name])) { $GLOBALS['_xh']['headers'][$header_name] .= ', ' . trim($cookie); } else { $GLOBALS['_xh']['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; $GLOBALS['_xh']['cookies'][$tag] = array(); $GLOBALS['_xh']['cookies'][$cookiename]['value'] = urldecode($val); } else { if ($tag != 'value') { $GLOBALS['_xh']['cookies'][$cookiename][$tag] = $val; } } } } } else { $GLOBALS['_xh']['headers'][$header_name] = trim($arr[1]); } } elseif (isset($header_name)) { /// @todo version1 cookies might span multiple lines, thus breaking the parsing above $GLOBALS['_xh']['headers'][$header_name] .= ' ' . trim($line); } } $data = substr($data, $bd); if ($this->debug && count($GLOBALS['_xh']['headers'])) { print '<PRE>'; foreach ($GLOBALS['_xh']['headers'] as $header => $value) { print htmlentities("HEADER: {$header}: {$value}\n"); } foreach ($GLOBALS['_xh']['cookies'] as $header => $value) { print htmlentities("COOKIE: {$header}={$value['value']}\n"); } print "</PRE>\n"; } // if CURL was used for the call, http headers have been processed, // and dechunking + reinflating have been carried out if (!$headers_processed) { // Decode chunked encoding sent by http 1.1 servers if (isset($GLOBALS['_xh']['headers']['transfer-encoding']) && $GLOBALS['_xh']['headers']['transfer-encoding'] == 'chunked') { if (!($data = decode_chunked($data))) { error_log('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to rebuild the chunked data received from server'); $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['dechunk_fail'], $GLOBALS['xmlrpcstr']['dechunk_fail']); return $r; } } // Decode gzip-compressed stuff // code shamelessly inspired from nusoap library by Dietrich Ayala if (isset($GLOBALS['_xh']['headers']['content-encoding'])) { $GLOBALS['_xh']['headers']['content-encoding'] = str_replace('x-', '', $GLOBALS['_xh']['headers']['content-encoding']); if ($GLOBALS['_xh']['headers']['content-encoding'] == 'deflate' || $GLOBALS['_xh']['headers']['content-encoding'] == 'gzip') { // if decoding works, use it. else assume data wasn't gzencoded if (function_exists('gzinflate')) { if ($GLOBALS['_xh']['headers']['content-encoding'] == 'deflate' && ($degzdata = @gzuncompress($data))) { $data = $degzdata; if ($this->debug) { print "<PRE>---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n" . htmlentities($data) . "\n---END---</PRE>"; } } elseif ($GLOBALS['_xh']['headers']['content-encoding'] == 'gzip' && ($degzdata = @gzinflate(substr($data, 10)))) { $data = $degzdata; if ($this->debug) { print "<PRE>---INFLATED RESPONSE---[" . strlen($data) . " chars]---\n" . htmlentities($data) . "\n---END---</PRE>"; } } else { error_log('XML-RPC: ' . __METHOD__ . ': errors occurred when trying to decode the deflated data received from server'); $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['decompress_fail'], $GLOBALS['xmlrpcstr']['decompress_fail']); return $r; } } else { error_log('XML-RPC: ' . __METHOD__ . ': the server sent deflated data. Your php install must have the Zlib extension compiled in to support this.'); $r = new xmlrpcresp(0, $GLOBALS['xmlrpcerr']['cannot_decompress'], $GLOBALS['xmlrpcstr']['cannot_decompress']); return $r; } } } } // end of 'if needed, de-chunk, re-inflate response' // real stupid hack to avoid PHP complaining about returning NULL by ref $r = null; $r =& $r; return $r; }
function http_request($ip, $uri = '/', $getdata = array(), $WSSE_headers = '', $timeout = 1, $res_hdr = false) { $ret = ''; $verb = 'GET'; /* HTTP Request Method (GET and POST supported) */ $port = 80; /* Target TCP port */ $cookie_str = ''; $getdata_str = count($getdata) ? '?' : ''; $chunked = true; foreach ($getdata as $k => $v) { $getdata_str .= urlencode($k) . '=' . urlencode($v) . '&'; } $crlf = "\r\n"; $req = $verb . ' ' . $uri . $getdata_str . ' HTTP/1.1' . $crlf; $req .= 'Host: ' . $ip . $crlf; $req .= 'Authorization: WSSE profile="UsernameToken"' . $crlf; $req .= $WSSE_headers . $crlf; $req .= $crlf; if (($fp = @fsockopen($ip, $port, $errno, $errstr)) == false) { return "Error {$errno}: {$errstr}\n"; } fputs($fp, $req); while ($line = fgets($fp)) { $ret .= $line; } fclose($fp); if (strpos($ret, "Transfer-Encoding: chunked") === false) { $chunked = false; } if (!$res_hdr) { $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4); } if ($chunked) { $ret = decode_chunked($ret); } return $ret; }