Skip to content

Commit

Permalink
Merge pull request #2 from xBazilio/master
Browse files Browse the repository at this point in the history
rewrited http client to curl
  • Loading branch information
mipxtx committed Apr 12, 2016
2 parents 3cace17 + c118e5a commit 015c993
Showing 1 changed file with 39 additions and 123 deletions.
162 changes: 39 additions & 123 deletions lib/RedmineApi/HttpClient.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
<?php
/**
* @author: mix
* @date: 01.05.14
*/
namespace RedmineApi;

/**
Expand All @@ -24,10 +20,6 @@ class HttpClient

private $timeout;

const READ_BLOCK_SIZE = 4096;

private $rawResponse = "";

/**
* @param string $server_url ie https://redmine.example.com
* @param string $key redmine api key (http://www.redmine.org/projects/redmine/wiki/Rest_api#Authentication)
Expand All @@ -49,11 +41,11 @@ public function __construct($server_url, $key, $timeout = 0.1)
*/
public function request($method, $requestUrl, array $data = [])
{
$requestUrl = $this->getRequestUrl($method, $requestUrl, $data);

$request = $this->getRawRequest($method, $requestUrl, $data);
$h = $this->sendRequest($request);
$ch = $this->prepareCurl($method, $requestUrl, $data);

return $this->getResponse($h, $request);
return $this->getResponse($ch);
}

public function enableDebug($length = 1024)
Expand Down Expand Up @@ -81,140 +73,64 @@ public function log($str)
}
}

public function getRawRequest($method, $requestUrl, array $data = [])
public function getRequestUrl($method, $requestUrl, array $data = [])
{

$serverUrl = parse_url($this->server_url);
$host = $serverUrl["host"];

if (in_array($method, ["GET", "DELETE"]) && $data) {
if ($method == 'GET' && $data) {
$params = [];
foreach ($data as $key => $value) {
$params[] = urlencode($key) . "=" . urlencode($value);
$params[] = urlencode($key) . '=' . urlencode($value);
}
$requestUrl .= "?" . implode("&", $params);

$requestUrl .= '?' . implode('&', $params);
}

$str = "{$method} {$requestUrl} HTTP/1.1\r\n" .
"Host: {$host}\r\n" .
"Connection: close\r\n" .
"X-Redmine-API-Key: {$this->key}\r\n";

if (in_array($method, ["POST", "PUT"])) {
$data = (json_encode($data));
$str .= "Content-Type: application/json\r\n";
$str .= "Content-Length: " . strlen($data) . "\r\n\r\n";
$str .= $data . "\r\n";
}

$str .= "\r\n";

return $str;
return $this->server_url . $requestUrl;
}

public function sendRequest($request)
public function prepareCurl($method, $requestUrl, array $data = [])
{
$scheme = "tcp";
$port = "80";
$serverUrl = parse_url($this->server_url);
$host = $serverUrl["host"];
if (isset($serverUrl["port"])) {
$port = $serverUrl["port"];
if ($serverUrl["scheme"] == "https") {
$scheme = "ssl";
} else {
$scheme = "tcp";
}
} else {
if ($serverUrl["scheme"] == "https") {
$port = "443";
$scheme = "ssl";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->timeout * 1000000);
$this->log("connecting to $requestUrl");

if ($method != 'GET') {
$data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$this->log($data);
}

$uri = "$scheme://$host:$port";
$this->log("connecting to $uri");
$h = stream_socket_client($uri, $errno, $errstr, $this->timeout);

$timeoutSec = floor($this->timeout);
$timeoutMsec = $this->timeout * 1000000;
stream_set_timeout($h, $timeoutSec, $timeoutMsec);
fwrite($h, $request);
$this->log($request);

return $h;
}
if ($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, 1);
} else if ($method == 'PUT' || $method == 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}

public function readLine($h)
{
$line = fgets($h);
if ($this->debug) {
$this->rawResponse .= $line;
$serverUrl = parse_url($this->server_url);
if ($serverUrl['scheme'] == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}

return trim($line);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Redmine-API-Key: ' . $this->key
]);

public function read($h, $length)
{
$toRead = $length;
$out = "";
while ($toRead > 0) {
$blockLength = ($toRead > self::READ_BLOCK_SIZE) ? self::READ_BLOCK_SIZE : $toRead;
$block = fread($h, $blockLength);
$toRead -= $blockLength;
if ($this->debug) {
$this->rawResponse .= $block;
}
$out .= $block;
}
return $out;
return $ch;
}

public function getResponse($h, $request)
public function getResponse($ch)
{
$lines = [];

$headers = [];
$line = $this->readLine($h);
$lines[]= $line;
do {
$headers[] = $line;
$line = $this->readLine($h);
$lines[] = $line;
} while ($line);

$length = 0;
foreach($headers as $header){
if(!strpos($header, ":")){
continue;
}
list($name, $value) = explode(":", $header);
if(trim($name) == 'Content-Length'){
$length = $value;
break;
}
}

$body = $this->read($h, $length);
$body = curl_exec($ch);
curl_close($ch);

$this->log($this->rawResponse);
$this->rawResponse = "";

if (!$body && $length > 0) {
throw new Exception(
"bad responce: '" . implode("\n", $lines)
. "' on request:\n " . var_export($request, 1)
);
}
$this->log($body);

$result = json_decode($body, 1);
if (isset($result["error"])) {
throw new Exception($result["error"]);
if (isset($result['error'])) {
throw new Exception($result['error']);
}

return $result;
}

}
}

0 comments on commit 015c993

Please sign in to comment.