Completed
Push — master ( 55f13a...79290f )
by
unknown
24:57 queued 23:17
created

Client::userAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 68
    public static function get($url, array $headers = array())
11
    {
12 68
        $request = new Request('GET', $url, $headers);
13 68
        return self::sendRequest($request);
14
    }
15
16 57
    public static function post($url, $body, array $headers = array())
17
    {
18 57
        $request = new Request('POST', $url, $headers, $body);
19 57
        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
        $finalMimeType = empty($mimeType) ? 'application/octet-stream' : $mimeType;
43 9
        $finalFileName = self::escapeQuotes($fileName);
44 9
        array_push($data, "Content-Disposition: form-data; name=\"$name\"; filename=\"$finalFileName\"");
45 9
        array_push($data, "Content-Type: $finalMimeType");
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 102
    private static function userAgent()
60
    {
61 102
        $sdkInfo = "QiniuPHP/" . Config::SDK_VER;
62
63 102
        $systemInfo = php_uname("s");
64 102
        $machineInfo = php_uname("m");
65
66 102
        $envInfo = "($systemInfo/$machineInfo)";
67
68 102
        $phpVer = phpversion();
69
70 102
        $ua = "$sdkInfo $envInfo PHP/$phpVer";
71 102
        return $ua;
72
    }
73
74 102
    public static function sendRequest($request)
75
    {
76 102
        $t1 = microtime(true);
77 102
        $ch = curl_init();
78
        $options = array(
79 102
            CURLOPT_USERAGENT => self::userAgent(),
80 102
            CURLOPT_RETURNTRANSFER => true,
81 102
            CURLOPT_SSL_VERIFYPEER => false,
82 102
            CURLOPT_SSL_VERIFYHOST => false,
83 102
            CURLOPT_HEADER => true,
84 102
            CURLOPT_NOBODY => false,
85 102
            CURLOPT_CUSTOMREQUEST => $request->method,
86 102
            CURLOPT_URL => $request->url
87 102
        );
88
89
        // Handle open_basedir & safe mode
90 102
        if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
91 102
            $options[CURLOPT_FOLLOWLOCATION] = true;
92 102
        }
93
94 102
        if (!empty($request->headers)) {
95 69
            $headers = array();
96 69
            foreach ($request->headers as $key => $val) {
97 69
                array_push($headers, "$key: $val");
98 69
            }
99 69
            $options[CURLOPT_HTTPHEADER] = $headers;
100 69
        }
101 102
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
102
103 102
        if (!empty($request->body)) {
104 39
            $options[CURLOPT_POSTFIELDS] = $request->body;
105 39
        }
106 102
        curl_setopt_array($ch, $options);
107 102
        $result = curl_exec($ch);
108 102
        $t2 = microtime(true);
109 102
        $duration = round($t2 - $t1, 3);
110 102
        $ret = curl_errno($ch);
111 102
        if ($ret !== 0) {
112 6
            $r = new Response(-1, $duration, array(), null, curl_error($ch));
113 6
            curl_close($ch);
114 6
            return $r;
115
        }
116 100
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
117 100
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
118 100
        $headers = self::parseHeaders(substr($result, 0, $header_size));
119 100
        $body = substr($result, $header_size);
120 100
        curl_close($ch);
121 100
        return new Response($code, $duration, $headers, $body, null);
122
    }
123
124 100
    private static function parseHeaders($raw)
125
    {
126 100
        $headers = array();
127 100
        $headerLines = explode("\r\n", $raw);
128 100
        foreach ($headerLines as $line) {
129 100
            $headerLine = trim($line);
130 100
            $kv = explode(':', $headerLine);
131 100
            if (count($kv) > 1) {
132 100
                $headers[$kv[0]] = trim($kv[1]);
133 100
            }
134 100
        }
135 100
        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