Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加HttpProfile中对https请求是否进行严格验证的选项 #82

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/TencentCloud/Common/Http/HttpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private function getOptions()
$options = ["allow_redirects" => false];
$options["timeout"] = $this->profile->getHttpProfile()->getReqTimeout();
$options["proxy"] = $this->profile->getHttpProfile()->getProxy();
$options["verify"] = $this->profile->getHttpProfile()->getVerify();
return $options;
}

Expand Down
32 changes: 30 additions & 2 deletions src/TencentCloud/Common/Profile/HttpProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace TencentCloud\Common\Profile;

use GuzzleHttp\RequestOptions;

/**
* http相关参数类
* Class HttpProfile
Expand Down Expand Up @@ -76,6 +78,11 @@ class HttpProfile
*/
private $proxy;

/**
* @var string|bool 请求时验证SSL证书行为
*/
private $verify;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

构造函数里给一个初始值,默认应该是开启检查

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

即使无初始值,默认是开启检查,不过严谨起见还是加入初始值吧。
文档给出的默认值为GuzzleHttp\RequestOptions::VERIFY
就是这个参数是否需要作为构造函数的输入参数之一?

public function __construct($protocol = null, $endpoint = null, $reqMethod = null,  $reqTimeout = null, $verify = RequestOptions::VERIFY)
    {
        $this->reqMethod = $reqMethod ? $reqMethod : HttpProfile::$REQ_POST;
        $this->endpoint = $endpoint;
        $this->reqTimeout = $reqTimeout ? $reqTimeout : HttpProfile::$TM_MINUTE;
        $this->protocol = $protocol ? $protocol : HttpProfile::$REQ_HTTPS;
        $this->verify = $verify;
    }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一般不会用到,感觉构造函数的参数列表太长了。


/**
* @var string
*/
Expand All @@ -88,8 +95,8 @@ class HttpProfile

/**
* HttpProfile constructor.
* @param string $protocol 请求协议
* @param string $endpoint 请求接入点域名(xx.[region.]tencentcloudapi.com)
* @param string $protocol 请求协议
* @param string $endpoint 请求接入点域名(xx.[region.]tencentcloudapi.com)
* @param string $reqMethod http请求方法,目前支持POST GET
* @param integer $reqTimeout 请求超时时间,单位:s
*/
Expand All @@ -99,6 +106,7 @@ public function __construct($protocol = null, $endpoint = null, $reqMethod = nul
$this->endpoint = $endpoint;
$this->reqTimeout = $reqTimeout ? $reqTimeout : HttpProfile::$TM_MINUTE;
$this->protocol = $protocol ? $protocol : HttpProfile::$REQ_HTTPS;
$this->verify = RequestOptions::VERIFY;
$this->rootDomain = "tencentcloudapi.com";
$this->keepAlive = false;
}
Expand Down Expand Up @@ -147,6 +155,17 @@ public function setProxy($proxy)
$this->proxy = $proxy;
}

/**
* 设置成 true 启用SSL证书验证,默认使用操作系统提供的CA包。
* 设置成 false 禁用证书验证(这是不安全的!)。
* 设置成字符串启用验证,并使用该字符串作为自定义证书CA包的路径
* @param string|bool $verify 请求时验证SSL证书行为
*/
public function setVerify($verify)
{
$this->verify = $verify;
}

/**
* 获取请求方法
* @return null|string 请求方法
Expand Down Expand Up @@ -192,6 +211,15 @@ public function getProxy()
return $this->proxy;
}

/**
* 请求时验证SSL证书行为
* @return bool|string
*/
public function getVerify()
{
return $this->verify;
}

public function setRootDomain($domain)
{
$this->rootDomain = $domain;
Expand Down