public static function stream_request($method, $target_url, $cookie_string, $headers0, $user_agent) { $existing_headers = headers_list(); $forwarded_for = UBUtil::array_fetch($_SERVER, 'HTTP_X_FORWARDED_FOR'); $remote_ip = UBUtil::array_fetch($_SERVER, 'REMOTE_ADDR'); $headers = UBHTTP::get_proxied_for_header($headers0, $forwarded_for, $remote_ip); UBLogger::debug_var('target_url', $target_url); $stream_headers = UBHTTP::stream_headers_function($existing_headers); $stream_body = UBHTTP::stream_response_function(); $curl = curl_init(); // http://php.net/manual/en/function.curl-setopt.php $curl_options = array(CURLOPT_URL => $target_url, CURLOPT_POST => $method == "POST", CURLOPT_CUSTOMREQUEST => $method, CURLOPT_USERAGENT => $user_agent, CURLOPT_COOKIE => $cookie_string, CURLOPT_HTTPHEADER => $headers, CURLOPT_HEADERFUNCTION => $stream_headers, CURLOPT_WRITEFUNCTION => $stream_body, CURLOPT_FOLLOWLOCATION => false, CURLOPT_TIMEOUT => 30); if ($method == "POST") { // Use raw post body to allow the same post key to occur more than once $curl_options[CURLOPT_POSTFIELDS] = file_get_contents('php://input'); } curl_setopt_array($curl, $curl_options); $resp = curl_exec($curl); if (!$resp) { $message = 'Error proxying to "' . $target_url . ", " . '": "' . curl_error($curl) . '" - Code: ' . curl_errno($curl); UBLogger::warning($message); http_response_code(500); $result = array(false, $message); } else { $result = array(true, null); } curl_close($curl); return $result; }