@@ -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 /**
0 commit comments