Http   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 57.41%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 87
ccs 31
cts 54
cp 0.5741
rs 10
wmc 18
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
F send() 0 52 15
A info() 0 4 1
A setCurlSSLVersion() 0 8 2
1
<?php
2
3
namespace Freyo\Flysystem\QcloudCOSv3\Client;
4
5
function my_curl_reset($handler)
6
{
7
    curl_setopt($handler, CURLOPT_URL, '');
8
    curl_setopt($handler, CURLOPT_HTTPHEADER, []);
9
    curl_setopt($handler, CURLOPT_POSTFIELDS, []);
10
    curl_setopt($handler, CURLOPT_TIMEOUT, 0);
11
    curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);
12
    curl_setopt($handler, CURLOPT_SSL_VERIFYHOST, 0);
13
}
14
class Http
15
{
16
    public static $_httpInfo = '';
17
    public static $_curlHandler;
18
19
    /**
20
     * send http request.
21
     *
22
     * @param array $rq http请求信息
23
     *                  url        : 请求的url地址
24
     *                  method     : 请求方法,'get', 'post', 'put', 'delete', 'head'
25
     *                  data       : 请求数据,如有设置,则method为post
26
     *                  header     : 需要设置的http头部
27
     *                  host       : 请求头部host
28
     *                  timeout    : 请求超时时间
29
     *                  cert       : ca文件路径
30
     *                  ssl_version: SSL版本号
31
     *
32
     * @return string http请求响应
33
     */
34 14
    public static function send($rq)
35
    {
36 14
        if (self::$_curlHandler) {
37 13
            if (function_exists('curl_reset')) {
38 13
                curl_reset(self::$_curlHandler);
39 13
            } else {
40
                my_curl_reset(self::$_curlHandler);
41
            }
42 13
        } else {
43 1
            self::$_curlHandler = curl_init();
44
        }
45 14
        curl_setopt(self::$_curlHandler, CURLOPT_URL, $rq['url']);
46 14
        switch (true) {
47 14
            case isset($rq['method']) && in_array(strtolower($rq['method']), ['get', 'post', 'put', 'delete', 'head']):
48 14
                $method = strtoupper($rq['method']);
49 14
                break;
50
            case isset($rq['data']):
51
                $method = 'POST';
52
                break;
53
            default:
54
                $method = 'GET';
55
        }
56 14
        $header = isset($rq['header']) ? $rq['header'] : [];
57 14
        $header[] = 'Method:'.$method;
58 14
        $header[] = 'User-Agent:'.Conf::getUA();
59 14
        $header[] = 'Connection: keep-alive';
60 14
        if ('POST' == $method) {
61 7
            $header[] = 'Expect: ';
62 7
        }
63
64 14
        isset($rq['host']) && $header[] = 'Host:'.$rq['host'];
65 14
        curl_setopt(self::$_curlHandler, CURLOPT_HTTPHEADER, $header);
66 14
        curl_setopt(self::$_curlHandler, CURLOPT_RETURNTRANSFER, 1);
67 14
        curl_setopt(self::$_curlHandler, CURLOPT_CUSTOMREQUEST, $method);
68 14
        isset($rq['timeout']) && curl_setopt(self::$_curlHandler, CURLOPT_TIMEOUT, $rq['timeout']);
69 14
        isset($rq['data']) && in_array($method, ['POST', 'PUT']) && curl_setopt(self::$_curlHandler, CURLOPT_POSTFIELDS, $rq['data']);
70 14
        $ssl = substr($rq['url'], 0, 8) == 'https://' ? true : false;
71 14
        if (isset($rq['cert'])) {
72
            curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYPEER, true);
73
            curl_setopt(self::$_curlHandler, CURLOPT_CAINFO, $rq['cert']);
74
            curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYHOST, 2);
75
            self::setCurlSSLVersion($rq);
76 14
        } elseif ($ssl) {
77
            curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYPEER, false); //true any ca
78
            curl_setopt(self::$_curlHandler, CURLOPT_SSL_VERIFYHOST, 0); //not check the names
79
            self::setCurlSSLVersion($rq);
80
        }
81 14
        $ret = curl_exec(self::$_curlHandler);
82 14
        self::$_httpInfo = curl_getinfo(self::$_curlHandler);
83
84 14
        return $ret;
85
    }
86
87
    public static function info()
88
    {
89
        return self::$_httpInfo;
90
    }
91
92
    private static function setCurlSSLVersion($rq)
93
    {
94
        if (isset($rq['ssl_version'])) {
95
            curl_setopt(self::$_curlHandler, CURLOPT_SSLVERSION, $rq['ssl_version']);
96
        } else {
97
            curl_setopt(self::$_curlHandler, CURLOPT_SSLVERSION, 4);
98
        }
99
    }
100
}
101