Completed
Push — master ( 915c93...810079 )
by Bai
14s
created

Client   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
dl 0
loc 137
rs 10
c 0
b 0
f 0
ccs 90
cts 93
cp 0.9677
wmc 17
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 5 1
A post() 0 5 1
B multipartPost() 0 36 3
A userAgent() 0 14 1
A parseHeaders() 0 13 3
A escapeQuotes() 0 6 1
C sendRequest() 0 49 7
1
<?php
2
namespace Qiniu\Http;
3
4
use Qiniu\Config;
5
use Qiniu\Http\Request;
6
use Qiniu\Http\Response;
7
8
final class Client
9
{
10 45
    public static function get($url, array $headers = array())
11
    {
12 45
        $request = new Request('GET', $url, $headers);
13 45
        return self::sendRequest($request);
14
    }
15
16 54
    public static function post($url, $body, array $headers = array())
17
    {
18 54
        $request = new Request('POST', $url, $headers, $body);
19 54
        return self::sendRequest($request);
20
    }
21
22 9
    public static function multipartPost(
23
        $url,
24
        $fields,
25
        $name,
26
        $fileName,
27
        $fileBody,
28
        $mimeType = null,
29
        array $headers = array()
30
    ) {
31 9
        $data = array();
32 9
        $mimeBoundary = md5(microtime());
33
34 9
        foreach ($fields as $key => $val) {
35 9
            array_push($data, '--' . $mimeBoundary);
36 9
            array_push($data, "Content-Disposition: form-data; name=\"$key\"");
37 9
            array_push($data, '');
38 9
            array_push($data, $val);
39 9
        }
40
41 9
        array_push($data, '--' . $mimeBoundary);
42 9
        $mimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType;
43 9
        $fileName = self::escapeQuotes($fileName);
44 9
        array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$fileName\"");
45 9
        array_push($data, "Content-Type: $mimeType");
46 9
        array_push($data, '');
47 9
        array_push($data, $fileBody);
48
49 9
        array_push($data, '--' . $mimeBoundary . '--');
50 9
        array_push($data, '');
51
52 9
        $body = implode("\r\n", $data);
53 9
        $contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
54 9
        $headers['Content-Type'] = $contentType;
55 9
        $request = new Request('POST', $url, $headers, $body);
56 9
        return self::sendRequest($request);
57
    }
58
59 96
    private static function userAgent()
60
    {
61 96
        $sdkInfo = "QiniuPHP/" . Config::SDK_VER;
62
63 96
        $systemInfo = php_uname("s");
64 96
        $machineInfo = php_uname("m");
65
66 96
        $envInfo = "($systemInfo/$machineInfo)";
67
68 96
        $phpVer = phpversion();
69
70 96
        $ua = "$sdkInfo $envInfo PHP/$phpVer";
71 96
        return $ua;
72
    }
73
74 96
    public static function sendRequest($request)
75
    {
76 96
        $t1 = microtime(true);
77 96
        $ch = curl_init();
78
        $options = array(
79 96
            CURLOPT_USERAGENT => self::userAgent(),
80 96
            CURLOPT_RETURNTRANSFER => true,
81 96
            CURLOPT_SSL_VERIFYPEER => false,
82 96
            CURLOPT_SSL_VERIFYHOST => false,
83 96
            CURLOPT_HEADER => true,
84 96
            CURLOPT_NOBODY => false,
85 96
            CURLOPT_CUSTOMREQUEST  => $request->method,
86 96
            CURLOPT_URL => $request->url
87 96
        );
88
89
        // Handle open_basedir & safe mode
90 96
        if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
91 96
            $options[CURLOPT_FOLLOWLOCATION] = true;
92 96
        }
93
94 96
        if (!empty($request->headers)) {
95 66
            $headers = array();
96 66
            foreach ($request->headers as $key => $val) {
97 66
                array_push($headers, "$key: $val");
98 66
            }
99 66
            $options[CURLOPT_HTTPHEADER] = $headers;
100 66
        }
101 96
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
102
103 96
        if (!empty($request->body)) {
104 39
            $options[CURLOPT_POSTFIELDS] = $request->body;
105 39
        }
106 96
        curl_setopt_array($ch, $options);
107 96
        $result = curl_exec($ch);
108 96
        $t2 = microtime(true);
109 96
        $duration = round($t2-$t1, 3);
110 96
        $ret = curl_errno($ch);
111 96
        if ($ret !== 0) {
112
            $r = new Response(-1, $duration, array(), null, curl_error($ch));
113
            curl_close($ch);
114
            return $r;
115
        }
116 96
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
117 96
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
118 96
        $headers = self::parseHeaders(substr($result, 0, $header_size));
119 96
        $body = substr($result, $header_size);
120 96
        curl_close($ch);
121 96
        return new Response($code, $duration, $headers, $body, null);
122
    }
123
124 96
    private static function parseHeaders($raw)
125
    {
126 96
        $headers = array();
127 96
        $headerLines = explode("\r\n", $raw);
128 96
        foreach ($headerLines as $line) {
129 96
            $headerLine = trim($line);
130 96
            $kv = explode(':', $headerLine);
131 96
            if (count($kv) >1) {
132 96
                $headers[$kv[0]] = trim($kv[1]);
133 96
            }
134 96
        }
135 96
        return $headers;
136
    }
137
138 9
    private static function escapeQuotes($str)
139
    {
140 9
        $find = array("\\", "\"");
141 9
        $replace = array("\\\\", "\\\"");
142 9
        return str_replace($find, $replace, $str);
143
    }
144
}
145