@@ -52,6 +52,11 @@ class Aliyun_Log_Client {
52
52
* @var string the local machine ip address.
53
53
*/
54
54
protected $ source ;
55
+
56
+ /**
57
+ * @var bool use https or use http.
58
+ */
59
+ protected $ useHttps ;
55
60
56
61
/**
57
62
* Aliyun_Log_Client constructor.
@@ -91,23 +96,25 @@ public function __construct(
91
96
$ this ->source = Aliyun_Log_Util::getLocalIp ();
92
97
}
93
98
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 ;
98
116
}
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 );
111
118
}
112
119
113
120
/**
@@ -228,17 +235,23 @@ private function send($method, $project, $body, $resource, $params, $headers) {
228
235
$ signature = Aliyun_Log_Util::getRequestAuthorization ( $ method , $ resource , $ credentials ->getAccessKeySecret (), $ credentials ->getSecurityToken (), $ params , $ headers );
229
236
$ headers ['Authorization ' ] = "LOG " .$ credentials ->getAccessKeyId ().": $ signature " ;
230
237
238
+ $ url = $ this ->buildUrl ($ project , $ resource , $ params );
239
+ return $ this ->sendRequest ( $ method , $ url , $ body , $ headers );
240
+ }
241
+
242
+ private function buildUrl ($ project , $ resource , $ params ) {
231
243
$ 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 );
240
247
}
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 " ;
242
255
}
243
256
244
257
/**
0 commit comments