Skip to content

Commit 7b45bf6

Browse files
authored
Bump 0.6.5, support https (#48)
1 parent 79f01e8 commit 7b45bf6

File tree

3 files changed

+70
-26
lines changed

3 files changed

+70
-26
lines changed

Aliyun/Log/Client.php

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class Aliyun_Log_Client {
5252
* @var string the local machine ip address.
5353
*/
5454
protected $source;
55+
56+
/**
57+
* @var bool use https or use http.
58+
*/
59+
protected $useHttps;
5560

5661
/**
5762
* Aliyun_Log_Client constructor.
@@ -91,23 +96,25 @@ public function __construct(
9196
$this->source = Aliyun_Log_Util::getLocalIp();
9297
}
9398
private function setEndpoint($endpoint) {
94-
$pos = strpos ( $endpoint, "://" );
95-
if ($pos !== false) { // be careful, !==
96-
$pos += 3;
97-
$endpoint = substr ( $endpoint, $pos );
99+
if (strpos($endpoint, '://') === false) {
100+
$endpoint = 'http://' . $endpoint; // default use http
101+
}
102+
$urlComponents = parse_url($endpoint);
103+
if ($urlComponents === false || !isset($urlComponents['host'])) {
104+
throw new InvalidArgumentException("Invalid endpoint: $endpoint");
105+
}
106+
107+
$this->useHttps = isset($urlComponents['scheme']) && $urlComponents['scheme'] === 'https';
108+
$this->logHost = $urlComponents['host'];
109+
110+
if (isset($urlComponents['port'])) {
111+
$this->port = $urlComponents['port'];
112+
$this->endpoint = $this->logHost . ':' . $this->port;
113+
} else {
114+
$this->port = $this->useHttps ? 443 : 80;
115+
$this->endpoint = $this->logHost;
98116
}
99-
$pos = strpos ( $endpoint, "/" );
100-
if ($pos !== false) // be careful, !==
101-
$endpoint = substr ( $endpoint, 0, $pos );
102-
$pos = strpos ( $endpoint, ':' );
103-
if ($pos !== false) { // be careful, !==
104-
$this->port = ( int ) substr ( $endpoint, $pos + 1 );
105-
$endpoint = substr ( $endpoint, 0, $pos );
106-
} else
107-
$this->port = 80;
108-
$this->isRowIp = Aliyun_Log_Util::isIp ( $endpoint );
109-
$this->logHost = $endpoint;
110-
$this->endpoint = $endpoint . ':' . ( string ) $this->port;
117+
$this->isRowIp = Aliyun_Log_Util::isIp($this->logHost);
111118
}
112119

113120
/**
@@ -228,17 +235,23 @@ private function send($method, $project, $body, $resource, $params, $headers) {
228235
$signature = Aliyun_Log_Util::getRequestAuthorization ( $method, $resource, $credentials->getAccessKeySecret(), $credentials->getSecurityToken(), $params, $headers );
229236
$headers ['Authorization'] = "LOG ".$credentials->getAccessKeyId().":$signature";
230237

238+
$url = $this->buildUrl($project, $resource, $params);
239+
return $this->sendRequest ( $method, $url, $body, $headers );
240+
}
241+
242+
private function buildUrl($project, $resource, $params) {
231243
$url = $resource;
232-
if ($params)
233-
$url .= '?' . Aliyun_Log_Util::urlEncode ( $params );
234-
if ($this->isRowIp)
235-
$url = "http://$this->endpoint$url";
236-
else{
237-
if(is_null($project))
238-
$url = "http://$this->endpoint$url";
239-
else $url = "http://$project.$this->endpoint$url";
244+
$schema = $this->useHttps ? "https://" : "http://";
245+
if ($params) {
246+
$url .= '?' . Aliyun_Log_Util::urlEncode($params);
240247
}
241-
return $this->sendRequest ( $method, $url, $body, $headers );
248+
if ($this->isRowIp) {
249+
return "$schema$this->endpoint$url";
250+
}
251+
if (is_null($project)) {
252+
return "$schema$this->endpoint$url";
253+
}
254+
return "$schema$project.$this->endpoint$url";
242255
}
243256

244257
/**

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"php": ">=7.1.7"
1212
},
1313
"require-dev": {
14+
"phpunit/phpunit": "^11.2"
1415
},
1516
"authors": [
1617
{
@@ -33,5 +34,5 @@
3334
},
3435
"name": "alibabacloud/aliyun-log-php-sdk",
3536
"license": "MIT",
36-
"version": "0.6.4"
37+
"version": "0.6.5"
3738
}

tests/EndpointTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
require_once realpath(dirname(__FILE__) . '/../Log_Autoload.php');
6+
7+
class EndpointTest extends TestCase {
8+
public function testBuildUrl() {
9+
$this->assertEquals($this->getUrl('https://cn-hangzhou.log.aliyuncs.com', 'test', '/', array()), 'https://test.cn-hangzhou.log.aliyuncs.com/');
10+
$this->assertEquals($this->getUrl('cn-hangzhou.log.aliyuncs.com', 'test', '/', array()), 'http://test.cn-hangzhou.log.aliyuncs.com/');
11+
$this->assertEquals($this->getUrl('http://cn-hangzhou.log.aliyuncs.com', 'test', '/logstores', array()), 'http://test.cn-hangzhou.log.aliyuncs.com/logstores');
12+
$this->assertEquals($this->getUrl('https://cn-hangzhou.log.aliyuncs.com:443', 'test', '/logstores', array()), 'https://test.cn-hangzhou.log.aliyuncs.com:443/logstores');
13+
$this->assertEquals($this->getUrl('https://111.111.111.111:80', 'test', '/logstores', array()), 'https://111.111.111.111:80/logstores');
14+
$this->assertEquals($this->getUrl('111.111.111.111:442', 'test', '/test', array()), 'http://111.111.111.111:442/test');
15+
$this->assertEquals($this->getUrl('111.111.111.111:442', null, '/test', array()), 'http://111.111.111.111:442/test');
16+
$this->assertEquals($this->getUrl('http://111.111.111.111:442', 'test', '/cursor', array('type' => 'cursor')), 'http://111.111.111.111:442/cursor?type=cursor');
17+
$this->assertEquals($this->getUrl('https://cn-hangzhou.log.aliyuncs.com', null, '/cursor', array('type' => 'cursor')), 'https://cn-hangzhou.log.aliyuncs.com/cursor?type=cursor');
18+
$this->assertEquals($this->getUrl('cn-hangzhou.log.aliyuncs.com', null, '/', array()), 'http://cn-hangzhou.log.aliyuncs.com/');
19+
}
20+
21+
public function getUrl($endpoint, $project, $resource, $params) {
22+
$accessKeyId = 'testKey';
23+
$accessKey = 'testAccessKey';
24+
$client = new Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey);
25+
$reflection = new ReflectionClass($client);
26+
$method = $reflection->getMethod('buildUrl');
27+
$method->setAccessible(true);
28+
return $method->invokeArgs($client, [$project, $resource, $params]);
29+
}
30+
}

0 commit comments

Comments
 (0)