private function prepare()
{
// remove anchors (#foo) from the URL
$this->url = $this->strip_anchors($this->url);
// merge query params from the URL with params given
$this->url = $this->merge_query_params($this->url, $this->params);
if ($this->method === 'POST') {
if (!isset($this->headers['Content-Type']) || $this->headers['Content-Type'] == 'application/x-www-form-urlencoded') {
// TODO should raise a warning
$this->add_header(array('Content-Type' => 'application/x-www-form-urlencoded'));
if ($this->body != '' && count($this->postdata) > 0) {
$this->body .= '&';
}
$this->body .= http_build_query($this->postdata, '', '&');
} elseif ($this->headers['Content-Type'] == 'multipart/form-data') {
$boundary = md5(Utils::nonce());
$this->headers['Content-Type'] .= '; boundary=' . $boundary;
$parts = array();
if ($this->postdata && is_array($this->postdata)) {
reset($this->postdata);
while (list($name, $value) = each($this->postdata)) {
$parts[] = "Content-Disposition: form-data; name=\"{$name}\"\r\n\r\n{$value}\r\n";
}
}
if ($this->files && is_array($this->files)) {
reset($this->files);
while (list($name, $fileinfo) = each($this->files)) {
$filename = basename($fileinfo['filename']);
if (!empty($fileinfo['override_filename'])) {
$filename = $fileinfo['override_filename'];
}
$part = "Content-Disposition: form-data; name=\"{$name}\"; filename=\"{$filename}\"\r\n";
$part .= "Content-Type: {$fileinfo['content_type']}\r\n\r\n";
$part .= file_get_contents($fileinfo['filename']) . "\r\n";
$parts[] = $part;
}
}
if (!empty($parts)) {
$this->body = "--{$boundary}\r\n" . join("--{$boundary}\r\n", $parts) . "--{$boundary}--\r\n";
}
}
$this->add_header(array('Content-Length' => strlen($this->body)));
}
}