function sendRequest($saveBody = true) { if (!is_a($this->_url, 'JieqiUrl')) { return $this->raiseError("URL not set"); } $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host; $port = isset($this->_proxy_port) ? $this->_proxy_port : $this->_url->port; // 检查是否支持ssl连接 if (strcasecmp($this->_url->protocol, 'https') == 0 and function_exists('file_get_contents') and extension_loaded('openssl')) { if (isset($this->_proxy_host)) { return $this->raiseError("not support proxy"); } $host = 'ssl://' . $host; } // 连接socket并提交数据 $err = $this->_sock->connect($host, $port, NULL, $this->_timeout, $this->_socketOptions); if ($err == false) { return false; } $err = $this->_sock->write($this->_buildRequest()); if ($err == false) { return false; } if (!empty($this->_readTimeout)) { $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]); } $this->_notify('sentRequest'); // 读取返回信息 $this->_response =& new JieqiResponse($this->_sock, $this->_listeners); $err = $this->_response->process($this->_saveBody && $saveBody); if ($err == false) { return false; } // 检查网址重定向 if ($this->_allowRedirects and $this->_redirects <= $this->_maxRedirects and $this->getResponseCode() > 300 and $this->getResponseCode() < 399 and !empty($this->_response->_headers['location'])) { $redirect = $this->_response->_headers['location']; // 绝对 URL if (preg_match('/^https?:\\/\\//i', $redirect)) { $this->_url =& new JieqiUrl($redirect); $this->addHeader('Host', $this->_generateHostHeader()); // 绝对路径 } elseif ($redirect[0] == '/') { $this->_url->path = $redirect; // 相对路径 } elseif (substr($redirect, 0, 3) == '../' or substr($redirect, 0, 2) == './') { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $redirect = JieqiUrl::resolvePath($redirect); $this->_url->path = $redirect; // 文件名,没路径 } else { if (substr($this->_url->path, -1) == '/') { $redirect = $this->_url->path . $redirect; } else { $redirect = dirname($this->_url->path) . '/' . $redirect; } $this->_url->path = $redirect; } $this->_redirects++; return $this->sendRequest($saveBody); // 重定向次数太多 } elseif ($this->_allowRedirects and $this->_redirects > $this->_maxRedirects) { $this->raiseError('too many redirects', JIEQI_ERROR_RETURN); return false; } $this->_sock->disconnect(); return true; }
function _redirectUrl($url, $location) { if (preg_match('!^https?://!i', $location)) { return $location; } else { if ('/' == $location[0]) { $url->path = JieqiUrl::resolvePath($location); } elseif ('/' == substr($url->path, -1)) { $url->path = JieqiUrl::resolvePath($url->path . $location); } else { $dirname = DIRECTORY_SEPARATOR == dirname($url->path) ? '/' : dirname($url->path); $url->path = JieqiUrl::resolvePath($dirname . '/' . $location); } $url->querystring = array(); $url->anchor = ''; return $url->getUrl(); } }