Completed
Pull Request — master (#300)
by r
08:40
created

Client::ucwordsHyphen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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