获取一个请求
string Module_Storage_Driver_Swift::get_response( string $uri , string $method [, array $headers = array(0) , array $query = array(0) , string $body = null , int $chunk_size = integer 10240 ] )
参数列表
参数 类型 描述 默认值 $uri
string
$url $method
string
$method $headers
array
$headers array(0) $query
array
$query array(0) $body
string
$body null $chunk_size
int
$chunk_size integer 10240
string
protected function get_response($uri, $method, $headers = array(), $query = array(), $body = null, $chunk_size = 10240)
{
# 获取连接对象
$fp = $this->connection();
# 获取连接HASH
$connection_hash = $this->get_connection_hash();
# 计数统计
Storage_Driver_Swift::$requests_num[$connection_hash]++;
$metadata = stream_get_meta_data($fp);
if ((Storage_Driver_Swift::$requests_num[$connection_hash] % 100)===0 || true===$metadata['timed_out'])
{
unset($fp);
$fp = $this->connection(true);
}
unset($metadata);
$method = strtoupper($method);
if (!$headers)$headers = array();
if (!$query)$query = array();
# 处理头信息
if (!isset($headers['Content-Length']))
{
if ($body)
{
if (is_resource($body))
{
$headers['Transfer-Encoding'] = 'chunked';
}
else
{
$headers['Content-Length'] = strlen($body);
}
}
else
{
$headers['Content-Length'] = '0';
}
}
$prepped_url = parse_url($this->storage_url);
$host = $prepped_url['host'];
$path = $prepped_url['path'];
$uri = $path . '/'. $this->warehouses . '/' . ($this->prefix?$this->prefix.'/':'') . $uri;
$headers['Host'] = $host;
if (IS_DEBUG)Core::debug()->info($this->storage_protocol .'://'. $host . ($this->port!=80&&$this->port!=443?':'.$this->port:'') . $uri, 'Swift '.$method);
# 拼接头信息
$message = $this->build_request_line($method, $uri, $query) . $this->build_headers($headers);
fwrite($fp, $message);
# 输入内容
if ($body)
{
if (is_resource($body))
{
while (!feof($body))
{
$data = fread($body, $chunk_size);
$len = dechex(strlen($data));
fwrite($fp, $len . "\n" . $data . "\r\n");
}
# 指针移回去
rewind($body, 0);
# HTTP结束符
fwrite($fp, "0\r\n\r\n");
}
else
{
fwrite($fp, $body);
}
}
$rs = $this->read($fp);
if (IS_DEBUG)Core::debug()->info('Swift get code:' . $rs['code']);
return $rs;
}